Skip to content

Commit 04330d4

Browse files
luisleo526claude
andcommitted
Adapter re-lowering: the kernel owns equity recording; the Pine host only selects the policy and marks its cadence (R5 lane R1)
NativeReportPolicy gains KernelRecordedAtHostMarks: the kernel performs the recording (update_equity_extremes + record_equity_point, the L2 code path) when the host marks a report point through the consumer's mark_script_report_point(engine, ts). The Pine host selects that policy in project() and marks its two cadence points (after the pending flushes on a published bar, and on a suppressed probe tail) instead of calling the engine's protected recording members itself; src/source/** no longer references update_equity_extremes / record_equity_point. The adapter's cadence (per published source slot, inside the callback, before the after-close fills), its bar.timestamp label and its own range-end producer and per-bar broker hash stay as they are — the kernel's KernelRecorded cadence cannot reproduce them, so the recording moved and the cadence did not. KernelRecordedAtHostMarks folds nothing into the continuation hash so every adapter per-bar hash is unchanged. Byte-identity: 312 corpus engine_trades.csv identical to main 06ef483 with the regenerated corpus, validation report identical, 24 cases' full equity curves (5,138,348 points, 105 metric fields) identical. tests/test_adapter_report_relower.cpp (421 checks: ordinary, COOF and suppressed-tail cadences against values harvested from the 06ef483 library). ci_verify release PASS (501). Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01CSmYixVAp4NqWpBZxkrLPT
1 parent b0cec54 commit 04330d4

10 files changed

Lines changed: 630 additions & 18 deletions

‎docs/pages/native-engine.md‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ Always set, with documented defaults in the header:
136136
Cash kinds are account currency per unit or per execution.
137137
- `close_execution`: `NextEligiblePoint` (default) or `AfterCalculation`
138138
- `allowed_open_directions`: `None`, `Long`, `Short`, `Both` (default)
139-
- `report_policy`: `HostRecorded` (default) or `KernelRecorded`;
140-
`report_open_position_at_end`: `false` (default). See *Reporting for native
141-
hosts* below.
139+
- `report_policy`: `HostRecorded` (default), `KernelRecorded` or
140+
`KernelRecordedAtHostMarks`; `report_open_position_at_end`: `false`
141+
(default). See *Reporting for native hosts* below.
142142
- `calculation`: `BarClose` (default), `BarCloseAndFills` or
143143
`EveryModeledPoint`; `max_recalculations_per_point`: `8` (default, any
144144
value including 0 is legal); `open_bar_view`: `Complete` (default) or
@@ -731,6 +731,21 @@ curve is identical with and without an intrabar path. The result is one point
731731
per script bar, a finite drawdown/run-up walk, and metrics computed over a
732732
real series.
733733

734+
`NativeReportPolicy::KernelRecordedAtHostMarks` records the very same series
735+
at the points the host marks, for a host whose report cadence is not one point
736+
per calculation. The consumer never records on its own initiative under it:
737+
the host calls the kernel's report mark from inside its own callback, naming
738+
the label the point carries, and the kernel performs the extremes fold and the
739+
curve append. The Pine adapter is that host — its report has one point per
740+
published *source* slot, which is not the same series of instants: a
741+
`calc_on_order_fills` re-entry marks the slot it opened at the fill and the
742+
bar's ordinary close calculation then marks nothing, and a suppressed probe
743+
tail advances source history, and marks, without running generated code at
744+
all. The point also has to land inside the callback, before the adapter takes
745+
that bar's broker-state hash and before its range-end rows re-mark the curve's
746+
last point. Under this policy the kernel owns what a report point is and the
747+
host owns only when.
748+
734749
`report_open_position_at_end` (`KernelRecorded` only) adds the rows a close of
735750
the still-open position at the last bar's close would record — one per
736751
physical lot, through the same row builder every full close uses, with
@@ -742,11 +757,13 @@ exactly as the run left them; the rows appear in `fill_report` and in
742757
`report_trade_count()` / `get_report_trade()`, never in `closed_trade_count()`
743758
/ `closed_trade()`.
744759

745-
Both fields are opt-in and fold into the continuation hash only once
746-
`report_policy` is non-default, so a spec that does not ask for kernel
747-
recording keeps the continuation identity it had before these fields existed.
748-
Recording does move the broker-state hash, because the equity extremes it
749-
folds are durable engine state.
760+
Both fields are opt-in and fold into the continuation hash only under
761+
`KernelRecorded` — the one policy under which the consumer decides, on its
762+
own, to act between two points of a run. `HostRecorded` and
763+
`KernelRecordedAtHostMarks` fold nothing, so a spec that does not hand the
764+
kernel its cadence keeps the continuation identity it had before these fields
765+
existed. Recording does move the broker-state hash, because the equity
766+
extremes it folds are durable engine state.
750767

751768
Per-trade reads: `closed_trade_count()` / `closed_trade(i)` return the closed
752769
rows this run booked; `report_trade_count()` / `get_report_trade(i)` span those

‎include/pineforge/native_run_spec.hpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ enum class NativeAbortReporting : std::uint32_t {
4242
// calculation, so a bare host gets a truthful curve, finite drawdown/run-up
4343
// metrics and a report whose walk is not degenerate. Recording is reporting:
4444
// it books no cash and places no order.
45+
// KernelRecordedAtHostMarks records the very same series, at the points the
46+
// host marks: a host whose report cadence is not one point per calculation —
47+
// a source adapter that re-enters its script on a fill, or publishes a bar
48+
// its script never calculates — keeps that cadence and still stops owning
49+
// what a report point is. The consumer never records on its own initiative
50+
// under it, so it leaves the continuation identity exactly where
51+
// HostRecorded leaves it (see hash_spec in native_execution_consumer.cpp).
4552
enum class NativeReportPolicy : std::uint32_t {
4653
HostRecorded = 0,
4754
KernelRecorded = 1,
55+
KernelRecordedAtHostMarks = 2,
4856
};
4957

5058
enum class NativeOpenDirections : std::uint32_t {

‎include/pineforge/source/pine_strategy_host.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ class PineStrategyHost : public NativeStrategyHost, public BrokerStateHashProvid
334334
void scheduler_feed_deferred_aux_security(int chart_index);
335335
void scheduler_finish_security_sequence();
336336
void scheduler_record_range_end(const Bar&);
337+
// One report point per published source slot. The kernel records it
338+
// (NativeReportPolicy::KernelRecordedAtHostMarks); this host owns only
339+
// the Pine cadence that says where the points fall.
340+
void scheduler_mark_report_point(std::int64_t script_bar_ts);
337341
void scheduler_record_broker_hash();
338342
void capture_script_continuation_hash();
339343
void scheduler_update_session_state(

‎src/native_execution_consumer.cpp‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,14 @@ void hash_spec(Fnv& f, const NativeRunSpec& spec) noexcept {
124124
f.b(spec.initial_margin_fraction.has_value());
125125
if (spec.initial_margin_fraction) f.d(*spec.initial_margin_fraction);
126126
f.u(native_intrabar_path_digest(spec.intrabar));
127-
// Report recording is opt-in, so it folds only when it is on: a spec that
128-
// leaves the kernel out of its report keeps the continuation identity it
129-
// had before the policy existed (same conditional shape as the precommit
130-
// digest below).
131-
if (spec.report_policy != NativeReportPolicy::HostRecorded) {
127+
// Report recording folds only when the consumer records of its own
128+
// initiative: a spec that leaves the kernel out of its report keeps the
129+
// continuation identity it had before the policy existed (same
130+
// conditional shape as the precommit digest below). A host-marked report
131+
// (KernelRecordedAtHostMarks) is the same case: the consumer decides
132+
// nothing and does nothing between two points of the run, so it folds
133+
// nothing, exactly as HostRecorded folds nothing.
134+
if (spec.report_policy == NativeReportPolicy::KernelRecorded) {
132135
f.u(static_cast<uint64_t>(spec.report_policy));
133136
f.b(spec.report_open_position_at_end);
134137
}
@@ -5806,8 +5809,29 @@ void NativeExecutionConsumer::record_script_report_point(
58065809
BacktestEngine& engine, int64_t script_open_ms) const {
58075810
const auto* spec = spec_ptr();
58085811
if (!spec || spec->report_policy != NativeReportPolicy::KernelRecorded) return;
5812+
record_report_point(engine, script_open_ms);
5813+
}
5814+
5815+
// The same fold and the same append, at an instant only the host can name
5816+
// (NativeReportPolicy::KernelRecordedAtHostMarks). A host whose report
5817+
// cadence is its own — a source adapter that publishes a script bar to its
5818+
// generated code, re-enters that script on a fill, or advances its source
5819+
// history over a bar the script never calculates — marks the point inside
5820+
// its own callback, where its broker-state hash and its report rows already
5821+
// read the curve. Recording is still the kernel's: the host names when, not
5822+
// what. Inert under every other policy, so a host that records its own
5823+
// report, or one that asked for the per-calculation cadence, is unaffected.
5824+
void NativeExecutionConsumer::mark_script_report_point(
5825+
BacktestEngine& engine, int64_t script_bar_ts) const {
5826+
const auto* spec = spec_ptr();
5827+
if (!spec || spec->report_policy != NativeReportPolicy::KernelRecordedAtHostMarks) return;
5828+
record_report_point(engine, script_bar_ts);
5829+
}
5830+
5831+
void NativeExecutionConsumer::record_report_point(
5832+
BacktestEngine& engine, int64_t report_ts) const {
58095833
engine.update_equity_extremes();
5810-
engine.record_equity_point(script_open_ms);
5834+
engine.record_equity_point(report_ts);
58115835
}
58125836

58135837
// A position still open when the feed ends is reported as the rows a close at

‎src/native_execution_consumer.hpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class NativeExecutionConsumer final : public IExecutionConsumer {
9696
}
9797
uint64_t high_water() const noexcept { return consumed_high_water_; }
9898
void reject_inherited_on_bar(BacktestEngine& engine);
99+
// Report truth at the host's own cadence
100+
// (NativeReportPolicy::KernelRecordedAtHostMarks): the host marks the
101+
// script bar it has just published and the kernel records the point.
102+
// Reporting only, and inert under every other policy.
103+
void mark_script_report_point(BacktestEngine& engine, int64_t script_bar_ts) const;
99104
std::optional<Bar> series_bar(std::size_t subscription) const;
100105
// L5 calculation timing readbacks. The partial bar is the lookahead-free
101106
// bar so far at the current cursor; the two counters are observation of
@@ -335,6 +340,7 @@ class NativeExecutionConsumer final : public IExecutionConsumer {
335340
// are reporting-only: they mark equity and synthesize report rows, and
336341
// never book cash, place an order or move the broker book.
337342
void record_script_report_point(BacktestEngine& engine, int64_t script_open_ms) const;
343+
void record_report_point(BacktestEngine& engine, int64_t report_ts) const;
338344
void record_open_position_report_rows(BacktestEngine& engine) const;
339345
void match_point(BacktestEngine& engine, const NativeDriverPoint& point);
340346
void match_discrete(BacktestEngine& engine, const NativeDriverPoint& point);

‎src/native_run_spec.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ bool valid_report_policy(NativeReportPolicy policy) noexcept {
9494
switch (policy) {
9595
case NativeReportPolicy::HostRecorded:
9696
case NativeReportPolicy::KernelRecorded:
97+
case NativeReportPolicy::KernelRecordedAtHostMarks:
9798
return true;
9899
}
99100
return false;

‎src/source/pine_adapter.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,14 @@ NativeRunSpec PineExecutionAdapter::project(const PineStrategyConfig& config,
14651465
// Pine's request_abort surface reports a cooperative cancellation through
14661466
// status, not through last_error(). Native-only hosts retain Error.
14671467
spec.abort_reporting = NativeAbortReporting::Quiet;
1468+
// RP3/RP9 (L2): the kernel records the equity curve and its extremes; the
1469+
// source host keeps only the Pine cadence that marks where the points
1470+
// fall (pine_strategy_host.cpp scheduler_mark_report_point). TradingView's
1471+
// range-end report — which re-marks the curve's last point and re-folds
1472+
// every extreme from it (pine_strategy_host.cpp scheduler_record_range_end)
1473+
// — is report shape, not a mark-to-market row, so the kernel's own
1474+
// range-end producer stays off and report_open_position_at_end with it.
1475+
spec.report_policy = NativeReportPolicy::KernelRecordedAtHostMarks;
14681476
// Contract P6: Pine pyramiding is adapter command policy. A resting source
14691477
// entry must not consume a generic physical-lot cap before it fills, so
14701478
// the projected native spec deliberately leaves max_open_lots unbounded.

‎src/source/pine_strategy_host.cpp‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,7 @@ void source::PineStrategyHost::scheduler_publish_source_bar(
15791579
adapter_.flush_pending_entries();
15801580
adapter_.flush_pending_bracket_legs();
15811581
if (advance_source_index) {
1582-
update_equity_extremes();
1583-
record_equity_point(bar.timestamp);
1582+
scheduler_mark_report_point(bar.timestamp);
15841583
prev_bar_timestamp_ = bar.timestamp;
15851584
}
15861585
}
@@ -1597,11 +1596,25 @@ void source::PineStrategyHost::scheduler_publish_suppressed_tail(const Bar& bar)
15971596
chart_day_partition_.empty() ? nullptr : &chart_day_partition_);
15981597
adapter_.begin_source_evaluation();
15991598
adapter_.observe_terminal_receipts();
1600-
update_equity_extremes();
1601-
record_equity_point(bar.timestamp);
1599+
scheduler_mark_report_point(bar.timestamp);
16021600
prev_bar_timestamp_ = bar.timestamp;
16031601
}
16041602

1603+
// The Pine report series has one point per SOURCE slot this host published,
1604+
// which is not the kernel's per-calculation cadence: a calc_on_order_fills
1605+
// re-entry marks the slot it opened at the fill, the ordinary close
1606+
// calculation then marks nothing, and the probe's suppressed tail marks a
1607+
// slot generated code never calculated. The point also has to land inside
1608+
// this callback, before scheduler_record_broker_hash() folds the extremes it
1609+
// just moved and before scheduler_record_range_end() re-marks the curve's
1610+
// last point. So the cadence stays here and the recording does not: the
1611+
// kernel owns what a report point is — the extremes fold and the curve
1612+
// append in engine.hpp — reached through the run spec's report policy.
1613+
void source::PineStrategyHost::scheduler_mark_report_point(std::int64_t script_bar_ts) {
1614+
as_native_consumer(execution_consumer())
1615+
.mark_script_report_point(*this, script_bar_ts);
1616+
}
1617+
16051618
void source::PineStrategyHost::scheduler_record_broker_hash() {
16061619
if (!broker_state_hash_recording_) return;
16071620
last_script_continuation_hash_ = execution_consumer().continuation_hash();

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ set(TEST_SOURCES
7676
test_native_execution_terms
7777
test_native_report_truth
7878
test_native_margin_model
79+
test_adapter_report_relower
7980
test_native_queued_reverse_to
8081
test_native_precommit_view
8182
test_native_fx_activation

0 commit comments

Comments
 (0)