In this section
- Line Card interaction · kuiper-belt / editions/202611020446/modules/202611020212-line-card.js lines 1–225
- Drift render · kuiper-belt / editions/202611020528/modules/202611020240-drift.js lines 1–331
Line Card
A JavaScript file in the editions folder of Kuiper Belt that works with row inner (`rowInner`) and about law (`aboutLaw`).
interactionkuiper-belt / editions/202611020446/modules/202611020212-line-card.js · lines 1–225 of 225 · version 2026-11-02 02:12 · Not reached from any entry point (candidate for review) · pattern hasText (shared by 2 files)
- What it does
- It draws its content. Defines 11 functions or classes, including hasText, header, keyText, render, show. Side effects: changes the web page (DOM).
- Why it exists in the grid workflow
- Domain stage agent_infra (repository name word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief). README: "Edition 7 is the same page, keys and modules with one module replaced: the line card's landscape revision (`modules/202611020212-line-card.js`, sha256 `8ec117f9dd436965e17fb860d5c12c485479cb3bdfdb6a845d924c65cc240fe3`, Opus team 2) in place of Edition 6's `202611012143-line-card.js`."
- What it achieves
- It is built from the has text helper (`hasText`), the header step (`header`) and the key text helper (`keyText`). Reads: nothing fetched (bodies are handed in by the page or a module) · writes: #card (its content, classes, aria), #ky-line-card-style, the empty [data-owner="line-card"] slot of the ABOUT panel, window.KY.modules.line-card. It responds to click, touchstart and touchmove events.
- Why it matters
- No other file in the estate was found to import it.
- Application
- kuiper-belt · editions/202611020446/modules · script
- Uses
- none found
- Used by
- none found
- Produces
- hasText, header, keyText, render, show, close, rowInner, row, behind, aboutLaw, inspect
- Defines here
- hasText, header, keyText, render, show, close, rowInner, row, behind, aboutLaw, inspect
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 editions/202611020446/modules/202611020212-line-card.js:1-225 · blob 412eeeb2974a17cbc72d401d8dbf64aa07931800 · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: agent_infra · users: ai_agent · canonical: unreached_review
/* kuiper module line-card, generation 202611020212 (UTC), Apache-2.0 (kuiper-belt) */
// requires: card esc fmt address describe sel lit redraw gbox · provides: line-card · schema kuiper.card.v1
// reads: nothing fetched (bodies are handed in by the page or a module) · writes: #card (its content, classes, aria),
// #ky-line-card-style, the empty [data-owner="line-card"] slot of the ABOUT panel, window.KY.modules.line-card
//
// The line of code is the header. Every card opens with the line itself, verbatim from the key; the key is the second row; every name a person gave the line comes after.
//
// Roadmap step 2 (neural-link FIXES/NL-010/ROADMAP.md "Step 2", team-1/STEPS.md:255-345), K0-K6:
// row 1 <header class="lc-h"><code> = body.line_text, escaped, pre-wrap, mono, #fff, 600; nothing before it.
// Empty line at the key: the code is empty and the dim words "empty line at key" follow it.
// No line_text field: the card refuses with "no text at key" (never the title, never the quote).
// row 2 the key: repo@commit7 path:line · key N, a link to body.destination.
// row 2b "register quote", only when quote !== line_text (with quote_key and "quoted one line below" when present).
// then the action row (open on GitHub, details) and, behind details, every name: title, n of N, pattern, finder,
// the wafer position (describe(address(key))).
// NL-009 round 1 patch 02-card (FABLE-02) folded in: bottom sheet on a phone, 44 px targets, 12 px type, 34dvh cap,
// safe-area padding, swipe-down / Escape / × close, scroll contained, steps behind an open panel.
// The module never wraps an engine function: the page (or a module) calls api.show(body, {index, of, onclose}).
// Revision 202611020212 (Edition 6) of 202611012143 (admitted in Edition 5, kuiper-belt main a432b43), one defect fixed:
// in Pixel 7 landscape (863x360) the card was capped at 30dvh = 108 px, already full before details opened, so the
// action row was cut and "details" half visible (handoff/10-integration/TEST.md runs 3-4, card-details). Now, on a
// short landscape screen, the cap is min(50dvh, the viewport minus the bar minus a 140 px band for the spark), and
// the action row (open on GitHub, details) is sticky to the card's visible top and bottom, so it stays whole however
// the card is scrolled, details open or closed; on a
// coarse pointer the action targets are 44 px at every width. Nothing else changed.
(function () {
'use strict';
var LAW = 'The line of code is the header. Every card opens with the line itself, verbatim from the key; the key is the second row; every name a person gave the line comes after.';
var STAMP = '202611020212';
// a line longer than this is laid out unwrapped and scrolls sideways inside the header; every byte stays in it.
// Measured for the 1,240,449-character line (kuiper-belt estate-network.json:1, key 63,013,167,769), Pixel 7 and
// Galaxy S24 emulation in one container: wrapped 9,313 ms (lead, once); unwrapped 1.4-3.6 s to show and lay out at
// load average 86-95 (evidence officer, handoff/02-card/EVIDENCE.md F-c). Not phone numbers.
var LONG = 2000;
if (!window.KY || typeof window.KY.register !== 'function') return; // no hook table: the page keeps its own card
var CSS = [
/* the card is a box on a desktop and a bottom sheet on a phone (NL-009 02-card F5-F7) */
'#card.lc{padding:0!important;bottom:12px!important;z-index:13;max-height:30vh;max-height:30dvh;overflow:auto;',
' overscroll-behavior:contain;touch-action:pan-y;-webkit-overflow-scrolling:touch;scrollbar-width:thin;position:fixed;white-space:normal}',
'#card.lc .lc-in{position:relative;padding:8px 48px 8px 12px}',
/* row 1, the header: the line itself */
'#card.lc .lc-h{display:block;margin:0 0 4px;max-height:7.5em;overflow:auto;overscroll-behavior:contain}',
'#card.lc .lc-h code{display:block;white-space:pre-wrap;overflow-wrap:anywhere;word-break:break-word;color:#fff;font-weight:600;',
' font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:inherit;line-height:1.45;tab-size:4}',
'#card.lc .lc-h.lc-long code{white-space:pre;overflow-wrap:normal;word-break:normal}',
'#card.lc .lc-h .lc-e,#card.lc .lc-h .lc-none{display:block;color:var(--dim);font-weight:400}',
'#card.lc .lc-h .lc-none{color:var(--am)}',
/* row 2, the key */
'#card.lc .lc-k{display:block;color:#cde;white-space:pre-wrap;overflow-wrap:anywhere;text-decoration:none;margin:0 0 2px}',
'#card.lc .lc-k:hover{text-decoration:underline}',
'#card.lc .lc-q{color:#cde;border-left:2px solid #3a4150;padding-left:8px;margin:4px 0;white-space:pre-wrap;overflow-wrap:anywhere}',
'#card.lc .lc-q i{color:var(--dim);font-style:normal}',
/* the action row and the close: 44 px targets on a phone (F1, F2) */
'#card.lc .lc-x{position:absolute;right:0;top:0;width:44px;height:44px;border:0;color:#8fb3bd;background:transparent;padding:0;',
' font:18px/1 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;cursor:pointer;touch-action:manipulation}',
'#card.lc .lc-x:hover,#card.lc .lc-x:active{color:#fff}',
'#card.lc .lc-a{display:flex;flex-wrap:wrap;align-items:center;gap:0 2px;margin:0 0 0 -8px;color:var(--dim);',
' position:sticky;top:0;bottom:0;z-index:1;background:#090c13}',
'#card.lc .lc-a a,#card.lc .lc-t{display:inline-flex;align-items:center;min-height:32px;padding:0 8px;touch-action:manipulation}',
'#card.lc .lc-t{appearance:none;border:0;background:transparent;color:var(--cy);cursor:pointer;font:inherit;text-decoration:underline dotted}',
'#card.lc .lc-d{display:none;color:var(--dim);margin-top:6px;border-top:1px solid #121a26;padding-top:6px;white-space:pre-wrap;overflow-wrap:anywhere}',
'#card.lc .lc-d .lc-title{color:var(--am);font-weight:600}',
'#card.lc.lc-open .lc-d{display:block}',
'#card.lc.lc-behind{visibility:hidden}',
'#card.lc .lc-grip{display:none}',
/* K6, the LINES rows: the line, then the key, then the title */
'#lineslist .lc-rt{display:block;white-space:pre;overflow:hidden;text-overflow:ellipsis;color:#fff;font-weight:600;',
' font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}',
'#lineslist .lc-rk{display:block;color:#cde}',
'#lineslist .lc-rn{display:block}',
'#lineslist .lc-rt ~ i{color:var(--dim);font-style:normal}',
'#lineslist .lc-rt ~ i.lc-none{color:var(--am)}',
'@media (pointer:coarse){ #card.lc .lc-a a,#card.lc .lc-t{min-height:44px} }',
'@media (max-width:640px){',
' #card.lc{left:0;right:0;bottom:0!important;max-width:none;max-height:34vh;max-height:34dvh;font-size:12px;line-height:1.5;',
' border-left:0;border-right:0;border-bottom:0;border-radius:12px 12px 0 0}',
' #card.lc .lc-in{padding:4px 48px calc(8px + env(safe-area-inset-bottom,0px)) 12px}',
' #card.lc .lc-grip{display:block;position:absolute;left:50%;top:4px;width:36px;height:4px;margin-left:-18px;border-radius:2px;background:#2a5560}',
' #card.lc .lc-h{margin-top:8px}',
' #card.lc .lc-x{height:48px}',
' #card.lc .lc-a a,#card.lc .lc-t{min-height:44px}',
' #card.lc .lc-d{font-size:11px}',
'}',
/* last, so it also wins over the phone rules on a narrow phone on its side. A short landscape screen: cap by the height that is there, keep a band for the spark */
'@media (orientation:landscape) and (max-height:500px){',
' #card.lc{bottom:0!important;max-height:min(50vh,calc(100vh - var(--barh,46px) - 140px));',
' max-height:min(50dvh,calc(100dvh - var(--barh,46px) - 140px))}',
' #card.lc .lc-in{padding-bottom:env(safe-area-inset-bottom,0px)}',
' #card.lc .lc-h{max-height:4.5em}',
'}'
].join('\n');
function hasText(b) { return b && typeof b.line_text === 'string'; }
window.KY.register({
schema: 'kuiper.module.v1', id: 'line-card', generation: STAMP,
requires: ['card', 'esc', 'fmt', 'address', 'describe', 'sel', 'lit', 'redraw', 'gbox'],
provides: ['line-card'],
install: function (KY) {
var H = KY.hooks, card = H.card, esc = H.esc, fmt = H.fmt;
var state = { shown: null, index: -1, of: 0, onclose: null, opened: 0, refused: 0, last: null };
var st = document.createElement('style'); st.id = 'ky-line-card-style'; st.textContent = CSS;
document.head.appendChild(st);
card.classList.add('lc');
card.setAttribute('aria-live', 'polite');
// the header text: the line at the key, or the refusal (K0)
function header(b) { return hasText(b) ? b.line_text : 'no text at key'; }
function keyText(b) {
return String(b.repo) + '@' + String(b.commit || '').slice(0, 7) + ' ' + String(b.path) + ':' + b.line
+ (Number.isFinite(b.key) ? ' · key ' + fmt(b.key) : '');
}
// K1, in this order and no other
function render(b, o) {
var h = hasText(b)
? '<header class="lc-h' + (b.line_text.length > LONG ? ' lc-long' : '') + '"><code>' + esc(b.line_text) + '</code>'
+ (b.line_text === '' ? '<span class="lc-e">empty line at key</span>' : '')
+ (b.line_text.length > LONG ? '<span class="lc-e">one line of ' + fmt(b.line_text.length) + ' characters, verbatim; it scrolls sideways</span>' : '') + '</header>'
: '<header class="lc-h"><code></code><span class="lc-none">no text at key</span></header>';
var k = '<a class="lc-k" href="' + esc(b.destination) + '" target="_blank" rel="noopener">' + esc(keyText(b)) + '</a>';
var q = '';
if (b.quote && (!hasText(b) || b.quote !== b.line_text)) {
q = '<div class="lc-q"><i>register quote' + (b.quote_key ? ', quoted one line below · key ' + esc(fmt(b.quote_key)) : '')
+ '</i>\n' + esc(b.quote) + '</div>';
}
var a = '<div class="lc-a"><a href="' + esc(b.destination) + '" target="_blank" rel="noopener">open on GitHub at '
+ esc(String(b.commit || '').slice(0, 7)) + '</a>·<button class="lc-t" type="button" aria-expanded="false">details</button></div>';
var addr = Number.isFinite(b.key) ? H.address(b.key) : null;
var d = '<div class="lc-d"><span class="lc-title">' + esc(b.title) + '</span>'
+ (o && o.of ? ' ' + fmt((o.index | 0) + 1) + ' of ' + fmt(o.of) : '')
+ (b.pattern ? '\npattern: ' + esc(b.pattern) : '')
+ (b.worker || b.finding ? '\nfound by ' + esc(b.worker) + ' ' + esc(b.finding) + ', verified by a controller' : '')
+ (b.quote_check ? '\nquote check: ' + esc(b.quote_check) : '')
+ (addr ? '\n' + H.describe(addr) : '') + '</div>';
// the header is the first element and the first text; the close and the grip come after it in the DOM
return '<div class="lc-in">' + h + k + q + a + d
+ '<span class="lc-grip" aria-hidden="true"></span><button class="lc-x" type="button" aria-label="close">×</button></div>';
}
function show(b, o) {
o = o || {};
if (!b) return false;
state.shown = b; state.index = o.index == null ? -1 : o.index; state.of = o.of || 0;
state.onclose = typeof o.onclose === 'function' ? o.onclose : null;
state.opened++; if (!hasText(b)) state.refused++;
card.classList.remove('lc-open');
card.innerHTML = render(b, o);
card.setAttribute('aria-label', header(b));
card.style.display = 'block'; card.scrollTop = 0;
behind();
if (Number.isFinite(b.key)) { var a = H.address(b.key); if (a && !a.silent) KY.lit.repo = a.c.repo; }
if (Number.isFinite(b.x) && Number.isFinite(b.y)) KY.sel.target = { x: b.x, y: b.y };
KY.redraw();
state.last = { header: header(b), key: keyText(b), k: b.key };
return true;
}
function close() {
var cb = state.onclose;
card.style.display = 'none'; card.classList.remove('lc-open');
state.shown = null; state.onclose = null;
KY.sel.target = null; KY.redraw();
if (cb) { try { cb(); } catch (e) { /* the caller's callback; the card is closed either way */ } }
}
// K6: a LINES row reads the line, then the key, then the title. rowInner(b) is the row's content only, for a
// module that wraps it in its own .ln[data-i] (Opus 3, #10 issuecomment-6564782359 item 3); row(b, i, cls) is the
// whole row the page's fillLines uses until then
function rowInner(b) {
var t = hasText(b) ? b.line_text : '';
return '<code class="lc-rt">' + esc(t) + '</code>'
+ (hasText(b) ? (t === '' ? '<i>empty line at key</i>' : '') : '<i class="lc-none">no text at key</i>')
+ '<span class="lc-rk">' + esc(keyText(b)) + '</span>'
+ '<span class="lc-rn">' + esc(b.title) + '</span>';
}
function row(b, i, cls) {
return '<div class="ln lc-row' + (cls ? ' ' + cls : '') + '" data-i="' + (i | 0) + '" role="button" tabindex="0">' + rowInner(b) + '</div>';
}
// the card steps behind any open panel (visibility, so the page can still measure it) and returns when it closes
function behind() { card.classList.toggle('lc-behind', !!document.querySelector('.gm-open')); }
new MutationObserver(behind).observe(document.body, { subtree: true, attributes: true, attributeFilter: ['class'] });
// clicks: × closes, details toggles, the link keeps its default (a new tab), a tap on the body keeps it open.
// Heard on window in the capture phase, before the page's own capture handler on #card, which stops the event.
window.addEventListener('click', function (e) {
if (!state.shown || !e.target || !e.target.closest || !card.contains(e.target)) return;
if (e.target.closest('.lc-x')) { e.preventDefault(); close(); return; }
var t = e.target.closest('.lc-t');
if (t) t.setAttribute('aria-expanded', String(card.classList.toggle('lc-open')));
}, true);
// a swipe down from the top of the sheet closes it (02-card F7)
var sw = null;
card.addEventListener('touchstart', function (e) {
var t = e.touches[0]; sw = (e.touches.length === 1 && card.scrollTop <= 0) ? { x: t.clientX, y: t.clientY } : null;
}, { passive: true });
card.addEventListener('touchmove', function (e) {
if (!sw) return; var t = e.touches[0], dx = t.clientX - sw.x, dy = t.clientY - sw.y;
if (dy > 56 && dy > 2 * Math.abs(dx)) { sw = null; close(); } else if (dy < -8 || Math.abs(dx) > 24) sw = null;
}, { passive: true });
card.addEventListener('touchend', function () { sw = null; });
// Escape closes the card only when no panel and no finder is open (capture, so an open panel is seen before it closes)
window.addEventListener('keydown', function (e) {
if (e.key !== 'Escape' || card.style.display !== 'block' || !state.shown) return;
if (document.querySelector('.gm-open') || H.gbox.style.display === 'block') return;
close();
}, true);
// K3: the shell's ABOUT panel keeps a slot for the law (data-owner="line-card"); the card fills it only when empty
function aboutLaw() {
var p = document.querySelector('[data-owner="line-card"][data-law="L11"]');
if (p && !p.textContent.trim()) p.textContent = LAW;
return p ? p.textContent : null;
}
aboutLaw();
function inspect() {
return { schema: 'kuiper.card.v1', law: LAW, open: card.style.display === 'block' && !!state.shown,
index: state.index, of: state.of, opened: state.opened, refused: state.refused, last: state.last, about: aboutLaw() };
}
return Object.freeze({ law: LAW, header: header, show: show, close: close, row: row, rowInner: rowInner, inspect: inspect });
},
inspect: function () { return { schema: 'kuiper.card.v1', law: LAW }; }
});
})();Drift
A JavaScript file in the editions folder of Kuiper Belt that converts to screen (`toScreen`), loads bodies (`loadBodies`), draws marks (`drawMarks`) and calculates planar distances.
renderkuiper-belt / editions/202611020528/modules/202611020240-drift.js · lines 1–331 of 331 · version 2026-11-02 02:40 · Not reached from any entry point (candidate for review) · pattern loadBodies (shared by 2 files)
- What it does
- It draws its content. Defines 24 functions or classes, including esc, fmt, sha7, toScreen, layerOn. Side effects: changes the web page (DOM); reads or writes files; makes network requests.
- Why it exists in the grid workflow
- Domain stage agent_infra (repository name word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief). README: "Edition 8 is the same page, keys and modules with one module added: the drift (`modules/202611020240-drift.js`, sha256 `f03b78350694362f0d0203647231c9809661a0aac7392f6fd776f519ce391480`, Opus team 8, NL-010 step 8), with its data `data/drift.json`."
- Inputs & outputs
- In: JSON file drift.json; JSON file keys-min.json; JSON file keys.json.
- What it achieves
- It fetches JSON (`fetchJson`), loads bodies (`loadBodies`) and draws marks (`drawMarks`). Kuiper module drift The module ONLY READS `data/drift.json` (kuiper.drift.v1, written by `tools/drift.py` at the cut); it never classifies a line itself. It responds to pointerdown, pointercancel and pointerup events.
- Why it matters
- No other file in the estate was found to import it.
- Application
- kuiper-belt · editions/202611020528/modules · script
- Uses
- none found
- Used by
- none found
- Produces
- esc, fmt, sha7, toScreen, layerOn, fetchJson, admit, refuse, load, loadBodies, headCommit, place
- Defines here
- esc, fmt, sha7, toScreen, layerOn, fetchJson, admit, refuse, load, loadBodies, headCommit, place, drawMarks, cardLines, markFor, onCard, bodyFor, open, wireTaps, buildPanel, paint, fillList, install, inspect
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 editions/202611020528/modules/202611020240-drift.js:1-331 · blob 66d0f8bc52f24c680e696b7caa558bad6fbe5c97 · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom, filesystem, network · domain stage: agent_infra · users: ai_agent · canonical: unreached_review
/* kuiper module drift, generation 202611020240 (UTC), Apache-2.0 (kuiper-belt code)
Opus team 8, NL-010 roadmap step 8 "the first intelligent algorithm: the drift of an edition from the live heads"
(neural-link FIXES/NL-010/ROADMAP.md step 8; FIXES/NL-010/team-4/STEPS.md step 8, :233-352).
The module ONLY READS data/drift.json (kuiper.drift.v1, written by tools/drift.py at the cut); it never classifies a
line itself. It reads keys-min.json (else keys.json) for the line text of a card or a row, as the page does
(index.html loadEdition, EDPATH/EDFULL); the line of code stays the header of every card (step 2, line-card).
Draws, inside the palette (kuiper-belt PALETTE.md:9-14): MOVED an amber spark at its NEW key (the head commit's
k0 on the wafer + head_lines_before + head_line - 1, the key law of tools/edition_keys.py:12, applied at the head);
GONE and FILE_GONE an amber ring with no fill at the edition's key; UNVERIFIED a grey-blue ring at the edition's key;
SAME adds nothing (the edition's own green spark is the record, lit).
Refuses in words, draws nothing, and leaves the wafer and the green sparks as they were when drift.json is missing,
is not JSON, is not kuiper.drift.v1, or names another edition: "data/drift.json did not load. The wafer will not
invent a head." (the engine's idiom, index.html:256 "... did not load. The wafer will not invent its keys.").
Adds: the readout line (five counts, the share with its denominator), two lines on every card of a keyed line
("at head <sha7>: SAME" / "MOVED to line N (+d)" / "GONE" / "FILE_GONE" / "UNVERIFIED · <why>", then the key at the
head or the observation), and a filter by class in the WAFER panel (no new bar title). Registers the layer record
`drift` with the layer registry (Opus 3) when that record exists; otherwise draws on its own and says so.
requires: wafer, keyPlace, o2, ov, cv, card, esc, fmt, commits, repos, SPACE, hud, flyTo, cam, sel, draw, redraw, panels
optional (never required): layers (api.register/get), line-card (api.show, inspect().last.k)
Revision 202611020240 (supersedes 202611012325, withdrawn at 02:25 UTC, issue #10 comment 6567584787): the drift
section is one collapsed 44 px row placed AFTER #lineslist, so the LINES filter, its first rows and their tap targets
are where they are without the module; the WAFER panel (shell) is used only when the page has no #lineslist.
provides: drift */
(function () {
'use strict';
var ID = 'drift', GEN = '202611020240', DATA = 'data/drift.json', SCHEMA = 'kuiper.drift.v1';
var REQUIRES = ['wafer', 'keyPlace', 'o2', 'ov', 'cv', 'card', 'esc', 'fmt', 'commits', 'repos', 'SPACE', 'hud', 'flyTo',
'cam', 'sel', 'draw', 'redraw', 'panels'];
var CLASSES = ['SAME', 'MOVED', 'GONE', 'FILE_GONE', 'UNVERIFIED'];
// PALETTE.md:9-14: green energised (the record, lit), amber attention (a boundary moved), grey-blue the record not
// yet measured. No sixth colour (PALETTE.md:19).
var INK = { SAME: '#22ee77', MOVED: '#ffb000', GONE: '#ffb000', FILE_GONE: '#ffb000', UNVERIFIED: '#c9d3e6' };
var NOLOAD = DATA + ' did not load. The wafer will not invent a head.';
var TAP_TOUCH = 24, TAP_MOUSE = 9, SLOP = 10, FLY_Z = 14; // index.html ED_TAP_TOUCH/ED_TAP_MOUSE/ED_SLOP, ED_Z
var KY = null, H = null, panel = null, list = null, chips = null;
var st = { status: 'WAIT', why: 'waiting for the wafer', data: null, counts: null, denominator: 0, share: '',
rows: [], byKey: {}, marks: [], bodies: null, bodyById: {}, edSparks: [], filter: 'MOVED', pending: null,
registry: 'none', cards: 0, cardLast: null, taps: 0, errors: [] };
function esc(v) { return H.esc(v); }
function fmt(n) { return H.fmt(n); }
function sha7(s) { return String(s || '').slice(0, 7); }
function toScreen(x, y) { var c = KY.cam; return [(x - c.x) * c.zoom + H.ov.width / 2, H.ov.height / 2 - (y - c.y) * c.zoom]; }
function layerOn() {
var L = KY.modules && KY.modules.layers && KY.modules.layers.api;
if (!L || typeof L.get !== 'function') return true;
try { var r = L.get(ID); return !r || r.on !== false; } catch (e) { return true; }
}
// ============================================================ SECTION 1: load and refuse (lead)
function fetchJson(file, noStore) {
return fetch(file, noStore ? { cache: 'no-store' } : undefined).then(function (r) {
if (!r.ok) throw new Error(file + ' HTTP ' + r.status);
return r.json();
});
}
function admit(j) {
if (!j || typeof j !== 'object') return 'not an object';
if (j.schema !== SCHEMA) return 'schema ' + j.schema + ' ≠ ' + SCHEMA;
if (j.edition !== KY.edition) return 'edition ' + j.edition + ' ≠ page ' + KY.edition;
if (!Array.isArray(j.rows) || !j.summary || !j.summary.counts) return 'no rows or no summary';
var sum = 0;
for (var i = 0; i < CLASSES.length; i++) sum += j.summary.counts[CLASSES[i]] | 0;
if (sum !== j.rows.length) return 'the five counts sum to ' + sum + ', rows ' + j.rows.length;
return '';
}
function refuse(why) {
st.status = 'REFUSED'; st.why = why; st.marks = []; st.counts = null;
paint(); KY.redraw();
}
function load() {
return fetchJson(DATA, true).then(function (j) {
var why = admit(j);
if (why) { refuse(NOLOAD + ' (' + why + ')'); return { placed: 0, total: 0, why: why }; }
st.data = j; st.rows = j.rows; st.counts = j.summary.counts; st.denominator = j.summary.denominator;
st.share = j.summary.share_text || '';
st.byKey = {};
j.rows.forEach(function (r) { if (r.key != null && !st.byKey[r.key]) st.byKey[r.key] = r; });
place();
st.status = 'OK'; st.why = '';
paint(); KY.redraw();
return { placed: st.marks.length, total: j.rows.length };
}, function (e) {
refuse(NOLOAD + ' (' + String(e && e.message || e) + ')');
return { placed: 0, total: 0, why: String(e && e.message || e) };
});
}
// the line text of a card or a row: the page's own file (index.html EDPATH keys-min.json, EDFULL keys.json)
function loadBodies() {
return fetchJson('keys-min.json').catch(function () { return fetchJson('keys.json'); }).then(function (j) {
var b = (j && j.bodies) || j || [];
st.bodies = b; st.bodyById = {};
b.forEach(function (x) { st.bodyById[x.id + '|' + x.repo] = x; });
st.edSparks = b.filter(function (x) { return x.on_wafer && x.verified && Number.isFinite(x.key) && x.key >= 0 && x.key < H.SPACE; })
.map(function (x) { var p = H.keyPlace(x.key); return [p[0], p[1]]; });
fillList();
}, function (e) { st.errors.push('keys-min.json and keys.json did not load: ' + (e && e.message)); });
}
// ============================================================ SECTION 3: placing the sparks (new keys for MOVED)
// The head commit is found on the wafer by its 12-hex sha and its repository's name (cosmos/wafer.tsv sha12, the
// page's commits[]); the new key is k0 + head_lines_before + head_line - 1 and must fall inside [k0, k1) of that
// commit. A head that is not on the wafer (kuiper-belt is not on it) leaves the spark at the edition's key, amber,
// and the card says so: nothing is placed where no key exists.
var headAt = {};
function headCommit(repo, sha) {
var k = repo + '@' + sha;
if (k in headAt) return headAt[k];
var s12 = String(sha || '').slice(0, 12), cs = H.commits, rs = H.repos, hit = null;
if (s12.length === 12) for (var i = 0; i < cs.length; i++) {
if (cs[i].sha === s12 && rs[cs[i].repo] && rs[cs[i].repo].name.toLowerCase() === String(repo).toLowerCase()) { hit = cs[i]; break; }
}
headAt[k] = hit; return hit;
}
function place() {
st.marks = [];
st.rows.forEach(function (r) {
if (r.class === 'SAME' || !Number.isFinite(r.key) || r.key < 0 || r.key >= H.SPACE) return;
var m = { row: r, cls: r.class, k: r.key, atNew: false, note: '' };
if (r.class === 'MOVED') {
var c = headCommit(r.repo, r.head);
if (c && Number.isFinite(r.head_lines_before)) {
var nk = c.k0 + r.head_lines_before + r.head_line - 1;
if (nk >= c.k0 && nk < c.k1) { m.k = nk; m.atNew = true; }
else m.note = 'the new key ' + fmt(nk) + ' falls outside the head commit on the wafer; drawn at the edition’s key';
} else m.note = 'head ' + sha7(r.head) + ' is not on the wafer; drawn at the edition’s key';
}
var p = H.keyPlace(m.k); m.x = p[0]; m.y = p[1];
st.marks.push(m);
});
}
function drawMarks(o2) {
o2 = o2 || H.o2;
var hud = H.hud;
if (st.status !== 'OK') {
if (st.status === 'REFUSED') hud.textContent += '\nDRIFT · ' + st.why;
return;
}
var on = layerOn(), c = st.counts;
hud.textContent += '\nDRIFT since the cut ' + KY.edition + ' · ' + CLASSES.map(function (k) { return fmt(c[k]) + ' ' + k; }).join(' · ')
+ ' · ' + st.share + (on ? '' : ' · drift layer off');
if (!on) return;
var W = H.ov.width, Hh = H.ov.height;
for (var i = 0; i < st.marks.length; i++) {
var m = st.marks[i], s = toScreen(m.x, m.y), x = s[0], y = s[1];
if (x < -10 || y < -10 || x > W + 10 || y > Hh + 10) continue;
o2.beginPath();
if (m.cls === 'MOVED') {
o2.arc(x, y, 3.6, 0, 6.2832); o2.fillStyle = 'rgba(255,176,0,.95)'; o2.fill();
} else if (m.cls === 'GONE' || m.cls === 'FILE_GONE') {
o2.arc(x, y, 5.5, 0, 6.2832); o2.strokeStyle = 'rgba(255,176,0,.95)'; o2.lineWidth = 1.6; o2.stroke();
} else {
o2.arc(x, y, 4.5, 0, 6.2832); o2.strokeStyle = 'rgba(201,211,230,.75)'; o2.lineWidth = 1; o2.stroke();
}
}
}
// ============================================================ SECTION 4: the card's two lines
function cardLines(r, m) {
var h = r.head ? 'at head ' + sha7(r.head) + ': ' : 'at head (none): ', l1, l2;
if (r.class === 'SAME') { l1 = h + 'SAME'; l2 = r.repo + '@' + sha7(r.head) + ' ' + r.path + ':' + r.line; }
else if (r.class === 'MOVED') {
l1 = h + 'MOVED to line ' + r.head_line + ' (' + (r.delta > 0 ? '+' : '') + r.delta + ')';
l2 = r.repo + '@' + sha7(r.head) + ' ' + r.path + ':' + r.head_line
+ (m && m.atNew ? ' · key ' + fmt(m.k) : (m && m.note ? ' · ' + m.note : ''));
} else if (r.class === 'UNVERIFIED') { l1 = h + 'UNVERIFIED · ' + (r.reason || ''); l2 = 'nothing is claimed for this line'; }
else { l1 = h + r.class; l2 = r.class === 'GONE' ? 'no line of ' + r.path + ' at ' + sha7(r.head) + ' equals the line' : (r.reason || 'no file at the path'); }
var hd = st.data && st.data.heads && st.data.heads[r.repo];
if (hd && hd.observed_at) l2 += ' · head observed ' + hd.observed_at;
return '<div class="ky-drift-cl" data-drift="' + esc(r.class) + '"><b style="color:' + INK[r.class] + '">' + esc(l1)
+ '</b>\n<span>' + esc(l2) + '</span></div>';
}
function markFor(r) { for (var i = 0; i < st.marks.length; i++) if (st.marks[i].row === r) return st.marks[i]; return null; }
function onCard() {
var card = H.card;
if (st.status !== 'OK' || card.style.display !== 'block' || card.querySelector('.ky-drift-cl')) return;
var r = null;
if (st.pending) { r = st.pending; st.pending = null; }
if (!r) {
var LC = KY.modules && KY.modules['line-card'];
var last = LC && typeof LC.inspect === 'function' ? (LC.inspect() || {}).last : null;
if (last && Number.isFinite(last.k)) r = st.byKey[last.k] || null;
}
if (!r) { // the page's own card (no line-card admitted): "repo path:line"
var t = card.textContent || '', mm = t.match(/\n?(\S+) (\S+):(\d+)/);
if (mm) for (var i = 0; i < st.rows.length; i++) { var x = st.rows[i]; if (x.repo === mm[1] && x.path === mm[2] && x.line === +mm[3]) { r = x; break; } }
}
if (!r) return;
var html = cardLines(r, markFor(r)), k = card.querySelector('.lc-q') || card.querySelector('.lc-k');
if (k) k.insertAdjacentHTML('afterend', html); else card.insertAdjacentHTML('beforeend', html);
st.cards++; st.cardLast = { id: r.id, class: r.class, text: card.querySelector('.ky-drift-cl').textContent };
}
// ============================================================ SECTION 5: open a row (from a tap or the filter)
function bodyFor(r, m) {
var b = st.bodyById[r.id + '|' + r.repo];
if (!b) return null;
var o = {}; for (var k in b) o[k] = b[k];
if (m) { o.x = m.x; o.y = m.y; } else if (Number.isFinite(b.key)) { var p = H.keyPlace(b.key); o.x = p[0]; o.y = p[1]; }
return o;
}
function open(r, fly) {
var m = markFor(r), b = bodyFor(r, m);
if (!b) return false;
st.pending = r;
var LC = KY.modules && KY.modules['line-card'] && KY.modules['line-card'].api;
if (LC && typeof LC.show === 'function') LC.show(b, { index: st.rows.indexOf(r), of: st.rows.length });
else { // no line-card: the line of code is still the header
var c = H.card;
c.innerHTML = '<button class="cx" aria-label="close">×</button><code style="white-space:pre-wrap;color:#fff">'
+ esc(String(b.line_text || '').slice(0, 2000)) + '</code>\n<span class="lc-k">' + esc(b.repo + '@' + sha7(b.commit) + ' ' + b.path + ':' + b.line + ' · key ' + fmt(b.key)) + '</span>';
c.style.display = 'block';
if (Number.isFinite(b.x)) KY.sel.target = { x: b.x, y: b.y };
}
onCard();
if (fly && Number.isFinite(b.x)) { try { KY.cam.atHome = false; } catch (e) { /* rev < 3 */ } H.flyTo(b.x, b.y, Math.max(FLY_Z, KY.cam.zoom)); }
KY.redraw();
return true;
}
// a tap on an amber spark at its new key opens its card: the page's own reach (24 px finger, 9 px mouse), one
// pointer, within 10 px of where it went down; it wins only when it is nearer than every green spark (the page's
// own hit test, which runs first, has already opened that one's card otherwise)
var down = null, live = {};
function wireTaps() {
H.cv.addEventListener('pointerdown', function (e) { live[e.pointerId] = 1; down = Object.keys(live).length === 1 ? { id: e.pointerId, x: e.clientX, y: e.clientY } : null; });
addEventListener('pointercancel', function (e) { delete live[e.pointerId]; down = null; });
addEventListener('pointerup', function (e) {
delete live[e.pointerId];
var d = down; down = null;
if (!d || d.id !== e.pointerId || e.target !== H.cv || st.status !== 'OK' || !layerOn() || Object.keys(live).length) return;
if (Math.hypot(e.clientX - d.x, e.clientY - d.y) > SLOP) return;
var R = e.pointerType === 'mouse' ? TAP_MOUSE : TAP_TOUCH, best = null, bd = R * R;
st.marks.forEach(function (m) { if (!m.atNew) return; var s = toScreen(m.x, m.y), q = Math.pow(s[0] - e.clientX, 2) + Math.pow(s[1] - e.clientY, 2); if (q < bd) { bd = q; best = m; } });
if (!best) return;
for (var i = 0; i < st.edSparks.length; i++) { var s = toScreen(st.edSparks[i][0], st.edSparks[i][1]); if (Math.pow(s[0] - e.clientX, 2) + Math.pow(s[1] - e.clientY, 2) < bd) return; }
st.taps++; open(best.row, false);
});
}
// ============================================================ SECTION 2: the readout and the filter (WAFER panel)
var CSS = '#ky-drift{font:12px/1.4 ui-monospace,Menlo,Consolas,monospace;color:#c9d3e6;padding:6px 0 4px;border-top:1px solid rgba(46,230,230,.25)}'
+ '#ky-drift .kd-h{color:#2ee6e6;letter-spacing:.08em;font-size:11px;margin:0;min-height:44px;display:flex;align-items:center;gap:6px;cursor:pointer;list-style:none}'
+ '#ky-drift .kd-h::-webkit-details-marker{display:none}#ky-drift .kd-h::before{content:"\\25b8"}#ky-drift details[open]>.kd-h::before{content:"\\25be"}'
+ '#ky-drift .kd-sum{color:#c9d3e6;letter-spacing:0}'
+ '#ky-drift .kd-s{white-space:pre-line}'
+ '#ky-drift .kd-c{display:flex;flex-wrap:wrap;gap:4px}'
+ '#ky-drift .kd-c button{display:inline-flex!important;align-items:center;width:auto!important;flex:0 0 auto;min-height:44px;min-width:44px;padding:0 8px;background:transparent;border:1px solid rgba(201,211,230,.35);color:inherit;font:inherit;border-radius:3px}'
+ '#ky-drift .kd-c button[aria-pressed=true]{border-color:#2ee6e6;color:#fff}'
+ '#ky-drift .kd-l{max-height:30vh;overflow:auto;overscroll-behavior:contain;margin-top:4px}'
+ '#ky-drift .kd-r{display:block!important;width:100%!important;text-align:left;min-height:44px;padding:4px 2px;background:transparent;border:0;border-bottom:1px solid rgba(201,211,230,.12);color:inherit;font:inherit}'
+ '#ky-drift .kd-r code{display:block;color:#fff;white-space:pre-wrap;word-break:break-all;max-height:4.2em;overflow:hidden}'
+ '#ky-drift .kd-r>span{display:block;white-space:normal;word-break:break-all}'
+ '#ky-drift .kd-w{color:#ffb000}'
+ '#card .ky-drift-cl{white-space:pre-wrap;margin:4px 0;font-size:12px}';
function buildPanel() {
if (!document.getElementById('ky-drift-css')) { var s = document.createElement('style'); s.id = 'ky-drift-css'; s.textContent = CSS; document.head.appendChild(s); }
panel = document.createElement('section'); panel.id = 'ky-drift'; panel.setAttribute('aria-label', 'drift since the cut');
panel.innerHTML = '<details class="kd-d"><summary class="kd-h">DRIFT SINCE THE CUT <span class="kd-sum"></span></summary><div class="kd-s" role="status"></div><div class="kd-c" role="group" aria-label="filter by class"></div><div class="kd-l" role="list"></div></details>';
chips = panel.querySelector('.kd-c'); list = panel.querySelector('.kd-l');
chips.addEventListener('click', function (e) { var b = e.target.closest('button[data-class]'); if (!b) return; st.filter = b.dataset.class; paint(); });
list.addEventListener('click', function (e) { var b = e.target.closest('[data-row]'); if (!b) return; var r = st.rows[+b.dataset.row]; if (r) open(r, true); });
// after the verified-lines list, never between its filter and its rows (issue #10 comment 6567584787)
var lsl = document.getElementById('lineslist');
if (lsl && lsl.parentNode) { lsl.parentNode.insertBefore(panel, lsl.nextSibling); return; }
try { KY.panels.add('wafer', panel, 'drift'); } catch (e) { st.errors.push('panels.add: ' + e.message); }
if (!panel.isConnected) st.errors.push('no #lineslist and no WAFER panel to hold the filter'); // the readout still speaks
}
function paint() {
if (!panel) return;
var s = panel.querySelector('.kd-s');
if (st.status !== 'OK') { panel.querySelector('.kd-sum').textContent = st.status; s.textContent = st.status === 'REFUSED' ? st.why : 'WAIT · ' + st.why; s.className = 'kd-s kd-w'; chips.innerHTML = ''; list.innerHTML = ''; return; }
var d = st.data, sm = d.summary;
s.className = 'kd-s';
panel.querySelector('.kd-sum').textContent = fmt(st.counts.MOVED) + ' moved \u00b7 ' + fmt(st.counts.GONE + st.counts.FILE_GONE) + ' gone \u00b7 ' + fmt(st.counts.UNVERIFIED) + ' unverified';
s.textContent = CLASSES.map(function (k) { return fmt(st.counts[k]) + ' ' + k; }).join(' · ') + '\n' + st.share
+ '\n' + sm.heads_observed + ' of ' + sm.repositories + ' heads observed · cut ' + d.cut_at + (st.registry.indexOf('own') === 0 ? '\nlayer: ' + st.registry : '');
chips.innerHTML = CLASSES.map(function (k) {
return '<button type="button" data-class="' + k + '" aria-pressed="' + (st.filter === k) + '"><span style="color:' + INK[k] + '">●</span> ' + k.toLowerCase().replace('_', ' ') + ' ' + fmt(st.counts[k]) + '</button>';
}).join('');
fillList();
}
function fillList() {
if (!list || st.status !== 'OK') return;
var out = [], n = 0;
st.rows.forEach(function (r, i) {
if (r.class !== st.filter) return; n++;
var b = st.bodyById[r.id + '|' + r.repo], t = b && typeof b.line_text === 'string' ? b.line_text : '';
var tt = t.length > 300 ? t.slice(0, 300) + '… (+' + fmt(t.length - 300) + ' characters, the whole line is on its card)' : t;
var w = r.class === 'MOVED' ? 'MOVED to line ' + r.head_line + ' (' + (r.delta > 0 ? '+' : '') + r.delta + ')' : r.class === 'UNVERIFIED' ? 'UNVERIFIED · ' + r.reason : r.class;
out.push('<button type="button" class="kd-r" role="listitem" data-row="' + i + '" data-class="' + r.class + '"><code>' + esc(tt) + '</code><span class="kd-k">'
+ esc(r.repo + '@' + sha7(r.edition_commit) + ' ' + r.path + ':' + r.line + (r.key != null ? ' · key ' + fmt(r.key) : '')) + '</span><span style="color:' + INK[r.class] + '">' + esc(w) + '</span></button>');
});
list.innerHTML = out.join('') || '<div class="kd-r">no ' + esc(st.filter) + ' rows</div>';
list.dataset.count = String(n);
}
// ============================================================ install
function install(ky) {
KY = ky; H = ky.hooks;
buildPanel(); paint();
new MutationObserver(function () { onCard(); }).observe(H.card, { childList: true });
wireTaps();
KY.draw.add(function (o2) { drawMarks(o2); }, 80);
return H.wafer.then(function () {
st.why = 'loading ' + DATA; paint();
var L = KY.modules && KY.modules.layers && KY.modules.layers.api, p;
if (L && typeof L.register === 'function') {
p = new Promise(function (done) {
try { L.register({ id: ID, module: ID, load: function () { var q = load(); q.then(done, done); return q; } }); st.registry = 'layers'; }
catch (e) { st.registry = 'own (' + e.message + ')'; load().then(done, done); }
});
} else { st.registry = 'own (no layer registry admitted)'; p = load(); }
loadBodies();
return p;
}, function (e) { refuse('the wafer refused, nothing to place: ' + (e && e.message || e)); });
}
function inspect() {
return { id: ID, generation: GEN, status: st.status, why: st.why, counts: st.counts, denominator: st.denominator,
share: st.share, rows: st.rows.length, marks: st.marks.length,
moved: st.marks.filter(function (m) { return m.cls === 'MOVED'; }).map(function (m) { return { id: m.row.id, repo: m.row.repo, k: m.k, atNew: m.atNew, x: m.x, y: m.y, note: m.note }; }),
filter: st.filter, listed: list ? +(list.dataset.count || 0) : 0, registry: st.registry, cards: st.cards, cardLast: st.cardLast,
taps: st.taps, panel: !!(panel && panel.isConnected), errors: st.errors.slice() };
}
var api = { schema: SCHEMA, generation: GEN, inspect: inspect, open: function (id) { var r = st.rows.find(function (x) { return x.id === id; }); return r ? open(r, true) : false; },
filter: function (k) { if (CLASSES.indexOf(k) < 0) return false; st.filter = k; paint(); return true; },
screen: function (id) { var r = st.rows.find(function (x) { return x.id === id; }), m = r && markFor(r); return m ? toScreen(m.x, m.y) : null; } };
if (window.KY && typeof window.KY.register === 'function')
window.KY.register({ schema: 'kuiper.module.v1', id: ID, generation: GEN, requires: REQUIRES, provides: [ID],
install: function (ky) { var p = install(ky); api.ready = p; return api; }, inspect: inspect });
})();