---
title: "Change the function, not the running page"
date: 2026-11-18
summary: "An agent-edited canvas used to restart whenever its scripts changed. Stable function stubs and a script-local registry now let compatible edits change behaviour without throwing away the running page."
tags: ["web", "agents", "state"]
url: https://blog.misherate.com/posts/change-the-function-not-the-running-page
thumbnail: https://blog.misherate.com/thumbnails/change-the-function-not-the-running-page-mwmn8j0v.jpg
---

An [iterate](https://iterate.com) mind is its own code and everything that code runs; today, [Cerebras Canvas](/introducing/cerebras-canvas) learned to change a running function without restarting the page.

Cerebras Canvas puts an editable web page beside its agent’s chat. A fast Cerebras model tries each change first; the full agent follows to check or repair it. This post is about keeping that preview alive as the code changes.

“Make it move faster” sounds like a tiny edit. It is not tiny if the editor's response is to destroy the animation, forget its position, and start the whole page again.

That is an awkward bargain for agent-built software: the source gets better while the thing you were using disappears. Our canvas experiment now keeps the running page for a useful subset of code changes. The trick is not persuading the browser to run a changed script tag. It is making the original function a stable doorway to a replaceable body.

## The timer has already taken its copy

The canvas puts an editable web page beside a chat. An agent changes an HTML file in git; the outer page hears the commit and updates the preview. Each canvas has its own agent and repository. The preview runs inside a sandboxed frame with scripts allowed but no shared origin, so generated code does not inherit the signed-in outer page's access.

At first, a new file meant a new frame. That is simple and honest: the saved page and the displayed page agree. It is also terrible for anything in motion.

The first improvement was document morphing with [Idiomorph](https://github.com/bigskysoftware/idiomorph). Rather than replacing the document wholesale, reconcile the existing elements with the new markup. Keep typed fields and ongoing animations where possible. A colour change need not reset a moving object.

But the DOM is only half the page. Its scripts have already run. If setup called `setInterval(move, 100)`, the timer holds a function reference. Changing the text of the script does not change that reference. Running setup again risks creating a second timer rather than fixing the first.

So the initial rule was conservative: morph when scripts are identical; reload when they differ. Behaviour changes still paid the full restart cost.

## Give the reference something stable to hold

The new layer parses inline scripts before the frame loads. Named top-level functions become forwarding stubs. Their implementations live in a registry, looked up each time the stub runs.

Here is the central idea, simplified—not the full source transformation:

```js
let step = 2;
let x = 0;
const bodies = {
  move: function () { x += step; }
};

function move(...args) {
  return bodies.move.apply(this, args);
}

setInterval(move, 100); // holds the stable stub

function replaceMove(source) {
  // Direct eval here can see this script's variables.
  const next = eval("(" + source + ")");
  bodies.move = next;
}

replaceMove("function () { x += step * 3; }");
```

The timer keeps the same reference. Its next call reaches the new body. The current position survives because setup never ran again.

Scope matters here. A replacement compiled somewhere else would not automatically see the original script's variables. The actual transformation inserts an evaluator *inside each script* and gives the registry access through it. That works for module scripts as well as classic ones. The replacement is compiled before it becomes the active body, so a syntax error is refused at the edit rather than thrown on every subsequent timer tick.

Literal-valued top-level settings get a similar treatment: eligible constants become mutable bindings in the loaded copy. The file in git remains ordinary JavaScript; the instrumentation exists only on its way into the preview.

## A narrow promise beats pretend hot reload

This is not arbitrary JavaScript hot replacement.

For each saved version, the loader computes a script “shape”: the surrounding code with replaceable functions and literal values abstracted away. Matching shapes permit a document morph plus registry updates. A function added or removed, changed setup code, or any other shape mismatch takes the old, reliable path: load a fresh frame. A failed morph or update also falls back to a reload.

Anonymous callbacks, nested functions and classes are outside this mechanism. So are values already consumed by another API. Updating an interval setting does not reschedule a timer that read it during setup. A movement function that reads a speed setting on every tick can use the new value; an API that copied the setting cannot.

Those limits are design guidance for the agent writing the page: put changeable behaviour in named top-level functions, and frequently adjusted settings in top-level variables. Do not claim that every edit can preserve state.

## Make small edits small all the way through

The same registry now helps the experiment's fast-model pass. Its screen snapshot includes editable values and function bodies, so it can answer a speed request with one new value instead of regenerating an entire page. The regular agent still follows, folding the change into the saved source. A compatible commit then updates the running page without restarting it.

The core hot-reload layer needs no model key or background schedule: it runs in the browser around repository updates. The optional fast pass additionally needs its inference provider's secret and an agent-context processor. Keeping those separate matters. Preserving the page should work for an ordinary agent commit, not only for one model's preferred edit format.

Browser checks exercised classic and module scripts, a held reference calling a replacement body, access to script-local state, rejection of an invalid body, and state surviving a hot update. That is evidence for the narrow contract, not proof that arbitrary generated programs are safely replaceable.

Vibe coding gets much less delightful when every adjustment wipes out the thing you were adjusting. Here, the useful unit of replacement turned out not to be the page or even the script. It was the implementation behind a reference we could leave alone.
