Skip to content

Commit 262a280

Browse files
committed
Complete native host tick aggregation and validated runner startup
1 parent 08b5c88 commit 262a280

21 files changed

Lines changed: 5308 additions & 733 deletions

‎include/pineforge/market_driver.hpp‎

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
namespace pineforge {
1111

12+
struct NativeRunSpec;
13+
1214
// Semantic versions hashed into native continuation identity.
13-
inline constexpr const char* kNativeDriverSemanticVersion = "native-driver/v1";
14-
inline constexpr const char* kNativeConsumerSemanticVersion = "native-consumer/v1";
15+
inline constexpr const char* kNativeDriverSemanticVersion = "native-driver/v3";
16+
inline constexpr const char* kNativeConsumerSemanticVersion = "native-consumer/v3";
1517
inline constexpr const char* kNativeCalendarSemanticVersion = "native-calendar/v1";
1618

1719
enum class NativePriceProvenance : std::uint8_t {
@@ -44,6 +46,7 @@ struct NativeCoordinate {
4446
uint64_t ordinal = 0;
4547
int interval_index = 0;
4648
int64_t open_ms = 0;
49+
int64_t eligible_open_ms = 0;
4750
int64_t last_traded_close_ms = 0;
4851
int64_t next_period_open_ms = 0;
4952
int64_t next_input_open_ms = 0;
@@ -84,4 +87,71 @@ class INativeDriverSink {
8487

8588
bool native_bar_structurally_valid(const Bar& bar) noexcept;
8689

90+
// Confirmed-bar labels admitted by v9: the immutable nominal slot origin
91+
// and the scheduled clipped eligible opening. Both identify one slot key
92+
// `interval.open_ms`. Mid-slot and other off-grid timestamps are refused.
93+
inline bool native_confirmed_bar_label_admitted(
94+
const native_calendar::NativeInterval& interval, int64_t timestamp) noexcept {
95+
return timestamp == interval.open_ms || timestamp == interval.eligible_open_ms;
96+
}
97+
98+
// Canonical exclusive completion of a confirmed input slot. Source-price
99+
// time remains a separate fact (bar.timestamp / last print).
100+
inline int64_t native_canonical_input_completion(
101+
const native_calendar::NativeInterval& interval) noexcept {
102+
return interval.next_period_open_ms;
103+
}
104+
105+
// ---------------------------------------------------------------------------
106+
// Shared pure input preflight (host + runner)
107+
//
108+
// Exact signature the runner worker should bind:
109+
// NativeInputPreflightResult
110+
// preflight_native_inputs(const NativeRunSpec& spec,
111+
// const Bar* bars,
112+
// int n,
113+
// NativeInputPolicy policy);
114+
//
115+
// Calendar-aware, allocation of diagnostic text is the caller's job.
116+
// No host reset, callback, identity mutation, high-water change, or
117+
// decision-floor change. Parses the spec's session/timezone/input_tf
118+
// on each call. Batch may be sparse; StreamWarmup refuses missing
119+
// in-session bars after a closed gap by walking next_input_open_ms
120+
// through interval_containing (Fri 16 -> Mon 11 cannot skip Mon 09:30).
121+
// Warmup complete-script policy is NOT decided here.
122+
// ---------------------------------------------------------------------------
123+
enum class NativeInputPolicy : std::uint8_t {
124+
Batch = 0,
125+
StreamWarmup = 1,
126+
};
127+
128+
enum class NativeInputPreflightError : std::uint16_t {
129+
None = 0,
130+
NullArray = 1,
131+
InvalidCount = 2,
132+
StructuralInvalid = 3,
133+
Unaligned = 4,
134+
OffGridLabel = 5,
135+
NotStrictlyIncreasing = 6,
136+
OverlappingSlot = 7,
137+
InSessionGap = 8,
138+
CalendarFailure = 9,
139+
};
140+
141+
struct NativeInputPreflightResult {
142+
NativeInputPreflightError error = NativeInputPreflightError::None;
143+
int index = -1; // first offending bar, or -1 when not index-specific
144+
145+
constexpr bool ok() const noexcept {
146+
return error == NativeInputPreflightError::None;
147+
}
148+
constexpr explicit operator bool() const noexcept { return ok(); }
149+
};
150+
151+
NativeInputPreflightResult preflight_native_inputs(
152+
const NativeRunSpec& spec,
153+
const Bar* bars,
154+
int n,
155+
NativeInputPolicy policy);
156+
87157
} // namespace pineforge

‎include/pineforge/native_calendar.hpp‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,15 @@ std::optional<SessionCalendar> parse_session(std::string_view session,
178178
// separate). Also used by public resolve_civil. Not for hot interval loops.
179179
//
180180
// Accepted:
181-
// IANA/TZif names and aliases installed under the system zoneinfo tree
181+
// IANA/TZif names and aliases installed under libc's effective zoneinfo
182+
// root (not a search of trees libc will not read):
182183
// charset [A-Za-z0-9/_+-], no absolute path, no `.` / `..` components;
183-
// realpath stays in TZDIR and the file magic is TZif. Leading `:` is a
184-
// tzfile reference only.
184+
// realpath stays in that root and the file magic is TZif. Leading `:`
185+
// is a tzfile reference only.
186+
// macOS: /var/db/timezone/zoneinfo (TZDIR ignored; tzset(3) does not
187+
// use it). glibc: nonempty TZDIR is exclusive (relative allowed;
188+
// missing/non-directory fails closed). Unset/empty TZDIR uses
189+
// /usr/share/zoneinfo.
185190
// UTC, GMT, Etc/UTC, Etc/GMT
186191
// UTC/GMT conventional offsets (TV sign):
187192
// (UTC|GMT)[+-](H|HH|HMM|HHMM|H:MM|HH:MM) hours 0–23, minutes 0–59;
@@ -195,6 +200,32 @@ std::optional<SessionCalendar> parse_session(std::string_view session,
195200
// traversal, and non-TZif zoneinfo files (zone.tab, iso3166.tab, +VERSION).
196201
bool timezone_accepted(std::string_view timezone);
197202

203+
// Observational identity for the native runner (setup/identity time only).
204+
// Shares the acceptance/normalization/effective-root resolver. Does not hash.
205+
// Semantics version 1. nullopt = backing facts could not be established;
206+
// the runner must refuse. Does not relax timezone_accepted.
207+
enum class TimezoneSourceKind : std::uint32_t {
208+
Utc = 1,
209+
FixedOffset = 2,
210+
PosixExplicit = 3,
211+
PosixDefaultDst = 4,
212+
Tzfile = 5,
213+
};
214+
215+
struct TimezoneIdentityDescriptor {
216+
static constexpr std::uint32_t kSemanticsVersion = 1;
217+
std::uint32_t semantics_version = 0;
218+
TimezoneSourceKind kind = TimezoneSourceKind::Utc;
219+
std::string input;
220+
std::string effective_definition;
221+
std::string zoneinfo_root;
222+
std::vector<std::string> resource_paths;
223+
bool valid() const noexcept;
224+
};
225+
226+
std::optional<TimezoneIdentityDescriptor>
227+
timezone_identity_descriptor(std::string_view timezone);
228+
198229
class SessionWindow {
199230
public:
200231
SessionWindow() = default;

‎include/pineforge/native_host.hpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdint>
1010
#include <optional>
1111
#include <string>
12+
#include <type_traits>
1213
#include <variant>
1314
#include <vector>
1415

@@ -77,6 +78,10 @@ struct NativeFailed { std::optional<NativeRunSpec> spec; NativeFailure failure;
7778
using NativeLifecycle = std::variant<NativeUnconfigured, NativeReady, NativeRunning,
7879
NativeCompleted, NativeFailed>;
7980

81+
static_assert(std::is_trivially_copyable_v<NativeFailure>);
82+
static_assert(std::is_nothrow_move_constructible_v<NativeFailed>);
83+
static_assert(std::is_nothrow_move_assignable_v<NativeFailed>);
84+
8085
struct NativeStateView {
8186
NativeLifecycleKind kind = NativeLifecycleKind::Unconfigured;
8287
const NativeRunSpec* spec = nullptr;
@@ -123,7 +128,12 @@ struct NativeSetupResult {
123128
};
124129

125130
// Most-derived native strategy host. Binds NativeExecutionConsumer in the
126-
// protected engine constructor. Noncopyable and nonmovable.
131+
// protected engine constructor. Noncopyable and nonmovable. Lives in the
132+
// same inline engine epoch as BacktestEngine so old-header/new-library
133+
// linkage cannot resolve an unversioned constructor against a different
134+
// base layout.
135+
#define PINEFORGE_HAS_NATIVE_STRATEGY_HOST_V12 1
136+
inline namespace engine_script_run_v12 {
127137
class NativeStrategyHost : public BacktestEngine {
128138
public:
129139
NativeStrategyHost();
@@ -153,8 +163,10 @@ class NativeStrategyHost : public BacktestEngine {
153163
std::vector<NativeMarketEvent> native_events(uint64_t after_ordinal) const;
154164
int64_t native_decision_floor() const;
155165
uint64_t native_consumed_high_water() const;
166+
uint64_t native_continuation_hash() const;
156167

157168
friend class NativeExecutionConsumer;
158169
};
159170

171+
} // inline namespace engine_script_run_v12
160172
} // namespace pineforge

‎include/pineforge/native_run_spec.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ struct NativeRunSpecValidation {
111111

112112
// Complete validation, with deterministic first-error field order. Every
113113
// string is semantic UTF-8 without embedded NUL (all cross C-string v1).
114-
// Required: identity key, both timeframe literals, tickerid, both timezones.
114+
// Required: identity key, both timeframe literals, tickerid, scheduling timezone.
115+
// Empty chart timezone is preserved as optional observation metadata.
115116
// Calendar parsing/compatibility remain in native_calendar. Batch monthly
116117
// pairings are accepted here; stream-only restrictions belong to begin.
117118
// Calendar parsing may allocate. Allocation/other dependency exceptions are

‎runner/CMakeLists.txt‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ endif()
1515
target_link_libraries(pineforge_live_support PUBLIC ${_pf_sqlite_target} CURL::libcurl OpenSSL::Crypto Threads::Threads)
1616
target_compile_options(pineforge_live_support PRIVATE -Wall -Wextra -Wpedantic)
1717

18+
add_library(pineforge_live_startup STATIC native_startup.cpp)
19+
target_compile_features(pineforge_live_startup PUBLIC cxx_std_17)
20+
target_include_directories(pineforge_live_startup PUBLIC
21+
${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR}/include)
22+
target_link_libraries(pineforge_live_startup PUBLIC pineforge_live_support PineForge::pineforge)
23+
target_compile_options(pineforge_live_startup PRIVATE -Wall -Wextra -Wpedantic -ffp-contract=off)
24+
1825
add_executable(pineforge-live main.cpp parser.cpp)
1926
target_include_directories(pineforge-live PRIVATE
2027
${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR}/include)
21-
target_link_libraries(pineforge-live PRIVATE pineforge_live_support ${CMAKE_DL_LIBS})
28+
target_link_libraries(pineforge-live PRIVATE pineforge_live_startup ${CMAKE_DL_LIBS})
2229
target_compile_options(pineforge-live PRIVATE -Wall -Wextra -Wpedantic -ffp-contract=off)
2330
install(TARGETS pineforge-live RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2431

@@ -41,6 +48,10 @@ if(PINEFORGE_BUILD_TESTS)
4148
target_include_directories(test_live_json PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
4249
target_compile_options(test_live_json PRIVATE -UNDEBUG)
4350
add_test(NAME test_live_json COMMAND test_live_json)
51+
add_executable(test_native_live_startup ${PROJECT_SOURCE_DIR}/tests/test_native_live_startup.cpp)
52+
target_link_libraries(test_native_live_startup PRIVATE pineforge_live_startup)
53+
target_compile_options(test_native_live_startup PRIVATE -UNDEBUG)
54+
add_test(NAME test_native_live_startup COMMAND test_native_live_startup)
4455
add_test(NAME native_live_help COMMAND pineforge-live --help)
4556
add_executable(test_live_parser ${PROJECT_SOURCE_DIR}/tests/test_live_parser.cpp parser.cpp)
4657
target_include_directories(test_live_parser PRIVATE ${PROJECT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR})
@@ -60,6 +71,24 @@ if(PINEFORGE_BUILD_TESTS)
6071
$<TARGET_FILE:native_live_example> $<TARGET_FILE:native_live_parser_example>
6172
$<TARGET_FILE:live_parser_fixture>)
6273
set_tests_properties(native_live_e2e PROPERTIES TIMEOUT 120)
74+
foreach(contract_kind absent unknown native_no_export native_stub)
75+
add_library(live_contract_${contract_kind} MODULE
76+
${PROJECT_SOURCE_DIR}/tests/test_live_contract_plugins.cpp)
77+
target_include_directories(live_contract_${contract_kind} PRIVATE
78+
${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR}/include)
79+
target_compile_options(live_contract_${contract_kind} PRIVATE -fvisibility=hidden)
80+
set_target_properties(live_contract_${contract_kind} PROPERTIES PREFIX "")
81+
endforeach()
82+
target_compile_definitions(live_contract_absent PRIVATE PF_LIVE_CONTRACT_ABSENT=1)
83+
target_compile_definitions(live_contract_unknown PRIVATE PF_LIVE_CONTRACT_UNKNOWN=1)
84+
target_compile_definitions(live_contract_native_no_export PRIVATE PF_LIVE_CONTRACT_NATIVE_NO_EXPORT=1)
85+
target_compile_definitions(live_contract_native_stub PRIVATE PF_LIVE_CONTRACT_NATIVE_STUB=1)
86+
add_test(NAME native_live_startup_e2e COMMAND ${Python3_EXECUTABLE}
87+
${PROJECT_SOURCE_DIR}/tests/native_live_startup_e2e.py $<TARGET_FILE:pineforge-live>
88+
$<TARGET_FILE:native_live_example> $<TARGET_FILE:native_market_example>
89+
$<TARGET_FILE:live_contract_absent> $<TARGET_FILE:live_contract_unknown>
90+
$<TARGET_FILE:live_contract_native_no_export> $<TARGET_FILE:live_contract_native_stub>)
91+
set_tests_properties(native_live_startup_e2e PROPERTIES TIMEOUT 120)
6392
foreach(native_test test_native_live_store test_native_live_websocket)
6493
add_executable(${native_test} ${PROJECT_SOURCE_DIR}/tests/${native_test}.cpp)
6594
target_link_libraries(${native_test} PRIVATE pineforge_live_support)

‎runner/examples/native_market_strategy.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ PF_API void run_backtest_full(pf_strategy_t s, pf_bar_t* bars, int n,
6161

6262
PF_API void report_free(pf_report_t*) {}
6363

64+
// Pull PF_API runtime/stream/native-config symbols from the static engine.
65+
PF_API int native_market_example_abi_version() { return pf_abi_version(); }
66+
6467
} // extern "C"

0 commit comments

Comments
 (0)