Record and replay

Some things a program does can come out differently every time: random numbers, the clock, the contents of a file, the network. In Cresset those are all effects, so they all pass through one place. That makes a powerful trick possible: record a run, then replay it exactly.

Try it

fn roll() needs random:
    return randint(1, 6)

fn main() needs print, random:
    total = 0
    for turn in range(1, 6):
        r = roll()
        total += r
        print("turn {turn}: rolled {r}")
    print("total: {total}")

Record one run:

node cresset.js run examples/dice.crs --allow random --record dice.json

Replay it as many times as you like:

node cresset.js run examples/dice.crs --allow random --replay dice.json

The replay prints the same rolls and the same total, every time. You can also try it in the playground: run the Dice example, then press Replay last run.

What is recorded

Every effect except print: what the user typed (input), reading files, writing files, the clock, random numbers and network calls. A recorded interactive session can be replayed with nobody typing, and it shows the same prompts. During a replay those are not performed again; the recorded result is fed back in. So a replay doesn't touch your disk or ask the clock anything.

The recording is a plain JSON file listing each call and its result in order.

Why it matters

Safety checks

Keep recordings private

A recording contains the results of effects, including the contents of any file the program read. Treat a recording like the data it came from. If it holds anything sensitive, don't share it without scrubbing it first.

Next

Keep secrets out of text you share: Redaction.