Preserve terminal inventories and add bounded decomposition

This commit is contained in:
dingo
2026-09-20 15:26:58 -04:00
parent 5196c0aadd
commit e17a9a7279
9 changed files with 365 additions and 15 deletions
+79 -6
View File
@@ -26,14 +26,16 @@ accounting::ReservoirId reservoir(std::uint32_t actor) noexcept {
}
} // namespace
FeedingCommunity::FeedingCommunity(ProducerProcess producer, std::vector<ConsumerConfig> consumers)
FeedingCommunity::FeedingCommunity(ProducerProcess producer, std::vector<ConsumerConfig> consumers,
DecompositionConfig decomposition)
: producer_{std::move(producer)}, consumers_{std::move(consumers)},
active_(consumers_.size() + 1, 1) {}
active_(consumers_.size() + 1, 1), decomposition_{decomposition} {}
std::expected<FeedingCommunity, FeedingError>
FeedingCommunity::create(ProducerConfig producer, accounting::Picojoules source,
std::array<accounting::Femtograms, 4> nutrients,
std::span<const ConsumerConfig> consumers) {
std::span<const ConsumerConfig> consumers,
DecompositionConfig decomposition) {
if (consumers.empty() || consumers.size() > max_consumers ||
std::ranges::any_of(consumers,
[](const auto& c) { return c.assimilation_per_thousand > 1000; })) {
@@ -54,7 +56,8 @@ FeedingCommunity::create(ProducerConfig producer, accounting::Picojoules source,
return std::unexpected{FeedingError::accounting_failure};
}
isolated->ledger_ = std::move(*ledger);
return FeedingCommunity{std::move(*isolated), {consumers.begin(), consumers.end()}};
return FeedingCommunity{
std::move(*isolated), {consumers.begin(), consumers.end()}, decomposition};
}
std::expected<bool, FeedingError> FeedingCommunity::active(std::uint32_t id) const noexcept {
@@ -105,13 +108,69 @@ std::expected<FeedReceipt, FeedingError> FeedingCommunity::feed(const FeedIntent
return FeedReceipt{input.sequence, assimilated, depleted};
}
std::expected<accounting::Inventory, FeedingError>
FeedingCommunity::corpse(std::uint32_t id) const noexcept {
const auto alive = active(id);
if (!alive) {
return std::unexpected{alive.error()};
}
if (*alive) {
return std::unexpected{FeedingError::not_corpse};
}
return balances()[reservoir(id).value - 1];
}
std::expected<void, FeedingError> FeedingCommunity::decompose() {
auto remaining = decomposition_.per_tick_limit;
// Final iteration is waste, after all inactive process reservoirs.
for (std::size_t index = 0; index <= active_.size(); ++index) {
if (index < active_.size() && active_[index] != 0) {
continue;
}
const auto from =
index == active_.size()
? accounting::ReservoirId{static_cast<std::uint32_t>(balances().size())}
: reservoir(static_cast<std::uint32_t>(index + 1));
const auto available = balances()[from.value - 1];
accounting::Inventory heat{}, nutrients{};
heat.energy.value = std::min(remaining.energy.value, available.energy.value);
for (std::size_t channel = 0; channel < 4; ++channel) {
nutrients.matter[channel].value =
std::min(remaining.matter[channel].value, available.matter[channel].value);
}
if (heat == accounting::Inventory{} && nutrients == accounting::Inventory{}) {
continue;
}
const std::array legs{accounting::Transfer{from, {4}, heat},
accounting::Transfer{from, {2}, nutrients}};
auto& ledger = producer_.ledger_;
if (ledger.last_action() == std::numeric_limits<std::uint64_t>::max() ||
!ledger.transfer(ledger.last_action() + 1, legs)) {
return std::unexpected{FeedingError::accounting_failure};
}
remaining.energy.value -= heat.energy.value;
for (std::size_t channel = 0; channel < 4; ++channel) {
remaining.matter[channel].value -= nutrients.matter[channel].value;
}
}
last_decomposed_.energy.value =
decomposition_.per_tick_limit.energy.value - remaining.energy.value;
for (std::size_t channel = 0; channel < 4; ++channel) {
last_decomposed_.matter[channel].value =
decomposition_.per_tick_limit.matter[channel].value - remaining.matter[channel].value;
}
return {};
}
std::expected<std::vector<FeedReceipt>, FeedingError>
FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs) {
FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs,
std::span<const TerminalEvent> terminal_events) {
if (progress().tick.value == std::numeric_limits<std::uint64_t>::max() ||
target.value != progress().tick.value + 1) {
return std::unexpected{FeedingError::wrong_tick};
}
if (inputs.size() > max_consumers || active_.empty()) {
if (inputs.size() > max_consumers || active_.empty() ||
terminal_events.size() > active_.size()) {
return std::unexpected{FeedingError::invalid_batch};
}
std::vector<FeedIntent> ordered{inputs.begin(), inputs.end()};
@@ -127,6 +186,17 @@ FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs) {
previous = input.sequence;
}
auto candidate = *this;
std::array<bool, max_consumers + 2> terminated{};
for (const auto event : terminal_events) {
if (event.actor == 0 || event.actor > active_.size() || terminated[event.actor]) {
return std::unexpected{FeedingError::invalid_batch};
}
if (candidate.active_[event.actor - 1] == 0) {
return std::unexpected{FeedingError::inactive_actor};
}
terminated[event.actor] = true;
candidate.active_[event.actor - 1] = 0;
}
if (candidate.active_[0] != 0) {
if (!candidate.producer_.advance(target)) {
return std::unexpected{FeedingError::accounting_failure};
@@ -143,6 +213,9 @@ FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs) {
}
receipts.push_back(*result);
}
if (const auto result = candidate.decompose(); !result) {
return std::unexpected{result.error()};
}
static_assert(std::is_nothrow_move_assignable_v<FeedingCommunity>);
*this = std::move(candidate);
return receipts;