Repository navigation
156 deterministic sim 1 the harness
Update (document 178): the check runs in CI (the engine's
.github/workflows/determinism.yml): a match recorded on Linux is replayed on Linux, on Windows and in a web export in headless Chromium, and all 1500 per-tick hashes agree on every platform. The match's setup is nowtests/determinism/determinism_match.gd, anddeterminism_check.gd -- --record <log> <hashes>records one.
Update (document 177): the float lint's allow-list is empty: 0 uses in the 36 simulation files. View code sits in
#region view/ai/loadregions; simulation code may call a view only as a whole statement, and a dotted read counts as a view read only when no simulation file has a name of its own spelled the same (and never for x, y, z, w).
Question: before converting anything to fixed-point (document 155, issue #133), how do we measure whether the simulation is deterministic, find the first place it isn't, and stop converted code from sliding back?
The deterministic simulation series: document 155 is the plan; this document is stage S0, the harness. Each later stage gets its own document.
A port feature, not a trace. Nothing here comes from RFIRE.BIN; it is test tooling for the port.
| Term | Meaning |
|---|---|
state fingerprint (SimHash) |
One 16-hex-digit number computed from the whole simulation state after a tick. Equal fingerprints mean equal state, down to the last bit of every float. |
| part | One of the match's own top-level variables (slots, soldiers, level, ...). Each has its own fingerprint, so a mismatch says where the state differs. |
| dump | The same walk as the fingerprint, written out as path = value lines, for a line-by-line diff. |
input log (InputLog) |
Every player's input word, in order, for a whole match, plus a free-form setup (level, skill, seed). |
| recording / replaying | Recording: the players' real controls (keys, or a computer player's brain) make the words and the log keeps them. Replaying: the log hands the words back and nobody else is asked. |
| xfail | "Expected failure": a check that is known to fail today and reports it without failing the test run. |
| ratchet | A limit that may only go down: the float lint's baseline. |
-
SimHashfingerprints the whole match state every tick by walking the match's own variables, and everything they reach, in a fixed order. It skips views, sound, resources, the pack, the controls and the computer players' brains. -
InputLogwithLoggedControlsrecords every player's words and replays them. The match can't tell the difference, because a vehicle only ever sees an ordinaryPlayerControls. This is exactly what lockstep netplay will send over the wire. -
determinism_checkrecords a 24-second computer-vs-computer fight, then replays it twice in-process and once in a second Godot process. It compares the fingerprints tick by tick. -
Today's result: all three comparisons first differ at tick 86, the first hit.
PlayerSlot.hit_at_msstores the wall clock (Time.get_ticks_msec()), so it differs in every run. That is S1's job. Until S1 and S2 land, the check prints this asxfail. -
float_lint_checkcounts 2,412 uses of floats,Vector2, library maths, global randomness, the wall clock anddeltaacross 36 simulation files. It fails if any file's count rises. Each conversion lowers the baseline; S6 needs zero.
Mermaid source
flowchart TB
subgraph rec["Recording run"]
direction TB
B1["each side's computer player"] --> L1["LoggedControls (recording)"]
L1 --> M1["match: one tick"]
L1 --> LOG["InputLog: words per player"]
M1 --> H1["SimHash per tick"]
end
subgraph rep["Replay runs (3)"]
direction TB
LOG2["the same InputLog"] --> L2["LoggedControls (replaying)"]
L2 --> M2["fresh match: one tick"]
M2 --> H2["SimHash per tick"]
end
LOG --> LOG2
H1 --> CMP["compare tick by tick: first difference, its part, its values"]
H2 --> CMP
SimHash.of_match(mc) starts from the match and walks its script variables in declaration order:
func _digest(mc: Object) -> Dictionary:
var parts := {}
var all := HashingContext.new()
all.start(HashingContext.HASH_SHA256)
_seen[mc.get_instance_id()] = 0
for name in _script_vars(mc):
_buf.clear()
_value(mc.get(name), name)
var d := _sha(_buf.data_array)
parts[name] = d
all.update(d.to_utf8_buffer())Translation: each of the match's variables is written into a byte buffer and hashed on its own; that gives the part fingerprints. The fingerprint of the whole state is a hash of those.
Every value is written with a type tag. A float is written as its exact IEEE bits (put_double), so a difference in the last bit still changes the fingerprint. Arrays and dictionaries are written in their own order. That order is part of the state, because the sim iterates them. An object reached a second time is written as a back-reference to its first visit. So a vehicle that both a slot and a target list point to is hashed once, and a cycle ends.
What is skipped, and why:
static func _is_sim(o: Object, cls: String) -> bool:
if o is WeakRef or o is Resource or o.get_script() == null:
return false
var sc: Script = o.get_script()
while sc != null: # a subclass of a skipped class is skipped too (LoggedControls is PlayerControls)
if SKIP_CLASSES.has(String(sc.get_global_name())):
return false
sc = sc.get_base_script()
if o is Node:
return SIM_NODES.has(cls)
return trueTranslation: weak references, resources (textures, sounds) and built-in Godot objects are never sim state. Neither is anything in SKIP_CLASSES (or a subclass of one):
- the pack, which never changes;
- the controls, whose words are the sim's input, kept by the log;
- the computer players' brains, which in netplay run on one peer only (document 155, "the computer players are not on this list");
- the camera, music and sound.
Nodes are presentation unless they are one of today's four sim nodes: Vehicle, Projectile, Mine, FlagMarker. Stage S4 turns those into plain objects. For those four, the node's position and rotation are hashed too. A skipped object writes the same bytes as null, so swapping one non-sim object for another is no change.
The level's _jitter, _facing and _decoration_lookup (SKIP_VARS) are caches built from the pack at load. They are skipped to keep the fingerprint cheap enough to take every tick. A 1,500-tick recording, fingerprinted every tick, takes about 5 seconds.
A vehicle polls its controls once a tick (Vehicle._process, the original's poll 0x42d290), and the hangar reads the same word / prev_word. So one class sitting where the controls are sees every word the sim gets:
func poll() -> int:
prev_word = word
if inner != null:
inner.poll()
word = inner.word
input_log.words[player].append(word)
else:
var w: Array = input_log.words[player]
if next < w.size():
word = w[next]
else:
word = 0
ran_out = true
next += 1
return wordTranslation: recording, LoggedControls polls the real controls (inner) and keeps the word. Replaying, it hands back the next logged word, and ran_out says if the replay polled more often than the recording did, which would itself be a divergence. InputLog.attach(mc, replay) puts one on every slot after the match's setup.
The log saves as JSON with the words as hex bytes, because a JSON number goes through a double, and a 32-bit word with its top bit set must come back exact.
tests/determinism_check.gd, on the engine's synthetic fixture:
-
The match. Both sides are computer players at the normal skill, so aim error and reaction times, and their randomness, are part of what is recorded. The fixture's map is small, and the no-spawn-camping rule (document 146) keeps each computer player away from the other's pad, so a match left alone has nobody shooting. The check therefore puts the two vehicles in the field five tiles apart, as
ai_combat_checkdoes. It asserts that the recording really had action: in the 1,500 ticks, shells were in the air on 1,479, both vehicles were hit, and two died. -
The tick is the one the engine tests already use (every
VehicleandProjectilenode, then the match, at 16 ms). Stage S1 makes a fixed order like this the engine's own. -
Four runs: the recording; two replays in this process; one replay in a second Godot process (
OS.executeof this same script with-- --child <log> <out>). - Three comparisons: replay against replay (is a replay repeatable?), recording against replay (the lockstep case: only the words are shared), and this process against the other.
- On a mismatch it runs the two sides again to the first differing tick with the dump on, and prints the part and the first differing values.
The harness's own checks come first: the same state hashes the same; moving a vehicle 1/1024 px changes the hash in the part that holds it; the dump names values by path; the controls are not state; the log round-trips exactly; a replay hands back every word in order and says when it runs out.
xfail deterministic: the same log replayed twice in one process; first difference at tick 86 (expected until S1 and S2, document 155): part 'slots'
slots[1].hit_at_ms = 20652.0 [00000000002bd440] vs 20666.0 [00000000802ed440]
Translation: the first hit lands at tick 86, and the hit time is stored in milliseconds of the wall clock. The radar's ping (document 107) reads it back against the clock (radar_ping_frame). Two runs land the same hit 14 ms apart in real time, so the state differs. The other two comparisons fail at the same tick for the same reason. It is the "wall clock in the sim" row of document 155's table, and S1 replaces it with a tick count. What diverges after it (the global randi(), the brains' rolls that a replay doesn't make) is hidden behind it for now. The check will show each in turn as the stages remove them.
EXPECT_DETERMINISTIC in the check flips to true once all three comparisons agree. From then on any divergence fails the test run.
tests/float_lint_check.gd reads each file in its SIM_FILES list, blanks out comments and strings, and counts six kinds of thing an integer simulation may not use:
| Kind | Counts | Today |
|---|---|---|
| float | the float type, float literals, the float helpers (absf, minf, floorf, ...) |
1,669 |
| vector |
Vector2 / Vector3 (not Vector2i) |
352 |
| math |
sin, cos, atan2, sqrt, ..., and .length(), .normalized(), .angle(), ... |
211 |
| delta | frame time | 130 |
| random |
randi, randf, ... (the sim's own CrtRand is fine) |
45 |
| clock |
Time.get_ticks_*, OS.get_ticks_*
|
5 |
The counts per file and kind are the baseline, tests/fixtures/float_lint_baseline.json. The check fails when any count is above it and names the file and kind; -- --list vehicle.gd prints each hit with its line. When a conversion lowers counts, the check says so and -- --update rewrites the baseline. The baseline never goes up, so converted code stays converted. A file added to the simulation joins SIM_FILES with a baseline of zero.
The check also tests its own patterns on known lines: a Vector2i and a hex constant don't count; a float in a comment or a string doesn't count; CrtRand.seed_with isn't the global seed(. A deliberate new sin(delta) in gate.gd fails it, which was tried and then reverted.
-
determinism_check: the harness checks above (all must pass), plus the three comparisons (xfailtoday). -
float_lint_check: the pattern self-test, every listed file exists, and no count above the baseline. - Every other engine check is unchanged. S0 adds files and changes no game code.
In the engine (alexdia25/openfire-engine):
-
game/sim/sim_hash.gd:SimHash -
game/sim/input_log.gd:InputLog -
game/sim/logged_controls.gd:LoggedControls -
tests/determinism_check.gd,tests/float_lint_check.gd,tests/fixtures/float_lint_baseline.json -
tests/README.md, section "Determinism"
Next: S1, one fixed tick (document 157). That means no delta and no wall clock in the sim, with the update order written down. After it, all three comparisons agree and EXPECT_DETERMINISTIC is on.
Return Fire, rebuilt
Elsewhere