Nilo

Journal14

Four faults, ten rounds

Bands of rust, tan and cream stone split by thin pale cracks into tall blocks that stand at different heights under faint cream bands

Strata: beds of stone laid one on another, pale above a height the token chooses, then cracked into blocks. Most blocks slip up or down from where the last one went, and each turns a little. Every block holds the same beds, in the same order. It is 72 lines, and ships as 3,615 bytes.

A model builds Nilo, and I asked it to fix four faults. It took ten rounds of building and review, about 22 hours, and then one more change to make the checks fast again. By the end, 62 files had changed and 6,818 lines had been added, and no shipped build of any piece had moved by a byte.

Four faults, one cause

The four were on the list of known faults, and they looked small.

  • A loop over a list never ended, when its body added to that same list by a route the compiler did not see.
  • An add to a list, made through a second name for it, was lost.
  • A call inside a ?: in the bound of a loop pulled an earlier call ahead of the start of the loop.
  • A value made inside one arm of a ?: was read after the ?:, where it might never have been made.

They had one cause. A line of Nilo is worked out from left to right, as it is written, and a call does what it does when it is reached. But the JavaScript the compiler writes does not always put things in that order. On the svg target, a circle wrote its radius before the second number of its centre. A function called with its values named out of order took them in its own order. So when one value called something that changed what another value read, the answer followed the order of the JavaScript, not the order of the line.

The compiler had no single rule for the order things happen in.

The job closed eight entries on the list: the four I asked for, and four more that had the same cause.

Every round passed

Each round fixed what it was shown and passed its own tests. Then a reviewer generated programs, built each one several ways, ran them, and compared what each build drew with a reference. Round after round, something came out drawn wrong.

The tests each round added covered the cases that round had been shown. Some of them did run a built piece and look at what it drew. None made new programs and compared every build with a reference.

In round 6 the reviewers built 2,300 programs 8 ways each. The compiler from before the job drew 4,105 of those builds wrong, round 5 drew 474 wrong, and round 6 drew 8. In round 9, with 3,480 programs, the old compiler drew 5,507 wrong, round 8 drew 548, and round 9 drew 36. All 36 were one fault the job had already written down. In round 1, 186 of 532 probe programs never finished, or ran out of memory, on the old compiler. On the new one, all finished.

The same mistake

The cause was the same each time. A round wrote its rule from the cases met so far: these kinds of thing are reads, these may change something, these are the shapes of code worth looking at. Then the reviewer found a case that was not among them.

It ended when the rule was built from complete lists instead. The part of the compiler that reads a piece knows 13 kinds of statement and 14 kinds of expression. One list now says, for every kind of statement and of expression, what it may change. Another says, for every kind of expression, how it reads. A kind missing from its list stops the compiler, and tests walk every kind, so a kind added later fails the tests until someone says what it does. Where the compiler cannot tell whether a call changes something, it takes it that it may.

For a piece, the rule is now simple. Reads and calls happen in the order the line gives them, in every build. The places where that is not yet true are on the list further down. A loop that visits each item of a list runs at most as many turns as the list held when it began. A list of colours is shared, like any other list.

Found on the way

The worst find was older than the job. v = vec(v.y, v.x) should swap the two parts of a point. From (3, 4) it gave (4, 4), in every build, since the language began. The new x was written first, and the new y then read the new x. Every build did the same thing, so no check that compares one build with another could ever have seen it. A reviewer found it in round 8. Now the new point is worked out in full before it is stored, whenever its second part reads its first. None of the pieces I have shipped did it: their builds would have moved, and none did.

There was a wrong turn as well. Round 4 made one line of 728 bytes come out as 90,423 bytes. A value used twice was written out twice, and again at every level it was nested in. Four levels deeper, the same line came to 786 KB. Round 5 fixed it. The check that came out of the job now fails when one of its own programs builds to more than a set size for its length.

The reference

The reviewers’ way of checking is now one of Nilo’s own checks. It holds 1,438 programs made from fixed seeds, the same ones on every run. Almost all of them come in two forms: as written, and with every call, roll and read moved into a variable of its own, in the order the line gives them. The second can only be worked out in one order, so it is the reference. Nilo builds a piece as a size build, made small, or as a speed build, made for a fast frame, and the size build can be searched for a smaller form. The check builds both forms all three ways, and each build of the first must draw what the same build of the reference draws. Twelve programs that move are checked another way, against circles the check works out for itself. Run on the compiler from before the job, the check fails.

It cannot see everything. The reference is built by the same compiler, so a fault the two share still passes. And every program runs from one fixed token whose eight 32-bit words are all the same, so two named randoms roll the same numbers, and a fault that swaps them draws the same thing. That one is on the list too, as a fault of the check.

From three minutes to fifteen, and back

During the job the check went from about three minutes to about fifteen. On a busy machine it then ran out of time and failed. By the rule in entry 13, that is a fault to find, not a run to repeat.

One part of the check was making more than 26,000 builds one after another, and many of them wrote the very same file. Now most of those builds are made side by side, and a file that has already been run is not run again: of 25,892 runs asked for, 3,950 were different. The check is back to about three minutes, and no case was taken out to get there.

How the job was run

Not everything that went wrong was in the compiler. The job grew from four faults into a rebuild of how the compiler keeps order, and the model running it did not stop to ask me whether that was still the job. Three times it said this should be the last round. Nothing showed that it would be.

