diff --git a/CMakeLists.txt b/CMakeLists.txt index 51a3572..d614be1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,9 +97,16 @@ include(CTest) if(EVO_BUILD_CLIENT OR EVO_BUILD_NETWORK) # SDL-free client value contracts (M9.3-M9.5), shared by the client and the dev # session codec. They copy read-only world/biology views, never the reverse. - add_library(evo_client_controls STATIC src/client/camera/camera.cpp src/client/input/input.cpp) + add_library(evo_client_controls STATIC src/client/camera/camera.cpp src/client/input/input.cpp src/client/ui/ui_state.cpp) target_include_directories(evo_client_controls PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") evo_owned_target(evo_client_controls) + if(BUILD_TESTING) + add_executable(evo-client-state-tests tests/client_ui_state.cpp) + target_link_libraries(evo-client-state-tests PRIVATE evo_client_controls) + evo_owned_target(evo-client-state-tests) + add_test(NAME client-ui-state COMMAND evo-client-state-tests) + set_tests_properties(client-ui-state PROPERTIES TIMEOUT 30 LABELS "client") + endif() add_library(evo_client_views STATIC src/client/views/environment_view.cpp src/client/views/organism_view.cpp) target_link_libraries(evo_client_views PUBLIC evo::world evo::biology) evo_owned_target(evo_client_views) diff --git a/CMakePresets.json b/CMakePresets.json index c444459..a258bf4 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -19,10 +19,10 @@ {"name": "analysis", "configurePreset": "analysis"}, {"name": "asan", "configurePreset": "asan"}, {"name": "tsan", "configurePreset": "tsan"}, - {"name": "client", "configurePreset": "client", "targets": ["evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests"]}, + {"name": "client", "configurePreset": "client", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests"]}, {"name": "client-probe", "configurePreset": "client-probe", "targets": ["evo-client-probe"]}, - {"name": "network", "configurePreset": "network", "targets": ["evo-network-tests", "evo-dev-session"]}, - {"name": "client-network", "configurePreset": "client-network", "targets": ["evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-network-tests", "evo-dev-session"]} + {"name": "network", "configurePreset": "network", "targets": ["evo-client-state-tests", "evo-network-tests", "evo-dev-session"]}, + {"name": "client-network", "configurePreset": "client-network", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-network-tests", "evo-dev-session"]} ], "testPresets": [ {"name": "debug", "configurePreset": "debug", "output": {"outputOnFailure": true}}, diff --git a/apps/evo-client/remote_session.hpp b/apps/evo-client/remote_session.hpp index fea8dfb..64b7456 100644 --- a/apps/evo-client/remote_session.hpp +++ b/apps/evo-client/remote_session.hpp @@ -146,6 +146,9 @@ class RemoteSession { std::printf("intent %llu outcome unknown (connection lost)\n", static_cast(e.sequence)); break; + case Kind::backpressure: + std::puts("session event backlog: disconnected; draining before reconnect"); + break; case Kind::exhausted: std::puts("session grants exhausted; issue a new bundle to reconnect"); break; diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index bd53f24..4deb4a6 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -43,13 +43,13 @@ RUN cmake -P tools/prepare-sdl3.cmake \ && cmake -P tools/prepare-imgui.cmake \ && cmake --preset client-probe -DCMAKE_CXX_COMPILER=clang++-19 -DCMAKE_C_COMPILER=clang-19 \ -DEVO_BUILD_CLIENT=ON -DEVO_BUILD_UI_PROBE=ON -DEVO_ENABLE_TIDY=ON -DSDL_X11=ON -DSDL_WAYLAND=OFF \ - && cmake --build build/client-probe --target evo-ui-probe evo-client-probe evo-client evo-client-lifecycle-tests evo-client-environment-tests evo-client-organism-tests --parallel 2 \ + && cmake --build build/client-probe --target evo-client-state-tests evo-ui-probe evo-client-probe evo-client evo-client-lifecycle-tests evo-client-environment-tests evo-client-organism-tests --parallel 2 \ && ctest --preset client-probe --no-tests=error \ && xvfb-run -a env SDL_VIDEO_DRIVER=x11 ./build/client-probe/evo-client-probe --renderer software \ && xvfb-run -a env SDL_VIDEO_DRIVER=x11 ./build/client-probe/evo-ui-probe --renderer software \ && xvfb-run -a env SDL_VIDEO_DRIVER=x11 ./build/client-probe/evo-client-lifecycle-tests \ && cmake --preset client-probe -DEVO_BUILD_CLIENT=ON -DEVO_SANITIZER=address -DEVO_ENABLE_TIDY=OFF \ - && cmake --build build/client-probe --target evo-ui-probe evo-client evo-client-lifecycle-tests evo-client-environment-tests evo-client-organism-tests --parallel 2 \ + && cmake --build build/client-probe --target evo-client-state-tests evo-ui-probe evo-client evo-client-lifecycle-tests evo-client-environment-tests evo-client-organism-tests --parallel 2 \ && ctest --preset client-probe --no-tests=error \ && xvfb-run -a env SDL_VIDEO_DRIVER=x11 ./build/client-probe/evo-ui-probe --renderer software \ && xvfb-run -a env SDL_VIDEO_DRIVER=x11 ./build/client-probe/evo-client-lifecycle-tests diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index d1ed01f..2e7303f 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -455,7 +455,9 @@ terrain meshing or procedural creature animation is required. | 9.8 Validate presentation and package the client | tools/client packaging, tests/client, docs/engineering | 9.1-9.7 | Reproducible native artifact, bounded snapshot/render queues, frame/memory report and documented manual visual checks | Install/run on supported dev platforms; long session, resolution/DPI matrix, headless regression suite | Declaring FUN or multiplayer scalability from a viewer | **M9.8a (2026-10-02):** isolated Dear ImGui 1.92.9b probe qualified on Windows/Linux; -application UI integration remains next. ADR-0084 is accepted for this probe. +M9.8b adds bounded UI state, protected external input capture and bounded session +event delivery. Launcher and visible widget integration remain M9.8c. ADR-0084 +records both scopes. **M9.8 planning (2026-10-02):** user-confirmed Windows ZIP, temporary Gitea Actions artifacts, labelled engineering UI/launcher and control rebinding. See the diff --git a/docs/STATUS.md b/docs/STATUS.md index 41f28df..ad31cac 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,6 +1,20 @@ # Project status and next steps -## Current gate — 2026-10-01 +## Current gate - 2026-10-02 + +M9.8a: Dear ImGui 1.92.9b is pinned and qualified in an isolated Windows/Linux +SDL3 probe (ADR-0084). M9.8b adds bounded notification/dialog state, protected +external input capture and bounded session events with disconnect/drain/reconnect +backpressure. Tests and limits are recorded in engineering/VALIDATION.md. + +Next: M9.8c launcher, visible dialogs/toasts, labelled views and click selection. +The client still displays the prior rectangle UI. Windows packages will be built +locally until a Windows runner exists. Testing is internal; 172.10.1.160 is the +RKE2 control-plane address. VM qualification and the cluster restart drill remain +outstanding, as agreed. See engineering/M9_8_DELIVERY_PLAN.md. + +## M9.7 delivered scope + M9.7 connects the engineering client to an authoritative dev region ([ADR-0083](adr/0083-dev-region-sessions.md), diff --git a/docs/adr/0084-engineering-ui-toolkit.md b/docs/adr/0084-engineering-ui-toolkit.md index 074c4db..cf7d2d4 100644 --- a/docs/adr/0084-engineering-ui-toolkit.md +++ b/docs/adr/0084-engineering-ui-toolkit.md @@ -79,3 +79,16 @@ docs/FONTS.md, imgui_draw.cpp and the stb headers' embedded notices at packaging. The bundled ProggyForever data also carries notices in imgui_draw.cpp; include the source notice inventory even when that font is not selected. Normal headless/client configuration does not require ImGui until explicitly enabled. + + +## M9.8b application state contract +Accepted for bounded toolkit-independent state. See +[CLIENT_UI_STATE](../engineering/CLIENT_UI_STATE.md): eight notifications with +240-byte text and explicit expiry/overflow; one id-matched modal. Input capture +requested by an external UI cannot be disabled by the legacy Tab shortcut. +Rendering and toolkit focus integration remain M9.8c. + +SessionClient's local event delivery is capped at 1024, reserving room for all 256 +unresolved intentions plus loss/backpressure events. On backlog it disconnects, +marks views stale and waits for the consumer to drain before reconnecting. Earlier +queued outcomes are preserved. This changes no wire format or biological rules. diff --git a/docs/architecture/CLIENT_ARCHITECTURE.md b/docs/architecture/CLIENT_ARCHITECTURE.md index 17dacac..5f6543a 100644 --- a/docs/architecture/CLIENT_ARCHITECTURE.md +++ b/docs/architecture/CLIENT_ARCHITECTURE.md @@ -77,3 +77,11 @@ Failure tests inject actual SDL events, including foreign window IDs, with no test-only production hooks. Real device failures and physical DPI/input require manual hardware qualification at M9.8. The native client is opt-in and never enters a server container. Headless configuration does not prepare/load SDL. + + +M9.8b adds [owned UI state](../engineering/CLIENT_UI_STATE.md): bounded notifications +and one modal, independent of toolkit/render/simulation types. The native adapter +protects externally requested capture from the legacy Tab shortcut. Session event +backpressure preserves queued events, disconnects and resolves remaining local +uncertainty as unknown, then waits for draining before reconnecting. This is not +a durable outcome-history database. The launcher/visible widgets follow in M9.8c. diff --git a/docs/engineering/CLIENT_UI_STATE.md b/docs/engineering/CLIENT_UI_STATE.md new file mode 100644 index 0000000..e2c51cc --- /dev/null +++ b/docs/engineering/CLIENT_UI_STATE.md @@ -0,0 +1,22 @@ +# M9.8b UI state and event bounds + +Owned, toolkit-independent state; all calls on the client main thread. No authority +or simulation state. Time is explicit monotonic milliseconds supplied by the host. + +Notifications: at most eight, text at most 240 bytes; reject invalid/oversized input, +expire by deadline, dismiss by unique id. Overflow evicts oldest and increments a +visible saturating counter. Notifications are not durable outcome history. +Dialogs: one active modal, title at most 96 bytes and body at most 240; opening a +second fails. Close requires the matching id so a stale click cannot close a newer +dialog. IDs never wrap. No credentials or grant bytes enter display messages. + +SessionClient keeps at most 1024 queued ClientEvents. At 766 regular events it emits +a local backpressure event, disconnects, and has room for loss plus 256 unknown +intentions. It preserves all previously queued events and pauses reconnect until +take_events drains the queue. This is a local condition, not a protocol change. +Unprocessed wire outcomes remain unknown, never assumed failed or committed. + +The native adapter's externally requested capture cannot be disabled by the legacy +Tab toggle. Releasing capture drains already queued input before new world actions. +Rendering dialogs/toasts and wiring toolkit capture occur with the launcher in +M9.8c; these contracts and their tests precede that integration. diff --git a/docs/engineering/M9_8_DELIVERY_PLAN.md b/docs/engineering/M9_8_DELIVERY_PLAN.md index d35804e..10315e4 100644 --- a/docs/engineering/M9_8_DELIVERY_PLAN.md +++ b/docs/engineering/M9_8_DELIVERY_PLAN.md @@ -1,6 +1,7 @@ # M9.8 delivery plan -Status: M9.8a isolated toolkit probe complete, 2026-10-02; remaining segments planned. +Status: M9.8a probe and M9.8b owned state/capture/event bounds complete, 2026-10-02; +remaining segments planned. No application UI or deployment completion is claimed. See ADR-0084 for the toolkit boundary. ## Confirmed scope diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 5383b46..6c9f34f 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -3566,7 +3566,7 @@ were not rebuilt; equivalent regressions above replace them. The `ci-client` Doc image and Gitea pipeline were not rerun here, and no cluster operation was performed. -## 2026-10-02 — consumed-grant ledger follow-up corrections +## 2026-10-02 — consumed-grant ledger follow-up corrections Resolved the two c588be2 review findings. Ledger open now acquires a nonblocking exclusive lifetime lock on a stable sibling file before reading or compacting @@ -3604,7 +3604,7 @@ repository-relative links and git diff --check. Documentation only: no code test package build, runner execution, endpoint verification or deployment performed. -## 2026-10-02 — M9.8a pinned engineering UI probe +## 2026-10-02 — M9.8a pinned engineering UI probe Adopted Dear ImGui 1.92.9b for an isolated SDL3/SDL_Renderer3 compatibility probe; normal client/headless builds do not require it. Exact upstream commit, locally @@ -3637,3 +3637,36 @@ explicitly deferred; control-plane IP is not assumed to be a MetalLB endpoint. Not run: Docker/Gitea image execution, physical GPU/DPI matrix, VM soak, client UI integration/launcher or packaging acceptance. Existing network and headless simulation behavior is unchanged; no simulation version or golden was modified. + + +## 2026-10-02 — M9.8b owned UI state and bounded session events + +Implemented toolkit-independent UiState: eight expiring/dismissible notices, +240-byte messages, an observable eviction count, one id-matched modal and explicit +monotonic time. Tests cover 10000-notice bursts, expiry boundary, malformed/oversized +messages, stale time, deadline overflow, modal contention and stale close IDs. +External native UI capture can no longer be escaped through the legacy Tab toggle. + +SessionClient queues at most 1024 events, reserves loss/unknown capacity, preserves +queued results, and disconnects on backlog until take_events drains it. The burst +regression accounts for every sent intention as a rejection or unknown, proves no +grant burning while paused and confirms reconnect after drain. Backpressure is a +local event, not a new wire message or simulation rule. Presets and the selective +Docker client targets include the new UI state test executable. + +Executed: Windows MSVC client-network configure/build/CTest 7/7; Linux GCC targeted +UI-state/network/native-lifecycle tests 3/3; Clang ASan/UBSan UI-state/network 2/2. +Static analysis initially flagged the new test main's escaping exception; added the +same process error boundary used by other executables. Windows exposed a timing +assumption in the initial one-message-per-sleep burst test; bounded batches replaced +that dependency. The explicit-target Windows preset initially omitted the new test +binary; fixed its target list and reran all seven tests successfully. + +Visible dialogs/toasts, launcher and toolkit focus routing remain M9.8c. These are +owned state and adapter contracts, not a claim of finished UI. No Docker/Gitea run, +VM qualification, live cluster test, packaging or deployment was performed. + +Final additional checks: Linux GCC state/network/lifecycle 3/3; Clang ASan/UBSan +native lifecycle 1/1; clang-tidy19 on owned state/session implementation, +state/network tests and native adapter/lifecycle tests passed with no owned +diagnostics. Final changed-file formatting passed. diff --git a/include/evo/client/application.hpp b/include/evo/client/application.hpp index cff3218..38c89e7 100644 --- a/include/evo/client/application.hpp +++ b/include/evo/client/application.hpp @@ -43,6 +43,7 @@ class Application { [[nodiscard]] WindowState state() const noexcept; [[nodiscard]] std::expected poll_events(); [[nodiscard]] std::expected present(const render::Frame& frame); + // External modal/widget capture cannot be disabled by the legacy Tab toggle. void set_ui_capture(bool captured) noexcept; [[nodiscard]] bool ui_captured() const noexcept; [[nodiscard]] Navigation take_navigation() noexcept; diff --git a/include/evo/client/ui_state.hpp b/include/evo/client/ui_state.hpp new file mode 100644 index 0000000..04d4e6c --- /dev/null +++ b/include/evo/client/ui_state.hpp @@ -0,0 +1,49 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include + +namespace evo::client { +enum class UiError : std::uint8_t { invalid, stale_time, busy, exhausted }; +enum class NoticeLevel : std::uint8_t { info, warning, error }; +struct Notice { + std::uint64_t id{}, expires_at_ms{}; + NoticeLevel level{}; + std::string text; +}; +struct Dialog { + std::uint64_t id{}; + std::string title, body; +}; +// Main-thread, bounded presentation state. Never store credentials or authority here. +class UiState { + public: + static constexpr std::size_t max_notices = 8, max_text = 240, max_title = 96; + [[nodiscard]] std::expected advance(std::uint64_t now_ms); + [[nodiscard]] std::expected + notify(std::string_view text, NoticeLevel level, std::uint64_t lifetime_ms = 5000); + [[nodiscard]] bool dismiss(std::uint64_t id); + [[nodiscard]] std::expected open_dialog(std::string_view title, + std::string_view body); + [[nodiscard]] bool close_dialog(std::uint64_t id); + [[nodiscard]] std::span notices() const noexcept { + return notices_; + } + [[nodiscard]] const std::optional& dialog() const noexcept { + return dialog_; + } + [[nodiscard]] std::uint64_t evicted_notices() const noexcept { + return evicted_; + } + + private: + std::uint64_t now_{}, next_id_{1}, evicted_{}; + std::vector notices_; + std::optional dialog_; +}; +} // namespace evo::client diff --git a/include/evo/network/session_client.hpp b/include/evo/network/session_client.hpp index 077e85a..38d5425 100644 --- a/include/evo/network/session_client.hpp +++ b/include/evo/network/session_client.hpp @@ -37,11 +37,12 @@ struct ClientEvent { enum class Kind : std::uint8_t { welcome, closed, - lost, // Transport/TLS failure or deadline: no Close received. - result, // IntentResult. - outcome, // IntentOutcome (durable or not committed). - unknown, // Admitted intention whose outcome died with its connection. - exhausted // No grants left; reconnection stops. + lost, // Transport/TLS failure or deadline: no Close received. + result, // IntentResult. + outcome, // IntentOutcome (durable or not committed). + unknown, // Sent intention whose outcome died with its connection. + backpressure, // Local event backlog: reconnect waits for take_events(). + exhausted // No grants left; reconnection stops. } kind{}; Welcome welcome{}; CloseReason reason{}; @@ -52,7 +53,7 @@ struct ClientEvent { enum class SessionClientError : std::uint8_t { configuration }; class SessionClient { public: - static constexpr std::size_t max_outstanding = 256; + static constexpr std::size_t max_outstanding = 256, max_events = 1024; [[nodiscard]] static std::expected, SessionClientError> create(SessionClientConfig config); SessionClient(const SessionClient&) = delete; @@ -69,6 +70,7 @@ class SessionClient { [[nodiscard]] std::optional welcome() const noexcept; [[nodiscard]] std::optional latest_view() const noexcept; [[nodiscard]] std::size_t grants_left() const noexcept; + // Drains at most max_events and resumes reconnect after local backpressure. [[nodiscard]] std::vector take_events(); private: diff --git a/src/client/platform/application.cpp b/src/client/platform/application.cpp index 41b9c4f..5c8b66e 100644 --- a/src/client/platform/application.cpp +++ b/src/client/platform/application.cpp @@ -50,6 +50,7 @@ struct Application::Impl { std::optional failure; InputState input; bool backlogged{false}; + bool external_capture{false}; std::expected refresh() { if (!SDL_GetWindowSize(window.get(), &state.logical_width, &state.logical_height) || @@ -72,6 +73,7 @@ WindowState Application::state() const noexcept { return result; } void Application::set_ui_capture(bool captured) noexcept { + impl_->external_capture = captured; if (impl_->input.captured() && !captured) { // An external UI can release capture with old input still queued in SDL. // Drain that batch before admitting fresh world presses. @@ -174,7 +176,7 @@ std::expected Application::poll_events() { event.key.windowID == id) { const bool down = event.type == SDL_EVENT_KEY_DOWN; if (event.key.scancode == SDL_SCANCODE_TAB && down && !event.key.repeat && - p.state.accepts_input() && !p.backlogged) + p.state.accepts_input() && !p.backlogged && !p.external_capture) p.input.capture(!p.input.captured()); if (const auto key = input_key(event.key.scancode)) { if (auto result = p.input.key(*key, down, event.key.repeat); !result) diff --git a/src/client/ui/ui_state.cpp b/src/client/ui/ui_state.cpp new file mode 100644 index 0000000..69e7b6b --- /dev/null +++ b/src/client/ui/ui_state.cpp @@ -0,0 +1,56 @@ +#include "evo/client/ui_state.hpp" +#include +#include +#include +namespace evo::client { +namespace { +bool valid_text(std::string_view text, std::size_t limit) { + return !text.empty() && text.size() <= limit && text.find('\0') == std::string_view::npos; +} +constexpr auto maximum = std::numeric_limits::max(); +} // namespace +std::expected UiState::advance(std::uint64_t now_ms) { + if (now_ms < now_) + return std::unexpected(UiError::stale_time); + now_ = now_ms; + std::erase_if(notices_, [&](const Notice& n) { return n.expires_at_ms <= now_; }); + return {}; +} +std::expected UiState::notify(std::string_view text, NoticeLevel level, + std::uint64_t lifetime_ms) { + if (!valid_text(text, max_text) || lifetime_ms == 0 || lifetime_ms > maximum - now_ || + (level != NoticeLevel::info && level != NoticeLevel::warning && + level != NoticeLevel::error)) + return std::unexpected(UiError::invalid); + if (next_id_ == maximum) + return std::unexpected(UiError::exhausted); + Notice notice{next_id_, now_ + lifetime_ms, level, std::string{text}}; + if (notices_.size() == max_notices) { + notices_.erase(notices_.begin()); + if (evicted_ != maximum) + ++evicted_; + } + notices_.push_back(std::move(notice)); + return next_id_++; +} +bool UiState::dismiss(std::uint64_t id) { + return std::erase_if(notices_, [&](const Notice& n) { return n.id == id; }) != 0; +} +std::expected UiState::open_dialog(std::string_view title, + std::string_view body) { + if (!valid_text(title, max_title) || !valid_text(body, max_text)) + return std::unexpected(UiError::invalid); + if (dialog_) + return std::unexpected(UiError::busy); + if (next_id_ == maximum) + return std::unexpected(UiError::exhausted); + dialog_.emplace(Dialog{next_id_, std::string{title}, std::string{body}}); + return next_id_++; +} +bool UiState::close_dialog(std::uint64_t id) { + if (!dialog_ || dialog_->id != id) + return false; + dialog_.reset(); + return true; +} +} // namespace evo::client diff --git a/src/network/session/client.cpp b/src/network/session/client.cpp index 5875e10..1b75a24 100644 --- a/src/network/session/client.cpp +++ b/src/network/session/client.cpp @@ -31,6 +31,7 @@ struct SessionClient::Impl { client::EnvironmentLayer environment; client::OrganismLayer organisms; std::vector events; + bool awaiting_event_drain{}; void disconnect(Clock::time_point now, std::optional reason) { if (step == Step::established || step == Step::awaiting_welcome || reason) @@ -87,6 +88,13 @@ struct SessionClient::Impl { return true; } bool frame(const OwnedFrame& f, Clock::time_point now) { + // Reserve loss + backpressure + every unresolved intention; never evict an event. + if (events.size() >= max_events - max_outstanding - 2) { + events.push_back({ClientEvent::Kind::backpressure, {}, {}, {}, {}, 0}); + awaiting_event_drain = true; + disconnect(now, std::nullopt); + return false; + } const FrameView v{f.type, f.payload}; last_received = now; if (step == Step::awaiting_welcome) { @@ -225,8 +233,14 @@ SessionClient::create(SessionClientConfig config) { void SessionClient::poll(std::chrono::steady_clock::time_point now) { auto& m = *impl_; m.polled_at = now; - if (m.phase == SessionPhase::exhausted) + if (m.awaiting_event_drain || m.phase == SessionPhase::exhausted) return; + if (m.events.size() >= max_events - max_outstanding - 2) { + m.events.push_back({ClientEvent::Kind::backpressure, {}, {}, {}, {}, 0}); + m.awaiting_event_drain = true; + m.disconnect(now, std::nullopt); + return; + } if (m.phase == SessionPhase::backoff) { if (now < m.retry_at) return; @@ -314,6 +328,7 @@ std::size_t SessionClient::grants_left() const noexcept { return impl_->config.grants.size() - impl_->next_grant; } std::vector SessionClient::take_events() { + impl_->awaiting_event_drain = false; return std::exchange(impl_->events, {}); } } // namespace evo::network diff --git a/tests/client_lifecycle.cpp b/tests/client_lifecycle.cpp index 35cfea3..ecf9bde 100644 --- a/tests/client_lifecycle.cpp +++ b/tests/client_lifecycle.cpp @@ -186,6 +186,9 @@ void native_input() { key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); app->set_ui_capture(true); + key(SDL_SCANCODE_TAB, true); // Legacy shortcut cannot escape an external modal. + key(SDL_SCANCODE_W, true); + CHECK(app->poll_events() && app->ui_captured()); CHECK(required(app->sample_intention(43)).move_y == 0); key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); diff --git a/tests/client_ui_state.cpp b/tests/client_ui_state.cpp new file mode 100644 index 0000000..347d531 --- /dev/null +++ b/tests/client_ui_state.cpp @@ -0,0 +1,44 @@ +#include "evo/client/ui_state.hpp" +#include +#include +#include +#include +using namespace evo::client; +void check(bool ok) { + if (!ok) + throw std::runtime_error("UI state contract"); +} +int main() try { + UiState ui; + check(ui.advance(100).has_value()); + auto first = ui.notify("Connected", NoticeLevel::info, 10); + check(first.has_value()); + check(ui.advance(109).has_value() && ui.notices().size() == 1); + check(ui.advance(110).has_value() && ui.notices().empty()); + check(!ui.dismiss(*first)); + check(!ui.advance(109)); + check(!ui.notify(std::string(241, 'x'), NoticeLevel::info)); + check(!ui.notify(std::string_view{"x\0y", 3}, NoticeLevel::info)); + check(!ui.notify("invalid", NoticeLevel::info, 0)); + for (int i = 0; i < 10000; ++i) + check(ui.notify("Burst", NoticeLevel::warning).has_value()); + check(ui.notices().size() == 8 && ui.evicted_notices() == 9992); + auto dismissed = ui.notices().back().id; + check(ui.dismiss(dismissed)); + check(ui.notices().size() == 7 && !ui.dismiss(dismissed)); + auto dialog = ui.open_dialog("Disconnect?", "Current intentions may remain unknown."); + check(dialog.has_value()); + check(!ui.open_dialog("Other", "Blocked")); + check(!ui.close_dialog(*dialog + 1)); + check(ui.close_dialog(*dialog)); + auto next = ui.open_dialog("New", "New modal"); + check(next.has_value()); + check(!ui.close_dialog(*dialog) && ui.dialog().has_value()); + check(ui.close_dialog(*next)); + check(ui.advance(std::numeric_limits::max()).has_value()); + check(!ui.notify("Overflow", NoticeLevel::error, 1)); + return 0; +} catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; +} diff --git a/tests/network_session.cpp b/tests/network_session.cpp index 5dbe6ad..2fb8f96 100644 --- a/tests/network_session.cpp +++ b/tests/network_session.cpp @@ -662,9 +662,42 @@ void liveness() { CHECK(h.until([&] { return again->phase() == SessionPhase::established; })); } } +void event_backpressure() { + Harness h; + h.tick(); + auto c = h.client(grants(7, 3, 100)); + CHECK(h.until([&] { return c->latest_view().has_value(); })); + std::uint64_t sent = 0; + CHECK(h.until([&] { + if (c->phase() != SessionPhase::established) + return true; + // Invalid vector: final rejection frees outstanding capacity, but the host + // intentionally never drains its event queue throughout this burst. + for (int batch = 0; batch < 16; ++batch) + if (c->send({sent + 1, 0, 1000, 1000})) + ++sent; + return false; + })); + CHECK(c->phase() == SessionPhase::backoff); + const auto remaining = c->grants_left(); + for (int i = 0; i < 100; ++i) + c->poll(Clock::now() + std::chrono::seconds{60}); + CHECK(c->grants_left() == remaining && c->phase() == SessionPhase::backoff); + CHECK(c->environment().freshness() == client::Freshness::stale); + const auto events = c->take_events(); + CHECK(events.size() <= SessionClient::max_events); + CHECK(has(events, ClientEvent::Kind::backpressure) && has(events, ClientEvent::Kind::lost)); + const auto accounted = std::ranges::count_if(events, [](const ClientEvent& e) { + return e.kind == ClientEvent::Kind::result || e.kind == ClientEvent::Kind::unknown; + }); + CHECK(static_cast(accounted) == sent); + CHECK(h.until([&] { return c->latest_view().has_value(); })); + CHECK(c->phase() == SessionPhase::established); +} } // namespace void session_tests() { + event_backpressure(); ledger_exclusive_owner(); ledger_incomplete_records(); connect_view_intents_and_outcomes();