#pragma once // Bounded polling for deferred results (chat, image, video). Elapsed time is // the sum of the sleeps, so the bound is deterministic and testable with a // fake sleeper. Nothing here waits unbounded. #include #include #include "xai/core/caps.h" #include "xai/core/params.h" namespace molo::xai::core { struct WaitPolicy { int interval_ms = Caps::kDefaultPollIntervalMs; int timeout_ms = Caps::kDefaultWaitMs; void validate() const { Check::range("poll interval ms", interval_ms, Caps::kMinPollIntervalMs, Caps::kMaxPollIntervalMs); Check::range("wait timeout ms", timeout_ms, Caps::kMinPollIntervalMs, Caps::kMaxWaitMs); } // Upper bound of step() calls: one immediately, then one per interval. int max_polls() const { return timeout_ms / interval_ms + 1; } }; using Sleeper = std::function; Sleeper real_sleeper(); // std::this_thread::sleep_for // Calls `step` until it yields a value. Returns nullopt when the policy's // budget is spent (the caller reports the timeout loudly). template std::optional poll_until(const WaitPolicy& policy, const std::function()>& step, const Sleeper& sleeper) { policy.validate(); const int polls = policy.max_polls(); for (int i = 0; i < polls; ++i) { if (std::optional v = step()) return v; if (i + 1 < polls) sleeper(policy.interval_ms); } return std::nullopt; } } // namespace molo::xai::core