Files
dingoandClaude Opus 5.5 a7a3d12283
Foundation / checks (deploy) (push) Successful in 3m29s
Foundation / checks (ecs) (push) Successful in 2m0s
Foundation / checks (client) (push) Successful in 12m51s
Foundation / checks (network) (push) Successful in 13m0s
Foundation / checks (tsan) (push) Successful in 7m50s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 32m4s
Foundation / checks (postgres) (push) Canceled after 19m13s
Wire M10.8c-1 region handoff: protocol v4, durable probes, crossing, ghosts
ADR-0096. Session protocol v4 (v3 is deployed and frozen) adds HandoffOffer,
SessionRedirect, close reasons handed_off and moved, and ghost marker records.
Sessions use the durable probe record: logins open it where it was saved
(redirected when it lives elsewhere), and session ends save it. A marker
crossing into a neighbour's area is frozen, prepared, its control released
and a signed ticket offered; uncommitted transfers are aborted after the
ticket's lifetime. The destination shows arriving probes as ghosts (schema
v13 incoming_probe_transfers) and opens the marker where it crossed.

Refreshes the dev session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 21:24:42 -04:00

115 lines
4.9 KiB
C++

#pragma once
#include "evo/network/probe_motion.hpp"
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <optional>
#include <span>
#include <vector>
// M10.7 client tick clock and remote-entity interpolation (ADR-0095). Headless,
// single-owner, presentation only: nothing here is authority or decides an outcome.
namespace evo::client {
// A drift-corrected, monotonic estimate of the server's simulated tick between snapshots.
// Each observation (a tick the client just learned the server had simulated) slews the
// estimate toward it by a fraction; a large disagreement snaps forward, never backward. The
// estimate never runs more than `max_ahead` ticks beyond the newest observation, so a
// paused or silent server freezes it instead of inventing time.
struct TickClockLimits {
double tick_ms{50};
double gain{0.1}; // Fraction of the error corrected per observation.
double snap_ticks{4}; // Errors beyond this snap (forward) instead of slewing.
double max_ahead{1.0}; // Ticks beyond the newest observation.
};
class TickClock {
public:
using Clock = std::chrono::steady_clock;
explicit TickClock(TickClockLimits limits = {}) : limits_{limits} {}
void reset() noexcept;
void observe(std::uint64_t tick, Clock::time_point now);
// The estimated server tick (fractional) at `now`; nothing before the first observation.
// Never decreases between resets.
[[nodiscard]] std::optional<double> at(Clock::time_point now);
[[nodiscard]] std::uint64_t snaps() const noexcept {
return snaps_;
}
// Ticks of local time since the last observation (nothing before the first): a silent
// server freezes the clock, so silence itself is what marks views stale.
[[nodiscard]] std::optional<double> silence(Clock::time_point now) const;
private:
[[nodiscard]] double raw(double base, Clock::time_point now) const;
TickClockLimits limits_;
std::optional<double> base_tick_;
Clock::time_point base_time_{}, seen_{};
std::uint64_t newest_{};
double returned_{};
std::uint64_t snaps_{};
};
// Remote probe markers (other sessions'), shown `delay` ticks behind the clock and
// interpolated between the buffered snapshots around that render tick. Beyond the newest
// snapshot a marker continues its replicated held vector for at most `max_extrapolation`
// ticks, then freezes and is marked stale. A marker appears at the tick of the snapshot that
// added it and disappears at the tick of the one that removed it (never interpolated toward a
// removal). Snapshots further apart than `max_gap` are not interpolated across: the older is
// held, marked stale, until the newer one's tick. The delay adapts to the observed gaps between
// received snapshots (lost or late ones), within [min_delay, max_delay].
struct InterpolationLimits {
double min_delay{2}, max_delay{6}; // Ticks (ADR-0095).
double max_extrapolation{2}; // Ticks beyond the newest snapshot.
std::uint64_t max_gap{8}; // Ticks between snapshots still interpolated.
std::size_t max_snapshots{16}; // Buffered snapshots.
std::size_t gap_window{40}; // Recent arrivals the delay adapts to.
double raise_per_second{2}, lower_per_second{0.5}; // Delay adaptation speed (ticks/s).
};
struct RemoteMarker {
std::uint64_t id{};
network::motion::State state;
bool ghost{}; // A probe being handed to this region (ADR-0096): read-only.
};
struct PresentedMarker {
std::uint64_t id{};
double x{}, y{}; // Micrometres.
bool extrapolated{}, stale{}, ghost{};
};
struct InterpolationStats {
std::uint64_t snapshots{}, ignored{}, extrapolated_frames{}, stale_frames{}, gap_snaps{};
};
class MarkerInterpolator {
public:
explicit MarkerInterpolator(InterpolationLimits limits = {})
: limits_{limits}, delay_{limits.min_delay}, target_{limits.min_delay} {}
void reset();
// A complete snapshot's remote markers (all of them: absence means removal). Snapshots
// not newer than the newest buffered one are ignored (reordering).
void push(std::uint64_t tick, std::span<const RemoteMarker> markers);
// Moves the delay toward its target (call once per frame).
void advance(double elapsed_ms);
// Markers at render tick `clock - delay()`.
[[nodiscard]] std::vector<PresentedMarker> at(double render_tick);
[[nodiscard]] double delay() const noexcept {
return delay_;
}
[[nodiscard]] double target_delay() const noexcept {
return target_;
}
[[nodiscard]] const InterpolationStats& stats() const noexcept {
return stats_;
}
private:
struct Snapshot {
std::uint64_t tick{};
std::vector<RemoteMarker> markers; // Sorted by id.
};
InterpolationLimits limits_;
std::deque<Snapshot> snapshots_;
std::deque<std::uint64_t> gaps_;
double delay_, target_;
InterpolationStats stats_;
};
} // namespace evo::client