The Smallest Fix in the Project So Far


The blog’s backlog file has carried an entry since April called “the smallest fix in the project so far”, with a note that it was eight lines of code. This is that post, written after the game was rewritten in another language, which turns out to be the right time to write it. The fix is still there. It is the one piece of April that every later piece leans on.

The bug

By 27 April the 3D viewer had a wire mode: numbered dots over the branches, one per branch, that you clicked to bend it. The dots were drawn by a function in the viewer, _draw_branch_hotspots, which worked out where the middle of each branch was in world space and projected that point to the screen. The branches themselves were drawn by the mesh builder in scr_bonsai_mesh, which worked out where each branch was in world space and built rings of vertices along it.

Two functions, one question, two answers. The mesh builder, there since the project’s first commit on 19 April, started a branch at the trunk’s surface: it computed the trunk’s radius at the branch’s height, with taper, and pushed the origin out by that much in the branch’s direction. The hotspot code arrived the next morning with a simplified copy of that arithmetic, starting the branch at the trunk’s centre line, x and y both zero. It was not an oversight. The same commit wrote the shortcut into ARCHITECTURE.md as a known rough edge, ending “Close enough for now.” Every dot sat one trunk-radius short of the branch it belonged to. The rough edge stood for a week, and for the last four days of it TODO.md carried the fix as item 1, size S.

The fix

Pull request #12 did not correct the copy. It deleted it. The mesh builder’s origin, direction and lift arithmetic moved into one helper with a comment saying who it was for:

// Shared by build_branch and the 3D viewer's hotspot code.
// t_along: 0 = trunk-surface origin, 1 = branch tip.
function branch_point(_tree, _branch, _t_along) {

build_branch then called it for every ring centre and for the tip where the foliage goes. The viewer’s sixteen lines of hotspot maths became one:

var _mid = branch_point(tree, _b, 0.7);

About the “eight lines”. The diff is 35 added and 53 removed across five files, three of them docs. In the two GML files it is 28 lines in and 44 out: the helper is about twenty lines including its comment, the hotspot function lost fifteen, and build_branch lost the rest. The GML got sixteen lines shorter, the whole pull request eighteen. So the memory is wrong on the count and right on the point, which is that the fix added almost nothing. It took one formula that existed twice and made it exist once.

Why it mattered later

What made it load-bearing was not that the dots lined up that afternoon. It was that from then on, nobody who changed the shape of a branch had to remember the hotspots.

The same day, the wire-removal work landed and put its click targets on the same dots. Eleven days later the trunk learned to bend properly, with a frame carried along its curve, which moved every branch’s origin with the trunk; the helper now started with a trunk_frame_at lookup and every caller moved with it. The day after that, branches learned to curve rather than point, and branch_point became a closed-form arc integral, as How a Branch Learns to Curve explains; the same day they learned to droop, and the helper grew a twelve-step numerical integration for the mixed case. Each of those changes was made in one place. Each time, the mesh, the foliage tips, the wire coils and the clickable dots agreed without anyone checking.

Wire mode in the prototype's viewer: a juniper with a bent trunk and nine branches sweeping to the left, copper coils wrapped along several of them, and a numbered amber dot sitting on each branch about two thirds of the way out. Below, the help line reads Drag to rotate, Scroll to zoom, R to reset camera.

That screenshot is from June, three curve rewrites after the fix. The dots are on the branches because the dots and the branches are the same function.

Across the rewrite

The TypeScript port kept the arrangement and tightened it. branch_point is now branchPoint in src/sim/geometry.ts, in the simulation module that imports nothing from the renderer, and the file’s header says what the April fix only implied:

 * Trunk and branch curve geometry, in display units (sim cm scaled by
 * DISPLAY_SCALE). This is the single source of truth for tree shape: the
 * renderer, the trunk hotspot UI and the trunk-shape style scorers all read
 * from here, so what the player sees is what they are scored on. Don't write a
 * parallel curve derivation; call these.

The callers are the same cast. treeMesh.ts builds the bark rings from it, foliageMesh.ts puts the leaf clusters at branchPoint(tree, branch, 1), and hotspots.ts places each branch dot at branchPoint(tree, branch, BRANCH_HOTSPOT_T), where the constant is the prototype’s 0.7. The style scorers the header mentions read the trunk’s frames from the same module rather than branchPoint itself, as the prototype’s scorers had since May; the promise is the same one, made for the trunk.

The other thing the port added is a test that would have failed in April:

it('places branch hotspots 70% along each branch', () => {
  const tree = starter();
  const hs = hotspotWorldPoints(tree, ui({ mode: 'clip' }));
  tree.branches.forEach((branch, i) => {
    expect(hs[i]?.world).toEqual(branchPoint(tree, branch, 0.7));
  });
});

It pins the hotspot to the helper, not to a number. If someone writes a second derivation for the dots, as I did in April, the test fails before the dots drift. A browser test in the same slice checks that every hotspot is inside the viewport at the default camera and that all of them move after an 80 px drag, which is the projection half of the same promise.

What I take from it

Invisible plumbing is the load-bearing kind. The visible work on the viewer since April is the curves, the coils, the foliage and the toolbar, and none of that is why the dots are where they should be. The reason is one function, twenty lines then and fifty-six now, with a comment saying who may call it, and a test that says who must.

The lesson I keep relearning is the shape of the original mistake. It was not a wrong formula, and it was not an accident. It was a shortcut taken on purpose on the second day, and written down in a document instead of being taken out of the code. A rough edge that is documented feels handled. It isn’t. It is a second copy waiting for the first one to improve, and once a branch’s origin started moving with the trunk, it would have. The fix for that is never to correct the copy. It is to make the second copy impossible, and then to write the test that notices if someone tries.