Skip to content

Commit 770f7b2

Browse files
luisleo526claude
andcommitted
Adapter: quiet-bar gates on every per-bar policy hook, and a commandless receipt read only moves its cursor (R5 lane PERF-L4, step 2)
Every TradingView policy hook the adapter runs at a bar boundary -- the opening's release, ranking, admission, deferral, re-acceptance, two-stop arbitration, fill-recalculation tail, declined-reversal and cap, margin and intraday-loss checkpoints; the publication's bracket, close, entry and relative-exit drains; the close's fill passes, admissions, re-arms, terminal policy and short checkpoints -- is now first asked whether its body has anything to act on, and is not run when it has not (src/source/pine_quiet_bar.hpp, 29 gates). Each condition is the hook's own no-op precondition: no live row of the family it acts on, an empty queue, a flat book, a run configuration its body refuses before reading the book, no loss rule, no due cap close. The gate sits at the hook's per-bar call site, before the hook builds its frame, or at the top of a hook that is also reached elsewhere; on an unbound adapter no gate fires, so require_host() refuses exactly as before. observe_terminal_receipts, read three times a bar, returns early when no command event lies above its cursor, moving the cursor to the high water as the full read would. It leaves the hashed terminal watermark alone, as the read would: a terminal receipt is a command, every command's ordinal exceeds the high water of the read before it, so the first read after one takes the full path. No value moves and no layout changes. tests/test_adapter_quiet_bar.cpp runs a battery of 272 runs (21 fixture scenarios aimed at the gated hooks, including a margin-call regime the corpus lacks -- 62 margin-call rows -- and 48 seeded random strategies, each magnifier off/on and hash recording on/off): * test_adapter_quiet_bar: every run's source folds, recorded broker-state hashes, final hash and trade rows equal the values pinned on f71cd82; and a bar with nothing to act on allocates only what the kernel does (a held bracket and a flat stop pair allocated 268 and 488 per 44 quiet bars on f71cd82, against the kernel's 4) -- the fail-before; * test_adapter_quiet_bar_differential: the shipped library's transcript (continuation included) equals the ungated build's (PINEFORGE_PINE_QUIET_BAR_GATES=0), line for line; * test_adapter_quiet_bar_probe: a quiet bar asks 30 gates and runs no hook body, and every gate is taken both ways across the battery. Mutating any of four gates (the entry drain's reset, the margin block, the receipt cursor, the admission's side) fails both the pinned and the differential rows. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> (cherry picked from commit 0cc0905208988ede88bcf4a786629bf01072eee9)
1 parent b301cb3 commit 770f7b2

7 files changed

Lines changed: 1870 additions & 28 deletions

‎src/source/pine_adapter.cpp‎

Lines changed: 283 additions & 23 deletions
Large diffs are not rendered by default.

