A child builds a simple game in Scratch: a character appears in the center and instantly jumps to a corner. The first move happens between frames — no one sees it. Removing that command is safe. But how does an automatic optimization optimizer know that? Today, the Scratch repository holds tens of millions of projects written by young authors. A single mistake by the improver could silently ruin their intent.
The solution resembles a customs inspection. An untrusted optimizer scans the project, builds a dependency Graphs, and proposes to delete, say, an unused costume or a dead command. The edit comes with a "certificate" — a digital declaration stating the type of change and safety conditions. A trusted inspector, not believing a word, reanalyzes both versions of the program. They recalculate every condition. The observation lens (the set of events visible to the player) serves as the main guide: if a resource write is overwritten before it can be observed, it can be removed. This is the central cooperative frame theorem, proved in Lean. No trick in the certificate can hide.
The experiment on 300 real projects showed: at least one safe improvement was found in 283 projects, i.e., 94,3%. Mostly they cleaned unused resources, but in 17 cases they also removed semantically dead writes. Most importantly, an adversarial attack of 4,278 edits with deliberately embedded errors never broke through. The inspector rejected all dangerous options. A differential oracle, comparing the behavior of original and optimized programs on a virtual machine, detected no discrepancies.
For the first time for a parallel event-driven language, a control theory system has been built where every change carries a verifiable certificate. This brings the discipline of verified compilers into a world where the cost of error is especially high. The cooperative frame theorem is parameterized by resource type and observation lens — one proof covers an entire class of optimizations. It ensures information theory completeness of correctness conditions, cutting off any unexpected observation channels. The trusted base is compact: 9 921 lines of Python and 5 909 lines of Lean proofs — a size manageable for audit by a small team. Moreover, the approach needs no probability theory assumptions or Bayesian inference: it gives absolute guarantees. A program can be viewed as a Dynamical Systems, and its verification as deterministic analysis, not a stochastic processes.
Code customs is already working. In the future, it could check edits proposed by artificial intelligence and become the foundation for trusted AI assistants. For now, the inspector is ready: when a child hits "run," their intent remains untouched.
An independent audit of the system uncovered eight false acceptances that traditional per‑family tests missed. All were due to one sprite peeking at another through unexpected channels — for example, reading a costume number that shifted when an unused resource was removed. After fixing, the errors are now permanently guarded by regression tests.