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="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Client, server and sessions</title>
|
| 7 |
+
<!-- Generated by build.py from fragments/. Runtime and theme tokens come from the research-article template. -->
|
| 8 |
+
<link rel="stylesheet" href="runtime/variables.css">
|
| 9 |
+
<script>
|
| 10 |
+
// The iframe can't read the course page's theme, so follow ?theme= if given, else the OS setting.
|
| 11 |
+
(function () {
|
| 12 |
+
const forced = new URLSearchParams(location.search).get('theme');
|
| 13 |
+
const mq = matchMedia('(prefers-color-scheme: dark)');
|
| 14 |
+
const apply = () => document.documentElement.setAttribute('data-theme', forced || (mq.matches ? 'dark' : 'light'));
|
| 15 |
+
apply();
|
| 16 |
+
if (!forced) mq.addEventListener('change', apply);
|
| 17 |
+
})();
|
| 18 |
+
</script>
|
| 19 |
+
<script src="runtime/color-palettes.js"></script>
|
| 20 |
+
<script src="runtime/rough.js"></script>
|
| 21 |
+
<script src="runtime/hand.js"></script>
|
| 22 |
+
<script src="runtime/embed-runtime.js"></script>
|
| 23 |
+
<style>
|
| 24 |
+
html, body { margin: 0; padding: 0; }
|
| 25 |
+
/* An opaque card with its own background, so it stays readable whatever theme the course page uses */
|
| 26 |
+
body { background: transparent; }
|
| 27 |
+
.figure-card { font-family: 'Source Sans 3', var(--default-font-family); color: var(--text-color); background: var(--page-bg);
|
| 28 |
+
border: 1px solid var(--border-color); border-radius: 10px; padding: 12px 14px 10px; }
|
| 29 |
+
</style>
|
| 30 |
+
<script>
|
| 31 |
+
// Clean look by default, in Source Sans. ?style=hand brings back the old hand-drawn look for comparison:
|
| 32 |
+
// it loads Excalifont and uses it for the whole card.
|
| 33 |
+
if (new URLSearchParams(location.search).get('style') === 'hand') {
|
| 34 |
+
const st = document.createElement('style');
|
| 35 |
+
st.textContent = "@font-face { font-family: 'Excalifont'; src: url('runtime/fonts/Excalifont-Regular.woff2') format('woff2'); font-display: swap; }" +
|
| 36 |
+
".figure-card { font-family: 'Excalifont', 'Source Sans 3', var(--default-font-family); }";
|
| 37 |
+
document.head.appendChild(st);
|
| 38 |
+
} else {
|
| 39 |
+
document.documentElement.classList.add('style-clean');
|
| 40 |
+
const l = document.createElement('link'); l.rel = 'stylesheet';
|
| 41 |
+
l.href = 'https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@400;600;700&display=swap';
|
| 42 |
+
document.head.appendChild(l);
|
| 43 |
+
}
|
| 44 |
+
</script>
|
| 45 |
+
</head>
|
| 46 |
+
<body>
|
| 47 |
+
<div class="figure-card">
|
| 48 |
+
<!-- title: Client, server and sessions -->
|
| 49 |
+
<div class="fig-client-server"></div>
|
| 50 |
+
<style>
|
| 51 |
+
.fig-client-server { position: relative; color: var(--text-color); font-size: 14px; }
|
| 52 |
+
.fig-client-server svg { display: block; width: 100%; overflow: visible; }
|
| 53 |
+
.fig-client-server svg .cs-hit { cursor: pointer; outline: none; }
|
| 54 |
+
.fig-client-server .fig-note { margin: 10px 0 0; line-height: 1.45; font-size: 15px; }
|
| 55 |
+
</style>
|
| 56 |
+
<script>
|
| 57 |
+
Embed.mount('fig-client-server', (ctx) => {
|
| 58 |
+
const NS = 'http://www.w3.org/2000/svg';
|
| 59 |
+
// The layers, with the names and order of the list on the OpenEnv page ("What runs the code"),
|
| 60 |
+
// from the strongest. Checked against envs/data_analysis_env/server/sandbox.py at 0c9f9cca and
|
| 61 |
+
// the server block of scripts/unit5/train_grpo_data.py (server as root, uids from 61000).
|
| 62 |
+
const LAYERS = [
|
| 63 |
+
{ name: 'A user per session', note: "The code can't read the trainer's or the server's environment or memory, files only root can read, or another session's files." },
|
| 64 |
+
{ name: 'No sockets', note: "The code can't open a connection to anything, on the internet or on the same machine." },
|
| 65 |
+
{ name: 'Limits and a clean environment', note: "No call runs past 30 seconds or past the session's memory cap, and no token in the server's environment reaches the code." },
|
| 66 |
+
{ name: 'An audit hook', note: "Python itself refuses reads outside the task's files, sockets and other known escapes, as an extra check on top of the others." },
|
| 67 |
+
];
|
| 68 |
+
const UID = 61000;
|
| 69 |
+
let active = 0, width = 640, C = null;
|
| 70 |
+
|
| 71 |
+
const svg = document.createElementNS(NS, 'svg');
|
| 72 |
+
const note = document.createElement('p');
|
| 73 |
+
note.className = 'fig-note';
|
| 74 |
+
note.setAttribute('aria-live', 'polite');
|
| 75 |
+
ctx.root.append(svg, note);
|
| 76 |
+
ctx.describe('An HF Job with two processes. On the left, the training process, with the TRL trainer and vLLM, is the only process that holds HF_TOKEN. It opens one client per episode. Each client has its own WebSocket connection to the environment server, and each connection gets its own session. On the right, the environment server, started inside the same job and running as root, has three sessions. Each session runs in its own worker process as its own user, uid 61000, 61001 and 61002, with its Python session and the task\'s files. The third session shows the four safety layers from the page, from the strongest. 1, a user per session: the code can\'t read the trainer\'s or the server\'s environment or memory, files only root can read, or another session\'s files. 2, no sockets: the code can\'t open a connection to anything. 3, limits and a clean environment: a 30-second timeout per call, a memory cap per session, and no token from the server\'s environment. 4, an audit hook: Python refuses reads outside the task\'s files, sockets and other escapes, as an extra check. Below the job, a note says the same client code can also connect to a Space on the Hub by URL.');
|
| 77 |
+
|
| 78 |
+
const measure = document.createElement('canvas').getContext('2d');
|
| 79 |
+
const textW = (s, size, weight = 400) => { measure.font = `${weight} ${size}px ${Hand.FONT}`; return measure.measureText(s).width; };
|
| 80 |
+
function wrap(text, maxW, size, weight) {
|
| 81 |
+
const out = [];
|
| 82 |
+
text.split(' ').forEach((w) => {
|
| 83 |
+
const last = out[out.length - 1];
|
| 84 |
+
if (last && textW(last + ' ' + w, size, weight) <= maxW) out[out.length - 1] = last + ' ' + w;
|
| 85 |
+
else out.push(w);
|
| 86 |
+
});
|
| 87 |
+
return out;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
function setActive(k) {
|
| 91 |
+
if (k === active) return;
|
| 92 |
+
active = k;
|
| 93 |
+
paintActive();
|
| 94 |
+
}
|
| 95 |
+
function writeNote(k) {
|
| 96 |
+
note.replaceChildren();
|
| 97 |
+
const strong = document.createElement('strong');
|
| 98 |
+
strong.textContent = `${k + 1}. ${LAYERS[k].name}.`;
|
| 99 |
+
note.append(strong, ' ' + LAYERS[k].note);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
let hiBack = null, hiFront = null, shells = [];
|
| 103 |
+
function paintActive() {
|
| 104 |
+
if (!hiBack) return;
|
| 105 |
+
hiBack.replaceChildren();
|
| 106 |
+
hiFront.replaceChildren();
|
| 107 |
+
const s = shells[active];
|
| 108 |
+
Hand.create(hiBack, { seed: 90 }).rect(s.x, s.y, s.w, s.h, { fill: C.yellowSoft, stroke: 'none', seed: 9001 });
|
| 109 |
+
Hand.create(hiFront, { seed: 91 }).rect(s.x, s.y, s.w, s.h, { stroke: C.ink, width: 2, seed: 9002 });
|
| 110 |
+
shells.forEach((sh, k) => sh.label.setAttribute('font-weight', k === active ? 700 : 400));
|
| 111 |
+
writeNote(active);
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
function draw() {
|
| 115 |
+
if (!C) return;
|
| 116 |
+
const narrow = width < 600;
|
| 117 |
+
const pad = narrow ? 8 : 12; // job frame to the boxes inside it
|
| 118 |
+
const inset = 10; // box edge to its content
|
| 119 |
+
const lh = 17;
|
| 120 |
+
const tool = Hand.role(C, 'tool'), model = Hand.role(C, 'assistant');
|
| 121 |
+
const y0 = 30; // under the job label
|
| 122 |
+
|
| 123 |
+
// --- geometry -------------------------------------------------------------------------
|
| 124 |
+
let T, S, gapW = 0; // trainer box, server box
|
| 125 |
+
if (narrow) {
|
| 126 |
+
T = { x: pad, w: width - 2 * pad };
|
| 127 |
+
S = { x: pad, w: width - 2 * pad };
|
| 128 |
+
} else {
|
| 129 |
+
const inner = width - 2 * pad;
|
| 130 |
+
T = { x: pad, w: Math.round(inner * 0.25) };
|
| 131 |
+
gapW = Math.round(inner * 0.16);
|
| 132 |
+
S = { x: T.x + T.w + gapW, w: inner - T.w - gapW };
|
| 133 |
+
}
|
| 134 |
+
const tLines = wrap('TRL trainer and vLLM', T.w - 2 * inset, 13);
|
| 135 |
+
const tokenW = Math.min(T.w - 2 * inset, textW('HF_TOKEN, only here', 12.5, 600) + 16);
|
| 136 |
+
const tHeadH = 26 + tLines.length * lh + 8 + 24; // title, lines, token chip
|
| 137 |
+
const sLines = wrap('started inside the same job, as root', S.w - 2 * inset, 13);
|
| 138 |
+
const sHeadH = 26 + sLines.length * lh + 6;
|
| 139 |
+
|
| 140 |
+
// sessions: two compact ones and one with its layers
|
| 141 |
+
const sx = S.x + inset, sw = S.w - 2 * inset;
|
| 142 |
+
const compactH = 14 + 3 * lh + 4;
|
| 143 |
+
const shellTop = 22, shellSide = 9, shellBottom = 9;
|
| 144 |
+
const shellW1 = sw - 2 * 8;
|
| 145 |
+
const coreW = shellW1 - 2 * 3 * shellSide - 16;
|
| 146 |
+
const stack = coreW < 300;
|
| 147 |
+
const coreH = stack ? 2 * 28 + 6 + 4 : 28 + 4;
|
| 148 |
+
const shellH1 = 4 * shellTop + 3 * shellBottom + coreH + shellBottom;
|
| 149 |
+
const detailH = 46 + shellH1 + 8;
|
| 150 |
+
const clientH = 40;
|
| 151 |
+
|
| 152 |
+
let tTop, tH, sTop, sH, comp = [], det, clients = [];
|
| 153 |
+
if (narrow) {
|
| 154 |
+
tTop = y0;
|
| 155 |
+
const colW = (T.w - 2 * inset - 2 * 8) / 3;
|
| 156 |
+
const cy = tTop + tHeadH + 4;
|
| 157 |
+
for (let k = 0; k < 3; k++) clients.push({ x: T.x + inset + k * (colW + 8), y: cy, w: colW, h: clientH });
|
| 158 |
+
tH = tHeadH + 4 + clientH + inset;
|
| 159 |
+
sTop = tTop + tH + 54;
|
| 160 |
+
// the connections come in from the top, so the server's name goes at the bottom of its box
|
| 161 |
+
const rowY = sTop + inset;
|
| 162 |
+
for (let k = 0; k < 2; k++) comp.push({ x: clients[k].x, y: rowY, w: colW, h: compactH });
|
| 163 |
+
det = { x: sx, y: rowY + compactH + 10, w: sw, h: detailH };
|
| 164 |
+
sH = det.y + det.h + 4 + sHeadH - sTop;
|
| 165 |
+
} else {
|
| 166 |
+
tTop = sTop = y0;
|
| 167 |
+
const first = sTop + Math.max(sHeadH, tHeadH - 4);
|
| 168 |
+
for (let k = 0; k < 2; k++) comp.push({ x: sx, y: first + k * (compactH + 10), w: sw, h: compactH });
|
| 169 |
+
det = { x: sx, y: first + 2 * (compactH + 10), w: sw, h: detailH };
|
| 170 |
+
sH = tH = det.y + det.h + inset - sTop;
|
| 171 |
+
const cx = T.x + inset, cw = T.w - 2 * inset;
|
| 172 |
+
comp.forEach((c) => clients.push({ x: cx, y: c.y + c.h / 2 - clientH / 2, w: cw, h: clientH }));
|
| 173 |
+
clients.push({ x: cx, y: det.y + 4, w: cw, h: clientH });
|
| 174 |
+
}
|
| 175 |
+
const jobBottom = (narrow ? sTop + sH : tTop + tH) + pad;
|
| 176 |
+
const spaceLines = narrow ? wrap('same client code, connected by URL', width - 2 * pad - 2 * inset, 13) : ['same client code, connected by URL'];
|
| 177 |
+
const spaceY = jobBottom + 16, spaceH = 26 + spaceLines.length * lh + 4;
|
| 178 |
+
const H = spaceY + spaceH + 4;
|
| 179 |
+
|
| 180 |
+
svg.replaceChildren();
|
| 181 |
+
svg.setAttribute('viewBox', `0 0 ${width} ${H}`);
|
| 182 |
+
svg.setAttribute('height', H);
|
| 183 |
+
hiBack = document.createElementNS(NS, 'g');
|
| 184 |
+
const base = document.createElementNS(NS, 'g'), over = document.createElementNS(NS, 'g');
|
| 185 |
+
hiFront = document.createElementNS(NS, 'g');
|
| 186 |
+
const hits = document.createElementNS(NS, 'g');
|
| 187 |
+
// the highlight's fill goes between the session's background and its layers
|
| 188 |
+
svg.append(base, hiBack, over, hiFront, hits);
|
| 189 |
+
const h = Hand.create(base, { seed: 57 }), ho = Hand.create(over, { seed: 58 });
|
| 190 |
+
|
| 191 |
+
// --- the job ------------------------------------------------------------------------
|
| 192 |
+
h.rect(1, 1, width - 2, jobBottom - 1, { stroke: C.muted, width: 1.2, dashed: true });
|
| 193 |
+
h.text(pad, 20, 'HF Job', { size: 15, weight: 600, color: C.ink });
|
| 194 |
+
|
| 195 |
+
// --- the training process -------------------------------------------------------------
|
| 196 |
+
h.rect(T.x, tTop, T.w, tH, { fill: model.fill, stroke: model.stroke, width: 1.2 });
|
| 197 |
+
h.text(T.x + inset, tTop + 22, 'Training process', { size: 15, weight: 600, color: C.ink });
|
| 198 |
+
tLines.forEach((l, k) => h.text(T.x + inset, tTop + 22 + (k + 1) * lh + 2, l, { size: 13, color: C.ink }));
|
| 199 |
+
const chipY = tTop + 26 + tLines.length * lh + 8;
|
| 200 |
+
h.rect(T.x + inset, chipY, tokenW, 22, { fill: C.redSoft, stroke: C.red, width: 1.1 });
|
| 201 |
+
h.text(T.x + inset + 8, chipY + 15.5, 'HF_TOKEN, only here', { size: 12.5, weight: 600, color: C.redText });
|
| 202 |
+
clients.forEach((c, k) => {
|
| 203 |
+
h.rect(c.x, c.y, c.w, c.h, { fill: C.bg, stroke: model.stroke, width: 1.1 });
|
| 204 |
+
h.text(c.x + 8, c.y + 17, 'client', { size: 13.5, weight: 600, color: C.ink });
|
| 205 |
+
h.text(c.x + 8, c.y + 33, `episode ${k + 1}`, { size: 12.5, color: C.muted });
|
| 206 |
+
});
|
| 207 |
+
|
| 208 |
+
// --- the environment server -----------------------------------------------------------
|
| 209 |
+
h.rect(S.x, sTop, S.w, sH, { fill: tool.fill, stroke: tool.stroke, width: 1.2 });
|
| 210 |
+
const sHeadY = narrow ? det.y + det.h + 4 : sTop;
|
| 211 |
+
h.text(S.x + inset, sHeadY + 22, 'Environment server', { size: 15, weight: 600, color: C.ink });
|
| 212 |
+
sLines.forEach((l, k) => h.text(S.x + inset, sHeadY + 22 + (k + 1) * lh + 2, l, { size: 13, color: C.muted }));
|
| 213 |
+
comp.forEach((c, k) => {
|
| 214 |
+
h.rect(c.x, c.y, c.w, c.h, { fill: C.bg, stroke: tool.stroke, width: 1.1 });
|
| 215 |
+
h.text(c.x + 8, c.y + 19, `session ${k + 1}`, { size: 13.5, weight: 600, color: C.ink });
|
| 216 |
+
h.text(c.x + 8, c.y + 19 + lh, 'worker process', { size: 12.5, color: C.muted });
|
| 217 |
+
h.text(c.x + 8, c.y + 19 + 2 * lh, `uid ${UID + k}`, { size: 12.5, color: C.muted });
|
| 218 |
+
if (!narrow) h.text(c.x + c.w - 8, c.y + 19 + lh, 'same layers as session 3', { size: 12.5, color: C.muted, anchor: 'end' });
|
| 219 |
+
});
|
| 220 |
+
|
| 221 |
+
// the session with its layers, nested from the strongest (outside) to the audit hook (inside)
|
| 222 |
+
h.rect(det.x, det.y, det.w, det.h, { fill: C.bg, stroke: tool.stroke, width: 1.1 });
|
| 223 |
+
h.text(det.x + 8, det.y + 19, 'session 3', { size: 13.5, weight: 600, color: C.ink });
|
| 224 |
+
h.text(det.x + 8, det.y + 19 + lh, `worker process, uid ${UID + 2}`, { size: 12.5, color: C.muted });
|
| 225 |
+
shells = LAYERS.map((L, k) => {
|
| 226 |
+
const x = det.x + 8 + k * shellSide, y = det.y + 46 + k * shellTop;
|
| 227 |
+
const w = shellW1 - 2 * k * shellSide, hh = shellH1 - k * (shellTop + shellBottom);
|
| 228 |
+
ho.rect(x, y, w, hh, { stroke: C.grey, width: 1.1 });
|
| 229 |
+
const label = ho.text(x + 8, y + 15.5, `${k + 1} ${L.name}`, { size: 12.5, color: C.ink });
|
| 230 |
+
return { x, y, w, h: hh, label };
|
| 231 |
+
});
|
| 232 |
+
const core = shells[3];
|
| 233 |
+
const cx = core.x + 8, cy = core.y + shellTop, cw = core.w - 16;
|
| 234 |
+
const chips = stack
|
| 235 |
+
? [[cx, cy, cw], [cx, cy + 34, cw]]
|
| 236 |
+
: [[cx, cy, (cw - 8) / 2], [cx + (cw + 8) / 2, cy, (cw - 8) / 2]];
|
| 237 |
+
[['Python session', tool], ["the task's files", { fill: C.greySoft, stroke: C.grey }]].forEach(([lab, col], k) => {
|
| 238 |
+
const [x, y, w] = chips[k];
|
| 239 |
+
ho.rect(x, y, w, 28, { fill: col.fill, stroke: col.stroke, width: 1.1 });
|
| 240 |
+
ho.text(x + w / 2, y + 18.5, lab, { size: 13, color: C.ink, anchor: 'middle' });
|
| 241 |
+
});
|
| 242 |
+
|
| 243 |
+
// --- the WebSocket connections, one session per connection --------------------------------
|
| 244 |
+
const targets = [...comp, det];
|
| 245 |
+
const ws = (x1, y1, x2, y2) => {
|
| 246 |
+
h.arrow([[x1, y1], [x2, y2]], { stroke: C.ink, width: 1.3, head: 7 });
|
| 247 |
+
h.arrow([[x2, y2], [x1, y1]], { stroke: C.ink, width: 1.3, head: 7 });
|
| 248 |
+
};
|
| 249 |
+
if (narrow) {
|
| 250 |
+
clients.forEach((c, k) => {
|
| 251 |
+
const x = c.x + c.w / 2;
|
| 252 |
+
ws(x, c.y + c.h + 2, x, targets[k].y - 2);
|
| 253 |
+
});
|
| 254 |
+
const lab = 'WebSocket, one session per connection';
|
| 255 |
+
const lw = textW(lab, 13) + 14, mx = width / 2, my = tTop + tH + 27;
|
| 256 |
+
h.rect(mx - lw / 2, my - 11, lw, 20, { fill: C.bg, stroke: 'none' });
|
| 257 |
+
h.text(mx, my + 4, lab, { size: 13, color: C.ink, anchor: 'middle' });
|
| 258 |
+
} else {
|
| 259 |
+
clients.forEach((c, k) => {
|
| 260 |
+
const t = targets[k], y = c.y + c.h / 2;
|
| 261 |
+
ws(c.x + c.w + 2, y, t.x - 2, k < 2 ? y : t.y + 4 + clientH / 2);
|
| 262 |
+
});
|
| 263 |
+
const gx = T.x + T.w + gapW / 2;
|
| 264 |
+
const gl = [['WebSocket', C.ink, 600], ...wrap('one session per connection', gapW - 8, 12.5).map((l) => [l, C.muted, 400])];
|
| 265 |
+
gl.forEach(([l, col, wt], k) => h.text(gx, sTop + 22 + k * 16, l, { size: k ? 12.5 : 13.5, color: col, weight: wt, anchor: 'middle' }));
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
// --- the other place the same client code can connect to ------------------------------------
|
| 269 |
+
const spX = narrow ? pad : S.x, spW = narrow ? width - 2 * pad : S.w;
|
| 270 |
+
h.rect(spX, spaceY, spW, spaceH, { stroke: C.muted, width: 1.1, dashed: true });
|
| 271 |
+
h.text(spX + inset, spaceY + 20, 'Or a Space on the Hub', { size: 14, weight: 600, color: C.ink });
|
| 272 |
+
spaceLines.forEach((l, k) => h.text(spX + inset, spaceY + 20 + (k + 1) * lh, l, { size: 13, color: C.muted }));
|
| 273 |
+
if (!narrow) {
|
| 274 |
+
const ax = T.x + T.w / 2;
|
| 275 |
+
const ay = spaceY + spaceH / 2;
|
| 276 |
+
h.line(ax, tTop + tH + 2, ax, ay, { stroke: C.muted, width: 1.2, dashed: true });
|
| 277 |
+
h.arrow([[ax, ay], [spX - 4, ay]], { stroke: C.muted, width: 1.2, dashed: true, head: 7 });
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
// --- hover, tap and keyboard targets, innermost on top --------------------------------------
|
| 281 |
+
shells.forEach((s, k) => {
|
| 282 |
+
const r = document.createElementNS(NS, 'rect');
|
| 283 |
+
r.setAttribute('x', s.x); r.setAttribute('y', s.y); r.setAttribute('width', s.w); r.setAttribute('height', s.h);
|
| 284 |
+
r.setAttribute('fill', 'transparent');
|
| 285 |
+
r.setAttribute('class', 'cs-hit');
|
| 286 |
+
r.setAttribute('tabindex', '0');
|
| 287 |
+
r.setAttribute('role', 'button');
|
| 288 |
+
r.setAttribute('aria-label', `Layer ${k + 1}: ${LAYERS[k].name}`);
|
| 289 |
+
r.addEventListener('pointerover', () => setActive(k), { signal: ctx.signal });
|
| 290 |
+
r.addEventListener('click', () => setActive(k), { signal: ctx.signal });
|
| 291 |
+
r.addEventListener('focus', () => setActive(k), { signal: ctx.signal });
|
| 292 |
+
hits.appendChild(r);
|
| 293 |
+
});
|
| 294 |
+
paintActive();
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// Keep the note as tall as its longest text, so the iframe height doesn't change on hover.
|
| 298 |
+
function layout() {
|
| 299 |
+
draw();
|
| 300 |
+
let tallest = 0;
|
| 301 |
+
LAYERS.forEach((_, k) => { note.style.minHeight = ''; writeNote(k); tallest = Math.max(tallest, note.offsetHeight); });
|
| 302 |
+
writeNote(active);
|
| 303 |
+
note.style.minHeight = tallest + 'px';
|
| 304 |
+
}
|
| 305 |
+
if (document.fonts && document.fonts.ready) document.fonts.ready.then(() => { if (!ctx.signal.aborted) layout(); });
|
| 306 |
+
return ({ width: w }) => { width = w; C = Hand.colors(ctx.root); layout(); };
|
| 307 |
+
}, { deps: [] });
|
| 308 |
+
</script>
|
| 309 |
+
|
| 310 |
+
</div>
|
| 311 |
+
</body>
|
| 312 |
+
</html>
|