PineForge HPO 1.11.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
window_plan.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pineforge/selected_window_plan.h>
4
5#include <cstddef>
6#include <cstdint>
7#include <filesystem>
8#include <functional>
9#include <string>
10
11namespace pineforge::hpo {
12
13namespace detail {
15struct WindowPlanTestAccess;
16} // namespace detail
17
19enum class PlanStatus : std::int32_t {
20 Ok = 0,
21 RequestInvalid = 1,
25 RowsUnordered = 5,
26 InternalError = 6
27};
28
32 std::int64_t start_ms = 0;
34 std::int64_t end_ms = 0;
36 std::int64_t fed_start_ms = 0;
38 std::uint32_t preroll_bars = 0;
40 std::string input_tf;
42 std::string script_tf;
44 std::string chart_timezone;
46 std::string engine_timezone;
48 std::string session;
50 bool feed_tolerant = true;
51};
52
54struct PlanResult {
56 pf_selected_plan_result_v1 raw{};
57
59 PlanStatus status() const noexcept { return static_cast<PlanStatus>(raw.status); }
61 bool ok() const noexcept { return status() == PlanStatus::Ok; }
62};
63
71class WindowPlanHelper final {
72public:
83 static WindowPlanHelper load(const std::filesystem::path& absolute_path,
84 const std::string& expected_sha256_hex,
85 const std::filesystem::path& private_run_directory);
86
97
103 PlanResult plan_selected_primary(const pf_bar_t* rows, std::size_t count,
104 const PlanRequest& request) const;
105
107 std::uint32_t version() const noexcept { return version_; }
109 const std::filesystem::path& source_path() const noexcept { return source_path_; }
111 std::uint64_t source_device() const noexcept { return source_device_; }
113 std::uint64_t source_inode() const noexcept { return source_inode_; }
115 std::uint64_t source_size() const noexcept { return source_size_; }
117 const std::filesystem::path& snapshot_path() const noexcept { return snapshot_path_; }
119 const std::string& sha256() const noexcept { return sha256_; }
121 static constexpr const char* self_test_id() noexcept {
122 return "selected-plan-utc-six-hours/v1";
123 }
124
125private:
126 friend struct detail::WindowPlanTestAccess;
127 using VersionFn = decltype(&::pf_selected_plan_version);
128 using PlanFn = decltype(&::pf_plan_selected_primary_v1);
129
130 WindowPlanHelper() = default;
138 static WindowPlanHelper load_impl(const std::filesystem::path& absolute_path,
139 const std::string& expected_sha256_hex,
140 const std::filesystem::path& private_run_directory,
141 const std::function<void()>& after_source_open,
142 const std::function<void(const std::filesystem::path&)>&
143 after_snapshot_close);
144 static VersionFn bind_version(void* library);
145 static PlanFn bind_plan(void* library);
146 void self_test() const;
147 void close() noexcept;
148
149 std::filesystem::path source_path_;
150 std::filesystem::path snapshot_path_;
151 std::string sha256_;
152 void* library_ = nullptr;
153 PlanFn plan_ = nullptr;
154 std::uint32_t version_ = 0;
155 std::uint64_t source_device_ = 0;
156 std::uint64_t source_inode_ = 0;
157 std::uint64_t source_size_ = 0;
158};
159
160} // namespace pineforge::hpo
Owns a verified, privately snapshotted standalone planner library.
std::uint64_t source_size() const noexcept
Returns the byte length of the opened, verified source.
~WindowPlanHelper()
Closes the library without deleting the snapshot.
WindowPlanHelper & operator=(WindowPlanHelper &&other) noexcept
Closes the previous library and transfers ownership without deleting either snapshot.
std::uint32_t version() const noexcept
Returns the validated bridge version, 1.
std::uint64_t source_inode() const noexcept
Returns the inode of the opened, verified source, even if its pathname was replaced.
const std::string & sha256() const noexcept
Returns the observed SHA-256 that matched the expected content before loading.
WindowPlanHelper(const WindowPlanHelper &)=delete
A library owner cannot be copied.
static constexpr const char * self_test_id() noexcept
Frozen behavior identifier; a later engine behavior change must bump this identifier.
WindowPlanHelper(WindowPlanHelper &&other) noexcept
Transfers ownership of the library and snapshot identity.
const std::filesystem::path & snapshot_path() const noexcept
Returns the absolute verified snapshot path actually passed to dlopen().
std::uint64_t source_device() const noexcept
Returns the device of the opened, verified source, even if its pathname was replaced.
WindowPlanHelper & operator=(const WindowPlanHelper &)=delete
A library owner cannot be copied.
PlanResult plan_selected_primary(const pf_bar_t *rows, std::size_t count, const PlanRequest &request) const
Calls the C bridge with exactly rows, without copying or repacking the bar array.
static WindowPlanHelper load(const std::filesystem::path &absolute_path, const std::string &expected_sha256_hex, const std::filesystem::path &private_run_directory)
Snapshots and hashes an absolute source path before loading any of its code.
const std::filesystem::path & source_path() const noexcept
Returns the absolute source path supplied to load(), for provenance only.
@ Ok
Every field is finite.
PlanStatus
Planner status words, independent of the C bridge's negative call return codes.
@ BoundaryUnaligned
T, E or F is not a script interval boundary.
@ FeedRangeInvalid
A supplied timestamp is outside [F, E).
@ RequestInvalid
The planner refused a request member or row pointer.
@ InternalError
The planner reported an internal failure as result data.
@ CalendarUnsupported
The calendar, clock or timeframe pairing is unsupported.
@ Ok
A complete plan is available.
@ RowsUnordered
Supplied timestamps are not strictly increasing.
Owned request strings and fixed-width fields for the selected primary C planner.
std::string script_tf
Primary script timeframe token.
std::string chart_timezone
Chart timezone; an empty string retains the engine default.
bool feed_tolerant
Enables the engine's feed-tolerant grouping.
std::string engine_timezone
Engine timezone; an empty string retains the engine default.
std::string input_tf
Explicit input timeframe token.
std::int64_t fed_start_ms
Supplied-feed floor F, in Unix milliseconds.
std::uint32_t preroll_bars
Requested pre-roll script groups N; the engine admits at most 5000.
std::int64_t end_ms
Exclusive selected end E, in Unix milliseconds.
std::string session
Session specification; an empty string retains the engine default.
std::int64_t start_ms
Selected start T, in Unix milliseconds.
Value copy of the complete 248-byte engine result, including planner refusals.
pf_selected_plan_result_v1 raw
Unmodified engine descriptor; optional timestamps are governed by present_mask.
bool ok() const noexcept
Returns whether this result contains a successful plan.
PlanStatus status() const noexcept
Returns the validated planner status, never a bridge-level return code.