feat(lifecycle): rules 13 owner on format 11 with the revamp tuning file
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 3s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m12s
Foundation / image (push) Successful in 4m40s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 3s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m12s
Foundation / image (push) Successful in 4m40s
Phase 1 of the cellular revamp build plan (ADR-0179). The cellular owner gains rules 13 on owner format 11: an explicit no-tick format-10/rules-12 upgrade declares the region's revamp rules (so far the body's essential upkeep share), existing lives keep their genomes, and founders born afterwards are founder 6 (genome schema 7, development 7). Format 11 is format 10 plus the revamp rules block; it stays undeployed while later phases extend it. The tick, perception and wildlife controller derive budgets with the declared body tuning, which earlier development versions ignore. Revamp tunables live in their own Git file, deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning, parsed by parse_cellular_revamp_tuning with the same format and refusals as the rules-12 file, so images from before rules 13 never meet keys they do not know. Both parsers now share read_tuning_lines; the rules-12 file's behaviour is unchanged. The revamp file is not yet mounted into the region (build plan, phase 6). Tests cover the upgrade and its refusals, mixed founder 5 and founder 6 lives ticking with closure, exact restore and continuation, trace replay, and the new tuning file including the checked-in DEV copy. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+21
-2
@@ -76,7 +76,8 @@ add_library(evo_biology STATIC
|
||||
src/biology/genome.cpp src/biology/development.cpp src/biology/metabolism.cpp
|
||||
src/biology/mutation.cpp src/biology/lineage.cpp src/biology/founder.cpp
|
||||
src/biology/cellular_motion.cpp src/biology/cellular_kinematics.cpp
|
||||
src/biology/cellular_tuning.cpp src/biology/cellular_currents.cpp
|
||||
src/biology/cellular_tuning.cpp src/biology/cellular_revamp_tuning.cpp
|
||||
src/biology/cellular_currents.cpp
|
||||
src/biology/cellular_deposit_motion.cpp
|
||||
src/biology/cellular_uptake.cpp
|
||||
src/biology/cellular_habitat.cpp src/biology/cellular_step.cpp
|
||||
@@ -99,7 +100,8 @@ add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp
|
||||
src/server/lifecycle/cellular_population_codec.cpp src/server/lifecycle/cellular_population_policy.cpp
|
||||
src/server/lifecycle/cellular_schedule.cpp src/server/lifecycle/cellular_population_batch.cpp
|
||||
src/server/lifecycle/cellular_environment.cpp src/server/lifecycle/cellular_environment_batch.cpp
|
||||
src/server/lifecycle/cellular_light.cpp src/server/lifecycle/cellular_liquid.cpp)
|
||||
src/server/lifecycle/cellular_light.cpp src/server/lifecycle/cellular_liquid.cpp
|
||||
src/server/lifecycle/cellular_revamp.cpp)
|
||||
add_library(evo::lifecycle ALIAS evo_lifecycle)
|
||||
target_link_libraries(evo_lifecycle PUBLIC evo::biology evo::world)
|
||||
evo_owned_target(evo_lifecycle)
|
||||
@@ -498,6 +500,17 @@ if(BUILD_TESTING)
|
||||
else()
|
||||
add_test(NAME cellular-tuning-contracts COMMAND evo-cellular-tuning-tests)
|
||||
endif()
|
||||
add_executable(evo-cellular-revamp-tuning-tests tests/cellular_revamp_tuning_tests.cpp)
|
||||
target_link_libraries(evo-cellular-revamp-tuning-tests PRIVATE evo::biology)
|
||||
evo_owned_target(evo-cellular-revamp-tuning-tests)
|
||||
set(evo_dev_revamp_tuning
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning")
|
||||
if(EXISTS "${evo_dev_revamp_tuning}")
|
||||
add_test(NAME cellular-revamp-tuning-contracts
|
||||
COMMAND evo-cellular-revamp-tuning-tests "${evo_dev_revamp_tuning}")
|
||||
else()
|
||||
add_test(NAME cellular-revamp-tuning-contracts COMMAND evo-cellular-revamp-tuning-tests)
|
||||
endif()
|
||||
set_tests_properties(cellular-tuning-contracts PROPERTIES TIMEOUT 60)
|
||||
add_executable(evo-cellular-currents-tests tests/cellular_currents_tests.cpp)
|
||||
target_link_libraries(evo-cellular-currents-tests PRIVATE evo::biology)
|
||||
@@ -749,6 +762,12 @@ if(BUILD_TESTING)
|
||||
evo_owned_target(evo-cellular-liquid-owner-tests)
|
||||
add_test(NAME cellular-liquid-owner-contracts COMMAND evo-cellular-liquid-owner-tests)
|
||||
set_tests_properties(cellular-liquid-owner-contracts PROPERTIES TIMEOUT 300)
|
||||
add_executable(evo-cellular-revamp-owner-tests tests/cellular_revamp_owner_tests.cpp)
|
||||
target_include_directories(evo-cellular-revamp-owner-tests PRIVATE tests)
|
||||
target_link_libraries(evo-cellular-revamp-owner-tests PRIVATE evo::lifecycle)
|
||||
evo_owned_target(evo-cellular-revamp-owner-tests)
|
||||
add_test(NAME cellular-revamp-owner-contracts COMMAND evo-cellular-revamp-owner-tests)
|
||||
set_tests_properties(cellular-revamp-owner-contracts PROPERTIES TIMEOUT 300)
|
||||
add_executable(evo-photosynthesis-tests tests/photosynthesis_tests.cpp)
|
||||
target_link_libraries(evo-photosynthesis-tests PRIVATE evo::lifecycle)
|
||||
evo_owned_target(evo-photosynthesis-tests)
|
||||
|
||||
@@ -18,8 +18,9 @@ rules 12.
|
||||
- **New versions, disposable DEV worlds.** Rules 13, genome schema 7, development 7,
|
||||
founder 6, a new owner format, bundle and schema, and a fresh `cellular-v10`
|
||||
opening. No upgrade from existing rules-12 regions (ADR-0177).
|
||||
- **One tuning file.** Every new tunable goes in
|
||||
`deploy/kubernetes/region/dev/tuning/cellular.tuning`, validated at startup.
|
||||
- **One revamp tuning file.** Every new tunable goes in
|
||||
`deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning`, beside the rules-12
|
||||
`cellular.tuning`, with the same format and validation.
|
||||
- **Client and server together** in every phase.
|
||||
- **Shared biology.** Derivations stay in the headless library, so the client
|
||||
predictor and the server agree.
|
||||
@@ -43,7 +44,8 @@ Server:
|
||||
from points; no legacy photosynthesis speed penalty.
|
||||
- Rules 13 owner skeleton (new owner format) and tuning keys for every body value.
|
||||
|
||||
Client: hover and HUD show the new body values.
|
||||
Client: none in this phase. Showing the new body values needs them on the wire, which
|
||||
session v14 adds in phase 2.
|
||||
|
||||
Verify: founder 6 derives the same body as founder 5 where the plans say "today's
|
||||
value"; the essential floor holds; encode/decode and bound checks for schema 7.
|
||||
@@ -62,7 +64,7 @@ Server:
|
||||
- Session v14 for the new perception fields and the exclusion control.
|
||||
|
||||
Client: energy display including what matter can stand in for; channel exclusion
|
||||
toggles; spent-pool drips drawn like infall.
|
||||
toggles; spent-pool drips drawn like infall; hover and HUD show the phase 1 body values.
|
||||
|
||||
Verify: conservation per channel and energy across burning, the pool, kills and
|
||||
drips; no burn of excluded matter except for upkeep; replay determinism.
|
||||
@@ -128,6 +130,9 @@ Emerge action only at the threshold.
|
||||
new eligibility.
|
||||
- Play summary: first-birth time, time to Adhesion, deaths by cause, energy share paid
|
||||
by matter, offers seen and chosen, mutations held per lineage.
|
||||
- Mount `tuning/cellular-revamp.tuning` into the provision Job beside `cellular.tuning`
|
||||
(kustomize `configMapGenerator`) in the push that promotes the new images. Revamp keys
|
||||
live in that separate file so images from before rules 13 never read them.
|
||||
- Opening `cellular-v10` in a new DEV region; promote the images; full Argo sync.
|
||||
- Playtest.
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ spec:
|
||||
env:
|
||||
- {name: REGION_SESSION, value: "dev"}
|
||||
- {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"}
|
||||
- {name: REGION_SESSION_WINDOW, value: "dev-20261022T220847-feb5f51f:1792721327"}
|
||||
- {name: REGION_SESSION_WINDOW, value: "dev-20261022T221857-44422cf1:1792721937"}
|
||||
- {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"}
|
||||
ports:
|
||||
# QUIC (ADR-0090): UDP only; nothing listens on TCP 7400.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# Cellular revamp trial values (rules 13, ADR-0179; Cellular_Revamp_Build_Plan.md).
|
||||
# Same format as cellular.tuning: `key = value`, integers only, `_` may separate digits,
|
||||
# every key required exactly once. Kept in its own file so images from before rules 13
|
||||
# never read keys they do not know. Not yet mounted into the region; see the build plan.
|
||||
tuning.version = 1
|
||||
|
||||
# Body (body and maintenance plan).
|
||||
body.essential_upkeep_per_mille = 250 # share of upkeep no upkeep-efficiency trait reduces
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/biology/cellular_tuning.hpp"
|
||||
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// Git-managed trial values for the cellular revamp (rules 13, ADR-0179), in their own file
|
||||
// beside the rules-12 tuning file so older images never meet keys they do not know. Same
|
||||
// format and rules: plain `key = value` integer lines, every key required exactly once and
|
||||
// range-checked. Later build phases add feeding, mutation and reproduction keys.
|
||||
namespace evo::biology {
|
||||
inline constexpr std::int64_t cellular_revamp_tuning_version = 1;
|
||||
inline constexpr std::size_t max_cellular_revamp_tuning_bytes = std::size_t{256} * 1'024;
|
||||
|
||||
struct CellularRevampTuning {
|
||||
std::int64_t version{};
|
||||
// Body: the share of upkeep no upkeep-efficiency trait reduces.
|
||||
std::int64_t body_essential_upkeep_per_mille{};
|
||||
auto operator<=>(const CellularRevampTuning&) const = default;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::expected<CellularRevampTuning, CellularTuningError>
|
||||
parse_cellular_revamp_tuning(std::string_view text);
|
||||
// Canonical text: every key once, in schema order.
|
||||
[[nodiscard]] std::string encode_cellular_revamp_tuning(const CellularRevampTuning& tuning);
|
||||
} // namespace evo::biology
|
||||
@@ -7,8 +7,10 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
// Git-managed trial values (Cellular_Region_Movement_Plan.md, tuning file). The file is
|
||||
// plain `key = value` lines with `#` comments and integer values; `_` may separate digits.
|
||||
@@ -73,6 +75,16 @@ struct CellularTuningError {
|
||||
|
||||
[[nodiscard]] std::expected<CellularTuning, CellularTuningError>
|
||||
parse_cellular_tuning(std::string_view text);
|
||||
// The shared line reader for Git tuning files: comments and blank lines dropped, each other
|
||||
// line split into a valid key and its unparsed value text. Views point into `text`.
|
||||
struct TuningLine {
|
||||
std::size_t line{}; // 1-based.
|
||||
std::string_view key, value;
|
||||
};
|
||||
[[nodiscard]] std::expected<std::vector<TuningLine>, CellularTuningError>
|
||||
read_tuning_lines(std::string_view text, std::size_t max_bytes);
|
||||
// Optional '-', then digits with single '_' separators between digits; at most 18 digits.
|
||||
[[nodiscard]] std::optional<std::int64_t> parse_tuning_integer(std::string_view text) noexcept;
|
||||
// Canonical text: every key once, in schema order, so equal tunings encode identically.
|
||||
[[nodiscard]] std::string encode_cellular_tuning(const CellularTuning& tuning);
|
||||
[[nodiscard]] std::string_view describe(CellularTuningErrorKind kind) noexcept;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "evo/biology/mutation.hpp"
|
||||
#include "evo/server/lifecycle/cellular_light.hpp"
|
||||
#include "evo/server/lifecycle/cellular_liquid.hpp"
|
||||
#include "evo/server/lifecycle/cellular_revamp.hpp"
|
||||
#include "evo/server/lifecycle/primitive_lives.hpp"
|
||||
|
||||
#include <compare>
|
||||
@@ -61,16 +62,23 @@ inline constexpr std::uint32_t cellular_lives_photosynthesis_rules = 11;
|
||||
// liquid rules and each life's motion state, on a format-3 habitat whose food moves.
|
||||
inline constexpr std::uint32_t cellular_lives_liquid_format = 10;
|
||||
inline constexpr std::uint32_t cellular_lives_liquid_rules = 12;
|
||||
// Rules that retain offspring plans (formats 8 to 10): 10 to 12.
|
||||
// Rules 13 (ADR-0179, Cellular_Revamp_Build_Plan.md): rules 12 with founder 6 (genome schema 7,
|
||||
// development 7) and the region's declared revamp rules. Format 11 is format 10 with those
|
||||
// rules; later build phases extend it before it is first deployed.
|
||||
inline constexpr std::uint32_t cellular_lives_revamp_format = 11;
|
||||
inline constexpr std::uint32_t cellular_lives_revamp_rules = 13;
|
||||
// Rules that retain offspring plans (formats 8 to 11): 10 to 13.
|
||||
[[nodiscard]] constexpr bool cellular_retained_plans(std::uint32_t rules) noexcept {
|
||||
return rules == cellular_lives_inheritance_rules ||
|
||||
rules == cellular_lives_photosynthesis_rules || rules == cellular_lives_liquid_rules;
|
||||
rules == cellular_lives_photosynthesis_rules || rules == cellular_lives_liquid_rules ||
|
||||
rules == cellular_lives_revamp_rules;
|
||||
}
|
||||
inline constexpr std::uint32_t cellular_growth_per_tick = 5; // Per 50 ms; trial rate.
|
||||
// The founder a region of these rules creates.
|
||||
[[nodiscard]] constexpr biology::FounderVersion
|
||||
cellular_founder_version(std::uint32_t rules) noexcept {
|
||||
return rules >= cellular_lives_liquid_rules ? biology::primitive_founder_v5
|
||||
return rules >= cellular_lives_revamp_rules ? biology::primitive_founder_v6
|
||||
: rules >= cellular_lives_liquid_rules ? biology::primitive_founder_v5
|
||||
: rules >= cellular_lives_photosynthesis_rules ? biology::primitive_founder_v4
|
||||
: rules >= cellular_lives_reproduction_rules ? biology::primitive_founder_v3
|
||||
: rules >= cellular_lives_balance_rules ? biology::primitive_founder_v2
|
||||
@@ -313,6 +321,13 @@ class CellularLives {
|
||||
// bubble) and lives start at rest. Founders born afterwards are founder 5.
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
liquid_upgrade_candidate(const CellularLiquidRules& rules) const;
|
||||
// Rules 13: explicit no-tick format-10/rules-12 -> format-11/rules-13 upgrade declaring the
|
||||
// region's revamp rules. Existing lives keep their genomes; founders born afterwards are
|
||||
// founder 6.
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
revamp_upgrade_candidate(const CellularRevampRules& rules) const;
|
||||
// Format 11: the declared revamp rules (defaults before).
|
||||
[[nodiscard]] const CellularRevampRules& revamp_rules() const noexcept;
|
||||
// Format 10: the declared liquid rules and the current field they generate (null before).
|
||||
[[nodiscard]] const CellularLiquidRules& liquid_rules() const noexcept;
|
||||
[[nodiscard]] const biology::CellularFlowField* flow_field() const noexcept;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
#include "evo/biology/development.hpp"
|
||||
|
||||
#include <compare>
|
||||
|
||||
// Rules 13 (ADR-0179, Cellular_Revamp_Build_Plan.md): what a cellular owner declares when it
|
||||
// upgrades to owner format 11. Taken from the Git tuning file when the region opens and
|
||||
// persisted with the owner, so replay and restore never read the file. Later build phases add
|
||||
// the feeding, mutation and reproduction tunables here.
|
||||
namespace evo::server {
|
||||
struct CellularRevampRules {
|
||||
biology::BodyTuning body{};
|
||||
auto operator<=>(const CellularRevampRules&) const = default;
|
||||
};
|
||||
|
||||
[[nodiscard]] bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept;
|
||||
// Rules from the revamp tuning file.
|
||||
[[nodiscard]] CellularRevampRules
|
||||
cellular_revamp_rules(const biology::CellularRevampTuning& tuning);
|
||||
} // namespace evo::server
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
struct Field {
|
||||
std::string_view key;
|
||||
std::int64_t CellularRevampTuning::* member;
|
||||
std::int64_t minimum, maximum;
|
||||
};
|
||||
|
||||
// Schema order is the canonical encoding order.
|
||||
constexpr std::array fields{
|
||||
Field{"tuning.version", &CellularRevampTuning::version, 1, 1'000},
|
||||
Field{"body.essential_upkeep_per_mille", &CellularRevampTuning::body_essential_upkeep_per_mille,
|
||||
0, 1'000},
|
||||
};
|
||||
|
||||
CellularTuningError error(CellularTuningErrorKind kind, std::size_t line, std::string_view key) {
|
||||
return {kind, line, std::string{key}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularRevampTuning, CellularTuningError>
|
||||
parse_cellular_revamp_tuning(std::string_view text) {
|
||||
const auto lines = read_tuning_lines(text, max_cellular_revamp_tuning_bytes);
|
||||
if (!lines)
|
||||
return std::unexpected{lines.error()};
|
||||
CellularRevampTuning tuning{};
|
||||
std::array<bool, fields.size()> seen{};
|
||||
for (const auto& [line_number, key, value_text] : *lines) {
|
||||
std::size_t index{};
|
||||
while (index < fields.size() && fields.at(index).key != key)
|
||||
++index;
|
||||
if (index == fields.size())
|
||||
return std::unexpected{error(CellularTuningErrorKind::unknown_key, line_number, key)};
|
||||
if (seen.at(index))
|
||||
return std::unexpected{error(CellularTuningErrorKind::duplicate_key, line_number, key)};
|
||||
const auto value = parse_tuning_integer(value_text);
|
||||
if (!value)
|
||||
return std::unexpected{error(CellularTuningErrorKind::invalid_value, line_number, key)};
|
||||
const auto& field = fields.at(index);
|
||||
if (*value < field.minimum || *value > field.maximum)
|
||||
return std::unexpected{error(CellularTuningErrorKind::out_of_range, line_number, key)};
|
||||
seen.at(index) = true;
|
||||
tuning.*field.member = *value;
|
||||
}
|
||||
for (std::size_t index = 0; index < fields.size(); ++index)
|
||||
if (!seen.at(index))
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::missing_key, 0, fields.at(index).key)};
|
||||
if (tuning.version != cellular_revamp_tuning_version)
|
||||
return std::unexpected{
|
||||
error(CellularTuningErrorKind::unsupported_version, 0, "tuning.version")};
|
||||
return tuning;
|
||||
}
|
||||
|
||||
std::string encode_cellular_revamp_tuning(const CellularRevampTuning& tuning) {
|
||||
std::string text;
|
||||
for (const auto& field : fields) {
|
||||
text.append(field.key);
|
||||
text.append(" = ");
|
||||
text.append(std::to_string(tuning.*field.member));
|
||||
text.push_back('\n');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
} // namespace evo::biology
|
||||
@@ -95,8 +95,9 @@ bool valid_key(std::string_view key) noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Optional '-', then digits with single '_' separators between digits; at most 18 digits.
|
||||
std::optional<std::int64_t> parse_integer(std::string_view text) noexcept {
|
||||
} // namespace
|
||||
|
||||
std::optional<std::int64_t> parse_tuning_integer(std::string_view text) noexcept {
|
||||
const bool negative = !text.empty() && text.front() == '-';
|
||||
if (negative)
|
||||
text.remove_prefix(1);
|
||||
@@ -121,6 +122,36 @@ std::optional<std::int64_t> parse_integer(std::string_view text) noexcept {
|
||||
return negative ? -value : value;
|
||||
}
|
||||
|
||||
std::expected<std::vector<TuningLine>, CellularTuningError>
|
||||
read_tuning_lines(std::string_view text, std::size_t max_bytes) {
|
||||
if (text.size() > max_bytes)
|
||||
return std::unexpected{CellularTuningError{CellularTuningErrorKind::too_large, 0, {}}};
|
||||
std::vector<TuningLine> lines;
|
||||
std::size_t line_number{};
|
||||
while (!text.empty()) {
|
||||
++line_number;
|
||||
const auto end = text.find('\n');
|
||||
auto line = text.substr(0, end);
|
||||
text.remove_prefix(end == std::string_view::npos ? text.size() : end + 1);
|
||||
if (const auto comment = line.find('#'); comment != std::string_view::npos)
|
||||
line = line.substr(0, comment);
|
||||
line = trim(line);
|
||||
if (line.empty())
|
||||
continue;
|
||||
const auto equals = line.find('=');
|
||||
if (equals == std::string_view::npos)
|
||||
return std::unexpected{
|
||||
CellularTuningError{CellularTuningErrorKind::syntax, line_number, {}}};
|
||||
const auto key = trim(line.substr(0, equals));
|
||||
if (!valid_key(key))
|
||||
return std::unexpected{
|
||||
CellularTuningError{CellularTuningErrorKind::syntax, line_number, {}}};
|
||||
lines.push_back({line_number, key, trim(line.substr(equals + 1))});
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::optional<CellularTuningError> inconsistent(const CellularTuning& t) {
|
||||
const auto error = [](std::string_view key) {
|
||||
return CellularTuningError{CellularTuningErrorKind::inconsistent, 0, std::string{key}};
|
||||
@@ -144,30 +175,12 @@ std::optional<CellularTuningError> inconsistent(const CellularTuning& t) {
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularTuning, CellularTuningError> parse_cellular_tuning(std::string_view text) {
|
||||
if (text.size() > max_cellular_tuning_bytes)
|
||||
return std::unexpected{CellularTuningError{CellularTuningErrorKind::too_large, 0, {}}};
|
||||
const auto lines = read_tuning_lines(text, max_cellular_tuning_bytes);
|
||||
if (!lines)
|
||||
return std::unexpected{lines.error()};
|
||||
CellularTuning tuning{};
|
||||
std::array<bool, fields.size()> seen{};
|
||||
std::size_t line_number{};
|
||||
while (!text.empty()) {
|
||||
++line_number;
|
||||
const auto end = text.find('\n');
|
||||
auto line = text.substr(0, end);
|
||||
text.remove_prefix(end == std::string_view::npos ? text.size() : end + 1);
|
||||
if (const auto comment = line.find('#'); comment != std::string_view::npos)
|
||||
line = line.substr(0, comment);
|
||||
line = trim(line);
|
||||
if (line.empty())
|
||||
continue;
|
||||
const auto equals = line.find('=');
|
||||
if (equals == std::string_view::npos)
|
||||
return std::unexpected{
|
||||
CellularTuningError{CellularTuningErrorKind::syntax, line_number, {}}};
|
||||
const auto key = trim(line.substr(0, equals));
|
||||
const auto value_text = trim(line.substr(equals + 1));
|
||||
if (!valid_key(key))
|
||||
return std::unexpected{
|
||||
CellularTuningError{CellularTuningErrorKind::syntax, line_number, {}}};
|
||||
for (const auto& [line_number, key, value_text] : *lines) {
|
||||
std::size_t index{};
|
||||
while (index < fields.size() && fields.at(index).key != key)
|
||||
++index;
|
||||
@@ -177,7 +190,7 @@ std::expected<CellularTuning, CellularTuningError> parse_cellular_tuning(std::st
|
||||
if (seen.at(index))
|
||||
return std::unexpected{CellularTuningError{CellularTuningErrorKind::duplicate_key,
|
||||
line_number, std::string{key}}};
|
||||
const auto value = parse_integer(value_text);
|
||||
const auto value = parse_tuning_integer(value_text);
|
||||
if (!value)
|
||||
return std::unexpected{CellularTuningError{CellularTuningErrorKind::invalid_value,
|
||||
line_number, std::string{key}}};
|
||||
|
||||
@@ -26,6 +26,7 @@ constexpr std::uint64_t cellular_lives_v7_magic = 0x3730564C43564541ULL; // AEV
|
||||
constexpr std::uint64_t cellular_lives_v8_magic = 0x3830564C43564541ULL; // AEVCLV08
|
||||
constexpr std::uint64_t cellular_lives_v9_magic = 0x3930564C43564541ULL; // AEVCLV09
|
||||
constexpr std::uint64_t cellular_lives_v10_magic = 0x3031564C43564541ULL; // AEVCLV10
|
||||
constexpr std::uint64_t cellular_lives_v11_magic = 0x3131564C43564541ULL; // AEVCLV11
|
||||
constexpr std::size_t cellular_lives_header_bytes = 212;
|
||||
constexpr std::size_t cellular_site_bytes = 8;
|
||||
constexpr std::size_t cellular_life_v1_bytes = 168;
|
||||
@@ -51,6 +52,9 @@ constexpr std::size_t cellular_lives_v9_header_bytes = cellular_lives_v8_header_
|
||||
// its motion state (two velocities, heading, reserved, two carries), 20 bytes.
|
||||
constexpr std::size_t cellular_lives_v10_header_bytes = cellular_lives_v9_header_bytes + 88;
|
||||
constexpr std::size_t cellular_life_motion_bytes = 20;
|
||||
// Format 11 (rules 13, ADR-0179): the revamp rules, so far the essential upkeep share (4) and a
|
||||
// reserved zero (4). Later build phases extend this block before format 11 is deployed.
|
||||
constexpr std::size_t cellular_lives_v11_header_bytes = cellular_lives_v10_header_bytes + 8;
|
||||
|
||||
bool valid_motion(const biology::CellularMotionState& m) noexcept {
|
||||
constexpr std::int64_t carry = biology::cellular_velocity_q10_per_um_s * 1'000'000;
|
||||
@@ -174,11 +178,13 @@ bool subtract_inventory(accounting::Inventory& total, const accounting::Inventor
|
||||
|
||||
std::expected<biology::MetabolicBudget, CellularLivesError>
|
||||
budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life,
|
||||
std::uint32_t temperature) {
|
||||
std::uint32_t temperature,
|
||||
const biology::BodyTuning& body = biology::default_body_tuning) {
|
||||
const auto genome = archive.find(life.identity.genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}});
|
||||
const auto budget =
|
||||
biology::metabolic_budget(*genome, life.growth, {temperature, {}}, 1000, body);
|
||||
if (!budget)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
return *budget;
|
||||
@@ -609,6 +615,7 @@ struct CellularLives::State {
|
||||
CellularLight light{}; // Format 9 (ADR-0175).
|
||||
accounting::Picojoules captured{}; // Format 9: light credited to reserves so far.
|
||||
CellularLiquidRules liquid{}; // Format 10 (rules 12).
|
||||
CellularRevampRules revamp{}; // Format 11 (rules 13).
|
||||
std::shared_ptr<const biology::CellularFlowField> field; // Generated from `liquid`.
|
||||
|
||||
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||
@@ -967,6 +974,25 @@ CellularLives::liquid_upgrade_candidate(const CellularLiquidRules& rules) const
|
||||
const CellularLiquidRules& CellularLives::liquid_rules() const noexcept {
|
||||
return state_->liquid;
|
||||
}
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::revamp_upgrade_candidate(const CellularRevampRules& rules) const {
|
||||
if (state_->format_version != cellular_lives_liquid_format ||
|
||||
state_->rules_version != cellular_lives_liquid_rules)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (!cellular_revamp_valid(rules))
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
auto candidate = restore(snapshot(), world(), region(), frame(), tick());
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
auto& next = *(*candidate)->state_;
|
||||
next.format_version = cellular_lives_revamp_format;
|
||||
next.rules_version = cellular_lives_revamp_rules;
|
||||
next.revamp = rules;
|
||||
return candidate;
|
||||
}
|
||||
const CellularRevampRules& CellularLives::revamp_rules() const noexcept {
|
||||
return state_->revamp;
|
||||
}
|
||||
const biology::CellularFlowField* CellularLives::flow_field() const noexcept {
|
||||
return state_->field.get();
|
||||
}
|
||||
@@ -1011,7 +1037,9 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const bool presence = state_->format_version >= cellular_lives_presence_format;
|
||||
const bool inheritance = state_->format_version >= cellular_lives_inheritance_format;
|
||||
const bool lit = state_->format_version >= cellular_lives_light_format;
|
||||
const bool liquid = state_->format_version == cellular_lives_liquid_format;
|
||||
// Format 11 is format 10 with the revamp rules.
|
||||
const bool revamp = state_->format_version == cellular_lives_revamp_format;
|
||||
const bool liquid = revamp || state_->format_version == cellular_lives_liquid_format;
|
||||
const auto life_bytes = liquid ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
|
||||
: inheritance ? cellular_life_v6_bytes + 8
|
||||
: state_->format_version == cellular_lives_legacy_format
|
||||
@@ -1025,7 +1053,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
: causes ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const bool migration = state_->format_version >= cellular_lives_migration_format;
|
||||
const auto header_bytes = liquid ? cellular_lives_v10_header_bytes
|
||||
const auto header_bytes = revamp ? cellular_lives_v11_header_bytes
|
||||
: liquid ? cellular_lives_v10_header_bytes
|
||||
: lit ? cellular_lives_v9_header_bytes
|
||||
: inheritance ? cellular_lives_v8_header_bytes
|
||||
: migration ? cellular_lives_v7_header_bytes
|
||||
@@ -1055,6 +1084,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
|
||||
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
|
||||
: state_->format_version == cellular_lives_presence_format ? cellular_lives_v6_magic
|
||||
: revamp ? cellular_lives_v11_magic
|
||||
: liquid ? cellular_lives_v10_magic
|
||||
: lit ? cellular_lives_v9_magic
|
||||
: inheritance ? cellular_lives_v8_magic
|
||||
@@ -1121,6 +1151,10 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto cost = state_->liquid.thrust_cost_per_mille;
|
||||
out.put(cost == 1'000 ? 0 : cost, 4);
|
||||
}
|
||||
if (revamp) {
|
||||
out.put(state_->revamp.body.essential_upkeep_per_thousand, 4);
|
||||
out.put(0, 4);
|
||||
}
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
for (const auto& life : state_->lives) {
|
||||
@@ -1236,7 +1270,8 @@ CellularLives::perceive(std::uint64_t life_id) const {
|
||||
if (life == state_->lives.end())
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
const auto budget =
|
||||
budget_for(state_->archive, *life, state_->config.ambient_temperature_millikelvin);
|
||||
budget_for(state_->archive, *life, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!budget)
|
||||
return std::unexpected{budget.error()};
|
||||
auto view = state_->habitat->perceive(life->position, budget->phenotype.extent);
|
||||
@@ -1373,7 +1408,8 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
if (living.presence == CellularPresence::suspended)
|
||||
continue; // Out of the world (ADR-0128).
|
||||
const auto living_budget =
|
||||
budget_for(state_->archive, living, state_->config.ambient_temperature_millikelvin);
|
||||
budget_for(state_->archive, living, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!living_budget)
|
||||
return std::unexpected{living_budget.error()};
|
||||
const auto separated = [](std::uint64_t left, std::uint64_t right,
|
||||
@@ -1449,6 +1485,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
candidate->state_->captured = state_->captured;
|
||||
candidate->state_->liquid = state_->liquid;
|
||||
candidate->state_->field = state_->field;
|
||||
candidate->state_->revamp = state_->revamp;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
@@ -1473,9 +1510,13 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
// Format 6 carries rules 5, 6 (founder 2) or 7 (founder 3, ADR-0137); no new state.
|
||||
// Format 9 is exactly rules 11; format 8 exactly rules 10.
|
||||
// Format 10 is exactly rules 12.
|
||||
const bool liquid_version = magic == cellular_lives_v10_magic &&
|
||||
format_version == cellular_lives_liquid_format &&
|
||||
rules_version == cellular_lives_liquid_rules;
|
||||
// Format 11 is exactly rules 13, and carries everything format 10 does.
|
||||
const bool revamp_version = magic == cellular_lives_v11_magic &&
|
||||
format_version == cellular_lives_revamp_format &&
|
||||
rules_version == cellular_lives_revamp_rules;
|
||||
const bool liquid_version = revamp_version || (magic == cellular_lives_v10_magic &&
|
||||
format_version == cellular_lives_liquid_format &&
|
||||
rules_version == cellular_lives_liquid_rules);
|
||||
const bool light_version =
|
||||
liquid_version ||
|
||||
(magic == cellular_lives_v9_magic && format_version == cellular_lives_light_format &&
|
||||
@@ -1519,6 +1560,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected(CellularLivesError::format);
|
||||
if (liquid_version && bytes.size() < cellular_lives_v10_header_bytes + 8)
|
||||
return std::unexpected(CellularLivesError::format);
|
||||
if (revamp_version && bytes.size() < cellular_lives_v11_header_bytes + 8)
|
||||
return std::unexpected(CellularLivesError::format);
|
||||
const auto saved_world = in.get();
|
||||
const auto saved_region = in.get();
|
||||
const auto saved_frame = in.get();
|
||||
@@ -1616,6 +1659,12 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
field = std::move(*generated);
|
||||
}
|
||||
CellularRevampRules revamp{};
|
||||
if (revamp_version) {
|
||||
revamp.body.essential_upkeep_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
if (in.get(4) != 0 || !cellular_revamp_valid(revamp))
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
}
|
||||
const auto life_bytes = liquid_version ? cellular_life_v6_bytes + 8 + cellular_life_motion_bytes
|
||||
: inheritance_version ? cellular_life_v6_bytes + 8
|
||||
: legacy ? cellular_life_v1_bytes
|
||||
@@ -1626,7 +1675,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
: bud_version ? cellular_corpse_v5_bytes
|
||||
: engulf_version ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto expected_size = (liquid_version ? cellular_lives_v10_header_bytes
|
||||
const auto expected_size = (revamp_version ? cellular_lives_v11_header_bytes
|
||||
: liquid_version ? cellular_lives_v10_header_bytes
|
||||
: light_version ? cellular_lives_v9_header_bytes
|
||||
: inheritance_version ? cellular_lives_v8_header_bytes
|
||||
: migration_version ? cellular_lives_v7_header_bytes
|
||||
@@ -1850,6 +1900,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
restored->state_->captured = captured;
|
||||
restored->state_->liquid = liquid;
|
||||
restored->state_->field = std::move(field);
|
||||
restored->state_->revamp = revamp;
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return restored;
|
||||
@@ -1946,7 +1997,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
if (life.presence == CellularPresence::suspended)
|
||||
continue; // Out of the world: no step, no contact (ADR-0128).
|
||||
const auto budget =
|
||||
budget_for(state_->archive, life, state_->config.ambient_temperature_millikelvin);
|
||||
budget_for(state_->archive, life, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!budget)
|
||||
return std::unexpected{budget.error()};
|
||||
const auto genome = state_->archive.find(life.identity.genome);
|
||||
@@ -2007,8 +2059,9 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
found->mutation.fed_energy_credit_pj += acquired;
|
||||
if (!found->mutation.offer) {
|
||||
const auto budget = budget_for(next_archive, *found,
|
||||
state_->config.ambient_temperature_millikelvin);
|
||||
const auto budget =
|
||||
budget_for(next_archive, *found, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!budget ||
|
||||
budget->maintenance.value > std::numeric_limits<std::uint64_t>::max() /
|
||||
cellular_mutation_surplus_seconds)
|
||||
@@ -2052,7 +2105,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
!next_archive.record_death(found->identity.organism, {proposal->end_tick}))
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto corpse_budget =
|
||||
budget_for(next_archive, *found, state_->config.ambient_temperature_millikelvin);
|
||||
budget_for(next_archive, *found, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!corpse_budget)
|
||||
return std::unexpected{corpse_budget.error()};
|
||||
next_corpses.push_back(
|
||||
@@ -2180,7 +2234,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
const auto temperature = state_->config.ambient_temperature_millikelvin;
|
||||
const auto genome = next_archive.find(cellular_offspring_genome(*parent));
|
||||
const auto expressed = next_archive.find(parent->identity.genome);
|
||||
const auto parent_budget = budget_for(next_archive, *parent, temperature);
|
||||
const auto parent_budget =
|
||||
budget_for(next_archive, *parent, temperature, state_->revamp.body);
|
||||
if (!genome || !expressed || !parent_budget)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto transition = biology::fund_offspring(
|
||||
@@ -2261,7 +2316,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
life.presence = CellularPresence::suspended;
|
||||
} else if (life.presence == CellularPresence::suspended && !frozen) {
|
||||
const auto budget =
|
||||
budget_for(next_archive, life, state_->config.ambient_temperature_millikelvin);
|
||||
budget_for(next_archive, life, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!budget)
|
||||
return std::unexpected{budget.error()};
|
||||
const auto spot = resume_position(life, budget->phenotype.extent, next_lives,
|
||||
@@ -2321,7 +2377,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
other.presence == CellularPresence::suspended)
|
||||
continue;
|
||||
const auto obstacle =
|
||||
budget_for(next_archive, other, state_->config.ambient_temperature_millikelvin);
|
||||
budget_for(next_archive, other, state_->config.ambient_temperature_millikelvin,
|
||||
state_->revamp.body);
|
||||
if (!obstacle)
|
||||
return std::unexpected(obstacle.error());
|
||||
clear = separated(life.position.x_um, other.position.x_um, extent,
|
||||
@@ -2449,7 +2506,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
input->intent.frozen)
|
||||
continue;
|
||||
const auto budget =
|
||||
budget_for(next->archive, life, next->config.ambient_temperature_millikelvin);
|
||||
budget_for(next->archive, life, next->config.ambient_temperature_millikelvin,
|
||||
next->revamp.body);
|
||||
if (!budget)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
const auto energy =
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "evo/server/lifecycle/cellular_revamp.hpp"
|
||||
|
||||
namespace evo::server {
|
||||
bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept {
|
||||
return rules.body.essential_upkeep_per_thousand <= 1'000;
|
||||
}
|
||||
|
||||
CellularRevampRules cellular_revamp_rules(const biology::CellularRevampTuning& t) {
|
||||
return {.body = {.essential_upkeep_per_thousand =
|
||||
static_cast<std::uint32_t>(t.body_essential_upkeep_per_mille)}};
|
||||
}
|
||||
} // namespace evo::server
|
||||
@@ -120,8 +120,8 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
|
||||
const auto other_genome = state.archive().find(other.identity.genome);
|
||||
if (!other_genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto other_budget =
|
||||
biology::metabolic_budget(*other_genome, other.growth, {temperature, {}});
|
||||
const auto other_budget = biology::metabolic_budget(
|
||||
*other_genome, other.growth, {temperature, {}}, 1000, state.revamp_rules().body);
|
||||
const auto membrane = other_genome->parameter(biology::Capability::membrane);
|
||||
if (!other_budget || !membrane)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
@@ -136,7 +136,8 @@ wildlife_intents(const CellularLives& state, std::uint64_t tick) {
|
||||
const auto genome = state.archive().find(life.identity.genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}});
|
||||
const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}}, 1000,
|
||||
state.revamp_rules().body);
|
||||
if (!budget)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
biology::CellularStepIntent intent{};
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
#include "evo/server/lifecycle/cellular_batch.hpp"
|
||||
#include "fixtures/cellular_ecology.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
// Rules 13 owner (Cellular_Revamp_Build_Plan.md phase 1, ADR-0179): the format 10 -> 11 upgrade,
|
||||
// founder 6 births, the declared revamp rules and exact restore and replay.
|
||||
namespace {
|
||||
using namespace evo;
|
||||
using namespace evo::server;
|
||||
using namespace evo::fixture;
|
||||
|
||||
CellularLiquidRules liquid() {
|
||||
return {.currents = {5, 48'000, 48'000, 250, 2, 4, 3'000, 6'000, 60, 300},
|
||||
.founder = {.thrust_q10 = 20'480,
|
||||
.reverse_thrust_q10 = 20'480,
|
||||
.forward_drag_ppm = 95'163,
|
||||
.lateral_drag_ppm = 95'163,
|
||||
.turn = 3'276},
|
||||
.deposits = {.drift_radius_um = 200,
|
||||
.max_drift_q10 = 100 * 1'024,
|
||||
.drift_decay_ppm = 95'163,
|
||||
.merged_cap = 8,
|
||||
.bubble_radius_um = biology::cellular_mote_radius_um}};
|
||||
}
|
||||
|
||||
CellularRevampRules revamp(std::uint32_t essential = 250) {
|
||||
return {.body = {.essential_upkeep_per_thousand = essential}};
|
||||
}
|
||||
|
||||
LifeAccount account(std::uint8_t last) {
|
||||
LifeAccount value{};
|
||||
value.back() = last;
|
||||
return value;
|
||||
}
|
||||
|
||||
// A rules-13 region with a founder 5 born before the upgrade and a founder 6 born after it.
|
||||
Owner revamp_region(std::uint32_t essential = 250) {
|
||||
auto state = opening(7, true, 2, false); // Funds two founders.
|
||||
checked(state->advance_tick({}), "genesis tick");
|
||||
state = checked(state->migration_upgrade_candidate(), "boundary upgrade");
|
||||
state = checked(state->maturation_upgrade_candidate(), "maturation upgrade");
|
||||
state = checked(state->inheritance_upgrade_candidate(), "inheritance upgrade");
|
||||
state = checked(state->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade");
|
||||
require(!state->revamp_upgrade_candidate(revamp()), "revamp upgrade skipped rules 12");
|
||||
state = checked(state->liquid_upgrade_candidate(liquid()), "liquid upgrade");
|
||||
state = checked(state->birth_candidate({account(1), 1}, {100, 100, 100, 100, {state->tick()}}),
|
||||
"founder 5 birth");
|
||||
require(!state->revamp_upgrade_candidate(revamp(1'001)), "an invalid essential share accepted");
|
||||
state = checked(state->revamp_upgrade_candidate(revamp(essential)), "revamp upgrade");
|
||||
require(state->format_version() == cellular_lives_revamp_format &&
|
||||
state->rules_version() == cellular_lives_revamp_rules &&
|
||||
state->revamp_rules() == revamp(essential) && state->liquid_rules() == liquid() &&
|
||||
state->flow_field() != nullptr && !state->revamp_upgrade_candidate(revamp()),
|
||||
"revamp upgrade repeated, wrong versions or lost rules");
|
||||
state = checked(state->birth_candidate({account(2), 1}, {101, 101, 101, 101, {state->tick()}}),
|
||||
"founder 6 birth");
|
||||
const auto old = state->archive().find(100);
|
||||
const auto fresh = state->archive().find(101);
|
||||
require(old && old->schema_version() == biology::genome_schema_v6,
|
||||
"the founder 5 life lost its genome in the upgrade");
|
||||
require(fresh && fresh->schema_version() == biology::genome_schema_v7 &&
|
||||
fresh->development_version() == 7 &&
|
||||
fresh->cellular_traits() == biology::founder_cellular_traits,
|
||||
"a rules-13 founder is not founder 6");
|
||||
return state;
|
||||
}
|
||||
|
||||
std::vector<CellularLifeIntent> swim(const CellularLives& state, biology::CellularIntent movement) {
|
||||
std::vector<CellularLifeIntent> result;
|
||||
for (const auto& life : state.lives()) {
|
||||
CellularLifeIntent input{life.identity.life, {}};
|
||||
input.intent.movement = movement;
|
||||
result.push_back(input);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void closed(const CellularLives& state) {
|
||||
const auto& ledger = state.boundary_ledger();
|
||||
auto expected = state.opening_total();
|
||||
expected.energy.value += ledger.incoming.energy.value - ledger.outgoing.energy.value;
|
||||
for (std::size_t channel = 0; channel < expected.matter.size(); ++channel)
|
||||
expected.matter[channel].value +=
|
||||
ledger.incoming.matter[channel].value - ledger.outgoing.matter[channel].value;
|
||||
require(state.current_total() == expected, "revamp region broke closure");
|
||||
}
|
||||
|
||||
void upgrade_and_restore() {
|
||||
const auto state = revamp_region(400);
|
||||
const auto copy = restore(*state);
|
||||
require(copy->snapshot() == state->snapshot() && copy->revamp_rules() == revamp(400) &&
|
||||
copy->liquid_rules() == liquid(),
|
||||
"format 11 did not restore exactly");
|
||||
// A tampered rules block is refused rather than read as another share.
|
||||
auto bytes = state->snapshot();
|
||||
require(bytes.size() > 8, "empty snapshot");
|
||||
bytes[8] = 10; // Format field: format 10 bytes with the format 11 magic.
|
||||
require(!CellularLives::restore(bytes, state->world(), state->region(), state->frame(),
|
||||
state->tick()),
|
||||
"a format 11 checkpoint restored as format 10");
|
||||
}
|
||||
|
||||
// Both generations live and pay their way in one region, and restore continues exactly.
|
||||
void mixed_generations_tick_and_continue() {
|
||||
auto continuous = revamp_region();
|
||||
for (int tick = 0; tick < 30; ++tick)
|
||||
checked(continuous->advance_tick(swim(*continuous, {600, 800})), "continuous tick");
|
||||
require(continuous->lives().size() == 2, "a life died in thirty ticks");
|
||||
closed(*continuous);
|
||||
auto resumed = restore(*continuous);
|
||||
for (int tick = 0; tick < 30; ++tick) {
|
||||
checked(continuous->advance_tick(swim(*continuous, {-700, 300})), "continuous tick");
|
||||
checked(resumed->advance_tick(swim(*resumed, {-700, 300})), "resumed tick");
|
||||
}
|
||||
require(continuous->snapshot() == resumed->snapshot(), "restore diverged from a live run");
|
||||
closed(*continuous);
|
||||
}
|
||||
|
||||
// A recorded trace replays to the exact live result under rules 13.
|
||||
void trace_replays() {
|
||||
auto live = revamp_region();
|
||||
const auto base = restore(*live);
|
||||
CellularBatch batch;
|
||||
batch.format_version = cellular_batch_inheritance_format;
|
||||
batch.header = {live->world(), live->region(), live->frame(), {}, {}, live->tick(), 0};
|
||||
batch.header.predecessor.fill(1);
|
||||
batch.header.predecessor_digest.fill(2);
|
||||
for (int tick = 0; tick < 24; ++tick) {
|
||||
const auto intents = swim(*live, {static_cast<std::int16_t>(tick < 12 ? 1'000 : -400), 0});
|
||||
checked(live->advance_tick(intents), "recorded tick");
|
||||
batch.ticks.push_back({intents, {}, {}});
|
||||
}
|
||||
batch.header.end_tick = live->tick();
|
||||
const auto bytes = checked(encode_cellular_batch(batch), "trace encodes");
|
||||
const auto decoded = checked(decode_cellular_batch(bytes), "trace decodes");
|
||||
const auto replayed = checked(replay_cellular_batch(*base, decoded), "trace replays");
|
||||
require(replayed->snapshot() == live->snapshot(), "replay diverged from the live run");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() try {
|
||||
upgrade_and_restore();
|
||||
mixed_generations_tick_and_continue();
|
||||
trace_replays();
|
||||
std::puts("PASS rules 13 owner: upgrade, founder 6, restore and replay");
|
||||
return 0;
|
||||
} catch (...) {
|
||||
std::puts("FAIL rules 13 owner");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
// The rules-13 revamp tuning file (ADR-0179): same format and refusals as the rules-12 file,
|
||||
// with its own keys.
|
||||
namespace {
|
||||
using namespace evo::biology;
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition)
|
||||
throw std::runtime_error("cellular revamp tuning line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(expression) check(static_cast<bool>(expression))
|
||||
|
||||
CellularRevampTuning trial() {
|
||||
return {.version = 1, .body_essential_upkeep_per_mille = 250};
|
||||
}
|
||||
|
||||
std::string with(std::string text, const std::string& key, const std::string& value) {
|
||||
const auto at = text.find(key + " = ");
|
||||
check(at != std::string::npos);
|
||||
const auto end = text.find('\n', at);
|
||||
return text.replace(at, end - at, key + " = " + value);
|
||||
}
|
||||
|
||||
CellularTuningErrorKind error_of(const std::string& text) {
|
||||
const auto parsed = parse_cellular_revamp_tuning(text);
|
||||
check(!parsed.has_value());
|
||||
return parsed.error().kind;
|
||||
}
|
||||
|
||||
void canonical_round_trip() {
|
||||
const auto text = encode_cellular_revamp_tuning(trial());
|
||||
const auto parsed = parse_cellular_revamp_tuning(text);
|
||||
CHECK(parsed && *parsed == trial());
|
||||
CHECK(encode_cellular_revamp_tuning(*parsed) == text);
|
||||
const auto commented =
|
||||
"# heading\n\n" + with(text, "body.essential_upkeep_per_mille", "2_50 # c");
|
||||
CHECK(parse_cellular_revamp_tuning(commented) == trial());
|
||||
}
|
||||
|
||||
void refusals() {
|
||||
const auto base = encode_cellular_revamp_tuning(trial());
|
||||
CHECK(error_of(base + "unknown.key = 1\n") == CellularTuningErrorKind::unknown_key);
|
||||
CHECK(error_of(base + "tuning.version = 1\n") == CellularTuningErrorKind::duplicate_key);
|
||||
CHECK(error_of(base + "no equals sign\n") == CellularTuningErrorKind::syntax);
|
||||
CHECK(error_of(with(base, "body.essential_upkeep_per_mille", "1001")) ==
|
||||
CellularTuningErrorKind::out_of_range);
|
||||
CHECK(error_of(with(base, "body.essential_upkeep_per_mille", "x")) ==
|
||||
CellularTuningErrorKind::invalid_value);
|
||||
CHECK(error_of(with(base, "tuning.version", "2")) ==
|
||||
CellularTuningErrorKind::unsupported_version);
|
||||
auto missing = base;
|
||||
missing.erase(missing.find("body.essential_upkeep_per_mille"));
|
||||
const auto parsed = parse_cellular_revamp_tuning(missing);
|
||||
CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key &&
|
||||
parsed.error().key == "body.essential_upkeep_per_mille");
|
||||
// Rules-12 keys belong in the other file.
|
||||
CHECK(error_of(base + "region.width_um = 60000\n") == CellularTuningErrorKind::unknown_key);
|
||||
CHECK(error_of(std::string(max_cellular_revamp_tuning_bytes + 1, '#')) ==
|
||||
CellularTuningErrorKind::too_large);
|
||||
}
|
||||
|
||||
// The checked-in DEV revamp tuning file must parse; CTest passes its path when it is present.
|
||||
void repository_file(const char* path) {
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
CHECK(file.good());
|
||||
const std::string text{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
|
||||
const auto parsed = parse_cellular_revamp_tuning(text);
|
||||
if (!parsed)
|
||||
std::fprintf(stderr, "%s: line %zu %s: %s\n", path, parsed.error().line,
|
||||
parsed.error().key.c_str(),
|
||||
std::string{describe(parsed.error().kind)}.c_str());
|
||||
CHECK(parsed && *parsed == trial());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
try {
|
||||
canonical_round_trip();
|
||||
refusals();
|
||||
if (argc > 1)
|
||||
repository_file(argv[1]);
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
std::puts("PASS cellular revamp tuning file");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user