Skip to content

Commit 425ff1d

Browse files
luisleo526claude
andcommitted
Merge the admission journal fold and exit scratch batches into the train
Two conflicts. The run-failure throw allowlist gains both sides' rows (the series window's rethrows and the exit scratch batch builder's two), each row unchanged. The native-feature-parity page was edited on both sides only in its line citations; the train's text is kept, and the citations into the adapter source that sit after the two lines this change inserts (one near line 25,856 and one near 26,372) are re-read from the merged source by the documentation anchor check that follows. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2 parents 1d4f883 + e466cc0 commit 425ff1d

18 files changed

Lines changed: 1564 additions & 49 deletions

‎ci/cr150-source-sites.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8424,7 +8424,7 @@
84248424
"scope": [
84258425
"namespace pineforge :: source #1",
84268426
"namespace detail #1",
8427-
"template < class T > class ExitScratchRows #1"
8427+
"template < class T , ExitScratchRowMode Mode = ExitScratchRowMode :: per_row > class ExitScratchRows #1"
84288428
],
84298429
"cls": "temp",
84308430
"spelling": "const_iterator",
@@ -8436,7 +8436,7 @@
84368436
"scope": [
84378437
"namespace pineforge :: source #1",
84388438
"namespace detail #1",
8439-
"template < class T > class ExitScratchRows #1"
8439+
"template < class T , ExitScratchRowMode Mode = ExitScratchRowMode :: per_row > class ExitScratchRows #1"
84408440
],
84418441
"cls": "decl",
84428442
"spelling": "const_iterator",
@@ -8448,7 +8448,7 @@
84488448
"scope": [
84498449
"namespace pineforge :: source #1",
84508450
"namespace detail #1",
8451-
"template < class T > class ExitScratchRows #1"
8451+
"template < class T , ExitScratchRowMode Mode = ExitScratchRowMode :: per_row > class ExitScratchRows #1"
84528452
],
84538453
"cls": "decl",
84548454
"spelling": "const_iterator",

