figures / unit1-trajectory.html
sergiopaniego's picture
sergiopaniego HF Staff
Brand teal accent, readable text colors, U4 nudge redesign, U7 figures
39824c1 verified
Raw History Blame Contribute Delete
12.9 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One rollout, turn by turn</title>
<!-- Generated by build.py from fragments/. Runtime and theme tokens come from the research-article template. -->
<link rel="stylesheet" href="runtime/variables.css">
<script>
// The iframe can't read the course page's theme, so follow ?theme= if given, else the OS setting.
(function () {
const forced = new URLSearchParams(location.search).get('theme');
const mq = matchMedia('(prefers-color-scheme: dark)');
const apply = () => document.documentElement.setAttribute('data-theme', forced || (mq.matches ? 'dark' : 'light'));
apply();
if (!forced) mq.addEventListener('change', apply);
})();
</script>
<script src="runtime/color-palettes.js"></script>
<script src="runtime/rough.js"></script>
<script src="runtime/hand.js"></script>
<script src="runtime/embed-runtime.js"></script>
<style>
html, body { margin: 0; padding: 0; }
/* An opaque card with its own background, so it stays readable whatever theme the course page uses */
body { background: transparent; }
.figure-card { font-family: 'Source Sans 3', var(--default-font-family); color: var(--text-color); background: var(--page-bg);
border: 1px solid var(--border-color); border-radius: 10px; padding: 12px 14px 10px; }
</style>
<script>
// Clean look by default, in Source Sans. ?style=hand brings back the old hand-drawn look for comparison:
// it loads Excalifont and uses it for the whole card.
if (new URLSearchParams(location.search).get('style') === 'hand') {
const st = document.createElement('style');
st.textContent = "@font-face { font-family: 'Excalifont'; src: url('runtime/fonts/Excalifont-Regular.woff2') format('woff2'); font-display: swap; }" +
".figure-card { font-family: 'Excalifont', 'Source Sans 3', var(--default-font-family); }";
document.head.appendChild(st);
} else {
document.documentElement.classList.add('style-clean');
const l = document.createElement('link'); l.rel = 'stylesheet';
l.href = 'https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@400;600;700&display=swap';
document.head.appendChild(l);
}
</script>
</head>
<body>
<div class="figure-card">
<!-- title: One rollout, turn by turn -->
<div class="fig-trajectory"></div>
<style>
.fig-trajectory { position: relative; color: var(--text-color); font-size: 14px; }
.fig-trajectory svg { display: block; width: 100%; overflow: visible; }
.fig-trajectory .fig-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
.fig-trajectory button {
font: inherit; font-size: 14px; height: 30px; padding: 0 12px; border-radius: 6px; cursor: pointer;
color: var(--text-color); background: var(--surface-bg); border: 1px solid var(--border-color);
}
.fig-trajectory button:disabled { opacity: 0.45; cursor: default; }
.fig-trajectory button[aria-pressed="true"] { border-color: var(--primary-color); }
.fig-trajectory button:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
.fig-trajectory .fig-step { color: var(--muted-color); margin-left: 4px; }
.fig-trajectory .fig-note { margin: 8px 0 0; min-height: 2.8em; color: var(--muted-color); }
</style>
<script>
Embed.mount('fig-trajectory', (ctx) => {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
ctx.root.appendChild(svg);
// The illustrative MBPP rollout from "From responses to trajectories" (Unit 1).
const MSGS = [
{ src: 'task', role: 'user', text: 'Write a python function to identify non-prime numbers.',
note: 'The task arrives as the first message: an MBPP problem with one example test. The model reads it.' },
{ src: 'model', role: 'assistant', text: 'calls run_python:\ndef is_not_prime(n): ... math.sqrt(n) ...',
note: 'The model calls run_python with a first version of the function.' },
{ src: 'tool', role: 'tool', text: "NameError: name 'math' is not defined", error: true,
note: 'The Python session runs the code and sends back the error. The model didn\'t write this message.' },
{ src: 'model', role: 'assistant', text: 'calls run_python:\nimport math\nprint(is_not_prime(2), is_not_prime(10))',
note: 'The model reads the error and fixes the code: it imports math and runs the two examples again.' },
{ src: 'tool', role: 'tool', text: 'False True',
note: 'This time the code runs and prints the result of each example.' },
{ src: 'model', role: 'assistant', text: 'The function is defined and works on the examples.',
note: 'The model answers without calling a tool, so the loop stops. Every model message and tool result so far is the trajectory.' },
];
const REWARD = { text: 'hidden tests: 3 of 3 pass\nreward 1.0',
note: 'Only after the last turn do the hidden tests run, in the same session. The fraction that pass is the reward, one number for the whole rollout.' };
const LAST = MSGS.length; // index of the reward step
let step = ctx.reducedMotion ? LAST : 0, width = 640, timer = null;
const controls = document.createElement('div');
controls.className = 'fig-controls';
const mk = (label, fn) => {
const b = document.createElement('button');
b.type = 'button';
b.textContent = label;
b.addEventListener('click', fn, { signal: ctx.signal });
controls.appendChild(b);
return b;
};
const prev = mk('Previous', () => { stop(); go(step - 1); });
const next = mk('Next', () => { stop(); go(step + 1); });
const play = mk('Play', () => (timer ? stop() : start()));
if (ctx.reducedMotion) play.style.display = 'none';
const counter = document.createElement('span');
counter.className = 'fig-step';
controls.appendChild(counter);
const note = document.createElement('p');
note.className = 'fig-note';
note.setAttribute('aria-live', 'polite');
ctx.root.append(controls, note);
ctx.describe('One rollout of the coding agent on an MBPP problem, one turn at a time. The task arrives as a user message. The model calls run_python with a first version of is_not_prime, the Python session returns NameError: name math is not defined, the model imports math and runs the examples again, the session prints False True, and the model answers without calling a tool. The task, the model messages and the tool results form the trajectory. After the last turn the hidden tests run and the fraction that pass, 3 of 3, is the reward.');
function go(s) { step = Math.max(0, Math.min(LAST, s)); draw(); }
function stop() {
if (timer) clearInterval(timer);
timer = null;
play.textContent = 'Play';
play.setAttribute('aria-pressed', 'false');
}
function start() {
if (step >= LAST) step = -1;
play.textContent = 'Pause';
play.setAttribute('aria-pressed', 'true');
go(step + 1);
timer = setInterval(() => { if (step >= LAST) stop(); else go(step + 1); }, 1900);
}
ctx.signal.addEventListener('abort', stop);
play.setAttribute('aria-pressed', 'false');
// Wrap text to a pixel width using the real font metrics.
function wrap(str, maxW, size) {
const probe = document.createElementNS('http://www.w3.org/2000/svg', 'text');
probe.setAttribute('font-family', Hand.FONT);
probe.setAttribute('font-size', size);
svg.appendChild(probe);
const w = (s) => { probe.textContent = s; return probe.getComputedTextLength(); };
const out = [];
str.split('\n').forEach((para) => {
let line = '';
para.split(' ').forEach((word) => {
const tryLine = line ? line + ' ' + word : word;
if (line && w(tryLine) > maxW) { out.push(line); line = word; } else line = tryLine;
});
out.push(line);
});
const widest = Math.max(...out.map(w));
probe.remove();
return { lines: out, size: widest > maxW ? Math.max(10, size * maxW / widest) : size };
}
function draw() {
prev.disabled = step === 0;
next.disabled = step === LAST;
counter.textContent = `Step ${step + 1} of ${LAST + 1}`;
note.textContent = step === LAST ? REWARD.note : MSGS[step].note;
const C = Hand.colors(ctx.root);
svg.replaceChildren();
const h = Hand.create(svg, { seed: 7 });
const narrow = width < 560;
const LH = 18, PAD = 9, GAP = narrow ? 8 : 10, FS = narrow ? 13 : 14;
// Actors: on the right on wide screens, on top on phones.
const actorW = narrow ? (width - 12) / 2 : Math.min(190, width * 0.27);
const actorH = narrow ? 52 : 62;
const top = narrow ? actorH + 22 : 4;
const roleW = narrow ? 80 : 92;
const rowX = roleW, rowW = narrow ? width - rowX - 2 : width - actorW - 56 - rowX;
const rows = [];
let y = top;
MSGS.forEach((m) => {
const t = wrap(m.text, rowW - 2 * 12, FS);
const hgt = t.lines.length * LH + 2 * PAD;
rows.push({ ...m, ...t, y, h: hgt });
y += hgt + GAP;
});
const trajEnd = y - GAP;
const rewardY = trajEnd + 26, rewardH = 2 * LH + 2 * PAD;
const H = rewardY + rewardH + 4;
svg.setAttribute('viewBox', `0 0 ${width} ${H}`);
svg.setAttribute('height', H);
const active = step === LAST ? 'tool' : MSGS[step].src;
const actors = narrow
? { model: { x: 0, y: 0 }, tool: { x: actorW + 12, y: 0 } }
: { model: { x: width - actorW, y: top + 10 }, tool: { x: width - actorW, y: Math.max(top + 150, (top + trajEnd) / 2 + 20) } };
[['model', 'Model', 'writes text and tool calls', 'assistant'], ['tool', 'Python session', 'runs the code it gets', 'tool']]
.forEach(([k, title, sub, role]) => {
const a = actors[k], on = active === k, R = Hand.role(C, role);
h.rect(a.x, a.y, actorW, actorH, { fill: R.fill, stroke: on ? C.ink : R.stroke, width: on ? 2.4 : 1.2 });
h.text(a.x + actorW / 2, a.y + (narrow ? 22 : 26), title, { anchor: 'middle', size: narrow ? 15 : 17, color: C.ink });
h.text(a.x + actorW / 2, a.y + (narrow ? 40 : 47), sub, { anchor: 'middle', size: narrow ? 11.5 : 13, color: C.muted });
});
rows.forEach((r, i) => {
if (i > step) {
h.rect(rowX, r.y, rowW, r.h, { stroke: C.border, width: 1, dashed: true, roughness: 0.6 });
return;
}
const cur = i === step;
const R = Hand.role(C, r.role);
h.rect(rowX, r.y, rowW, r.h, {
fill: R.fill, stroke: cur ? C.ink : R.stroke, width: cur ? 2.2 : 1.2,
});
h.text(rowX - 10, r.y + PAD + 13, r.role, { anchor: 'end', size: narrow ? 13 : 14, color: C.muted });
h.text(rowX + 12, r.y + PAD + 13, r.lines.join('\n'), { size: r.size, color: r.error ? C.redText : C.ink });
if (cur && !narrow && r.src !== 'task') {
const a = actors[r.src];
h.arrow([[a.x - 6, a.y + actorH / 2], [rowX + rowW + 8, r.y + r.h / 2]], { stroke: C.ink, width: 1.6 });
}
});
// The trajectory bracket, once the rollout is over.
if (step >= LAST - 1) {
const bx = narrow ? 5 : 10;
h.path(`M${bx + 10},${top - 2} L${bx},${top - 2} L${bx},${trajEnd + 2} L${bx + 10},${trajEnd + 2}`, { stroke: C.ink, width: 2 });
const lbl = h.text(0, 0, 'trajectory', { size: narrow ? 13 : 15, color: C.ink, anchor: 'middle' });
lbl.setAttribute('transform', `translate(${bx - 4},${(top + trajEnd) / 2}) rotate(-90)`);
}
// The reward, outside the trajectory.
if (step === LAST) {
const rw = Math.min(rowW, 260);
h.rect(rowX, rewardY, rw, rewardH, { fill: C.greenSoft, stroke: C.ink, width: 2.2 });
h.text(rowX - 10, rewardY + PAD + 13, 'reward', { anchor: 'end', size: narrow ? 13 : 14, color: C.muted });
h.text(rowX + 12, rewardY + PAD + 13, REWARD.text, { size: FS, color: C.ink });
if (!narrow) {
const a = actors.tool;
h.arrow([[a.x + actorW / 2, a.y + actorH + 6], [a.x + actorW / 2, rewardY + rewardH / 2], [rowX + rw + 10, rewardY + rewardH / 2]],
{ stroke: C.ink, width: 1.6 });
h.text(a.x + actorW / 2 - 8, rewardY + rewardH / 2 - 8, 'runs the hidden tests', { anchor: 'end', size: 13, color: C.muted });
}
} else {
h.rect(rowX, rewardY, Math.min(rowW, 260), rewardH, { stroke: C.border, width: 1, dashed: true, roughness: 0.6 });
}
}
if (document.fonts && document.fonts.ready) document.fonts.ready.then(() => { if (!ctx.signal.aborted) draw(); });
return ({ width: w }) => { width = w; draw(); };
}, { deps: [] });
</script>
</div>
</body>
</html>