mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-13 11:52:03 +00:00
feat(multiplayer): Phase 4 prediction correctness + two input-death fixes
Closes Phase 4's outstanding action-sequence-correctness invariant, then fixes two server-side bugs an adversarial review of that work uncovered. Server simulation, bot observations, collision resources and tick rate are unchanged: the server_physics_parity trace is byte-for-byte identical to HEAD across 360 ticks including both ships' full observation vectors. 4.11 - prediction history filed under the ISSUING sequence _send_local_input filed each post-step predicted state under the timeline's estimate of the sequence the server would consume this tick, trailing issuance by input_lead. The body had integrated the intent issued under _input_seq, so predicted[S] held "state after the intent from now" while the server's authority for S is "state after action(S)". They agree only while the stick is still. Filing under _input_seq costs nothing: which action the ship uses is decided in LocalNetShipController.get_action() and is untouched. Every prior Phase 4 gate held its input steady, and a steady input cannot falsify a sequence label - the 60s runs honestly reported marker=0/3784. New --exercise-input-transitions role toggles thrust every 6 ticks; it is the only gate that can catch a label regression. Verified non-vacuous: the old label fails it at 50%. 4.12 - issued-but-unsimulated sequences, and the release path An attack (delta > 1) issues and sends several sequences for one local physics step. Those gap sequences had no recorded prediction, so a server ack of one reported missing_not_recorded - indistinguishable from ring loss, costing a teleport and resync suppression several times a minute. They are now recorded stateless via record_unsimulated() and answered with a new "skip" decision mode. Free-flight hard snaps: 25/8/4 -> 0/0/0. A release (delta == 0) re-recorded at the unchanged _input_seq, filing the current intent under a sequence that went out carrying a different action; LocalInputTimeline deliberately refuses to mutate an issued sequence, so the ring contradicted the wire. Recording is now skipped on release ticks. 4.13 - two Phase 3 bugs silently killing player input (a) InputJitterBuffer.consume() advanced last_applied_seq on every tick including a starve. Since ingest() discards seq <= last_applied_seq, one starve on a sequence the client had not sent yet stranded the stream one ahead of arrivals permanently - both sides advancing in lockstep, every honest packet discarded on arrival. The client's own input_lead release is enough to trigger it, so input died for ~30 ticks roughly every 6.5s on a clean LAN. Now only gives up on a sequence once strictly newer data proves it lost. Silent-client stall and ring-overflow resync are unchanged. (b) The seq-range guard bounded incoming seq against highest_ingested_seq, which only advances inside ingest(), which that guard gates. After a ~2s host hitch every packet was rejected forever with no diagnostic (600+ consecutive rejections reproduced via SIGSTOP). Third iteration of this guard; each previous version bounded against a value only the accepted path could advance. Adds an escape after 10 consecutive rejections, which grants an attacker nothing the rate limiter does not already bound. (c) The transitions gate reported PASS at 3.76% while input was completely dead, because suppression stops _record_metrics - a worse outage yields fewer samples and a LOWER rate. Now scales the required sample count with run length and asserts the wire's server_stalled bit. Reverting both fixes makes it fail at samples 292/600, server_stalled=true, input_lead=12. Fixing (a) also explained a residual the review had already traced: 151 of 151 action-marker mismatches were the server repeating a stale action on a starve, not a prediction defect. Marker is now 0.00% in all three conditions (was 1.7-2.5%), and free-flight p99 improved to 0.141/0.168/0.154m from 0.170/0.176/0.184m. Two pre-existing test defects fixed alongside: the ball gate asserted RTT-masking on a link with no RTT (flaked 2 in 5; now asserted only at rtt >= 20ms, 5/5 under latency), and the two-bot CI compared scores across a 3-5s window (now polls the scores the server actually held; note score_changed is emitted only on the client path). QA: 72 unit tests; 60s free-flight at LAN/80+-20ms/5% loss; transition gate in all three; 2.0s and 3.5s host-freeze recovery; ball contact x5; two-bot CI x3; all three abuse roles; net/match_net/clock/lobby smokes. Phase 4 sign-off still pending a human playtest at ~100ms RTT - the milestone asks how it feels, which no gate here answers.
This commit is contained in:
@@ -93,14 +93,22 @@ func test_ring_wraparound_does_not_confuse_a_stale_slot_with_a_fresh_one() -> vo
|
||||
buf.ingest(0, [_action(0.0)])
|
||||
buf.consume()
|
||||
|
||||
# Advance last_applied_seq well past one full lap of the ring (32
|
||||
# entries) purely via starvation, with nothing re-ingested — every
|
||||
# ring slot's stored seq is now far behind "expected" at each step, so
|
||||
# none of them should ever be misread as valid.
|
||||
# Advance last_applied_seq well past one full lap of the ring (32 entries)
|
||||
# so every stored slot tag is far behind "expected" and none may be
|
||||
# misread as valid.
|
||||
#
|
||||
# This used to drive that purely by starvation with nothing re-ingested.
|
||||
# It can't any more, and shouldn't: starvation only gives up on a sequence
|
||||
# once strictly newer data proves it lost, because advancing past a
|
||||
# sequence the client has not sent yet permanently strands the stream (see
|
||||
# test_starving_ahead_of_the_client_does_not_permanently_discard_its_input).
|
||||
# Drive it the way the real failure does instead — the client's epoch runs
|
||||
# ahead while the intervening packets are lost.
|
||||
var far := InputJitterBuffer.RING_SIZE * 3
|
||||
buf.ingest(far, [_action(0.1)])
|
||||
for i in InputJitterBuffer.RING_SIZE * 2:
|
||||
buf.consume()
|
||||
assert_eq(buf.last_applied_seq, InputJitterBuffer.RING_SIZE * 2, "advanced purely by starvation")
|
||||
assert_true(buf.stalled, "long starvation run ends stalled")
|
||||
assert_true(buf.last_applied_seq > InputJitterBuffer.RING_SIZE, "advanced past a full lap of the ring")
|
||||
|
||||
# Now a fresh packet lands at the seq the ring slot for "expected" was
|
||||
# LAST used for, one full lap ago — if slot-tagging didn't work, this
|
||||
@@ -151,3 +159,76 @@ func test_ring_overflow_resyncs_to_fresh_data_instead_of_starving_forever() -> v
|
||||
# Normal sequential consumption resumes correctly from the resync point.
|
||||
var next := buf.consume()
|
||||
assert_almost_eq(next.thrust.z, float(expected_resync_seq + 1) * 0.01, 0.0001, "next tick continues in order from the resync point")
|
||||
|
||||
|
||||
# --- Starvation must not strand the stream (adversarial review, B2) ---------
|
||||
# consume() used to advance last_applied_seq on EVERY tick including a starve.
|
||||
# Because ingest() discards anything `seq <= last_applied_seq`, one starve on a
|
||||
# sequence the client had not sent yet left the server permanently one ahead of
|
||||
# arrivals: both sides then advance one per tick, the gap never closes, and
|
||||
# every honest packet is discarded on arrival. Reproduced on a clean LAN — the
|
||||
# client's own input_lead release (delta == 0, which issues no new sequence for
|
||||
# one tick) was enough to trigger it, roughly every 6.5s of ordinary play.
|
||||
|
||||
func _thrust(value: float) -> ShipAction:
|
||||
var a := ShipAction.new()
|
||||
a.thrust = Vector3(0.0, 0.0, value)
|
||||
return a
|
||||
|
||||
|
||||
func test_starving_ahead_of_the_client_does_not_permanently_discard_its_input() -> void:
|
||||
var buffer := InputJitterBuffer.new()
|
||||
buffer.ingest(1, [_thrust(1.0)])
|
||||
assert_almost_eq(buffer.consume().thrust.z, 1.0, 0.001, "seq 1 applies normally")
|
||||
|
||||
# The client issues NO new sequence this tick (an input_lead release), so
|
||||
# nothing newer than seq 1 exists. The server must keep expecting seq 2
|
||||
# rather than consuming — and discarding — it.
|
||||
assert_almost_eq(buffer.consume().thrust.z, 1.0, 0.001, "a starve repeats the last action")
|
||||
assert_eq(buffer.last_applied_seq, 1, "and does NOT advance past a sequence the client has not sent")
|
||||
|
||||
# The client's next real packet must still be accepted and applied.
|
||||
buffer.ingest(2, [_thrust(-1.0)])
|
||||
assert_almost_eq(buffer.consume().thrust.z, -1.0, 0.001, "the next honest input is still applied, not discarded")
|
||||
|
||||
|
||||
func test_sustained_release_pattern_does_not_black_out_input() -> void:
|
||||
# The full B2 shape: client and server both advance one per tick, but the
|
||||
# client duplicates one sequence (a release). Pre-fix, every packet from
|
||||
# this point on was discarded and the ship froze for 30 ticks.
|
||||
var buffer := InputJitterBuffer.new()
|
||||
buffer.ingest(1, [_thrust(1.0)])
|
||||
buffer.consume()
|
||||
buffer.consume() # release tick: server starves
|
||||
|
||||
var applied_real_input := 0
|
||||
for seq in range(2, 40):
|
||||
buffer.ingest(seq, [_thrust(1.0)])
|
||||
if absf(buffer.consume().thrust.z - 1.0) < 0.001:
|
||||
applied_real_input += 1
|
||||
assert_true(applied_real_input >= 35, "input keeps flowing after a release (applied %d/38)" % applied_real_input)
|
||||
assert_true(not buffer.stalled, "and the buffer never reports a stall")
|
||||
|
||||
|
||||
func test_a_genuinely_lost_packet_is_still_skipped_rather_than_waited_on() -> void:
|
||||
# The control for the two tests above: holding must not become "wait
|
||||
# forever". When strictly newer data has arrived, the missing sequence is
|
||||
# provably lost or reordered and must be given up on immediately.
|
||||
var buffer := InputJitterBuffer.new()
|
||||
buffer.ingest(1, [_thrust(1.0)])
|
||||
buffer.consume()
|
||||
buffer.ingest(3, [_thrust(-1.0)]) # seq 2 never arrives; 3 does
|
||||
buffer.consume() # starves on 2, but 3 is newer -> skip it
|
||||
assert_eq(buffer.last_applied_seq, 2, "a lost sequence is skipped once newer data exists")
|
||||
assert_almost_eq(buffer.consume().thrust.z, -1.0, 0.001, "and the newer sequence applies on the next tick")
|
||||
|
||||
|
||||
func test_a_silent_client_still_zeroes_and_stalls_on_schedule() -> void:
|
||||
# The other control: holding must not defeat the disconnect behaviour.
|
||||
var buffer := InputJitterBuffer.new()
|
||||
buffer.ingest(1, [_thrust(1.0)])
|
||||
buffer.consume()
|
||||
for i in InputJitterBuffer.STARVE_ZERO_TICKS + 2:
|
||||
buffer.consume()
|
||||
assert_true(buffer.stalled, "a silent client still stalls")
|
||||
assert_almost_eq(buffer.last_action.thrust.z, 0.0, 0.001, "and its ship still stops")
|
||||
|
||||
Reference in New Issue
Block a user