summaryrefslogtreecommitdiffstats
path: root/assets/wp001.js
blob: 85ffb87d0b87c7031ffadadbeaa6b701e19a679a (plain)
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
// WP-001 results figure. The cell rows replay the setup (ten days of turns, a memory rewrite each night,
// some turns corrupted) and the chart shows final recall per condition from Table 2 of the paper
// (mean over three conversation histories, bars are one standard deviation).
(function(){
"use strict";
var reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
var RATES = [0,5,10,20,40];
var DATA = {
  detail: {mean:[76,76,71,69,60], sd:[5,5,2,2,4], color:"#d8a36c", rgb:"216,163,108", name:"details changed"},
  moby:   {mean:[76,78,77,70,76], sd:[5,4,5,5,3], color:"#8fa0ff", rgb:"143,160,255", name:"Moby-Dick text"}
};
var NO_REWRITE = 14, CLEAN = 76;
var DAYS = 10, PER = 8, N = DAYS*PER, END = DAYS + 1;
var COL = {ink:"235,233,226", line2:"#1a1d23", line3:"#2a2d33", muted:"#8d929c", soft:"#bfc2c9", panel:"#090b10", guide:"#5a5f69"};

function rng(seed){ return function(){ seed|=0; seed=seed+0x6D2B79F5|0; var t=Math.imul(seed^seed>>>15,1|seed); t=t+Math.imul(t^t>>>7,61|t)^t; return ((t^t>>>14)>>>0)/4294967296; }; }
var R = rng(20261014), ORDER = [];
for (var i=0;i<N;i++) ORDER.push(i);
for (i=N-1;i>0;i--){ var j=Math.floor(R()*(i+1)), tmp=ORDER[i]; ORDER[i]=ORDER[j]; ORDER[j]=tmp; }
// rank[cell] = position in a fixed shuffle, so a higher rate corrupts the same cells plus more
var RANK = new Int16Array(N); ORDER.forEach(function(c,k){ RANK[c]=k; });

var el = function(id){ return document.getElementById(id); };
var chart = el("chart"), cT = el("cellsT"), cM = el("cellsM"), readout = el("readout"), dayT = el("dayT"), scrub = el("scrub");
var state = {kind:"detail", dose:4};
var ring = {x:4, y:60, fx:4, fy:60, t0:0};
var t = reduce ? END : 0, playing = !reduce, hold = 0, dirty = true, visible = true, last = performance.now();
var TWEEN = reduce ? 1 : 650;

function ease(x){ return x>=1 ? 1 : 1 - Math.pow(2, -10*x); }
function fit(cv){
  var dpr = Math.min(2, window.devicePixelRatio||1), w = cv.clientWidth, h = cv.clientHeight;
  if (cv.width !== Math.round(w*dpr) || cv.height !== Math.round(h*dpr)){ cv.width=Math.round(w*dpr); cv.height=Math.round(h*dpr); }
  var c = cv.getContext("2d"); c.setTransform(dpr,0,0,dpr,0,0); return {c:c,w:w,h:h};
}
function corrupted(cell){ return RANK[cell] < Math.round(RATES[state.dose]/100*N); }

var geo = null;
function drawChart(now){
  var g = fit(chart), c = g.c, W = g.w, H = g.h;
  var L = 38, Rt = 14, T = 12, Bm = 40, pw = W-L-Rt, ph = H-T-Bm, pad = Math.min(28, pw*0.06);
  var X = function(i){ return L + pad + i*(pw-2*pad)/(RATES.length-1); }, Y = function(v){ return T + (1-v/100)*ph; };
  geo = {X:X, Y:Y};
  c.clearRect(0,0,W,H);
  c.font = "500 10px 'IBM Plex Mono', monospace";
  c.textBaseline = "middle";
  for (var v=0; v<=100; v+=25){
    var y = Math.round(Y(v))+.5;
    c.strokeStyle = v===0 ? "#3a3e46" : COL.line2; c.lineWidth = 1;
    c.beginPath(); c.moveTo(L,y); c.lineTo(W-Rt,y); c.stroke();
    c.fillStyle = COL.muted; c.textAlign = "right"; c.fillText(v+"%", L-7, y);
  }
  c.textAlign = "center"; c.textBaseline = "top";
  RATES.forEach(function(r,i){ c.fillStyle = COL.muted; c.fillText(r+"%", X(i), H-Bm+7); });
  c.fillText("messages corrupted each day", L + pw/2, H-Bm+23);
  // no-rewrite baseline
  c.save(); c.setLineDash([4,4]); c.strokeStyle = "rgba(141,146,156,.7)"; c.lineWidth = 1;
  c.beginPath(); c.moveTo(L, Y(NO_REWRITE)+.5); c.lineTo(W-Rt, Y(NO_REWRITE)+.5); c.stroke(); c.restore();
  c.textAlign = "left"; c.textBaseline = "bottom"; c.fillStyle = COL.muted;
  c.fillText("no nightly rewrite, 14%", L+6, Y(NO_REWRITE)-4);

  var order = state.kind === "detail" ? ["moby","detail"] : ["detail","moby"];
  order.forEach(function(k){
    var d = DATA[k], on = k === state.kind, a = on ? 1 : 0.32;
    c.strokeStyle = "rgba("+d.rgb+","+(0.55*a).toFixed(3)+")"; c.lineWidth = 1;
    d.mean.forEach(function(m,i){
      var x = Math.round(X(i))+.5;
      c.beginPath(); c.moveTo(x, Y(m+d.sd[i])); c.lineTo(x, Y(m-d.sd[i]));
      c.moveTo(x-3, Y(m+d.sd[i])); c.lineTo(x+3, Y(m+d.sd[i])); c.moveTo(x-3, Y(m-d.sd[i])); c.lineTo(x+3, Y(m-d.sd[i])); c.stroke();
    });
    c.lineJoin = "round"; c.lineCap = "round";
    c.beginPath(); d.mean.forEach(function(m,i){ if (i) c.lineTo(X(i),Y(m)); else c.moveTo(X(i),Y(m)); });
    c.strokeStyle = "rgba("+d.rgb+","+a+")"; c.lineWidth = on ? 2.5 : 1.5; c.stroke();
    d.mean.forEach(function(m,i){ c.beginPath(); c.arc(X(i),Y(m), on ? 3 : 2.2, 0, Math.PI*2); c.fillStyle = "rgba("+d.rgb+","+a+")"; c.fill(); });
  });

  // ring on the selected condition, moving between selections
  var k = ease(Math.min(1, (now-ring.t0)/TWEEN));
  var ri = ring.fx + (ring.x-ring.fx)*k, rv = ring.fy + (ring.y-ring.fy)*k;
  var lx = L + pad + ri*(pw-2*pad)/(RATES.length-1), ly = Y(rv), col = DATA[state.kind].color;
  c.strokeStyle = COL.guide; c.lineWidth = 1; c.beginPath(); c.moveTo(Math.round(lx)+.5, T); c.lineTo(Math.round(lx)+.5, Y(0)); c.stroke();
  c.beginPath(); c.arc(lx, ly, 6, 0, Math.PI*2); c.fillStyle = COL.panel; c.fill(); c.lineWidth = 2.5; c.strokeStyle = col; c.stroke();
  var shown = Math.max(0, Math.min(1, (t - DAYS)/0.3));
  if (shown > 0){
    c.globalAlpha = shown;
    c.font = "500 12px 'IBM Plex Mono', monospace"; c.fillStyle = col; c.textBaseline = "bottom";
    var label = Math.round(rv) + "%", tw = c.measureText(label).width;
    c.textAlign = lx + 10 + tw > W - Rt ? "right" : "left";
    c.fillText(label, c.textAlign === "right" ? lx - 10 : lx + 10, ly - 8);
    c.globalAlpha = 1;
  }
  return k;
}

function cellGeom(W, H){ var gap = W < 420 ? 2 : 3; return {gap:gap, cw:(W-gap*(DAYS-1))/DAYS, ch:(H-gap*(PER-1))/PER}; }
function hatch(c, x, y, w, h, rgb){
  c.save(); c.beginPath(); c.rect(x,y,w,h); c.clip();
  c.fillStyle = "rgba("+rgb+",.16)"; c.fillRect(x,y,w,h);
  c.strokeStyle = "rgba("+rgb+",.95)"; c.lineWidth = 1.2; c.beginPath();
  for (var s=-h; s<w; s+=4.5){ c.moveTo(x+s, y+h); c.lineTo(x+s+h, y); }
  c.stroke(); c.restore();
}
// how far day d has got at time t: turns appear in the first 60 percent, memory is written in the rest
function dayProgress(d){ var f = t - d; return {turns: f<=0 ? 0 : Math.min(1, f/0.6), mem: f<=0.6 ? 0 : Math.min(1, (f-0.6)/0.4)}; }

function drawCells(cv, memory){
  var g = fit(cv), c = g.c, W = g.w, H = g.h, m = cellGeom(W,H), D = DATA[state.kind];
  c.clearRect(0,0,W,H);
  for (var d=0; d<DAYS; d++){
    var p = dayProgress(d), fill = memory ? p.mem : p.turns;
    for (var r=0; r<PER; r++){
      var cell = d*PER + r, x = d*(m.cw+m.gap), y = r*(m.ch+m.gap);
      if (fill*PER <= r){ c.strokeStyle = COL.line2; c.lineWidth = 1; c.strokeRect(x+.5,y+.5,m.cw-1,m.ch-1); continue; }
      var bad = corrupted(cell);
      if (!bad){ c.fillStyle = "rgba("+COL.ink+","+(memory ? .5 : .2)+")"; c.fillRect(x,y,m.cw,m.ch); }
      else if (state.kind === "detail"){ c.fillStyle = D.color; c.fillRect(x,y,m.cw,m.ch); }
      else if (!memory){ hatch(c, x, y, m.cw, m.ch, D.rgb); }
      else {
        c.strokeStyle = "rgba("+D.rgb+",.7)"; c.lineWidth = 1; c.strokeRect(x+.5,y+.5,m.cw-1,m.ch-1);
        var dw = Math.min(m.cw*0.5, 10); c.fillStyle = D.color; c.fillRect(x+m.cw/2-dw/2, y+m.ch/2-.75, dw, 1.5);
      }
    }
  }
  if (t < DAYS){
    var cd = Math.floor(t), pr = dayProgress(cd), active = memory ? pr.mem > 0 : pr.mem === 0;
    if (active){
      var xs = cd*(m.cw+m.gap)-m.gap/2;
      c.strokeStyle = COL.soft; c.lineWidth = 1.5;
      c.strokeRect(Math.max(.75,xs), .75, Math.min(W-.75, xs+m.cw+m.gap) - Math.max(.75,xs), H-1.5);
    }
  }
}

function phaseText(){
  if (t >= DAYS) return "test";
  var d = Math.floor(t), p = dayProgress(d);
  return (p.mem > 0 ? "night " : "day ") + (d+1);
}
function readText(){
  var D = DATA[state.kind], rate = RATES[state.dose];
  if (t >= DAYS){
    return "Day 11 test: final recall <b style=\"color:" + D.color + "\">" + D.mean[state.dose] + "%</b> with " + D.name + " in " + rate + "% of messages, against " + CLEAN + "% with no corruption";
  }
  var d = Math.floor(t), p = dayProgress(d);
  if (p.mem > 0) return "Night " + (d+1) + ": the agent rewrites its memory from the day's transcript";
  return "Day " + (d+1) + ": the agent talks with the user, and " + rate + "% of messages get " + D.name;
}

function frame(now){
  var dt = Math.min(0.05, (now-last)/1000); last = now;
  if (playing && visible){
    if (t < END){ t = Math.min(END, t + dt*0.9); dirty = true; }
    else { hold += dt; if (hold > 3.2){ hold = 0; t = 0; dirty = true; } }
  }
  if (now - ring.t0 < TWEEN + 50) dirty = true;
  if (dirty && visible){
    dirty = false;
    drawChart(now);
    drawCells(cT, false);
    drawCells(cM, true);
    dayT.textContent = phaseText();
    if (document.activeElement !== scrub) scrub.value = t.toFixed(2);
    scrub.style.setProperty("--p", (t/END*100) + "%");
    var html = readText();
    if (readout.innerHTML !== html) readout.innerHTML = html;
  }
  requestAnimationFrame(frame);
}

function select(kind, dose){
  var now = performance.now(), k = ease(Math.min(1,(now-ring.t0)/TWEEN));
  ring.fx = ring.fx + (ring.x-ring.fx)*k; ring.fy = ring.fy + (ring.y-ring.fy)*k;
  state.kind = kind; state.dose = dose;
  ring.x = dose; ring.y = DATA[kind].mean[dose]; ring.t0 = now;
  if (reduce){ ring.fx = ring.x; ring.fy = ring.y; }
  [].forEach.call(document.querySelectorAll("[data-kind]"), function(b){ b.setAttribute("aria-pressed", String(b.dataset.kind === kind)); });
  [].forEach.call(document.querySelectorAll("[data-dose]"), function(b){ b.setAttribute("aria-pressed", String(+b.dataset.dose === dose)); });
  el("keys").setAttribute("data-kind", kind);
  chart.setAttribute("aria-label", "Final recall against percent of chat messages corrupted. Changed details: " + DATA.detail.mean.join(", ") + " percent at 0, 5, 10, 20 and 40 percent. Moby-Dick text: " + DATA.moby.mean.join(", ") + " percent. No nightly rewrite: 14 percent. Selected: " + DATA[kind].name + " at " + RATES[dose] + " percent, final recall " + DATA[kind].mean[dose] + " percent.");
  dirty = true;
}

var playBtn = el("play"), playI = el("playI");
function setPlaying(pl){
  playing = pl; playBtn.setAttribute("aria-label", pl ? "Pause" : "Play");
  playI.innerHTML = pl ? '<rect x="2" y="1" width="3" height="10" fill="currentColor"/><rect x="7" y="1" width="3" height="10" fill="currentColor"/>' : '<path d="M3 1 L11 6 L3 11 Z" fill="currentColor"/>';
  if (pl && t >= END){ t = 0; hold = 0; }
  dirty = true;
}
playBtn.addEventListener("click", function(){ setPlaying(!playing); });
scrub.addEventListener("input", function(){ setPlaying(false); t = +scrub.value; dirty = true; });
[].forEach.call(document.querySelectorAll("[data-kind]"), function(b){ b.addEventListener("click", function(){ select(b.dataset.kind, state.dose); }); });
[].forEach.call(document.querySelectorAll("[data-dose]"), function(b){ b.addEventListener("click", function(){ select(state.kind, +b.dataset.dose); }); });

// clicking a point on the chart selects that condition
function hit(e){
  if (!geo) return null;
  var r = chart.getBoundingClientRect(), x = e.clientX - r.left, y = e.clientY - r.top, best = null, bd = 22;
  ["detail","moby"].forEach(function(k){ DATA[k].mean.forEach(function(m,i){ if (!i) return; var dd = Math.hypot(geo.X(i)-x, geo.Y(m)-y); if (dd < bd){ bd = dd; best = {kind:k, dose:i}; } }); });
  return best;
}
chart.addEventListener("pointermove", function(e){ chart.style.cursor = hit(e) ? "pointer" : "default"; });
chart.addEventListener("click", function(e){ var h = hit(e); if (h) select(h.kind, h.dose); });

if ("IntersectionObserver" in window){
  new IntersectionObserver(function(es){ visible = es[0].isIntersecting; if (visible){ dirty = true; last = performance.now(); } }).observe(el("wpfig"));
}
window.addEventListener("resize", function(){ dirty = true; });
if (document.fonts && document.fonts.ready) document.fonts.ready.then(function(){ dirty = true; });

select("detail", 4); ring.fx = ring.x; ring.fy = ring.y; ring.t0 = 0;
setPlaying(playing);
requestAnimationFrame(function(n){ last = n; frame(n); });
})();