Add funded reproduction with retained lineage identity
Close the M2.5 fixture cycle. A bounded list of trusted reproduction intents runs after feeding and before decomposition, so a consumer can fund offspring from what it ingested and a process terminated in that tick cannot reproduce. The intent names a withdrawal from the parent reservoir; a fixed construction fraction endows the child and the remainder becomes real heat and waste. Nothing is credited from outside the community, so conserved totals are unchanged by a birth. Reserved slots are pre-allocated at creation, so a birth never grows the ledger and a full community rejects rather than funding a smaller child. Slots never return to the unborn state, so process identities are never reused, and a parent reference plus origin tick outlive both organisms and survive decomposition. A child duplicates the configuration of its parent because no genome exists yet; this is duplication, not heredity. Default zero capacity leaves the M2.4 reservoir layout and every existing result unchanged, including golden d61ad5bf0c928c76. Native Release, clang-tidy analysis, ASan and format checks pass; Linux CI has not run for this increment yet. This defines no reproductive strategy, fecundity rule, mutation model, gestation, spatial placement or account ownership. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+175
-32
@@ -27,16 +27,26 @@ accounting::ReservoirId reservoir(std::uint32_t actor) noexcept {
|
||||
} // namespace
|
||||
|
||||
FeedingCommunity::FeedingCommunity(ProducerProcess producer, std::vector<ConsumerConfig> consumers,
|
||||
DecompositionConfig decomposition)
|
||||
DecompositionConfig decomposition,
|
||||
ReproductionConfig reproduction, std::uint32_t living)
|
||||
: producer_{std::move(producer)}, consumers_{std::move(consumers)},
|
||||
active_(consumers_.size() + 1, 1), decomposition_{decomposition} {}
|
||||
states_(consumers_.size() + 1, State::unborn), parents_(consumers_.size() + 1, 0),
|
||||
origins_(consumers_.size() + 1, Tick{}), decomposition_{decomposition},
|
||||
reproduction_{reproduction} {
|
||||
// Index zero is the producer; reserved slots stay unborn until a funded birth.
|
||||
for (std::size_t index = 0; index <= living; ++index) {
|
||||
states_[index] = State::active;
|
||||
}
|
||||
}
|
||||
|
||||
std::expected<FeedingCommunity, FeedingError>
|
||||
FeedingCommunity::create(ProducerConfig producer, accounting::Picojoules source,
|
||||
std::array<accounting::Femtograms, 4> nutrients,
|
||||
std::span<const ConsumerConfig> consumers,
|
||||
DecompositionConfig decomposition) {
|
||||
DecompositionConfig decomposition, ReproductionConfig reproduction) {
|
||||
if (consumers.empty() || consumers.size() > max_consumers ||
|
||||
reproduction.capacity > max_consumers - consumers.size() ||
|
||||
reproduction.construction_per_thousand > 1000 ||
|
||||
std::ranges::any_of(consumers,
|
||||
[](const auto& c) { return c.assimilation_per_thousand > 1000; })) {
|
||||
return std::unexpected{FeedingError::invalid_config};
|
||||
@@ -50,30 +60,94 @@ FeedingCommunity::create(ProducerConfig producer, accounting::Picojoules source,
|
||||
for (const auto& consumer : consumers) {
|
||||
opening.push_back(consumer.opening);
|
||||
}
|
||||
// Reserved reservoirs precede waste, so the waste index remains last.
|
||||
opening.insert(opening.end(), reproduction.capacity, accounting::Inventory{});
|
||||
opening.emplace_back(); // Waste is owned matter, not destroyed inventory.
|
||||
auto ledger = accounting::Ledger::create(opening);
|
||||
if (!ledger) {
|
||||
return std::unexpected{FeedingError::accounting_failure};
|
||||
}
|
||||
isolated->ledger_ = std::move(*ledger);
|
||||
return FeedingCommunity{
|
||||
std::move(*isolated), {consumers.begin(), consumers.end()}, decomposition};
|
||||
std::vector<ConsumerConfig> slots{consumers.begin(), consumers.end()};
|
||||
slots.resize(consumers.size() + reproduction.capacity);
|
||||
return FeedingCommunity{std::move(*isolated), std::move(slots), decomposition, reproduction,
|
||||
static_cast<std::uint32_t>(consumers.size())};
|
||||
}
|
||||
|
||||
std::expected<FeedingCommunity::State, FeedingError>
|
||||
FeedingCommunity::state(std::uint32_t id) const noexcept {
|
||||
if (id == 0 || id > states_.size()) {
|
||||
return std::unexpected{FeedingError::invalid_actor};
|
||||
}
|
||||
return states_[id - 1];
|
||||
}
|
||||
|
||||
std::expected<bool, FeedingError> FeedingCommunity::active(std::uint32_t id) const noexcept {
|
||||
if (id == 0 || id > active_.size()) {
|
||||
return std::unexpected{FeedingError::invalid_actor};
|
||||
const auto current = state(id);
|
||||
if (!current) {
|
||||
return std::unexpected{current.error()};
|
||||
}
|
||||
return active_[id - 1] != 0;
|
||||
return *current == State::active;
|
||||
}
|
||||
|
||||
std::expected<bool, FeedingError> FeedingCommunity::born(std::uint32_t id) const noexcept {
|
||||
const auto current = state(id);
|
||||
if (!current) {
|
||||
return std::unexpected{current.error()};
|
||||
}
|
||||
return *current != State::unborn;
|
||||
}
|
||||
|
||||
std::expected<std::uint32_t, FeedingError>
|
||||
FeedingCommunity::parent(std::uint32_t id) const noexcept {
|
||||
const auto exists = born(id);
|
||||
if (!exists) {
|
||||
return std::unexpected{exists.error()};
|
||||
}
|
||||
if (!*exists) {
|
||||
return std::unexpected{FeedingError::unborn_actor};
|
||||
}
|
||||
return parents_[id - 1];
|
||||
}
|
||||
|
||||
std::expected<Tick, FeedingError> FeedingCommunity::origin(std::uint32_t id) const noexcept {
|
||||
const auto exists = born(id);
|
||||
if (!exists) {
|
||||
return std::unexpected{exists.error()};
|
||||
}
|
||||
if (!*exists) {
|
||||
return std::unexpected{FeedingError::unborn_actor};
|
||||
}
|
||||
return origins_[id - 1];
|
||||
}
|
||||
|
||||
std::expected<accounting::Inventory, FeedingError>
|
||||
FeedingCommunity::corpse(std::uint32_t id) const noexcept {
|
||||
const auto current = state(id);
|
||||
if (!current) {
|
||||
return std::unexpected{current.error()};
|
||||
}
|
||||
if (*current == State::unborn) {
|
||||
return std::unexpected{FeedingError::unborn_actor};
|
||||
}
|
||||
if (*current == State::active) {
|
||||
return std::unexpected{FeedingError::not_corpse};
|
||||
}
|
||||
return balances()[reservoir(id).value - 1];
|
||||
}
|
||||
|
||||
std::expected<FeedReceipt, FeedingError> FeedingCommunity::feed(const FeedIntent& input) {
|
||||
if (input.consumer < 2 || input.consumer > active_.size() || input.target == 0 ||
|
||||
input.target > active_.size() || input.target == input.consumer) {
|
||||
if (input.consumer < 2 || input.consumer > states_.size() || input.target == 0 ||
|
||||
input.target > states_.size() || input.target == input.consumer) {
|
||||
return std::unexpected{FeedingError::invalid_actor};
|
||||
}
|
||||
if (active_[input.consumer - 1] == 0 || active_[input.target - 1] == 0) {
|
||||
return std::unexpected{FeedingError::inactive_actor};
|
||||
for (const auto actor : {input.consumer, input.target}) {
|
||||
if (states_[actor - 1] == State::unborn) {
|
||||
return std::unexpected{FeedingError::unborn_actor};
|
||||
}
|
||||
if (states_[actor - 1] != State::active) {
|
||||
return std::unexpected{FeedingError::inactive_actor};
|
||||
}
|
||||
}
|
||||
const auto& config = consumers_[input.consumer - 2];
|
||||
if (input.amount == accounting::Inventory{} || !fits(input.amount, config.intake_limit)) {
|
||||
@@ -103,32 +177,74 @@ std::expected<FeedReceipt, FeedingError> FeedingCommunity::feed(const FeedIntent
|
||||
}
|
||||
const bool depleted = balances()[from.value - 1] == accounting::Inventory{};
|
||||
if (depleted) {
|
||||
active_[input.target - 1] = 0;
|
||||
states_[input.target - 1] = State::corpse;
|
||||
}
|
||||
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()};
|
||||
std::expected<ReproduceReceipt, FeedingError>
|
||||
FeedingCommunity::reproduce(const ReproduceIntent& input, Tick target) {
|
||||
// The producer is funded by capture; treating it as a parent would conflate the two.
|
||||
if (input.parent < 2 || input.parent > states_.size()) {
|
||||
return std::unexpected{FeedingError::invalid_actor};
|
||||
}
|
||||
if (*alive) {
|
||||
return std::unexpected{FeedingError::not_corpse};
|
||||
if (states_[input.parent - 1] == State::unborn) {
|
||||
return std::unexpected{FeedingError::unborn_actor};
|
||||
}
|
||||
return balances()[reservoir(id).value - 1];
|
||||
if (states_[input.parent - 1] != State::active) {
|
||||
return std::unexpected{FeedingError::inactive_actor};
|
||||
}
|
||||
if (input.investment == accounting::Inventory{}) {
|
||||
return std::unexpected{FeedingError::invalid_endowment};
|
||||
}
|
||||
const auto from = reservoir(input.parent);
|
||||
if (!fits(input.investment, balances()[from.value - 1])) {
|
||||
return std::unexpected{FeedingError::insufficient_inventory};
|
||||
}
|
||||
const auto slot = std::ranges::find(states_, State::unborn);
|
||||
if (slot == states_.end()) {
|
||||
return std::unexpected{FeedingError::no_capacity};
|
||||
}
|
||||
const auto child = static_cast<std::uint32_t>(slot - states_.begin()) + 1;
|
||||
accounting::Inventory endowment{}, heat{}, waste{};
|
||||
endowment.energy.value =
|
||||
portion(input.investment.energy.value, reproduction_.construction_per_thousand);
|
||||
heat.energy.value = input.investment.energy.value - endowment.energy.value;
|
||||
for (std::size_t index = 0; index < 4; ++index) {
|
||||
endowment.matter[index].value =
|
||||
portion(input.investment.matter[index].value, reproduction_.construction_per_thousand);
|
||||
waste.matter[index].value =
|
||||
input.investment.matter[index].value - endowment.matter[index].value;
|
||||
}
|
||||
if (!fits(reproduction_.minimum_endowment, endowment)) {
|
||||
return std::unexpected{FeedingError::invalid_endowment};
|
||||
}
|
||||
const std::array legs{
|
||||
accounting::Transfer{from, reservoir(child), endowment},
|
||||
accounting::Transfer{from, {4}, heat},
|
||||
accounting::Transfer{from, {static_cast<std::uint32_t>(balances().size())}, waste}};
|
||||
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};
|
||||
}
|
||||
*slot = State::active;
|
||||
// No genome exists yet, so a child duplicates the configuration of its parent.
|
||||
consumers_[child - 2] = consumers_[input.parent - 2];
|
||||
parents_[child - 1] = input.parent;
|
||||
origins_[child - 1] = target;
|
||||
return ReproduceReceipt{input.sequence, child, endowment, {heat.energy, waste.matter}};
|
||||
}
|
||||
|
||||
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) {
|
||||
// Final iteration is waste, after all corpse reservoirs. Unborn slots are skipped.
|
||||
for (std::size_t index = 0; index <= states_.size(); ++index) {
|
||||
if (index < states_.size() && states_[index] != State::corpse) {
|
||||
continue;
|
||||
}
|
||||
const auto from =
|
||||
index == active_.size()
|
||||
index == states_.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];
|
||||
@@ -164,13 +280,14 @@ std::expected<void, FeedingError> FeedingCommunity::decompose() {
|
||||
|
||||
std::expected<std::vector<FeedReceipt>, FeedingError>
|
||||
FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs,
|
||||
std::span<const TerminalEvent> terminal_events) {
|
||||
std::span<const TerminalEvent> terminal_events,
|
||||
std::span<const ReproduceIntent> births) {
|
||||
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() ||
|
||||
terminal_events.size() > active_.size()) {
|
||||
if (inputs.size() > max_consumers || states_.empty() ||
|
||||
terminal_events.size() > states_.size() || births.size() > max_consumers) {
|
||||
return std::unexpected{FeedingError::invalid_batch};
|
||||
}
|
||||
std::vector<FeedIntent> ordered{inputs.begin(), inputs.end()};
|
||||
@@ -185,19 +302,35 @@ FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs,
|
||||
seen[input.consumer] = true;
|
||||
previous = input.sequence;
|
||||
}
|
||||
// Reproduction sequences form a separate space; a consumer may feed and then reproduce.
|
||||
std::vector<ReproduceIntent> ordered_births{births.begin(), births.end()};
|
||||
std::ranges::sort(ordered_births, {}, &ReproduceIntent::sequence);
|
||||
std::array<bool, max_consumers + 2> expecting{};
|
||||
previous = 0;
|
||||
for (const auto& birth : ordered_births) {
|
||||
if (birth.sequence == 0 || birth.sequence == previous || birth.parent < 2 ||
|
||||
birth.parent > consumers_.size() + 1 || expecting[birth.parent]) {
|
||||
return std::unexpected{FeedingError::invalid_batch};
|
||||
}
|
||||
expecting[birth.parent] = true;
|
||||
previous = birth.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]) {
|
||||
if (event.actor == 0 || event.actor > states_.size() || terminated[event.actor]) {
|
||||
return std::unexpected{FeedingError::invalid_batch};
|
||||
}
|
||||
if (candidate.active_[event.actor - 1] == 0) {
|
||||
if (candidate.states_[event.actor - 1] == State::unborn) {
|
||||
return std::unexpected{FeedingError::unborn_actor};
|
||||
}
|
||||
if (candidate.states_[event.actor - 1] != State::active) {
|
||||
return std::unexpected{FeedingError::inactive_actor};
|
||||
}
|
||||
terminated[event.actor] = true;
|
||||
candidate.active_[event.actor - 1] = 0;
|
||||
candidate.states_[event.actor - 1] = State::corpse;
|
||||
}
|
||||
if (candidate.active_[0] != 0) {
|
||||
if (candidate.states_[0] == State::active) {
|
||||
if (!candidate.producer_.advance(target)) {
|
||||
return std::unexpected{FeedingError::accounting_failure};
|
||||
}
|
||||
@@ -213,6 +346,16 @@ FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs,
|
||||
}
|
||||
receipts.push_back(*result);
|
||||
}
|
||||
std::vector<ReproduceReceipt> delivered;
|
||||
delivered.reserve(ordered_births.size());
|
||||
for (const auto& birth : ordered_births) {
|
||||
const auto result = candidate.reproduce(birth, target);
|
||||
if (!result) {
|
||||
return std::unexpected{result.error()};
|
||||
}
|
||||
delivered.push_back(*result);
|
||||
}
|
||||
candidate.last_births_ = std::move(delivered);
|
||||
if (const auto result = candidate.decompose(); !result) {
|
||||
return std::unexpected{result.error()};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user