F6: Headless server entry point and smoke test

`tools/godot.sh -- --server` runs a headless dedicated server with
--seed, --port, --players, --bots, --ticks, --unthrottled and
--log-every. ServerLoop drives the simulation and times every tick
without a scene tree, so tests run it directly. A placeholder WanderBot
gives smoke runs realistic load until the v0.4 bots land.

Logs are JSON lines that all carry the seed. An ErrorCounter logger
makes any engine or script error exit 1; a bad command line exits 2.

`make smoke` (now part of `make check`) runs 1,000 ticks with 24 bots,
prints min/mean/p50/p95/p99/max tick time against the 12 ms budget,
reruns to confirm the state hash reproduces, and proves a deliberate
script error and a bad config both exit non-zero.

Closes #6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 20:07:39 -05:00
co-authored by Claude Opus 5.5
parent 46b131fe84
commit d9d4c04312
18 changed files with 621 additions and 3 deletions
+130
View File
@@ -0,0 +1,130 @@
## A headless dedicated server. It runs the match at the real tick rate, or back to back
## with `--unthrottled`, and writes structured logs: one JSON object per line on stdout,
## each carrying the match seed so any run can be reproduced.
##
## Exits 0 after a clean run, 1 if any engine or script error happened, and 2 if the
## command line was invalid.
class_name HeadlessServer
extends Node
## Catch-up limit from docs/ARCHITECTURE.md.
const MAX_TICKS_PER_FRAME: int = 5
## Ticks per frame when unthrottled. Keeps the engine's main loop responsive.
const UNTHROTTLED_BATCH: int = 250
## Engine frame cap in real-time mode, so an idle server doesn't spin a core.
const REALTIME_MAX_FPS: int = 120
const FAULT_TICK: int = 10
var config: ServerConfig
var loop: ServerLoop
var _errors: ErrorCounter = ErrorCounter.new()
var _accumulator: float = 0.0
var _started_usec: int = 0
var _finished: bool = false
func _init(server_config: ServerConfig) -> void:
config = server_config
func _ready() -> void:
OS.add_logger(_errors)
_started_usec = Time.get_ticks_usec()
if not config.is_valid():
for problem: String in config.errors:
_log("config_error", {"message": problem})
_finish(2)
return
if not config.has_seed:
config.match_seed = _fresh_seed()
Engine.max_fps = 0 if config.unthrottled else REALTIME_MAX_FPS
loop = ServerLoop.new(config.match_seed, config.players, config.bots)
var started: Dictionary = config.to_dictionary()
started["engine"] = Engine.get_version_info()["string"]
started["tick_rate_hz"] = Tuning.TICK_RATE_HZ
started["listening"] = false
started["note"] = "networking arrives in v0.5; players join as idle slots until then"
_log("start", started)
func _process(delta: float) -> void:
if _finished or loop == null:
return
var due: int = UNTHROTTLED_BATCH
if not config.unthrottled:
_accumulator += delta
due = mini(floori(_accumulator * Tuning.TICK_RATE_HZ), MAX_TICKS_PER_FRAME)
_accumulator = maxf(_accumulator - float(due) / Tuning.TICK_RATE_HZ, 0.0)
if due == MAX_TICKS_PER_FRAME:
# Too far behind to catch up: drop the backlog rather than spiral.
_accumulator = minf(_accumulator, 1.0 / Tuning.TICK_RATE_HZ)
for _index: int in due:
if config.ticks > 0 and loop.sim.tick >= config.ticks:
break
_tick()
if config.ticks > 0 and loop.sim.tick >= config.ticks:
_finish(0)
func _exit_tree() -> void:
# Stopped from outside, for example by SIGTERM: still report what happened.
if not _finished and loop != null:
_finish(0)
func _tick() -> void:
if config.fault == "script-error" and loop.sim.tick == FAULT_TICK:
_deliberate_script_error()
loop.run_tick()
if config.log_every > 0 and loop.sim.tick % config.log_every == 0:
var stats: Dictionary = loop.timing_stats()
_log("progress", {"tick": loop.sim.tick, "p95_us": stats["p95_us"], "max_us": stats["max_us"]})
func _finish(requested_code: int) -> void:
_finished = true
var code: int = requested_code
if loop != null:
var stats: Dictionary = loop.timing_stats()
stats["state_hash"] = loop.sim.state_hash()
stats["entities"] = loop.sim.entities.count()
_log("stats", stats)
if code == 0 and _errors.count() > 0:
code = 1
var stopped: Dictionary = {
"exit_code": code,
"errors": _errors.count(),
"wall_ms": roundi((Time.get_ticks_usec() - _started_usec) / 1000.0),
}
if _errors.count() > 0:
stopped["first_error"] = _errors.first()
_log("stop", stopped)
OS.remove_logger(_errors)
if is_inside_tree():
get_tree().quit(code)
func _log(event: String, fields: Dictionary) -> void:
var record: Dictionary = {
"t_ms": roundi((Time.get_ticks_usec() - _started_usec) / 1000.0),
"event": event,
"seed": config.match_seed,
}
if loop != null:
record["tick"] = loop.sim.tick
record.merge(fields, false)
print(JSON.stringify(record))
## A non-negative 63-bit seed. Only the server picks seeds; the simulation never does.
static func _fresh_seed() -> int:
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.randomize()
return (rng.randi() << 31 | rng.randi()) & 0x7FFFFFFFFFFFFFFF
## Calls a method on a null object, the most common real script error.
func _deliberate_script_error() -> void:
var missing: Object = null
missing.call("deliberate_fault")