‎src/source/pine_quiet_bar.hpp‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
3+
// R5 lane PERF-L4: the Pine adapter's quiet-bar gates.
4+
//
5+
// Every TradingView policy hook the adapter runs at a bar boundary is first
6+
// asked whether its body has anything to act on -- a live handle of the
7+
// family it handles, a queued command, a held position, an armed rule or
8+
// configuration -- and is not run when it has not. The gate sits at the
9+
// hook's per-bar call site (on_bar_open, on_bar_close, the host's source
10+
// publication), or at the top of a hook that is also reached elsewhere. Each
11+
// precondition is the hook's own logic restated as the condition under which
12+
// its body changes nothing and calls no kernel mutator, so a skipped hook
13+
// leaves the adapter, the kernel and every hash exactly as the body would
14+
// have. A gate never fires on an unbound adapter: the hook then refuses
15+
// through require_host() exactly as it always did.
16+
//
17+
// Two test-only switches build the witness's references
18+
// (tests/test_adapter_quiet_bar.cpp); the shipped library sets neither:
19+
// PINEFORGE_PINE_QUIET_BAR_GATES=0 compiles every gate out: the ungated
20+
// reference the differential row runs;
21+
// PINEFORGE_PINE_QUIET_BAR_PROBE=1 counts every gate decision per hook:
22+
// the cost and coverage rows.
23+
// Not part of the installed API.
24+
25+
#include <cstddef>
26+
#include <cstdint>
27+
28+
#ifndef PINEFORGE_PINE_QUIET_BAR_GATES
29+
#define PINEFORGE_PINE_QUIET_BAR_GATES 1
30+
#endif
31+
#ifndef PINEFORGE_PINE_QUIET_BAR_PROBE
32+
#define PINEFORGE_PINE_QUIET_BAR_PROBE 0
33+
#endif
34+
35+
namespace pineforge::source::detail {
36+
37+
// One entry per gated hook, in the order a bar reaches them.
38+
enum class QuietHook : std::uint8_t {
39+
ReceiptRead, // observe_terminal_receipts: no command above the cursor
40+
DelayedRelease, // release_delayed_orders at the open: nothing delayed
41+
L4cPriority, // update_l4c_priority at the open: no pair to rank
42+
OpenMarketAdmission, // apply_open_market_admission: no entry of the prior bar
43+
OpenMarketableSells, // defer_open_marketable_sells: no marketable stop-entry pair
44+
GappedBracketReaccept, // reaccept_gapped_bracket_behind_same_id_add: no market add
45+
DualEntryPath, // on_bar_open's flat two-stop arbitration: no touched pair
46+
CoofTail, // flush_coof_tail at the open: nothing staged
47+
CoofDeclinedReversal, // suspend_coof_declined_reversal_at_open: no reversal entry
48+
DueCapClose, // execute_due_cap_close: no filled-orders cap close due
49+
SlippedPoocMoney, // submit_slipped_pooc_opening_money_call: out of scope
50+
TvMoneyLongMargin, // submit_tv_money_long_margin_call: out of scope
51+
TvMoneyTrailMargin, // schedule_tv_money_long_margin_before_trail: out of scope
52+
OpenMarginCheckpoints, // on_bar_open's open/path margin block: flat book
53+
DeclinedReversalAtOpen, // declined_reversal_at_open: no reversal entry
54+
RoundedPoocShortMargin, // defer_rounded_pooc_short_margin_until_close: out of scope
55+
IntradayLossClose, // submit_intraday_loss_close at the open: no loss rule
56+
IntradayLossPath, // schedule_intraday_loss_path at the open: no loss rule
57+
PreopenMarginSlice, // schedule_preopen_margin_slice: no default-percent opening
58+
BracketLegs, // flush_pending_bracket_legs at publication: nothing staged
59+
PendingCloses, // flush_pending_closes at publication: no close batch
60+
PendingEntries, // flush_pending_entries at publication: nothing queued
61+
RelativeExits, // anchor_relative_exits at publication: none queued
62+
PoocLimitEntryFills, // flush_pooc_marketable_limit_entry_fills: no entry of this bar
63+
PoocExitFills, // flush_pooc_marketable_exit_fills: no exit leg of this bar
64+
DeferredSellAdmission, // admit_deferred_open_marketable_sells: none deferred
65+
ThrottledReopens, // rearm_throttled_reopens at the close: none throttled
66+
TerminalExplicitMarket, // apply_terminal_explicit_market_policy: no entry pair
67+
CloseMarginCheckpoints, // on_bar_close's short checkpoints: out of scope
68+
Count,
69+
};
70+
71+
inline constexpr std::size_t kQuietHookCount = static_cast<std::size_t>(QuietHook::Count);
72+
73+
#if PINEFORGE_PINE_QUIET_BAR_PROBE
74+
// Per hook: how often its gate was asked, and how often the answer was quiet.
75+
struct QuietBarCounts {
76+
std::uint64_t asked[kQuietHookCount] = {};
77+
std::uint64_t quiet[kQuietHookCount] = {};
78+
};
79+
QuietBarCounts& quiet_bar_counts() noexcept;
80+
#endif
81+
82+
// True when the hook is skipped: `quiet` is the hook's exact no-op
83+
// precondition, evaluated where the hook would run.
84+
inline bool skip_quiet(QuietHook hook, bool quiet) noexcept {
85+
#if PINEFORGE_PINE_QUIET_BAR_PROBE
86+
auto& counts = quiet_bar_counts();
87+
++counts.asked[static_cast<std::size_t>(hook)];
88+
if (quiet) ++counts.quiet[static_cast<std::size_t>(hook)];
89+
#else
90+
(void)hook;
91+
#endif
92+
return PINEFORGE_PINE_QUIET_BAR_GATES != 0 && quiet;
93+
}
94+
95+
} // namespace pineforge::source::detail