Four rules came out of it, for every job from now on.

  • When a job doubles, stop and ask.
  • Ask for the complete list in the first brief, not the cases met so far.
  • Whoever builds a change runs the check of what it draws before handing it back.
  • One fault to a job.

What is not right yet

The job wrote down what it found and did not fix. Some of it:

  • A roll that nothing reads can be dropped, with anything the roll itself would have changed, when nothing else rolls from the same named random.
  • The two svg builds can round a coordinate that falls exactly on a half tenth in different directions.
  • The same colour can be written as #rrggbb in one piece and as rgb(r,g,b) in another. The image is the same; the text is not.

About ten of the recorded faults are places where two builds, or the two targets, still disagree.

Two more that it wrote down I have since settled as rules, not faults.

  • Reading past the end of a list is a mistake in the piece. What happens then is not defined, and the builds may differ.
  • A call that changes nothing, and whose value nothing reads, may be left out.

Next

  • A second reading of the language, one that does not go through the compiler, so that a fault every build shares has somewhere to show.
  • The faults where the builds still disagree.
  • Then the editor support.

The piece at the top

The image at the top comes from this token, built for the svg target: 0x332c23ffa4f34f68216c509a62ab236719f9925e0f1e54c4d92af491c305dd86

// strata: beds of stone laid one on another, then cracked into blocks; most slip, and each turns a little
plate(1920, 2400)
use noise v2, colour v2
random layout, bed, slip

let field = noise.table(layout)
let ramps = [#2b211c, #6b4a33, #b98b5a, #ecdcbc, #1f2629, #4d5b5c, #9aa39a, #e4e2d6,
  #2a1d24, #7a3e32, #c98a5b, #efdcc2, #23272e, #5b6470, #b7a98e, #efe6d2]
let pick = layout.int(0 ..< 4) * 4
let ramp = [ramps[pick], ramps[pick + 1], ramps[pick + 2], ramps[pick + 3]]
let sky = colour.lighten(ramp[3], amount: 0.3)
let n = 97, step = 2200 / (n - 1)    // points across each bed, from just off one side of the plate to the other
let ceiling = layout.float(700 ..< 1300), dip = layout.float(-0.12 ..< 0.12)

// the beds, from the foot up, each laid on the top of the one before (ys: the base, then the top of each bed, n heights a row); how thick
// and how dark holds a while, then changes; over the height the token chooses, the beds are pale and swell less
let ys = num[], inks = color[]
for i in 0 ..< n { ys.add(2900) }
var top: num = 2900, thick: num = 30, tone: num = 0.3, k = 0, stone = 0
while top > -300 {
  let low = top > ceiling
  if low { stone = k + 1 }
  if bed.chance(0.2) { thick = bed.float(3 ..< 40) }
  if bed.chance(0.25) { tone = bed.unit() }
  for i in 0 ..< n {
    let swell = noise.octaves(field, point: vec(i * 0.05, k * 0.7), layers: 3) * (low ? 0.5 : 0.2)
    ys.add(ys[k * n + i] - thick * (1 + swell))
  }
  let ink = colour.at(ramp, along: tone + bed.float(-0.08 ..< 0.08)), pale = bed.float(0 ..< 0.06)
  inks.add(low ? ink : colour.mix(sky, toward: ink, amount: pale))
  top -= thick
  k += 1
}

// the foot of bed b (row b of ys; row 0 is the base), bent by one broad fold and tipped, and everything over it
fn face(b: int): vec[] {
  let pts = [vec(2100, -500), vec(-100, -500)]
  for i in 0 ..< n {
    let x = i * step - 100
    pts.add(vec(x, ys[b * n + i] + 150 * noise.at(field, point: vec(x * 0.0005, 9)) + dip * (x - 1000)))
  }
  return pts
}

// the cracks, left to right, each wandering down the plate in 12 steps and kept at least 60 to the right of the one
// before; the first and the last are off the plate
let count = slip.int(3 ..< 12), heave = slip.float(100 ..< 300)
let cracks = vec[]
for c in 0 ..= count {
  var x = c == 0 ? -400 : (c == count ? 2320 : -60 + c * 2040 / count)
  for j in 0 ..= 12 {
    if c > 0 { x = max(x, cracks[c * 13 + j - 13].x + 60) }
    cracks.add(vec(x, j * 220 - 120))
    x += slip.float(-30 ..< 30)
  }
}

// each block slips from where the last one went, turns a little, and has the beds laid in it again, in their order
var drop: num = 0
for c in 0 ..< count {
  if slip.chance(0.75) { drop = clamp(drop + slip.float(-heave ..< heave), -350, 350) }
  // the outline of the block: down its left crack and back up the next
  let next = vec[], block = vec[]
  for j in 0 ..= 12 { next.add(cracks[c * 13 + 13 + j]) }
  for j in 0 ..= 25 { block.add(j < 13 ? cracks[c * 13 + j] : next[25 - j]) }
  let mid = lerp(block[6], next[6], 0.5)
  place(clip: block) {
    place(mid + vec(0, drop), turn: slip.float(-0.004 ..< 0.004)) {
      place(vec(0, 0) - mid) {
        for b in 0 ..< inks.len {
          polygon(face(b), fill: inks[b], stroke: colour.darken(inks[b], amount: b < stone ? 0.12 : 0.02), width: 2)
        }
      }
    }
  }
  polyline(next, stroke: sky, width: 5)
}