Skip to content

Commit b0cec54

Browse files
luisleo526claude
andcommitted
Port the wave-2 lanes' hash-neutrality guards to native_run_spec_digest()
The continuation identity folds the host's resolved timezone resources — this tree's zoneinfo root is /private/var/db/timezone/tz/2026c.1.0/zoneinfo, a glibc runner's is /usr/share/zoneinfo — so a raw native_continuation_hash() or stream_state_hash() constant pinned on one machine cannot pass on another. The three wave-2 lane tests pinned exactly that, the same way the wave-1 lanes did before b07c57a retired it. native_run_spec_digest() (native_run_spec_v3) is the consumer's own spec fold and nothing else; it never reaches hash_tz_identity, which belongs to continuation_hash() alone. Each of the three tests now pins that digest for its lane's default spec, asserts it is unchanged when the lane's fields are restated at their defaults and moves when one is set, and keeps every event, fill, book, trade and counter assertion it already made: test_native_margin_model margin absent folds nothing; a model left at every default still moves the fold. test_native_calc_timing BarClose/Complete/8 restated folds nothing, and the recalculation bound is inert on its own; trigger or open-bar view moves it. test_native_htf_subscriptions_ an empty series list stated outright folds stream nothing; declaring one moves it. The remaining continuation and stream-state comparisons are between two runs in this process. Proven under TZDIR=/nonexistent and TZ=Asia/Kolkata (note that Darwin ignores TZDIR by design, src/native_calendar.cpp:820-829, so those runs show insensitivity, not the cross-machine move itself). continuation_hash, hash_spec's field order and every lane's semantics are untouched. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 3b60b17 commit b0cec54

3 files changed

Lines changed: 174 additions & 42 deletions

File tree

