feat(lifecycle): replay retained offspring plans with explicit trace version
Foundation / checks (ecs) (push) Successful in 2m23s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 12m39s
Foundation / checks (client) (push) Canceled after 12m44s
Foundation / checks (checks) (push) Canceled after 12m48s
Foundation / checks (tsan) (push) Canceled after 10m18s
Foundation / checks (postgres) (push) Canceled after 12m44s

This commit is contained in:
dingo
2026-10-19 14:04:52 -04:00
parent 2c3a3c65a6
commit c0c66fa11a
15 changed files with 673 additions and 35 deletions
+44 -28
View File
@@ -32,6 +32,24 @@ bool valid_header(const CellularBatchHeader& h, std::size_t ticks) {
!all_zero(h.predecessor_digest) && h.end_tick > h.begin_tick &&
h.end_tick - h.begin_tick == ticks && ticks > 0 && ticks <= max_cellular_batch_ticks;
}
bool known_format(std::uint32_t format) {
return format == cellular_batch_legacy_format || format == cellular_batch_v2_format ||
format == cellular_batch_format || format == cellular_batch_inheritance_format;
}
bool owner_version_matches(const CellularLives& owner, const CellularBatch& batch) {
if (!known_format(batch.format_version))
return false;
if (batch.format_version == cellular_batch_inheritance_format)
return owner.format_version() == cellular_lives_inheritance_format &&
owner.rules_version() == cellular_lives_inheritance_rules;
return owner.format_version() < cellular_lives_inheritance_format;
}
bool payload_version_matches(const CellularBatch& batch) {
return std::ranges::all_of(batch.ticks, [&](const auto& tick) {
return (batch.format_version >= cellular_batch_v2_format || tick.births.empty()) &&
(batch.format_version >= cellular_batch_format || tick.mutations.empty());
});
}
bool valid_intent(const CellularLifeIntent& life) {
const auto& step = life.intent;
return life.life_id != 0 && step.movement.x >= -movement_limit &&
@@ -153,21 +171,11 @@ void put_intent(std::vector<std::uint8_t>& out, const CellularLifeIntent& life)
std::expected<std::vector<std::uint8_t>, CellularBatchError>
encode_cellular_batch(const CellularBatch& batch) {
if ((batch.format_version != cellular_batch_format &&
batch.format_version != cellular_batch_v2_format &&
batch.format_version != cellular_batch_legacy_format) ||
if (!known_format(batch.format_version) || !payload_version_matches(batch) ||
!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks))
return std::unexpected{CellularBatchError::format};
if (batch.format_version == cellular_batch_legacy_format &&
std::ranges::any_of(batch.ticks, [](const auto& tick) {
return !tick.births.empty() || !tick.mutations.empty();
}))
return std::unexpected{CellularBatchError::format};
if (batch.format_version == cellular_batch_v2_format &&
std::ranges::any_of(batch.ticks, [](const auto& tick) { return !tick.mutations.empty(); }))
return std::unexpected{CellularBatchError::format};
const auto tick_header_bytes =
batch.format_version == cellular_batch_format ? cellular_batch_tick_header_bytes
batch.format_version >= cellular_batch_format ? cellular_batch_tick_header_bytes
: batch.format_version == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes
: cellular_batch_v1_tick_header_bytes;
std::size_t size = cellular_batch_header_bytes;
@@ -176,7 +184,7 @@ encode_cellular_batch(const CellularBatch& batch) {
(batch.format_version >= cellular_batch_v2_format
? tick.births.size() * cellular_batch_birth_bytes
: 0) +
(batch.format_version == cellular_batch_format
(batch.format_version >= cellular_batch_format
? tick.mutations.size() * cellular_batch_mutation_bytes
: 0);
if (records > max_cellular_batch_bytes - size - tick_header_bytes)
@@ -202,7 +210,7 @@ encode_cellular_batch(const CellularBatch& batch) {
put(out, tick.lives.size(), 4);
if (batch.format_version >= cellular_batch_v2_format)
put(out, tick.births.size(), 4);
if (batch.format_version == cellular_batch_format) {
if (batch.format_version >= cellular_batch_format) {
put(out, tick.mutations.size(), 4);
put(out, 0, 4);
}
@@ -217,7 +225,7 @@ encode_cellular_batch(const CellularBatch& batch) {
put(out, birth.identity, 8);
}
for (const auto& mutation : tick.mutations) {
if (batch.format_version != cellular_batch_format)
if (batch.format_version < cellular_batch_format)
break;
put(out, mutation.life_id, 8);
put(out, mutation.event_index, 8);
@@ -238,9 +246,8 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
!std::ranges::equal(bytes.first(magic.size()), magic))
return std::unexpected{CellularBatchError::format};
const auto format = get(bytes, 8, 4);
if ((format != cellular_batch_format && format != cellular_batch_v2_format &&
format != cellular_batch_legacy_format) ||
get(bytes, 12, 4) != 4)
if (format > cellular_batch_inheritance_format ||
!known_format(static_cast<std::uint32_t>(format)) || get(bytes, 12, 4) != 4)
return std::unexpected{CellularBatchError::version};
if (get(bytes, 108, 4) != 0)
return std::unexpected{CellularBatchError::format};
@@ -266,7 +273,7 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
out.ticks.reserve(static_cast<std::size_t>(tick_count));
for (std::uint64_t tick_index = 0; tick_index < tick_count; ++tick_index) {
const auto tick_header_bytes =
format == cellular_batch_format ? cellular_batch_tick_header_bytes
format >= cellular_batch_format ? cellular_batch_tick_header_bytes
: format == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes
: cellular_batch_v1_tick_header_bytes;
if (bytes.size() - offset < tick_header_bytes)
@@ -279,7 +286,7 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
offset += 4;
}
std::uint64_t mutation_count{};
if (format == cellular_batch_format) {
if (format >= cellular_batch_format) {
mutation_count = get(bytes, offset, 4);
if (get(bytes, offset + 4, 4) != 0)
return std::unexpected{CellularBatchError::format};
@@ -358,7 +365,7 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
}
out.ticks.push_back(std::move(step));
}
if (offset != bytes.size())
if (offset != bytes.size() || !valid_ticks(out.ticks))
return std::unexpected{CellularBatchError::format};
return out;
}
@@ -407,11 +414,11 @@ std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
std::vector<CellularMutationOutcome>& mutation_outcomes) {
mutation_outcomes.clear();
// ADR-0165: this trace has no admitted offspring-plan outcome contract.
if (base.format_version() >= cellular_lives_inheritance_format)
if (!owner_version_matches(base, batch))
return std::unexpected{CellularBatchError::version};
const auto& h = batch.header;
if (!valid_header(h, batch.ticks.size()) || !valid_ticks(batch.ticks))
if (!payload_version_matches(batch) || !valid_header(h, batch.ticks.size()) ||
!valid_ticks(batch.ticks))
return std::unexpected{CellularBatchError::format};
if (base.world() != h.world || base.region() != h.region || base.frame() != h.frame ||
base.tick() != h.begin_tick)
@@ -420,12 +427,13 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
base.tick());
if (!replay)
return std::unexpected{CellularBatchError::state};
std::vector<CellularMutationOutcome> complete_outcomes;
for (const auto& tick : batch.ticks) {
const auto executed = (*replay)->advance_tick_with_mutations(tick.lives, tick.mutations);
if (!executed)
return std::unexpected{CellularBatchError::state};
for (const auto& mutation : executed->mutations)
mutation_outcomes.push_back(
complete_outcomes.push_back(
{(*replay)->tick(), mutation.life_id, mutation.successor_id, mutation.result});
for (const auto& birth : tick.births) {
const LifeRequest request{birth.account, birth.request};
@@ -440,6 +448,7 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
*replay = std::move(*born);
}
}
mutation_outcomes = std::move(complete_outcomes);
return std::move(*replay);
}
@@ -530,7 +539,10 @@ decode_cellular_mutation_action_block(std::span<const std::uint8_t> bytes, std::
std::expected<void, CellularActionBlockError>
match_cellular_actions_to_trace(const CellularLives& predecessor, const CellularBatch& batch,
std::span<const CellularActionRecord> actions) {
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) ||
if (!owner_version_matches(predecessor, batch))
return std::unexpected{CellularActionBlockError::mismatch};
if (!payload_version_matches(batch) || !valid_header(batch.header, batch.ticks.size()) ||
!valid_ticks(batch.ticks) ||
!valid_actions(actions, batch.header.begin_tick, batch.header.end_tick))
return std::unexpected{CellularActionBlockError::format};
if (predecessor.world() != batch.header.world || predecessor.region() != batch.header.region ||
@@ -569,7 +581,10 @@ std::expected<void, CellularActionBlockError>
match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
const CellularBatch& batch,
std::span<const CellularMutationActionRecord> actions) {
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) ||
if (!owner_version_matches(predecessor, batch))
return std::unexpected{CellularActionBlockError::mismatch};
if (!payload_version_matches(batch) || !valid_header(batch.header, batch.ticks.size()) ||
!valid_ticks(batch.ticks) ||
!valid_mutation_actions(actions, batch.header.begin_tick, batch.header.end_tick))
return std::unexpected{CellularActionBlockError::format};
if (predecessor.world() != batch.header.world || predecessor.region() != batch.header.region ||
@@ -618,7 +633,8 @@ match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
selection->operator_id != action.input.operator_id ||
life->mutation.event_index != action.input.event_index ||
life->mutation.offer_tick != action.input.offer_tick ||
life->identity.life != action.input.life_id || offer.parent != life->identity.genome ||
life->identity.life != action.input.life_id ||
offer.parent != cellular_offspring_genome(*life) ||
(action.input.operator_id != 0 &&
!std::ranges::any_of(offer.candidates, [&](const auto& candidate) {
return candidate.operator_id == action.input.operator_id;