@intentius/behold 0.29.40 → 0.29.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +180 -10
- package/README.md +6 -2
- package/dist/cli.js +16121 -13141
- package/example-writes/README.md +1 -1
- package/example-writes/chant.config.ts +0 -6
- package/package.json +5 -2
- package/web/app.js +327 -21
- package/web/carve-export.js +142 -0
- package/web/carve-narration.js +294 -0
- package/web/carve-narration.test.js +48 -0
- package/web/change-set-overlay.js +147 -0
- package/web/change-set-overlay.test.js +83 -0
- package/web/dots-border.js +66 -0
- package/web/dots-border.test.js +27 -0
- package/web/dots-kiosk.js +279 -0
- package/web/dots-kiosk.test.js +60 -0
- package/web/dots-layout.d.ts +10 -0
- package/web/dots-layout.js +201 -0
- package/web/dots-live.js +316 -0
- package/web/dots-live.test.js +220 -0
- package/web/dots-lod.js +122 -0
- package/web/dots-lod.test.js +78 -0
- package/web/dots-recording.d.ts +7 -0
- package/web/dots-recording.js +247 -0
- package/web/dots-recording.test.js +71 -0
- package/web/dots-scene.js +294 -0
- package/web/dots-scene.test.js +118 -0
- package/web/dots-timeline.js +128 -0
- package/web/dots-timeline.test.js +83 -0
- package/web/dots-tour.js +153 -0
- package/web/dots.js +2124 -0
- package/web/dots.test.js +15 -0
- package/web/event-help-modal.js +1 -1
- package/web/index.html +215 -121
- package/web/kiosk-carve.js +225 -0
- package/web/kiosk-carve.test.js +26 -0
- package/web/kiosk-event-help-ui.js +7 -1
- package/web/kiosk-link.js +68 -0
- package/web/kiosk-link.test.js +69 -0
- package/web/kiosk-metrics.js +2 -2
- package/web/kiosk-trace.js +1 -1
- package/web/kiosk.html +186 -135
- package/web/kiosk.js +65 -7
- package/web/motion.js +42 -0
- package/web/motion.test.js +64 -0
- package/web/operator.js +1 -1
- package/web/run-playhead.js +102 -19
- package/web/run-playhead.test.js +81 -11
- package/web/terralith.html +2 -2
- package/web/theme.js +99 -26
- package/web/theme.test.js +70 -6
- package/web/tooltip.js +212 -0
- package/web/tooltip.test.js +193 -0
- package/web/tour-player.js +204 -0
- package/web/tour-player.test.js +115 -0
- package/web/tours/behold.tour.v1.schema.json +182 -0
- package/web/tours/terralith.json +330 -0
- package/web/trunk-lane.js +406 -0
- package/web/trunk-lane.test.js +224 -0
- package/web/workspace-gates.js +29 -4
- package/web/workspace-gates.test.js +23 -2
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// The dots layer's layout (#712, #715, after #457's scripts/dot-bench/layout.js).
|
|
2
|
+
//
|
|
3
|
+
// Inside an estate's box, a dot's slot is a function of what the resource is:
|
|
4
|
+
// its unit (module path, block stem without its role suffix, count index),
|
|
5
|
+
// with the unit's anchor in the middle, its other taggable members beside it
|
|
6
|
+
// and its untaggable members on rings round them. Which box a dot is in is its
|
|
7
|
+
// owner's (web/dots-scene.js places the boxes). Pure: roster in, positions out.
|
|
8
|
+
|
|
9
|
+
const ROLE_SUFFIXES = [
|
|
10
|
+
"_managed_attach", "_custom_attach", "_attach", "_inline", "_policy", "_profile", "_role",
|
|
11
|
+
"_exec", "_execution", "_task", "_taskdef", "_service", "_svc",
|
|
12
|
+
];
|
|
13
|
+
const ANCHOR_TYPES = ["aws_iam_role", "aws_ecs_service", "aws_route53_zone", "aws_vpc"];
|
|
14
|
+
|
|
15
|
+
/** `module.team_pod["pod-a"].aws_iam_role.pod_role[3]` -> { module, type, name, index }. */
|
|
16
|
+
export function parseAddress(address) {
|
|
17
|
+
const parts = [];
|
|
18
|
+
let depth = 0;
|
|
19
|
+
let cur = "";
|
|
20
|
+
for (const ch of address) {
|
|
21
|
+
if (ch === "[") depth++;
|
|
22
|
+
if (ch === "]") depth--;
|
|
23
|
+
if (ch === "." && depth === 0) {
|
|
24
|
+
parts.push(cur);
|
|
25
|
+
cur = "";
|
|
26
|
+
} else cur += ch;
|
|
27
|
+
}
|
|
28
|
+
parts.push(cur);
|
|
29
|
+
const modules = [];
|
|
30
|
+
while (parts[0] === "module") {
|
|
31
|
+
modules.push(`module.${parts[1]}`);
|
|
32
|
+
parts.splice(0, 2);
|
|
33
|
+
}
|
|
34
|
+
if (parts[0] === "data") parts.shift();
|
|
35
|
+
const type = parts[0] ?? "";
|
|
36
|
+
const m = /^([^[]+)(\[.*\])?$/.exec(parts[1] ?? "");
|
|
37
|
+
return { module: modules.join("."), type, name: m ? m[1] : "", index: m && m[2] ? m[2] : "" };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The unit a resource belongs to: its module path, its block stem and its count index. */
|
|
41
|
+
export function unitOf(address, type) {
|
|
42
|
+
const a = parseAddress(address);
|
|
43
|
+
if (/^aws_route53_/.test(type || a.type)) return `${a.module}|dns`;
|
|
44
|
+
let stem = a.name;
|
|
45
|
+
for (const s of ROLE_SUFFIXES) {
|
|
46
|
+
if (stem.endsWith(s) && stem.length > s.length) {
|
|
47
|
+
stem = stem.slice(0, -s.length);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (/^pod(_|$)/.test(stem)) stem = "pod";
|
|
52
|
+
// A short numbered suffix (`team_a_0`, `team_a_2`: one team's log groups)
|
|
53
|
+
// is the same unit; a generated four-digit id (`svc_0007`) is its own.
|
|
54
|
+
stem = stem.replace(/_\d{1,2}$/, "");
|
|
55
|
+
// A for_each key on a resource (records["www"]) is not a unit of its own;
|
|
56
|
+
// a count index on a team block is, since each index is a whole team.
|
|
57
|
+
const index = /^\["/.test(a.index) ? "" : a.index;
|
|
58
|
+
return `${a.module}|${stem}${index}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const byAddress = (a, b) => (a.address < b.address ? -1 : a.address > b.address ? 1 : 0);
|
|
62
|
+
|
|
63
|
+
/** roster [{address, type, taggable}] -> [{key, anchor, others, children}] in key order. */
|
|
64
|
+
export function unitsOf(roster) {
|
|
65
|
+
const units = new Map();
|
|
66
|
+
for (const r of roster) {
|
|
67
|
+
const key = unitOf(r.address, r.type);
|
|
68
|
+
let u = units.get(key);
|
|
69
|
+
if (!u) units.set(key, (u = { key, members: [] }));
|
|
70
|
+
u.members.push(r);
|
|
71
|
+
}
|
|
72
|
+
return [...units.values()]
|
|
73
|
+
.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
|
|
74
|
+
.map((u) => {
|
|
75
|
+
const taggable = u.members.filter((m) => m.taggable).sort(byAddress);
|
|
76
|
+
const children = u.members.filter((m) => !m.taggable).sort(byAddress);
|
|
77
|
+
const anchor = taggable.find((m) => ANCHOR_TYPES.includes(m.type)) ?? taggable[0] ?? children.shift();
|
|
78
|
+
return { key: u.key, anchor, others: taggable.filter((m) => m !== anchor), children };
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Address -> the anchor of its unit, for every untaggable member (a follower's parent). */
|
|
83
|
+
export function parentsOf(roster) {
|
|
84
|
+
const out = new Map();
|
|
85
|
+
for (const u of unitsOf(roster)) for (const c of u.children) out.set(c.address, u.anchor.address);
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The rings a unit's untaggable members sit on: concentric, one pitch apart,
|
|
91
|
+
* each holding as many as fit at one pitch spacing, so a zone with 140
|
|
92
|
+
* records is a compact disc of rings rather than one huge ring.
|
|
93
|
+
*/
|
|
94
|
+
export function ringsOf(n, start = 1.6) {
|
|
95
|
+
const rings = [];
|
|
96
|
+
let r = start;
|
|
97
|
+
let left = n;
|
|
98
|
+
while (left > 0) {
|
|
99
|
+
const cap = Math.max(4, Math.floor(2 * Math.PI * r));
|
|
100
|
+
const k = Math.min(cap, left);
|
|
101
|
+
rings.push({ r, n: k });
|
|
102
|
+
left -= k;
|
|
103
|
+
r += 1;
|
|
104
|
+
}
|
|
105
|
+
return rings;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A unit's square cell, in dot pitches: its anchor and taggable members in the middle, the rings around them. */
|
|
109
|
+
export function unitCell(u) {
|
|
110
|
+
// With taggable members round the anchor, the rings start outside them.
|
|
111
|
+
const rings = ringsOf(u.children.length, u.others.length ? 2.2 : 1.6);
|
|
112
|
+
const core = 1 + Math.ceil(u.others.length / 2);
|
|
113
|
+
// The core's members sit one pitch from the anchor; rings start outside them.
|
|
114
|
+
const shift = u.others.length > 6 ? Math.ceil(u.others.length / 6) : 0;
|
|
115
|
+
const outer = rings.length ? rings[rings.length - 1].r + shift : 0;
|
|
116
|
+
return { rings, shift, cell: 2 * Math.max(outer + 1, core) + 1 };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The row width (in pitches) that keeps a block of these units near `aspect` (width / height). */
|
|
120
|
+
export function blockWidth(units, aspect = 1.3) {
|
|
121
|
+
const cells = units.map((u) => unitCell(u).cell);
|
|
122
|
+
const area = cells.reduce((s, c) => s + c * c, 0);
|
|
123
|
+
return Math.max(Math.sqrt(area * aspect) * 1.04, ...cells, 1);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The dot slots of one block, in pitches. Units are packed into rows of a
|
|
128
|
+
* fixed `width`, so a box keeps its width as units come and go.
|
|
129
|
+
*
|
|
130
|
+
* `fromEnd` packs from the last unit backwards, right to left and bottom to
|
|
131
|
+
* top, with the bottom edge at y = 0 (slots have y <= 0). A unit's slot then
|
|
132
|
+
* depends only on the units after it, so units leaving from the front (the
|
|
133
|
+
* carve takes them in unit order) never move the ones that stay: the box just
|
|
134
|
+
* loses its top rows. Reading order is kept: the first unit is still top-left.
|
|
135
|
+
* Without it, units are packed from the top-left, and a unit appended at the
|
|
136
|
+
* end never moves the ones before it.
|
|
137
|
+
*/
|
|
138
|
+
export function packBlock(units, { width = blockWidth(units), fromEnd = false } = {}) {
|
|
139
|
+
const list = fromEnd ? [...units].reverse() : units;
|
|
140
|
+
const slots = new Map();
|
|
141
|
+
let x = 0;
|
|
142
|
+
let y = 0;
|
|
143
|
+
let rowH = 0;
|
|
144
|
+
let maxX = 0;
|
|
145
|
+
for (const u of list) {
|
|
146
|
+
const { rings, shift, cell } = unitCell(u);
|
|
147
|
+
if (x + cell > width && x > 0) {
|
|
148
|
+
x = 0;
|
|
149
|
+
y += rowH;
|
|
150
|
+
rowH = 0;
|
|
151
|
+
}
|
|
152
|
+
const put = (a, px, py, parent) => {
|
|
153
|
+
const sx = fromEnd ? width - px : px;
|
|
154
|
+
const sy = fromEnd ? -py : py;
|
|
155
|
+
slots.set(a, parent ? { x: sx, y: sy, parent } : { x: sx, y: sy });
|
|
156
|
+
};
|
|
157
|
+
const cx = x + cell / 2;
|
|
158
|
+
const cy = y + cell / 2;
|
|
159
|
+
put(u.anchor.address, cx, cy);
|
|
160
|
+
u.others.forEach((m, i) => {
|
|
161
|
+
const a = (i / Math.max(1, u.others.length)) * Math.PI * 2;
|
|
162
|
+
const r = 1 + Math.floor(i / 6) * 0.9;
|
|
163
|
+
put(m.address, cx + Math.cos(a) * r, cy + Math.sin(a) * r);
|
|
164
|
+
});
|
|
165
|
+
let j = 0;
|
|
166
|
+
for (const ring of rings) {
|
|
167
|
+
for (let q = 0; q < ring.n; q++, j++) {
|
|
168
|
+
const a = (q / ring.n) * Math.PI * 2 - Math.PI / 2;
|
|
169
|
+
const r = ring.r + shift;
|
|
170
|
+
put(u.children[j].address, cx + Math.cos(a) * r, cy + Math.sin(a) * r, u.anchor.address);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
x += cell;
|
|
174
|
+
maxX = Math.max(maxX, x);
|
|
175
|
+
rowH = Math.max(rowH, cell);
|
|
176
|
+
}
|
|
177
|
+
const h = y + rowH;
|
|
178
|
+
return { slots, w: fromEnd ? width : maxX, h, top: fromEnd ? -h : 0 };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Shelf-pack boxes (each {key, w, h}) in the given order into rows about
|
|
183
|
+
* `width` wide, with `gap` between them. Returns Map<key, {x, y}> (top-left).
|
|
184
|
+
*/
|
|
185
|
+
export function shelfPack(boxes, { width, gap = 0 }) {
|
|
186
|
+
const out = new Map();
|
|
187
|
+
let x = 0;
|
|
188
|
+
let y = 0;
|
|
189
|
+
let rowH = 0;
|
|
190
|
+
for (const b of boxes) {
|
|
191
|
+
if (x > 0 && x + b.w > width) {
|
|
192
|
+
x = 0;
|
|
193
|
+
y += rowH + gap;
|
|
194
|
+
rowH = 0;
|
|
195
|
+
}
|
|
196
|
+
out.set(b.key, { x, y });
|
|
197
|
+
x += b.w + gap;
|
|
198
|
+
rowH = Math.max(rowH, b.h);
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
package/web/dots-live.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// The dots layer's live driver (#715): terragucci's estate rosters and its
|
|
2
|
+
// migrations' progress lines (/api/terragucci/carve, src/terragucci-carve.ts)
|
|
3
|
+
// read into the same "show" a recording makes (web/dots-recording.js), so the
|
|
4
|
+
// one layer draws both. Pure: the route's answer in, plain data out.
|
|
5
|
+
//
|
|
6
|
+
// What a dot is: one resource instance an estate's roster or a progress line
|
|
7
|
+
// names, keyed by its live id. A stock root's roster has no live ids (it is
|
|
8
|
+
// written from the state), so its dots are keyed by estate and address until a
|
|
9
|
+
// stamp names their live id; a move then finds them by that id.
|
|
10
|
+
//
|
|
11
|
+
// Each progress line is one event:
|
|
12
|
+
// stamped an adoption: the dot gains a ring, since it now wears its tags
|
|
13
|
+
// (and moves, when the line names another estate);
|
|
14
|
+
// moved a split: the dot flies to its new estate's box;
|
|
15
|
+
// refused the move is refused: the dot bounces back and stays lit red;
|
|
16
|
+
// skipped nothing to draw; it is counted.
|
|
17
|
+
//
|
|
18
|
+
// Lines play as they arrive, in real time. A line already in the first
|
|
19
|
+
// answer happened before the page looked, so it is drawn where it ended, with
|
|
20
|
+
// no flight; a line that arrives later plays from the moment it was read. An
|
|
21
|
+
// adoption on floci stamps 82 resources in under a second, so its lines come
|
|
22
|
+
// in one read and play as the one burst they were. Slowed playback is a
|
|
23
|
+
// person's choice (the kiosk's "slowed ×N", web/tour-player.js SLOWED): the
|
|
24
|
+
// lines of one read are then drawn `stepMs` apart, queued after the last one
|
|
25
|
+
// still to play. A recording's replay paces itself (web/dots-scene.js).
|
|
26
|
+
//
|
|
27
|
+
// An adoption turns a stock root into an estate (the answer's `adopted`
|
|
28
|
+
// pairs them). That is one box changing its name, not two boxes: the
|
|
29
|
+
// estate's resources and lines are drawn in the stock root's box, the stamps
|
|
30
|
+
// ring the dots where they are, and the box takes the estate's name when the
|
|
31
|
+
// first stamp plays.
|
|
32
|
+
|
|
33
|
+
/** How far apart the lines of one read play by default (0: real time, as read), and how far ahead of a line its flight may start. */
|
|
34
|
+
export const LIVE = { stepMs: 0, lead: 2_500 };
|
|
35
|
+
|
|
36
|
+
/** A progress line's key: the same line in two reads is the same event. */
|
|
37
|
+
export function lineKey(migration, line) {
|
|
38
|
+
return `${migration}|${line.at}|${line.estate_from}|${line.address}|${line.outcome}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* When each line is drawn from: `seen` maps a line's key to that time. Lines
|
|
43
|
+
* of the first read keep their own `at` (the past); the lines new in a later
|
|
44
|
+
* read play `stepMs` apart in the order they were written, from `now`, or
|
|
45
|
+
* after the last line of an earlier batch still waiting to play. Returns a new
|
|
46
|
+
* map; `seen` is not changed.
|
|
47
|
+
*/
|
|
48
|
+
export function stampArrivals(answer, seen, now, { first = false, stepMs = LIVE.stepMs } = {}) {
|
|
49
|
+
const out = new Map(seen);
|
|
50
|
+
const fresh = [];
|
|
51
|
+
for (const m of answer && answer.migrations ? answer.migrations : []) {
|
|
52
|
+
for (const l of m.lines) {
|
|
53
|
+
const k = lineKey(m.name, l);
|
|
54
|
+
if (out.has(k)) continue;
|
|
55
|
+
if (first) out.set(k, Date.parse(l.at) || now);
|
|
56
|
+
else fresh.push({ k, at: Date.parse(l.at) || 0 });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
fresh.sort((a, b) => a.at - b.at);
|
|
60
|
+
let queued = -Infinity;
|
|
61
|
+
for (const t of seen.values()) if (t > queued) queued = t;
|
|
62
|
+
let next = Math.max(now, queued + stepMs);
|
|
63
|
+
for (const f of fresh) {
|
|
64
|
+
out.set(f.k, next);
|
|
65
|
+
next += stepMs;
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The name an estate's box carries at show time `t`: a stock root an
|
|
72
|
+
* adoption replaced takes its estate's name once the first stamp plays.
|
|
73
|
+
*/
|
|
74
|
+
export function boxName(show, estate, t) {
|
|
75
|
+
const r = show && show.renames ? show.renames.get(estate) : undefined;
|
|
76
|
+
return r && t >= r.at ? r.to : estate;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const unknownType = (address) => {
|
|
80
|
+
const m = /(?:^|\.)([a-z][a-z0-9]*_[a-z0-9_]+)\.[^.]+$/.exec(address.replace(/\[[^\]]*\]/g, ""));
|
|
81
|
+
return m ? m[1] : "";
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The live carve as a show. `seen` is {@link stampArrivals}' map; `now` is the
|
|
86
|
+
* show's end so far. `t0`, when given, is the start an earlier read chose, so
|
|
87
|
+
* the show's clock stays put from one read to the next.
|
|
88
|
+
*/
|
|
89
|
+
export function liveShow(answer, seen, now, { t0: start = null } = {}) {
|
|
90
|
+
const dots = new Map(); // id -> { id, address, type, taggable, estate0, live_id, marked }
|
|
91
|
+
const byLive = new Map();
|
|
92
|
+
const byAddr = new Map();
|
|
93
|
+
const usedAddr = new Set();
|
|
94
|
+
const place = (estate, address) => `${estate}\u0000${address}`;
|
|
95
|
+
const addDot = (estate, r) => {
|
|
96
|
+
// A dot's address is its address where it was first seen, kept unique so the layout can key on it.
|
|
97
|
+
let address = r.address;
|
|
98
|
+
for (let n = 2; usedAddr.has(address); n++) address = `${r.address}#${n}`;
|
|
99
|
+
usedAddr.add(address);
|
|
100
|
+
const d = { id: address, address, type: r.type || unknownType(r.address), taggable: !r.follows, estate0: estate, live_id: r.live_id || null, marked: !!r.marked, follows: r.follows || null, home: estate };
|
|
101
|
+
dots.set(d.id, d);
|
|
102
|
+
byAddr.set(place(estate, r.address), d);
|
|
103
|
+
if (d.live_id) byLive.set(d.live_id, d);
|
|
104
|
+
return d;
|
|
105
|
+
};
|
|
106
|
+
const find = (estate, r) => (r.live_id && byLive.get(r.live_id)) || byAddr.get(place(estate, r.address));
|
|
107
|
+
|
|
108
|
+
// An adopted stock root and its estate are one box, keyed by the stock root's name.
|
|
109
|
+
const alias = new Map(((answer && answer.adopted) || []).map((a) => [a.to, a.from]));
|
|
110
|
+
const as = (e) => alias.get(e) ?? e;
|
|
111
|
+
const estates = ((answer && answer.estates) || []).map((e) => ({ ...e, estate: as(e.estate) }));
|
|
112
|
+
const migrations = ((answer && answer.migrations) || []).map((m) => ({ ...m, lines: m.lines.map((l) => ({ ...l, estate_from: as(l.estate_from), estate_to: as(l.estate_to), key: lineKey(m.name, l), into: l.estate_to, rawFrom: l.estate_from })) }));
|
|
113
|
+
// 1. A stock root's resources, which only an address names.
|
|
114
|
+
for (const e of estates) for (const r of e.resources) if (!r.live_id && !find(e.estate, r)) addDot(e.estate, r);
|
|
115
|
+
// 2. Each line in time order, finding its dot by live id, else where it came from.
|
|
116
|
+
const lines = [];
|
|
117
|
+
for (const m of migrations) for (const l of m.lines) lines.push({ m: m.name, l, t: seen.get(l.key) ?? (Date.parse(l.at) || now) });
|
|
118
|
+
lines.sort((a, b) => a.t - b.t);
|
|
119
|
+
const claimed = new Set();
|
|
120
|
+
for (const x of lines) {
|
|
121
|
+
const { l } = x;
|
|
122
|
+
let d = find(l.estate_from, l);
|
|
123
|
+
// A destination roster written after the move (a wave's, from planned values, so with no live ids) already drew
|
|
124
|
+
// the resource in the estate it moved to: the line moves that dot, from where it came, never a second one.
|
|
125
|
+
if (!d) {
|
|
126
|
+
const there = byAddr.get(place(l.estate_to, l.address));
|
|
127
|
+
if (there && !claimed.has(there) && there.estate0 === l.estate_to) {
|
|
128
|
+
d = there;
|
|
129
|
+
d.estate0 = l.estate_from;
|
|
130
|
+
byAddr.set(place(l.estate_from, l.address), d);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!d) d = addDot(l.estate_from, { address: l.address, type: "", live_id: l.live_id, follows: l.follows });
|
|
134
|
+
if (l.live_id && !d.live_id) {
|
|
135
|
+
d.live_id = l.live_id;
|
|
136
|
+
byLive.set(l.live_id, d);
|
|
137
|
+
}
|
|
138
|
+
// The destination's roster named this resource by address only (a wave's roster, written after the move), and
|
|
139
|
+
// an earlier line of the cycle (the adoption's stamp) already made its dot: one dot, under the plain address.
|
|
140
|
+
const old = byAddr.get(place(l.estate_to, l.address));
|
|
141
|
+
if (old && old !== d && !claimed.has(old) && old.estate0 === l.estate_to && !old.live_id) {
|
|
142
|
+
dots.delete(old.id);
|
|
143
|
+
if (d.id !== old.id) {
|
|
144
|
+
dots.delete(d.id);
|
|
145
|
+
usedAddr.delete(d.id);
|
|
146
|
+
d.id = d.address = old.id;
|
|
147
|
+
dots.set(d.id, d);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
byAddr.set(place(l.estate_to, l.address), d);
|
|
151
|
+
claimed.add(d);
|
|
152
|
+
x.dot = d;
|
|
153
|
+
}
|
|
154
|
+
// 3. Every other estate's resources: by live id, else by address, else a dot of their own.
|
|
155
|
+
for (const e of estates) {
|
|
156
|
+
for (const r of e.resources) {
|
|
157
|
+
const d = find(e.estate, r);
|
|
158
|
+
if (d) {
|
|
159
|
+
if (r.marked) d.marked = true;
|
|
160
|
+
if (!d.type && r.type) d.type = r.type;
|
|
161
|
+
if (r.follows) d.follows = d.follows || r.follows;
|
|
162
|
+
d.home = e.estate;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
addDot(e.estate, r).home = e.estate;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The source: where the first line came from, else the estate holding the most dots.
|
|
170
|
+
const owners0 = new Map([...dots.values()].map((d) => [d.id, d.estate0]));
|
|
171
|
+
let source = lines.length ? lines[0].l.estate_from : null;
|
|
172
|
+
if (!source) {
|
|
173
|
+
const n = new Map();
|
|
174
|
+
for (const e of owners0.values()) n.set(e, (n.get(e) ?? 0) + 1);
|
|
175
|
+
source = [...n].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))[0]?.[0] ?? (estates[0] ? estates[0].estate : "estate");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Parents: a line or roster entry that follows another names it by address.
|
|
179
|
+
const parents = new Map();
|
|
180
|
+
for (const d of dots.values()) {
|
|
181
|
+
if (!d.follows) continue;
|
|
182
|
+
const p = byAddr.get(place(d.estate0, d.follows)) || [...dots.values()].find((q) => q.address === d.follows);
|
|
183
|
+
if (p && p !== d) parents.set(d.id, p.id);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Events.
|
|
187
|
+
const events = [];
|
|
188
|
+
const writeOf = new Map(); // `${migration}|${parent id}` -> its write, for followers
|
|
189
|
+
const followerLines = new Map(); // dot id -> the line of a follower that flew with its parent
|
|
190
|
+
let t0 = now;
|
|
191
|
+
for (const d of dots.values()) if (d.marked && !lines.some((x) => x.dot === d && x.l.outcome === "stamped")) events.push({ kind: "adopt", t: -Infinity, start: -Infinity, address: d.id, before: true });
|
|
192
|
+
for (const x of lines) {
|
|
193
|
+
const { l, t, dot } = x;
|
|
194
|
+
t0 = Math.min(t0, t);
|
|
195
|
+
if (l.outcome === "skipped") continue;
|
|
196
|
+
if (l.outcome === "stamped") {
|
|
197
|
+
events.push({ kind: "adopt", t: t + 300, start: t, address: dot.id, migration: x.m, at: Date.parse(l.at) || t, line: { outcome: "stamped", address: l.address, from: l.rawFrom, to: l.into, at: l.at } });
|
|
198
|
+
if (l.estate_to === l.estate_from) continue;
|
|
199
|
+
}
|
|
200
|
+
const ok = l.outcome !== "refused";
|
|
201
|
+
const parent = l.follows ? parents.get(dot.id) : undefined;
|
|
202
|
+
const pw = parent && writeOf.get(`${x.m}|${parent}`);
|
|
203
|
+
if (pw && ok && pw.ok) {
|
|
204
|
+
pw.followers.push(dot.id);
|
|
205
|
+
// A follower's own line, for its provenance: it moved with its parent's write.
|
|
206
|
+
followerLines.set(dot.id, { outcome: l.outcome, address: l.address, from: l.rawFrom, to: l.into, at: l.at, migration: x.m, start: t, follows: l.follows });
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const w = { kind: "write", t: t + 1, start: t, address: dot.id, followers: [], from: l.estate_from, to: l.estate_to, ok, migration: x.m, at: Date.parse(l.at) || t, line: { outcome: l.outcome, address: l.address, from: l.rawFrom, to: l.into, at: l.at, reason: l.reason || null }, ...(ok ? {} : { refusal: "refused", refusalSummary: l.reason || "the move was refused" }) };
|
|
210
|
+
writeOf.set(`${x.m}|${dot.id}`, w);
|
|
211
|
+
events.push(w);
|
|
212
|
+
}
|
|
213
|
+
t0 = start !== null && Number.isFinite(start) ? Math.min(start, t0 - LIVE.lead) : Math.min(t0, now) - LIVE.lead;
|
|
214
|
+
for (const ev of events) if (ev.before) ev.t = ev.start = t0;
|
|
215
|
+
events.sort((a, b) => a.start - b.start);
|
|
216
|
+
|
|
217
|
+
const ownersEnd = new Map(owners0);
|
|
218
|
+
for (const ev of events) {
|
|
219
|
+
if (ev.kind !== "write" || !ev.ok) continue;
|
|
220
|
+
ownersEnd.set(ev.address, ev.to);
|
|
221
|
+
for (const f of ev.followers) ownersEnd.set(f, ev.to);
|
|
222
|
+
}
|
|
223
|
+
for (const [child, parent] of parents) if (!events.some((e) => e.address === child)) ownersEnd.set(child, ownersEnd.get(parent) ?? ownersEnd.get(child));
|
|
224
|
+
|
|
225
|
+
// The box's new name plays with the first stamp into the estate; with none, it has the estate's name already.
|
|
226
|
+
const renames = new Map();
|
|
227
|
+
for (const [to, from] of alias) {
|
|
228
|
+
const stamps = lines.filter((x) => x.l.outcome === "stamped" && x.l.into === to).map((x) => x.t);
|
|
229
|
+
renames.set(from, { to, at: stamps.length ? Math.min(...stamps) : -Infinity });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const roster = [...dots.values()].map((d) => ({ address: d.id, type: d.type, taggable: d.taggable, ...(d.live_id ? { live_id: d.live_id } : {}) }));
|
|
233
|
+
const names = [...new Set([source, ...estates.map((e) => e.estate), ...ownersEnd.values()])];
|
|
234
|
+
return {
|
|
235
|
+
kind: "live",
|
|
236
|
+
title: source,
|
|
237
|
+
recordedAt: null,
|
|
238
|
+
source,
|
|
239
|
+
estates: names,
|
|
240
|
+
roster,
|
|
241
|
+
owners0,
|
|
242
|
+
ownersEnd,
|
|
243
|
+
events,
|
|
244
|
+
threads: [],
|
|
245
|
+
phases: [],
|
|
246
|
+
parents,
|
|
247
|
+
t0,
|
|
248
|
+
t1: now,
|
|
249
|
+
spaced: false,
|
|
250
|
+
renames,
|
|
251
|
+
followerLines,
|
|
252
|
+
counts: Object.fromEntries(estates.map((e) => [e.estate, e.count])),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The write that landed a dot by show time `t`, as provenance reads it: the
|
|
258
|
+
* newest move, refusal or stamp of that dot that has played, with the
|
|
259
|
+
* migration that wrote it. Null when no line of this cycle names the dot.
|
|
260
|
+
*/
|
|
261
|
+
export function landedWrite(show, id, t) {
|
|
262
|
+
let best = null;
|
|
263
|
+
for (const ev of (show && show.events) || []) {
|
|
264
|
+
if (!ev.line || ev.before || ev.start > t) continue;
|
|
265
|
+
const mine = ev.address === id;
|
|
266
|
+
if (!mine) continue;
|
|
267
|
+
if (!best || ev.start >= best.start) best = { ...ev.line, migration: ev.migration, start: ev.start };
|
|
268
|
+
}
|
|
269
|
+
const f = show && show.followerLines ? show.followerLines.get(id) : null;
|
|
270
|
+
if (f && f.start <= t && (!best || f.start >= best.start)) best = f;
|
|
271
|
+
return best;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** What changed between two shows, so the layer rebuilds only when it must. */
|
|
275
|
+
export function showKey(show) {
|
|
276
|
+
const last = show.events.length ? show.events[show.events.length - 1] : null;
|
|
277
|
+
const ends = new Map();
|
|
278
|
+
for (const e of show.ownersEnd.values()) ends.set(e, (ends.get(e) ?? 0) + 1);
|
|
279
|
+
return [show.source, show.roster.length, show.events.length, last ? `${last.address}@${last.start}` : "", [...ends].sort().join(",")].join("|");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The newest migration that is running, else the newest with lines; null when none. */
|
|
283
|
+
export function currentMigration(answer) {
|
|
284
|
+
const ms = (answer && answer.migrations) || [];
|
|
285
|
+
const newest = (m) => m.lines.reduce((a, l) => Math.max(a, Date.parse(l.at) || 0), 0);
|
|
286
|
+
const running = ms.filter((m) => m.running).sort((a, b) => newest(b) - newest(a));
|
|
287
|
+
if (running.length) return running[0];
|
|
288
|
+
return ms.filter((m) => m.lines.length).sort((a, b) => newest(b) - newest(a))[0] || null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Whether the layer has anything to draw: the gate is open and some roster or line names a resource. */
|
|
292
|
+
export function carveDrawable(answer) {
|
|
293
|
+
if (!answer || answer.live !== true) return false;
|
|
294
|
+
return (answer.estates || []).some((e) => e.resources.length > 0) || (answer.migrations || []).some((m) => m.lines.length > 0);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* A migration's counts as the page has played them by show time `t`: each
|
|
299
|
+
* line counts once its paced time has come. While lines are still to play,
|
|
300
|
+
* the counts are those lines' and the rest are left; once all have played,
|
|
301
|
+
* the record's own counts. `playing` says which.
|
|
302
|
+
*/
|
|
303
|
+
export function playedCounts(migration, seen, t) {
|
|
304
|
+
const c = { moved: 0, stamped: 0, refused: 0, skipped: 0 };
|
|
305
|
+
let n = 0;
|
|
306
|
+
for (const l of migration.lines) {
|
|
307
|
+
const at = seen.get(lineKey(migration.name, l)) ?? (Date.parse(l.at) || 0);
|
|
308
|
+
if (at > t) continue;
|
|
309
|
+
c[l.outcome] = (c[l.outcome] ?? 0) + 1;
|
|
310
|
+
n++;
|
|
311
|
+
}
|
|
312
|
+
if (n === migration.lines.length) return { counts: migration.counts, playing: false };
|
|
313
|
+
const r = migration.counts;
|
|
314
|
+
const total = r.moved + r.stamped + r.refused + r.skipped + (r.left ?? 0);
|
|
315
|
+
return { counts: { ...c, left: Math.max(0, Math.max(total, migration.lines.length) - n) }, playing: true };
|
|
316
|
+
}
|