‎tests/test_native_calc_timing.cpp‎

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@ std::string partial_of(const NativeStrategyHost& host) {
122122

123123
// ---- 1. the BarClose default surface --------------------------------------
124124

125-
// Pinned from a clean b01ef03 build running this exact spec and batch through
126-
// a host that implements only on_native_bar (the pre-L5 contract).
127-
constexpr std::uint64_t kDefaultContinuationHash = 9019705263044865965ull;
125+
// The cadence folds into the run-spec fold only once the trigger or the
126+
// open-bar view is non-default, so a BarClose spec folds exactly the pre-L5
127+
// fields. That is pinned as native_run_spec_digest() and not as a continuation
128+
// hash: the continuation identity also folds the machine's resolved timezone
129+
// resources (zoneinfo root and zone file paths), so a raw constant passes here
130+
// and fails on CI. Observed on THIS tree for base_spec("native-calc-timing");
131+
// it guards the fold's field list and order, while the neutrality claim is the
132+
// equalities in test_bar_close_default — including the callback log, the book
133+
// and the recalculation counters, which are the pre-L5 contract itself.
134+
constexpr std::uint64_t kDefaultSpecDigest = 12300031127478902007ull;
128135

129136
class DefaultHost final : public NativeStrategyHost {
130137
public:
@@ -183,24 +190,55 @@ void test_bar_close_default() {
183190
// of the run is the script bar's own.
184191
CHECK(host.native_recalculation_count() == 0);
185192
CHECK(host.native_recalculations_skipped() == 0);
186-
// 7. hash neutrality against the pre-L5 tip.
187-
if (host.native_continuation_hash() != kDefaultContinuationHash) {
188-
std::printf(" continuation hash %llu, pinned %llu\n",
189-
static_cast<unsigned long long>(host.native_continuation_hash()),
190-
static_cast<unsigned long long>(kDefaultContinuationHash));
193+
// 7. hash neutrality against the pre-L5 tip, stated portably.
194+
const auto base = base_spec("native-calc-timing");
195+
const auto digest = native_run_spec_digest(base);
196+
if (digest != kDefaultSpecDigest) {
197+
std::printf(" spec digest %llu, pinned %llu\n",
198+
static_cast<unsigned long long>(digest),
199+
static_cast<unsigned long long>(kDefaultSpecDigest));
191200
}
192-
CHECK(host.native_continuation_hash() == kDefaultContinuationHash);
193-
194-
// The spec folds the cadence only when it is non-default, so the two
195-
// opted-in specs below cannot share this continuation identity.
196-
auto moved = base_spec("native-calc-timing");
201+
CHECK(digest == kDefaultSpecDigest);
202+
203+
// Spelling all three cadence fields out at their defaults folds nothing,
204+
// and the recalculation bound is inert while the cadence is BarClose.
205+
auto stated = base;
206+
stated.calculation = NativeCalculationTrigger::BarClose;
207+
stated.max_recalculations_per_point = 8;
208+
stated.open_bar_view = NativeOpenBarView::Complete;
209+
CHECK(native_run_spec_digest(stated) == kDefaultSpecDigest);
210+
auto bound_only = base;
211+
bound_only.max_recalculations_per_point = 3;
212+
CHECK(native_run_spec_digest(bound_only) == kDefaultSpecDigest);
213+
214+
// Moving the trigger, or the open-bar view on its own, is what moves it.
215+
auto moved = base;
197216
moved.calculation = NativeCalculationTrigger::BarCloseAndFills;
217+
CHECK(native_run_spec_digest(moved) != kDefaultSpecDigest);
218+
auto open_only = base;
219+
open_only.open_bar_view = NativeOpenBarView::OpenOnly;
220+
CHECK(native_run_spec_digest(open_only) != kDefaultSpecDigest);
221+
222+
// The same three facts at run level, compared between runs in this process
223+
// so no continuation constant is needed: the restated defaults reproduce
224+
// this run's callback log and identity, and the opted-in spec cannot share
225+
// that identity.
226+
DefaultHost restated;
227+
CHECK(restated.configure_native(stated).status == NativeSetupStatus::Applied);
228+
restated.run(bars.data(), static_cast<int>(bars.size()), "1", "1", false, 4,
229+
MagnifierDistribution::ENDPOINTS);
230+
CHECK(restated.last_error().empty());
231+
CHECK(restated.log == want);
232+
CHECK(restated.native_recalculation_count() == 0);
233+
CHECK(restated.native_recalculations_skipped() == 0);
234+
CHECK(restated.native_continuation_hash() == host.native_continuation_hash());
235+
198236
DefaultHost opted;
199237
CHECK(opted.configure_native(moved).status == NativeSetupStatus::Applied);
200238
opted.run(bars.data(), static_cast<int>(bars.size()), "1", "1", false, 4,
201239
MagnifierDistribution::ENDPOINTS);
202240
CHECK(opted.last_error().empty());
203-
CHECK(opted.native_continuation_hash() != kDefaultContinuationHash);
241+
CHECK(opted.native_continuation_hash() != host.native_continuation_hash());
204242
}
205243

206244
// The BarClose calculation reaches on_native_recalculate too, and its default

‎tests/test_native_htf_subscriptions_stream.cpp‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// 4. native_series_bar answers with the latest delivered bucket on both
1313
// sides of the warmup -> live boundary;
1414
// 5. a stream whose spec declares no series keeps this tree's event
15-
// sequence, continuation hash and stream state hash, pinned from a build
16-
// of the tip this lane branched from;
15+
// sequence and folds nothing new into its run-spec digest (the portable
16+
// half of the continuation identity), with the continuation and stream
17+
// state hashes compared between two streams in this process;
1718
// 6. a session-clipped daily series (a calendar aggregator, whose buckets
1819
// close on the session close and not on a bar count) over a stream whose
1920
// warmup stops mid-session is the batch's series, bucket for bucket;
@@ -435,11 +436,17 @@ void test_series_bar_across_the_boundary() {
435436

436437
// ---- 5. a stream without subscriptions is unchanged -----------------------
437438

438-
// Pinned from a build of 9f2c61b — this lane's base tip — running this exact
439-
// stream (8 warmup bars, 4 pushed, stream_end(false)) through a bare
440-
// NativeStrategyHost whose spec declares no series.
441-
constexpr std::uint64_t kNeutralStreamContinuationHash = 3068282625134746017ull;
442-
constexpr std::uint64_t kNeutralStreamStateHash = 11067946060992835522ull;
439+
// The portable pin of that identity. Neither native_continuation_hash() nor
440+
// stream_state_hash() — which folds broker_state_hash(), and so the
441+
// continuation — can be pinned as a constant: both carry the machine's
442+
// resolved timezone resources (zoneinfo root and zone file paths), so the same
443+
// stream hashes differently here and on each CI runner.
444+
// native_run_spec_digest() is exactly the consumer's run-spec fold and nothing
445+
// else, so it is the same number everywhere. Observed on THIS tree for
446+
// base_spec("15", "15", "native-htf-stream-neutral"); it guards the fold's
447+
// field list and order, while the neutrality itself is the event sequence and
448+
// the in-process equalities below.
449+
constexpr std::uint64_t kNeutralStreamSpecDigest = 10367175860638888234ull;
443450

444451
void test_stream_without_subscriptions_is_unchanged() {
445452
scenario = "stream neutrality";
@@ -468,22 +475,43 @@ void test_stream_without_subscriptions_is_unchanged() {
468475
}
469476
check_logs_equal(host.log, want_log);
470477

471-
const std::uint64_t continuation = host.native_continuation_hash();
472-
if (continuation != kNeutralStreamContinuationHash) {
473-
std::printf(" continuation hash %llu, pinned %llu\n",
474-
static_cast<unsigned long long>(continuation),
475-
static_cast<unsigned long long>(kNeutralStreamContinuationHash));
478+
const std::uint64_t digest = native_run_spec_digest(spec);
479+
if (digest != kNeutralStreamSpecDigest) {
480+
std::printf(" spec digest %llu, pinned %llu\n",
481+
static_cast<unsigned long long>(digest),
482+
static_cast<unsigned long long>(kNeutralStreamSpecDigest));
476483
}
477-
CHECK(continuation == kNeutralStreamContinuationHash);
484+
CHECK(digest == kNeutralStreamSpecDigest);
485+
486+
// Stating the field at its default — an empty series list — folds nothing.
487+
NativeRunSpec stated = spec;
488+
stated.subscriptions.clear();
489+
CHECK(native_run_spec_digest(stated) == kNeutralStreamSpecDigest);
490+
491+
// The run's own identity is compared between two streams in this process,
492+
// never against a constant: the same stream under the spec with its empty
493+
// series list stated outright reaches the same event sequence, the same
494+
// continuation identity and the same stream state.
495+
const std::uint64_t continuation = host.native_continuation_hash();
478496
const std::uint64_t stream_state = host.stream_state_hash();
479-
if (stream_state != kNeutralStreamStateHash) {
480-
std::printf(" stream state hash %llu, pinned %llu\n",
481-
static_cast<unsigned long long>(stream_state),
482-
static_cast<unsigned long long>(kNeutralStreamStateHash));
497+
SeriesHost restated;
498+
CHECK(restated.configure_native(stated).status == NativeSetupStatus::Applied);
499+
CHECK(restated.stream_begin(bars.data(), 8, "15", "15"));
500+
for (int i = 8; i < 12; ++i) {
501+
CHECK(restated.stream_push_bar(bars[static_cast<std::size_t>(i)]));
483502
}
484-
CHECK(stream_state == kNeutralStreamStateHash);
485-
486-
// Declaring a series cannot land on the same continuation.
503+
CHECK(restated.stream_end(false));
504+
CHECK(restated.last_error().empty());
505+
CHECK(restated.bars_seen == 12);
506+
CHECK(restated.deliveries.empty());
507+
check_logs_equal(restated.log, want_log);
508+
CHECK(restated.native_continuation_hash() == continuation);
509+
CHECK(restated.stream_state_hash() == stream_state);
510+
511+
// Declaring a series cannot land on the same run-spec fold, nor on the
512+
// same continuation.
513+
CHECK(native_run_spec_digest(hourly_spec("native-htf-stream-neutral", false))
514+
!= kNeutralStreamSpecDigest);
487515
SeriesHost declared;
488516
if (begin_stream(declared, bars, 8, "native-htf-stream-neutral", false)) {
489517
for (int i = 8; i < 12; ++i) {

‎tests/test_native_margin_model.cpp‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* test_native_margin_model.cpp — R5 lane L4: the generic native margin model.
33
*
44
* Everything here is opt-in through NativeRunSpec::margin. The first scenario
5-
* pins that a spec WITHOUT a margin model still books the same fills and the
6-
* same continuation hash as the pre-L4 tree, so the adapter — which never sets
7-
* the field — is byte-identical by construction.
5+
* pins that a spec WITHOUT a margin model still books the same fills, the same
6+
* book and the same run-spec fold as the pre-L4 tree, so the adapter — which
7+
* never sets the field — is byte-identical by construction.
88
*
99
* Hand arithmetic used throughout (point_value = 1, account fx = 1, no fee):
1010
* equity(P) = capital + realized + dir * (P - entry) * units
@@ -16,6 +16,7 @@
1616
*/
1717
#include "native_current_fixture.hpp"
1818

19+
#include <cstdint>
1920
#include <cstdio>
2021
#include <string>
2122
#include <vector>
@@ -26,6 +27,21 @@ namespace {
2627

2728
constexpr const char* kLiquidationLabel = "__kernel_liquidation__";
2829

30+
// ── Portable spec-fold pins ─────────────────────────────────────────────
31+
// A raw native_continuation_hash() constant is NOT portable: the consumer
32+
// folds the run's resolved timezone identity — the zoneinfo root and the zone
33+
// file paths of the machine that ran it — so the same run hashes differently
34+
// here and on each CI runner. native_run_spec_digest() is exactly the
35+
// consumer's run-spec fold and nothing else, so it is the same number
36+
// everywhere. Both constants are observed on THIS tree for the margin-free
37+
// specs of scenarios 1+10 and guard the fold's field list and order; the
38+
// neutrality claim itself is the equalities beside them — `margin` folds
39+
// nothing while it is unset, and declaring a model (even one whose every field
40+
// is its default) is what moves the fold — plus the fills, the book and the
41+
// trade counts, which are unchanged from the pre-L4 tree.
42+
constexpr std::uint64_t kNeutralSpecDigest = 2166775980498865536ULL;
43+
constexpr std::uint64_t kNeutralRichSpecDigest = 17505314342075340318ULL;
44+
2945
Bar ohlc(int index, double open, double high, double low, double close,
3046
double volume = 1.0) {
3147
return {open, high, low, close, volume, T + static_cast<int64_t>(index) * 60000};
@@ -145,17 +161,46 @@ void neutral_without_margin() {
145161
CHECK(liquidations(host).empty());
146162
CHECK(host.margin_calls.empty());
147163
near(host.physical_position().signed_units, 20.0);
148-
// Pinned from the pre-L4 tree (b01ef03) by compiling the identical probe
149-
// against both libraries: 0xb8bcdfcd5f474996 on each.
150-
CHECK(host.native_continuation_hash() == 0xb8bcdfcd5f474996ULL);
164+
// The portable half of the pre-L4 neutrality: the run-spec fold this run
165+
// applies is pinned, stating `margin` as absent keeps it exactly there,
166+
// and declaring a model is what moves it.
167+
const auto digest = native_run_spec_digest(s);
168+
if (digest != kNeutralSpecDigest) {
169+
std::printf(" neutral spec digest %llu, pinned %llu\n",
170+
static_cast<unsigned long long>(digest),
171+
static_cast<unsigned long long>(kNeutralSpecDigest));
172+
}
173+
CHECK(digest == kNeutralSpecDigest);
174+
auto stated = s;
175+
stated.margin.reset();
176+
CHECK(native_run_spec_digest(stated) == kNeutralSpecDigest);
177+
// Presence is the opt-in: a model left at every default still moves the
178+
// fold, against the same spec without one (no initial_margin_fraction
179+
// here, which the model would conflict with).
180+
auto bare = margin_spec("l4-neutral");
181+
auto declared = bare;
182+
declared.margin = NativeMarginModel{};
183+
CHECK(native_run_spec_digest(declared) != native_run_spec_digest(bare));
184+
185+
// The run-level half, compared between two runs in this process rather
186+
// than against a constant: the same margin-free spec with `margin` spelled
187+
// out as absent books the same fill and reaches the same continuation.
188+
MarginHost restated;
189+
restated.open_units = 20.0;
190+
drive(restated, stated, twin_tape());
191+
CHECK(events<no::ExecutionAppliedEvent>(restated).size() == 1);
192+
CHECK(liquidations(restated).empty());
193+
near(restated.physical_position().signed_units, 20.0);
194+
CHECK(restated.native_continuation_hash() == host.native_continuation_hash());
151195
// No maintenance fraction anywhere: the accessor stays silent.
152196
CHECK(!host.native_liquidation_price().has_value());
153197
}
154198

155199
// A richer margin-free population — market, resting limit, resting stop,
156200
// cancel, pyramided add, flatten, slippage, percent fee, lot cap — so every
157201
// RequestOrigin::Host definition and every CommandEvent tag takes part in the
158-
// digest. Pinned from the same pre-L4 tree: 0xc600f565783631d0.
202+
// digest, which stays where the pre-L4 tree left it: authorship folds nothing
203+
// for a host request.
159204
struct RichHost final : Host {
160205
int bars = 0;
161206
std::optional<no::RequestHandle> resting;
@@ -202,7 +247,28 @@ void neutral_rich_population() {
202247
CHECK(host.last_error().empty());
203248
CHECK(host.trade_count() == 4);
204249
near(host.physical_position().signed_units, 0.0);
205-
CHECK(host.native_continuation_hash() == 0xc600f565783631d0ULL);
250+
251+
const auto digest = native_run_spec_digest(s);
252+
if (digest != kNeutralRichSpecDigest) {
253+
std::printf(" rich spec digest %llu, pinned %llu\n",
254+
static_cast<unsigned long long>(digest),
255+
static_cast<unsigned long long>(kNeutralRichSpecDigest));
256+
}
257+
CHECK(digest == kNeutralRichSpecDigest);
258+
auto stated = s;
259+
stated.margin.reset();
260+
CHECK(native_run_spec_digest(stated) == kNeutralRichSpecDigest);
261+
262+
// In process, not against a constant: the same population under the same
263+
// spec with `margin` stated as absent books the same trades, the same flat
264+
// book and the same continuation identity.
265+
RichHost restated;
266+
REQUIRE(restated.configure_native(stated).status == NativeSetupStatus::Applied);
267+
restated.run(bars.data(), static_cast<int>(bars.size()));
268+
CHECK(restated.last_error().empty());
269+
CHECK(restated.trade_count() == 4);
270+
near(restated.physical_position().signed_units, 0.0);
271+
CHECK(restated.native_continuation_hash() == host.native_continuation_hash());
206272
}
207273

208274
// ------------------------------------------------------------------- 2

0 commit comments

Comments
 (0)