‎src/source/pine_strategy_host.cpp‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <pineforge/compat/pine/trail_ticks.hpp>
77
#include "../timezone.hpp"
88
#include "../native_execution_consumer.hpp"
9+
#include "pine_quiet_bar.hpp"
910

1011
#include <algorithm>
1112
#include <cmath>
@@ -1822,8 +1823,14 @@ void source::PineStrategyHost::scheduler_publish_source_bar(
18221823
// evaluation. Draining it here keeps the body's position state, and the
18231824
// levels it re-prices, on the same side of the fill as the owner; a leg that
18241825
// carries a predecessor receipt stays staged for the flush below the body.
1825-
adapter_.flush_pending_bracket_legs({}, /*post_calculation=*/false,
1826-
/*pre_script_drain=*/true);
1826+
// Quiet-bar gates (pine_quiet_bar.hpp): a drain with nothing staged or
1827+
// queued is not called.
1828+
using detail::QuietHook;
1829+
using detail::skip_quiet;
1830+
if (!skip_quiet(QuietHook::BracketLegs, adapter_.pending_bracket_legs_.empty())) {
1831+
adapter_.flush_pending_bracket_legs({}, /*post_calculation=*/false,
1832+
/*pre_script_drain=*/true);
1833+
}
18271834
struct ChartEmaNaWarmupScope {
18281835
bool previous;
18291836
explicit ChartEmaNaWarmupScope(bool enabled)
@@ -1846,13 +1853,18 @@ void source::PineStrategyHost::scheduler_publish_source_bar(
18461853
bar_index_ = previous_bar_index;
18471854
barstate_islast_ = previous_barstate_islast;
18481855
}
1849-
adapter_.flush_pending_closes();
1856+
if (!skip_quiet(QuietHook::PendingCloses, adapter_.close_batch_callsites_.empty()))
1857+
adapter_.flush_pending_closes();
18501858
adapter_.flush_pending_entries();
1851-
adapter_.flush_pending_bracket_legs();
1859+
if (!skip_quiet(QuietHook::BracketLegs, adapter_.pending_bracket_legs_.empty()))
1860+
adapter_.flush_pending_bracket_legs();
18521861
// After every command of this evaluation is in the kernel: a queued
18531862
// relative strategy.exit whose parent entry is now a live request becomes
18541863
// that parent's anchored bracket child.
1855-
adapter_.anchor_relative_exits();
1864+
if (!skip_quiet(QuietHook::RelativeExits, adapter_.anchored_relative_legs_.empty()
1865+
&& adapter_.pending_relative_exits_.empty())) {
1866+
adapter_.anchor_relative_exits();
1867+
}
18561868
if (advance_source_index) {
18571869
scheduler_mark_report_point(bar.timestamp);
18581870
prev_bar_timestamp_ = bar.timestamp;

‎tests/CMakeLists.txt‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,11 @@ list(APPEND TEST_SOURCES test_native_command_history_read)
902902
# on fc7aad62 (receipt cursors, recorded broker-state hashes, trades), and a
903903
# commandless read that allocates nothing. Source-bound.
904904
list(APPEND TEST_SOURCES test_adapter_receipts_in_place)
905+
# R5 PERF-L4: the Pine adapter's quiet-bar gates move no value -- the whole
906+
# battery's observations pinned on f71cd820 -- and a bar with nothing to act
907+
# on allocates nothing in the adapter's hooks. Source-bound. Its ungated and
908+
# probed twins are built from the same TU below the target loop.
909+
list(APPEND TEST_SOURCES test_adapter_quiet_bar)
905910
# R5 PERF-K3: the matcher reuses its candidate rows after an allowance
906911
# refresh. The cost stays linear in the live requests (scaling), the reuse is
907912
# the full rescan bit for bit over randomized books (differential), and the
@@ -1414,6 +1419,53 @@ foreach(test_name ${TEST_SOURCES})
14141419
endif()
14151420
endforeach()
14161421

1422+
# R5 PERF-L4: the witness's two references. The source layer is compiled
1423+
# twice more, with the kernel archive below it: once with every quiet-bar gate
1424+
# compiled out (PINEFORGE_PINE_QUIET_BAR_GATES=0), once gated; both carry the
1425+
# gate probe (src/source/pine_quiet_bar.hpp). The differential row requires
1426+
# the shipped library's transcript to be the ungated one, line for line; the
1427+
# probe row requires a quiet bar to run no hook body and every gate to be
1428+
# taken both ways across the battery. Neither archive is installed.
1429+
if(PINEFORGE_BUILD_SOURCE_LAYER AND TARGET test_adapter_quiet_bar)
1430+
get_target_property(_pf_quiet_library_options pineforge COMPILE_OPTIONS)
1431+
set(_pf_quiet_sources)
1432+
foreach(_pf_quiet_source IN LISTS PINEFORGE_SOURCE_LAYER_SOURCES)
1433+
list(APPEND _pf_quiet_sources ${PROJECT_SOURCE_DIR}/${_pf_quiet_source})
1434+
endforeach()
1435+
foreach(_pf_quiet_variant ungated probe)
1436+
set(_pf_quiet_gates 1)
1437+
if(_pf_quiet_variant STREQUAL "ungated")
1438+
set(_pf_quiet_gates 0)
1439+
endif()
1440+
set(_pf_quiet_library pineforge_source_quiet_${_pf_quiet_variant})
1441+
add_library(${_pf_quiet_library} STATIC ${_pf_quiet_sources})
1442+
target_compile_options(${_pf_quiet_library} PRIVATE ${_pf_quiet_library_options})
1443+
target_include_directories(${_pf_quiet_library} PRIVATE ${PROJECT_SOURCE_DIR}/src)
1444+
target_compile_definitions(${_pf_quiet_library} PRIVATE
1445+
PINEFORGE_PINE_QUIET_BAR_GATES=${_pf_quiet_gates}
1446+
PINEFORGE_PINE_QUIET_BAR_PROBE=1)
1447+
target_link_libraries(${_pf_quiet_library} PUBLIC pineforge_kernel)
1448+
set(_pf_quiet_test test_adapter_quiet_bar_${_pf_quiet_variant})
1449+
add_executable(${_pf_quiet_test} test_adapter_quiet_bar.cpp)
1450+
target_link_libraries(${_pf_quiet_test} PRIVATE ${_pf_quiet_library})
1451+
target_compile_definitions(${_pf_quiet_test} PRIVATE
1452+
PINEFORGE_QUIET_BAR_WITNESS_PROBE
1453+
PINEFORGE_PINE_QUIET_BAR_GATES=${_pf_quiet_gates}
1454+
PINEFORGE_PINE_QUIET_BAR_PROBE=1)
1455+
target_compile_options(${_pf_quiet_test} PRIVATE -ffp-contract=off -UNDEBUG)
1456+
if(PINEFORGE_ENABLE_COVERAGE)
1457+
target_compile_options(${_pf_quiet_test} PRIVATE ${_pf_cov_test_flags})
1458+
endif()
1459+
endforeach()
1460+
add_test(NAME test_adapter_quiet_bar_probe COMMAND test_adapter_quiet_bar_probe)
1461+
add_test(NAME test_adapter_quiet_bar_differential
1462+
COMMAND ${CMAKE_COMMAND}
1463+
-DGATED=$<TARGET_FILE:test_adapter_quiet_bar>
1464+
-DUNGATED=$<TARGET_FILE:test_adapter_quiet_bar_ungated>
1465+
-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/quiet_bar_differential
1466+
-P ${CMAKE_CURRENT_SOURCE_DIR}/quiet_bar_differential.cmake)
1467+
endif()
1468+
14171469
pineforge_add_twin_test(test_l8c_short_seed_p0_10 test_l8c_short_seed_guards p0-10)
14181470
pineforge_add_twin_test(test_l8c_short_seed_p1_16 test_l8c_short_seed_guards p1-16)
14191471
pineforge_add_twin_test(test_l8c_short_seed_p1_18 test_l8c_short_seed_guards p1-18)

‎tests/quiet_bar_differential.cmake‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# R5 lane PERF-L4: the differential row of the Pine adapter's quiet-bar gates.
2+
#
3+
# GATED is test_adapter_quiet_bar on the shipped library, UNGATED the same TU on
4+
# the source layer compiled without a gate. Each prints its battery bar by bar
5+
# (`transcript`): every source-layer fold, recorded broker-state hash row,
6+
# final hash, continuation and trade row of every run. The two must be the
7+
# same, line for line.
8+
foreach(_required GATED UNGATED WORK_DIR)
9+
if(NOT DEFINED ${_required})
10+
message(FATAL_ERROR "quiet_bar_differential.cmake: ${_required} is required")
11+
endif()
12+
endforeach()
13+
file(MAKE_DIRECTORY "${WORK_DIR}")
14+
set(_gated "${WORK_DIR}/gated.txt")
15+
set(_ungated "${WORK_DIR}/ungated.txt")
16+
execute_process(COMMAND "${GATED}" transcript OUTPUT_FILE "${_gated}"
17+
RESULT_VARIABLE _gated_result)
18+
execute_process(COMMAND "${UNGATED}" transcript OUTPUT_FILE "${_ungated}"
19+
RESULT_VARIABLE _ungated_result)
20+
if(NOT _gated_result EQUAL 0 OR NOT _ungated_result EQUAL 0)
21+
message(FATAL_ERROR "quiet_bar_differential: transcript runs failed "
22+
"(gated ${_gated_result}, ungated ${_ungated_result})")
23+
endif()
24+
file(STRINGS "${_gated}" _gated_lines)
25+
file(STRINGS "${_ungated}" _ungated_lines)
26+
list(LENGTH _gated_lines _gated_count)
27+
list(LENGTH _ungated_lines _ungated_count)
28+
if(_gated_count EQUAL 0)
29+
message(FATAL_ERROR "quiet_bar_differential: the gated transcript is empty")
30+
endif()
31+
execute_process(COMMAND "${CMAKE_COMMAND}" -E compare_files "${_gated}" "${_ungated}"
32+
RESULT_VARIABLE _differ)
33+
if(_differ)
34+
set(_line 0)
35+
foreach(_gated_line IN LISTS _gated_lines)
36+
if(_line GREATER_EQUAL _ungated_count)
37+
break()
38+
endif()
39+
list(GET _ungated_lines ${_line} _ungated_line)
40+
if(NOT _gated_line STREQUAL _ungated_line)
41+
break()
42+
endif()
43+
math(EXPR _line "${_line} + 1")
44+
endforeach()
45+
math(EXPR _shown "${_line} + 1")
46+
message(FATAL_ERROR "quiet_bar_differential: the transcripts differ at line ${_shown} "
47+
"(gated ${_gated_count} lines, ungated ${_ungated_count}); "
48+
"see ${_gated} and ${_ungated}")
49+
endif()
50+
list(FILTER _gated_lines INCLUDE REGEX "^run ")
51+
list(LENGTH _gated_lines _runs)
52+
message("test_adapter_quiet_bar_differential: ${_runs} runs, ${_gated_count} lines, "
53+
"gated and ungated identical")

0 commit comments

Comments
 (0)