‎include/pineforge/detail/journal_codec.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct WorkObserver {
1818

1919
// Borrowed registration on the calling thread. A nonnull observer must have a
2020
// valid callback and outlive its registration; restore the returned pointer.
21+
// payload_source/read cover stored and admission-encoded sources alike.
2122
// payload_read reports requested bytes; the other operations report zero.
2223
inline thread_local const WorkObserver* work_observer = nullptr;
2324
inline const WorkObserver* set_work_observer(const WorkObserver* observer) noexcept {

‎include/pineforge/detail/scratch_backing.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ struct ScratchIoCharge {
6969
ScratchStatus status;
7070
};
7171

72+
// A passive accounting sink. Its callback must not call back into the engine,
73+
// a store or a consumer.
7274
struct ScratchIoObserver {
7375
void* context = nullptr;
7476
void (*charge)(void*, const ScratchIoCharge&) noexcept = nullptr;
@@ -95,6 +97,8 @@ class ScratchBacking {
9597

9698
ScratchBackingKind kind() const noexcept { return kind_; }
9799
std::size_t page_size() const noexcept { return page_size_; }
100+
// True when this backing carries a registered passive accounting sink.
101+
bool has_io_observer() const noexcept { return observer_.charge != nullptr; }
98102
virtual std::uint64_t page_count() const noexcept = 0;
99103
// Construction-time arena region bound, including a zero-byte share.
100104
// No value for disk/test backings; this policy is not logical store state.

‎include/pineforge/detail/scratch_store.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class CommandScratchStore final : public ScratchReadback {
103103
// True from a successful begin until a successful commit or rollback,
104104
// including while the transaction is rollback-only.
105105
bool transaction_active() const noexcept;
106+
// True when the borrowed backing has a passive logical-I/O observer.
107+
bool has_io_observer() const noexcept;
106108

107109
// begin snapshots logical state including handle allocation. Nested begin
108110
// is programming_error. Reads thereafter include successful pending edits.

‎include/pineforge/detail/scratch_store_impl.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,10 @@ inline std::uint64_t CommandScratchStore::record_count() const noexcept {
18091809
inline bool CommandScratchStore::transaction_active() const noexcept {
18101810
return state_ != nullptr && state_->open_status_ == ScratchStatus::ok && state_->active_;
18111811
}
1812+
inline bool CommandScratchStore::has_io_observer() const noexcept {
1813+
return state_ != nullptr && state_->backing_ != nullptr
1814+
&& state_->backing_->has_io_observer();
1815+
}
18121816
inline ScratchStatus CommandScratchStore::begin() noexcept {
18131817
return state_ == nullptr ? ScratchStatus::programming_error : state_->begin();
18141818
}

‎include/pineforge/source/exit_scratch.hpp‎

Lines changed: 134 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ using pineforge::detail::ScratchSpanSource;
3535
using pineforge::detail::ScratchStatus;
3636
using pineforge::detail::CommandScratchStore;
3737

38-
// At most this many scope rows per teardown transaction. The final
39-
// partial batch commits once; scope population never sizes resident storage.
38+
// At most this many selected rows per construction transaction, or stored
39+
// records per teardown transaction. Each final partial batch commits once;
40+
// scope population never sizes resident storage.
4041
inline constexpr std::size_t kExitScopeEraseBatch = 64;
4142

4243
// Terminal diagnostic only, like CommandScratchStore::carried_failure().
@@ -353,23 +354,25 @@ inline ScratchHandle exit_scratch_seek(ExitScratchContext& context,
353354
exit_scratch_check(status, context.store);
354355
return handle;
355356
}
357+
inline void exit_scratch_edit_failure(ExitScratchContext& context,
358+
ScratchStatus status) {
359+
// Rollback makes further backing calls. Preserve the first failure before
360+
// cleanup can replace the backing's borrowed diagnostic.
361+
std::optional<pineforge::detail::ReportScratchIoError> failure;
362+
if (status == ScratchStatus::io_error)
363+
if (const auto* carried = context.store.carried_failure()) failure = *carried;
364+
const auto rollback_status = context.store.rollback();
365+
pineforge::detail::rethrow_carried_scratch_failure(failure ? &*failure : nullptr);
366+
exit_scratch_check(rollback_status, context.store);
367+
exit_scratch_failure(status);
368+
}
356369
template<class Edit> void exit_scratch_edit(ExitScratchContext& context, Edit operation) {
357370
exit_scratch_check_teardown(context);
358371
exit_scratch_check(context.store.begin(), context.store);
359372
auto status = operation();
360373
if (status == ScratchStatus::ok)
361374
status = context.store.commit(ScratchDurability::buffered);
362-
if (status != ScratchStatus::ok) {
363-
// Rollback makes further backing calls. Preserve the first failure
364-
// before cleanup can replace the backing's borrowed diagnostic.
365-
std::optional<pineforge::detail::ReportScratchIoError> failure;
366-
if (status == ScratchStatus::io_error)
367-
if (const auto* carried = context.store.carried_failure()) failure = *carried;
368-
const auto rollback_status = context.store.rollback();
369-
pineforge::detail::rethrow_carried_scratch_failure(failure ? &*failure : nullptr);
370-
exit_scratch_check(rollback_status, context.store);
371-
exit_scratch_failure(status);
372-
}
375+
if (status != ScratchStatus::ok) exit_scratch_edit_failure(context, status);
373376
}
374377

375378
// Teardown uses only status-returning store operations. Snapshot the original
@@ -578,41 +581,96 @@ template<> struct ExitScratchValue<native_order::RequestHandle> {
578581
}
579582
};
580583

581-
// No store visitor is active while a caller cancels, replaces, executes or
582-
// creates another scope. Each edit commits separately, so nested Applied
583-
// callbacks require neither nested transactions nor owning snapshots.
584-
template<class T> class ExitScratchRows {
584+
// No store visitor survives a call. Per-row mode commits each edit separately;
585+
// bounded mode can keep a transaction open across appends until finish().
586+
// Finish construction before a caller cancels, replaces, executes or creates
587+
// another scope, so nested Applied callbacks need no nested transactions or
588+
// owning snapshots. Observed construction retains per-row commits.
589+
enum class ExitScratchRowMode : std::uint8_t {
590+
per_row,
591+
bounded_batch,
592+
};
593+
594+
template<class T, ExitScratchRowMode Mode = ExitScratchRowMode::per_row>
595+
class ExitScratchRows {
585596
public:
586597
explicit ExitScratchRows(const PineExecutionAdapter& owner)
587598
: ExitScratchRows(exit_scratch(owner)) {}
588599
explicit ExitScratchRows(ExitScratchContext& context) : scope_(context) {}
589600
ExitScratchRows(const ExitScratchRows&) = delete;
590601
ExitScratchRows& operator=(const ExitScratchRows&) = delete;
591602
ExitScratchRows(ExitScratchRows&& other) noexcept
592-
: scope_(std::move(other.scope_)), count_(other.count_) {}
603+
: scope_(std::move(other.scope_)), count_(other.count_),
604+
batch_open_(std::exchange(other.batch_open_, false)) {}
605+
~ExitScratchRows() noexcept(false) { rollback_batch_noexcept(); }
593606
std::uint64_t scope() const noexcept { return scope_.scope(); }
594607
ExitScratchContext& context() const noexcept { return scope_.context(); }
595608
std::size_t size() const noexcept { return static_cast<std::size_t>(count_); }
596609
bool empty() const noexcept { return count_ == 0; }
597610

598611
void push_back(const T& value) {
599612
if constexpr (std::is_same_v<T, native_order::RequestHandle>) {
600-
if (value.run != context().run)
613+
if (value.run != context().run) {
614+
if constexpr (Mode == ExitScratchRowMode::bounded_batch)
615+
if (batch_open_ && context().store.transaction_active())
616+
fail_batch(ScratchStatus::programming_error);
601617
exit_scratch_failure(ScratchStatus::programming_error);
618+
}
619+
}
620+
if constexpr (Mode == ExitScratchRowMode::bounded_batch) {
621+
if (batch_enabled()) {
622+
if (count_ % kExitScopeEraseBatch == 0
623+
|| !context().store.transaction_active())
624+
begin_batch();
625+
put_batch(count_, value);
626+
++count_;
627+
if (count_ % kExitScopeEraseBatch == 0) finish();
628+
} else {
629+
finish();
630+
put(count_, value);
631+
++count_;
632+
}
633+
} else {
634+
put(count_, value);
635+
++count_;
602636
}
603-
put(count_, value);
604-
++count_;
605637
observe();
606638
}
639+
// Publish this builder's open partial batch before consumers can mutate
640+
// scratch or create another scope. Per-row/finished builders are no-ops.
641+
// After an append/finish failure, abandon the builder: a rolled-back batch
642+
// can include earlier appends. Destruction rolls back any unfinished batch
643+
// before scope erase; it never implicitly publishes construction.
644+
void finish() {
645+
if constexpr (Mode == ExitScratchRowMode::bounded_batch) {
646+
if (!batch_open_) return;
647+
if (!context().store.transaction_active()) {
648+
batch_open_ = false;
649+
return;
650+
}
651+
try {
652+
exit_scratch_check_teardown(context());
653+
} catch (...) {
654+
rollback_batch_noexcept();
655+
throw;
656+
}
657+
const auto status = context().store.commit(ScratchDurability::buffered);
658+
if (status != ScratchStatus::ok)
659+
fail_batch(status);
660+
batch_open_ = false;
661+
}
662+
}
607663
void set(std::size_t index, const T& value) {
608664
static_assert(!std::is_same_v<T, std::string>, "text selections are immutable");
665+
finish();
609666
put(index, value);
610667
}
611668
auto operator[](std::size_t index) const {
612669
return ExitScratchValue<T>::read(context(), find(index));
613670
}
614671
auto front() const { return (*this)[0]; }
615672
void clear() {
673+
finish();
616674
scope_.clear();
617675
count_ = 0;
618676
observe();
@@ -679,6 +737,55 @@ template<class T> class ExitScratchRows {
679737
const_iterator end() const { return {this, size()}; }
680738

681739
private:
740+
bool batch_enabled() const noexcept {
741+
if constexpr (Mode != ExitScratchRowMode::bounded_batch) {
742+
return false;
743+
} else {
744+
return !context().store.has_io_observer()
745+
&& context().observe_rows == nullptr
746+
&& context().observe_lookup == nullptr;
747+
}
748+
}
749+
void begin_batch() {
750+
exit_scratch_check_teardown(context());
751+
exit_scratch_check(context().store.begin(), context().store);
752+
batch_open_ = true;
753+
}
754+
void fail_batch(ScratchStatus status) {
755+
try {
756+
exit_scratch_edit_failure(context(), status);
757+
} catch (...) {
758+
batch_open_ = context().store.transaction_active();
759+
throw;
760+
}
761+
}
762+
void rollback_batch_noexcept() noexcept {
763+
if constexpr (Mode == ExitScratchRowMode::bounded_batch) {
764+
if (!batch_open_) return;
765+
batch_open_ = false;
766+
if (!context().store.transaction_active()) return;
767+
const auto status = context().store.rollback();
768+
context().teardown_failure.remember(status, context().store);
769+
}
770+
}
771+
ScratchStatus put_value(std::uint64_t index, const T& value) {
772+
const ExitScratchKey key(scope(), 0, 0, index);
773+
const auto payload = ExitScratchValue<T>::source(value);
774+
ScratchHandle handle = 0;
775+
auto status = context().store.put(key, payload, handle);
776+
if constexpr (std::is_same_v<T, std::string>) {
777+
if (status != ScratchStatus::ok) return status;
778+
const ExitScratchKey ordered(scope(), 1, 0, index, &payload);
779+
const ScratchSpanSource empty({nullptr, 0});
780+
status = context().store.put(ordered, empty, handle);
781+
}
782+
return status;
783+
}
784+
void put_batch(std::uint64_t index, const T& value) {
785+
const auto status = put_value(index, value);
786+
if (status != ScratchStatus::ok)
787+
fail_batch(status);
788+
}
682789
void observe() const noexcept {
683790
if (context().observe_rows)
684791
context().observe_rows(context().observation, scope(), size());
@@ -692,23 +799,18 @@ template<class T> class ExitScratchRows {
692799
return handle;
693800
}
694801
void put(std::uint64_t index, const T& value) {
695-
const ExitScratchKey key(scope(), 0, 0, index);
696-
const auto payload = ExitScratchValue<T>::source(value);
697802
exit_scratch_edit(context(), [&] {
698-
ScratchHandle handle = 0;
699-
auto status = context().store.put(key, payload, handle);
700-
if constexpr (std::is_same_v<T, std::string>) {
701-
if (status != ScratchStatus::ok) return status;
702-
const ExitScratchKey ordered(scope(), 1, 0, index, &payload);
703-
const ScratchSpanSource empty({nullptr, 0});
704-
status = context().store.put(ordered, empty, handle);
705-
}
706-
return status;
803+
return put_value(index, value);
707804
});
708805
}
709806
ExitScratchScope scope_;
710807
std::uint64_t count_ = 0;
808+
// Transient transaction ownership only; never part of durable row state.
809+
bool batch_open_ = false;
711810
};
712811

812+
template<class T>
813+
using ExitScratchBatchRows = ExitScratchRows<T, ExitScratchRowMode::bounded_batch>;
814+
713815
} // namespace detail
714816
} // namespace pineforge::source

‎scripts/check_market_admission_schema.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def check(root: Path = ROOT) -> int:
3131
for token in (
3232
"void reflect(const Draft& value", "void reflect(const Event& value",
3333
"void Journal::reflect", "Journal::Prefix Journal::folded_window() const",
34-
"fold_journal_record(source,page,result.digest)",
34+
"fold_validated_journal_record(source,page,summary,result.digest,",
3535
):
3636
if token not in source:
3737
raise ValueError("admission reflection/hash fold is missing: " + token)

‎scripts/run_failure_throw_allowlist.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include/pineforge/map.hpp | throw; | 1 | rethrow after PineMap::put rolls its ne
3030
include/pineforge/drawing.hpp | throw; | 1 | rethrow after a drawing record block's copy constructor destroys its partial copies: the allocation failure (out_of_memory) passes through unchanged
3131
include/pineforge/series.hpp | rethrow_exception(std::exchange(cache_failure_, {})); | 1 | rethrow the exception captured by the noexcept window-cache backing callback: preserve its dynamic type, errno and carried run-failure arguments after get returns
3232
include/pineforge/series.hpp | throw; | 2 | rethrow from read_backing and write_backing after incrementing the I/O error counter: preserve the original ScratchFile system_error type, errno and carried run-failure code
33+
include/pineforge/source/exit_scratch.hpp | throw; | 2 | rethrows in the exit scratch batch builder: finish() rolls the open batch back and rethrows the failure of its teardown check, and fail_batch records whether a transaction is still open and rethrows the coded edit failure; the original exception keeps its own code
3334
include/pineforge/source/pine_adapter.hpp | source placement handle is absent | 2 | engine invariant (PlacementTable::at, mutable and const): the adapter reads only placements it holds; std::out_of_range reads engine_invariant; an installed source header, whose inline bodies stay byte-for-byte (script ABI receipts)
3435
include/pineforge/source/pine_adapter.hpp | source placement incarnation is out of range | 1 | engine invariant (PlacementTable::try_emplace with incarnation 0 or UINT64_MAX): std::length_error reads engine_invariant; an installed source header, whose inline bodies stay byte-for-byte (script ABI receipts)
3536
include/pineforge/source/pine_adapter.hpp | throw; | 2 | rethrow (OriginRoster::push_back) after a failed insert is rolled back, and rethrow (PlacementTable::erase) after the derived sequence index is dropped because its write failed: the original exception (allocation failure or a coded scratch error) passes through unchanged

‎scripts/test_market_admission_schema.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_missing_adapter_hash_is_rejected(self):
3838
try:
3939
for relative, token in (
4040
("src/source/market_admission.cpp",
41-
"fold_journal_record(source,page,result.digest)"),
41+
"fold_validated_journal_record(source,page,summary,result.digest,"),
4242
("src/source/pine_state_hash.cpp",
4343
"const auto folded = admission_journal.folded_window();"),
4444
("src/source/pine_state_hash.cpp",

0 commit comments

Comments
 (0)