From 17fe23e5f0a7d3af915533306f28650daaef8fba Mon Sep 17 00:00:00 2001 From: dingo Date: Fri, 9 Oct 2026 13:16:55 -0400 Subject: [PATCH] M11.1: genome schema 2 with per-second rates (ADR-0099, segment 2) Movement and ingestion are stored per second in schema 2 (ranges x20). Schema 1 records stay valid, are never rewritten, and read as exactly x20 via Genome::per_second; drafts default to schema 1 so existing literals keep their meaning. Development version 1 converts genome rates at its own 50 ms quantum (round down), so phenotypes are unchanged. Mutation keeps the parent's schema and step; the lineage archive header names exactly the highest schema held (all-schema-1 archives keep their bytes); the development-v1 opening names schema 1. No golden changed; two tests that used 2 as an unknown schema now use the next one. Refresh the dev session window. Co-Authored-By: Claude Opus 5.5 --- .../region/dev-session-route/listener.yaml | 2 +- docs/STATUS.md | 8 +- docs/adr/0099-time-based-biological-rates.md | 21 +++++ docs/biology/GENOME_MODEL.md | 8 ++ docs/engineering/VALIDATION.md | 27 ++++++ include/evo/biology/development.hpp | 3 + include/evo/biology/genome.hpp | 52 +++++++++-- schemas/genome-v2.md | 39 ++++++++ src/biology/development.cpp | 18 +++- src/biology/genome.cpp | 52 ++++++++--- src/biology/lineage.cpp | 17 +++- src/biology/mutation.cpp | 16 ++-- src/processes/development_opening.cpp | 1 + tests/development_tests.cpp | 25 ++++++ tests/genome_tests.cpp | 89 ++++++++++++++----- tests/lineage_tests.cpp | 28 +++++- tests/mutation_tests.cpp | 35 +++++++- 17 files changed, 385 insertions(+), 56 deletions(-) create mode 100644 schemas/genome-v2.md diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 7ba83ae..c3b92ae 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -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-20261009T164435-675a:1791578075"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261009T171039-559d:1791579639"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/STATUS.md b/docs/STATUS.md index a072151..620c952 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -20,8 +20,12 @@ the maintainer chose directional rounding): - **Segment 1 (done):** `accounting/rates.hpp` converts a per-second rate to a per-tick amount without saved state. Costs round up and capacities down, so rounding never favours the organism. The migration's exact ×20 inverse refuses inexact values. -- **Segments 2–3 (next):** genome schema v2, development version 2, and the process and - checkpoint fields, with unchanged 50 ms goldens. +- **Segment 2 (done):** genome schema 2 stores rates per second. Schema 1 records are + read ×20 and never rewritten. Development version 1 converts at its own 50 ms + quantum, mutation keeps a lineage's schema, and an archive of schema 1 records keeps + its bytes. No behaviour or golden changed. +- **Segment 3 (next):** development outputs, feeding and decay limits per second, + converted at each process's tick. Open from M10 (BACKLOG): - the maintainer's native-client observation, moved to the later 2k drill; diff --git a/docs/adr/0099-time-based-biological-rates.md b/docs/adr/0099-time-based-biological-rates.md index 098c12b..5726259 100644 --- a/docs/adr/0099-time-based-biological-rates.md +++ b/docs/adr/0099-time-based-biological-rates.md @@ -77,6 +77,27 @@ contract and a versioned migration that preserves 50 ms behaviour. or not at all, would rescale biology. The goldens at 50 ms are the guard against this. +## Segment 2 as implemented (2026-10-09) +- **Genome schema 2** ([genome-v2.md](../../schemas/genome-v2.md)): movement and + ingestion are per second, with ranges ×20. `Genome::per_second` reads either schema. + A schema 1 rate reads as exactly ×20. +- **Drafts** default to schema 1, so every existing literal keeps its per-tick + meaning. New records name schema 2. +- **Development version 1 stays defined in 50 ms ticks** + (`development_v1_tick_microseconds`). It reads genome rates per second and converts + them at that quantum, rounding down (capacities). Its per-tick outputs are unchanged. + A schema 1 genome and its exact schema 2 translation develop identically. + Development's outputs move to per-second rates in segment 3, at the consumers. +- **Mutation** keeps the parent's schema and step. Mutation offers are seeded from the + genome's digest, so migrating a lineage's records would change every later offer; + no lineage is migrated. +- **The lineage archive** header names exactly the highest schema held. An all-schema-1 + archive, which is what dev worlds hold, keeps its bytes. +- **The development opening** (`development-v1`) names schema 1 explicitly; it is a + fixed genesis. +- **Tests that used 2 as an unknown future schema** now use the next unknown one. The + archive decoder refuses a header that misstates its contents. + ## Consequences - Segment 1: `accounting/rates.hpp` (`per_tick`, `exact_per_tick`, `per_second_from_per_tick`) and the `rate-contracts` test. diff --git a/docs/biology/GENOME_MODEL.md b/docs/biology/GENOME_MODEL.md index e72a8f7..74d3983 100644 --- a/docs/biology/GENOME_MODEL.md +++ b/docs/biology/GENOME_MODEL.md @@ -116,6 +116,14 @@ viable organism: validation proves neither developmental feasibility nor biological plausibility, and the schema ranges are expressible bounds rather than balance. Fitness remains an outcome, not a score held in the record. +### Schema 2 (M11.1, ADR-0099) +Schema 2 states the movement and ingestion rates per second (×20 ranges), so biology +is no longer tied to the 50 ms tick. Schema 1 records stay valid and are never +rewritten. They read as exactly ×20 through `Genome::per_second`. Drafts default to +schema 1. Mutation keeps a lineage's schema. Development version 1 converts rates +back to its own 50 ms quantum, so nothing behaves differently. +See [genome-v2.md](../../schemas/genome-v2.md). + ## Implemented M3.2 development and phenotype [ADR-0031](../adr/0031-development-and-phenotype.md) adds `derive_phenotype` and diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index b4aba55..a735c4c 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -5372,3 +5372,30 @@ Executed: - Windows `debug`: 61/61. No biology uses the conversion yet, so no golden is affected. + +## 2026-10-09 — M11.1 time-based rates, segment 2: genome schema 2 (ADR-0099) + +Genome schema 2 stores movement and ingestion per second (ranges ×20; +`schemas/genome-v2.md`). Schema 1 records stay valid and read as exactly ×20, and drafts +default to schema 1. Development version 1 converts genome rates at its 50 ms quantum. +Mutation keeps the parent's schema and step. The lineage archive header names exactly +the highest schema held, and the development opening names schema 1. + +New tests: +- `schema_v2_rates` (genome); +- range boundaries per schema; +- `schemas_mix_without_rewriting` (archive); +- `schema_v2_rates_develop_identically`: phenotypes and plans equal across schemas, and + sub-tick rates round down; +- `successors_keep_their_parents_schema` (mutation). + +Two existing tests used 2 as "an unknown future schema". They now use the next unknown +schema, and the archive decoder now refuses a header that misstates its contents. No +golden changed: the frozen schema 1 record and its digest are byte-identical. + +Executed: +- WSL `linux-debug` and `linux-asan`: 61/61 each. +- clang-tidy 19 on the changed sources and tests: clean. +- The 18 Postgres suites passed, including region upgrade and clone comparison, so + checkpoints are unchanged. +- Windows `debug`: 61/61. diff --git a/include/evo/biology/development.hpp b/include/evo/biology/development.hpp index a960fad..1f84371 100644 --- a/include/evo/biology/development.hpp +++ b/include/evo/biology/development.hpp @@ -19,6 +19,9 @@ struct EnvironmentState { accounting::Inventory available{}; auto operator<=>(const EnvironmentState&) const = default; }; +// Development version 1 is defined in ticks of this quantum: its per-tick constants and +// outputs, and the genome rates it reads (converted from per second, ADR-0099). +inline constexpr std::uint64_t development_v1_tick_microseconds = 50'000; // Chosen constants, reviewable as a unit and selected by the genome's // development version. Declared model parameters, not balance. struct DevelopmentRules { diff --git a/include/evo/biology/genome.hpp b/include/evo/biology/genome.hpp index e73fa93..c967088 100644 --- a/include/evo/biology/genome.hpp +++ b/include/evo/biology/genome.hpp @@ -25,10 +25,12 @@ inline constexpr std::size_t capability_count = 7; enum class Unit : std::uint8_t { per_thousand, micrometres, - micrometres_per_tick, + micrometres_per_tick, // Schema 1: per 50 ms tick. femtograms, - femtograms_per_tick, - millikelvin + femtograms_per_tick, // Schema 1: per 50 ms tick. + millikelvin, + micrometres_per_second, // Schema 2 (ADR-0099). + femtograms_per_second // Schema 2 (ADR-0099). }; struct CapabilitySpec { Unit unit; @@ -42,8 +44,8 @@ struct CapabilitySpec { return 1U << static_cast(kind); } // Chosen schema constants, reviewable as a unit. These are ranges of expressible -// capability, not balance, stats or a power score. -inline constexpr std::array capability_schema{ +// capability, not balance, stats or a power score. Schema 1 states rates per 50 ms tick. +inline constexpr std::array capability_schema_v1{ CapabilitySpec{Unit::per_thousand, 1, 1000, true, 0}, CapabilitySpec{Unit::micrometres, 1, 10'000, true, 0}, CapabilitySpec{Unit::micrometres_per_tick, 0, 100'000, false, @@ -54,6 +56,23 @@ inline constexpr std::array capability_schema{ capability_bit(Capability::energy_conversion)}, CapabilitySpec{Unit::femtograms, 1, 1'000'000'000, true, 0}, CapabilitySpec{Unit::millikelvin, 1, 100'000, false, 0}}; +// Schema 2 (ADR-0099): the same capabilities with rates per second. Each rate range is +// schema 1's times 20, so every schema 1 value has an exact schema 2 equivalent. +inline constexpr std::array capability_schema_v2{ + CapabilitySpec{Unit::per_thousand, 1, 1000, true, 0}, + CapabilitySpec{Unit::micrometres, 1, 10'000, true, 0}, + CapabilitySpec{Unit::micrometres_per_second, 0, 2'000'000, false, + capability_bit(Capability::membrane)}, + CapabilitySpec{Unit::femtograms_per_second, 0, 20'000'000, false, + capability_bit(Capability::membrane)}, + CapabilitySpec{Unit::per_thousand, 1, 1000, false, + capability_bit(Capability::energy_conversion)}, + CapabilitySpec{Unit::femtograms, 1, 1'000'000'000, true, 0}, + CapabilitySpec{Unit::millikelvin, 1, 100'000, false, 0}}; +// The current schema's table. +inline constexpr const auto& capability_schema = capability_schema_v2; +// Schema 1 rates are per tick of this quantum. +inline constexpr std::uint64_t schema_v1_tick_microseconds = 50'000; struct CapabilityRecord { Capability kind{}; @@ -69,6 +88,10 @@ struct GenomeDraft { std::uint32_t development_version{}; std::vector parents; std::vector capabilities; + // The schema the parameters are written in. Schema 1 is the default so an existing + // literal keeps its meaning (rates per 50 ms tick); new records name schema 2. Last, + // so positional initialisers are unaffected. + std::uint32_t schema_version{1}; }; enum class GenomeError : std::uint8_t { unsupported_version, @@ -83,7 +106,16 @@ enum class GenomeError : std::uint8_t { digest_mismatch, invalid_encoding }; -inline constexpr std::uint32_t genome_schema_version = 1; +// The current schema. Schema 1 records stay valid and are never rewritten (ADR-0099). +inline constexpr std::uint32_t genome_schema_version = 2; +inline constexpr std::uint32_t genome_schema_v1 = 1; +// The capability table of a supported schema (1 or 2); nullptr otherwise. +[[nodiscard]] constexpr const std::array* +capability_schema_for(std::uint32_t schema) noexcept { + return schema == genome_schema_v1 ? &capability_schema_v1 + : schema == genome_schema_version ? &capability_schema_v2 + : nullptr; +} inline constexpr std::size_t max_parents = 2; inline constexpr std::size_t max_genome_bytes = 256; @@ -108,7 +140,7 @@ class Genome { return origin_event_; } [[nodiscard]] std::uint32_t schema_version() const noexcept { - return genome_schema_version; + return schema_version_; } [[nodiscard]] std::uint32_t development_version() const noexcept { return development_version_; @@ -124,6 +156,11 @@ class Genome { [[nodiscard]] bool carries(Capability kind) const noexcept; [[nodiscard]] std::expected parameter(Capability kind) const noexcept; + // A rate capability (movement, ingestion) per second, whatever the schema: a + // schema 1 per-tick value times 20, exactly. Other capabilities are returned as + // stored. + [[nodiscard]] std::expected + per_second(Capability kind) const noexcept; // Covers identity and ancestry, so distinct origins never share a digest. // An integrity and deduplication aid, never a content key and never authenticity. [[nodiscard]] std::uint64_t content_digest() const noexcept { @@ -134,6 +171,7 @@ class Genome { private: Genome() = default; [[nodiscard]] std::vector body() const; + std::uint32_t schema_version_{genome_schema_v1}; std::uint64_t id_{}; std::uint64_t origin_event_{}; std::uint32_t development_version_{}; diff --git a/schemas/genome-v2.md b/schemas/genome-v2.md new file mode 100644 index 0000000..c38f81c --- /dev/null +++ b/schemas/genome-v2.md @@ -0,0 +1,39 @@ +# Genome record, schema version 2 + +Scope: schema 2 of the immutable genome record ([genome-v1.md](genome-v1.md), +[ADR-0030](../docs/adr/0030-immutable-genome-record.md)). It changes only the units of +the two rate capabilities, per [ADR-0099](../docs/adr/0099-time-based-biological-rates.md). +Its encoder and decoder are `src/biology/genome.cpp`, which reads schemas 1 and 2. + +## Layout +The layout is byte for byte that of schema 1, except offset 8, which holds 2. The +magic stays `AEVOGEN1`; the schema field, not the magic, selects the capability table. +The digest and checksum rules are unchanged. The digest covers the schema field, so a +schema 1 record and its schema 2 translation are distinct records with distinct +digests. + +## Capability table + +| Kind | Unit (schema 2) | Range | Schema 1 unit and range | +|---|---|---|---| +| energy conversion | per thousand | 1-1000, required | same | +| membrane | micrometres | 1-10,000, required | same | +| movement | **micrometres per second** | 0-2,000,000 | um per 50 ms tick, 0-100,000 | +| ingestion | **femtograms per second** | 0-20,000,000 | fg per 50 ms tick, 0-1,000,000 | +| reproduction | per thousand | 1-1000 | same | +| target size | femtograms | 1-1,000,000,000, required | same | +| thermal tolerance | millikelvin | 1-100,000 | same | + +Each rate range is schema 1's times 20, so every schema 1 value has an exact schema 2 +equivalent. + +## Compatibility +- **Schema 1 records** stay valid and are never rewritten. `Genome::per_second` reads + a schema 1 rate as exactly 20 times its per-tick value. +- **Drafts** default to schema 1, so existing literals keep their meaning. New + records name schema 2. +- **Mutation** keeps the parent's schema and that schema's adjustment step, one + hundredth of the range. +- **Lineage archives** name the highest schema they hold. An archive of schema 1 + records keeps its exact bytes. +- **Unknown schemas** fail closed. diff --git a/src/biology/development.cpp b/src/biology/development.cpp index b2e069b..92ebdc3 100644 --- a/src/biology/development.cpp +++ b/src/biology/development.cpp @@ -1,5 +1,7 @@ #include "evo/biology/development.hpp" +#include "evo/accounting/rates.hpp" + #include #include @@ -96,13 +98,23 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi phenotype.conversion_per_thousand = static_cast(*genome.parameter(Capability::energy_conversion)); // A capability the genome does not carry yields nothing, whatever the size. + // Development version 1 is defined in 50 ms ticks: genome rates (per second) enter it + // at that quantum, rounded down as capacities (ADR-0099; exact for schema 1 records). + const auto rate_per_tick = [](std::uint64_t per_second) { + return accounting::per_tick(per_second, development_v1_tick_microseconds, + accounting::Rounding::down); + }; if (genome.carries(Capability::movement)) { - phenotype.movement_speed = - static_cast(*genome.parameter(Capability::movement)); + const auto speed = rate_per_tick(*genome.per_second(Capability::movement)); + if (!speed || *speed > std::numeric_limits::max()) { + return std::unexpected{DevelopmentError::overflow}; + } + phenotype.movement_speed = static_cast(*speed); } if (genome.carries(Capability::ingestion)) { + const auto intake = rate_per_tick(*genome.per_second(Capability::ingestion)); std::uint64_t uptake{}; - if (!multiply(uptake, *genome.parameter(Capability::ingestion), phenotype.surface)) { + if (!intake || !multiply(uptake, *intake, phenotype.surface)) { return std::unexpected{DevelopmentError::overflow}; } phenotype.ingestion_rate = uptake / rules->reference_surface; diff --git a/src/biology/genome.cpp b/src/biology/genome.cpp index 4207475..106259d 100644 --- a/src/biology/genome.cpp +++ b/src/biology/genome.cpp @@ -1,5 +1,7 @@ #include "evo/biology/genome.hpp" +#include "evo/accounting/rates.hpp" + #include namespace evo::biology { @@ -57,14 +59,17 @@ class Reader { constexpr std::uint32_t every_capability = (1U << capability_count) - 1U; -const CapabilitySpec& spec(Capability kind) noexcept { - return capability_schema[static_cast(kind)]; -} - -std::expected validate(std::uint64_t id, std::uint64_t origin, - std::uint32_t development, +std::expected validate(std::uint32_t schema, std::uint64_t id, + std::uint64_t origin, std::uint32_t development, std::span parents, std::span capabilities) { + const auto* table = capability_schema_for(schema); + if (table == nullptr) { + return std::unexpected{GenomeError::unsupported_version}; + } + const auto spec = [table](Capability kind) -> const CapabilitySpec& { + return (*table)[static_cast(kind)]; + }; if (id == 0 || origin == 0) { return std::unexpected{GenomeError::invalid_identity}; } @@ -112,7 +117,7 @@ std::expected validate(std::uint64_t id, std::uint64_t origin } for (std::size_t index = 0; index < capability_count; ++index) { const auto kind = static_cast(index); - if (capability_schema[index].required && (present & capability_bit(kind)) == 0) { + if ((*table)[index].required && (present & capability_bit(kind)) == 0) { return std::unexpected{GenomeError::missing_capability}; } } @@ -153,7 +158,7 @@ std::vector Genome::body() const { std::vector bytes; bytes.reserve(max_genome_bytes); bytes.insert(bytes.end(), magic.begin(), magic.end()); - append(bytes, genome_schema_version, 4); + append(bytes, schema_version_, 4); append(bytes, development_version_, 4); append(bytes, id_, 8); append(bytes, origin_event_, 8); @@ -184,12 +189,13 @@ std::expected Genome::create(const GenomeDraft& draft) { if (draft.parents.size() > max_parents) { return std::unexpected{GenomeError::invalid_ancestry}; } - if (const auto valid = validate(draft.id, draft.origin_event, draft.development_version, - draft.parents, ordered); + if (const auto valid = validate(draft.schema_version, draft.id, draft.origin_event, + draft.development_version, draft.parents, ordered); !valid) { return std::unexpected{valid.error()}; } Genome genome; + genome.schema_version_ = draft.schema_version; genome.id_ = draft.id; genome.origin_event_ = draft.origin_event; genome.development_version_ = draft.development_version; @@ -209,7 +215,8 @@ std::expected Genome::decode(std::span } Reader reader{bytes}; static_cast(reader.read(magic.size())); - if (reader.read(4) != genome_schema_version) { + const auto schema = static_cast(reader.read(4)); + if (capability_schema_for(schema) == nullptr) { return std::unexpected{GenomeError::unsupported_version}; } const auto development = static_cast(reader.read(4)); @@ -254,10 +261,12 @@ std::expected Genome::decode(std::span const auto prerequisites = static_cast(reader.read(4)); capabilities.push_back({static_cast(kind), parameter, prerequisites}); } - if (const auto valid = validate(id, origin, development, parents, capabilities); !valid) { + if (const auto valid = validate(schema, id, origin, development, parents, capabilities); + !valid) { return std::unexpected{valid.error()}; } Genome genome; + genome.schema_version_ = schema; genome.id_ = id; genome.origin_event_ = origin; genome.development_version_ = development; @@ -285,4 +294,23 @@ std::expected Genome::parameter(Capability kind) con } return std::unexpected{GenomeError::missing_capability}; } + +std::expected Genome::per_second(Capability kind) const noexcept { + const auto stored = parameter(kind); + if (!stored) { + return std::unexpected{stored.error()}; + } + const auto unit = capability_schema_v1[static_cast(kind)].unit; + const bool per_tick = unit == Unit::micrometres_per_tick || unit == Unit::femtograms_per_tick; + if (schema_version_ == genome_schema_v1 && per_tick) { + // Exact: a 50 ms tick is a twentieth of a second (ADR-0099). + const auto rate = + accounting::per_second_from_per_tick(*stored, schema_v1_tick_microseconds); + if (!rate) { + return std::unexpected{GenomeError::parameter_out_of_range}; + } + return *rate; + } + return std::uint64_t{*stored}; +} } // namespace evo::biology diff --git a/src/biology/lineage.cpp b/src/biology/lineage.cpp index a665095..e941811 100644 --- a/src/biology/lineage.cpp +++ b/src/biology/lineage.cpp @@ -234,7 +234,11 @@ std::vector LineageArchive::encode() const { (organisms_.size() * archived_organism_bytes) + 8); put(out, archive_magic, 8); put(out, lineage_archive_format, 4); - put(out, genome_schema_version, 4); + // The highest genome schema held: an archive of schema 1 records keeps its bytes. + std::uint32_t schema = genome_schema_v1; + for (const auto& g : genomes_) + schema = std::max(schema, g.schema_version()); + put(out, schema, 4); put(out, genomes_.size(), 4); put(out, organisms_.size(), 4); put(out, 0, 8); // Total length, patched below. @@ -261,7 +265,9 @@ std::expected LineageArchive::decode(std::span bytes) { if (bytes.size() < archive_header_bytes + 8 || get(bytes, 0, 8) != archive_magic) return std::unexpected{ArchiveCodecError::format}; - if (get(bytes, 8, 4) != lineage_archive_format || get(bytes, 12, 4) != genome_schema_version) + const auto schema = get(bytes, 12, 4); + if (get(bytes, 8, 4) != lineage_archive_format || schema < genome_schema_v1 || + schema > genome_schema_version) return std::unexpected{ArchiveCodecError::version}; const auto genomes = get(bytes, 16, 4); const auto organisms = get(bytes, 20, 4); @@ -275,6 +281,7 @@ LineageArchive::decode(std::span bytes) { return std::unexpected{ArchiveCodecError::checksum}; LineageArchive archive; + std::uint32_t highest = genome_schema_v1; std::size_t at = archive_header_bytes; for (std::uint64_t g = 0; g < genomes; ++g) { if (at + 2 > body.size()) @@ -284,12 +291,16 @@ LineageArchive::decode(std::span bytes) { if (length == 0 || length > max_genome_bytes || at + length > body.size()) return std::unexpected{ArchiveCodecError::format}; const auto genome = Genome::decode(body.subspan(at, length)); - if (!genome) + if (!genome || genome->schema_version() > schema) return std::unexpected{ArchiveCodecError::genome}; + highest = std::max(highest, genome->schema_version()); if (!archive.admit(*genome)) return std::unexpected{ArchiveCodecError::history}; at += length; } + // Canonical: the header names exactly the highest schema held (ADR-0099). + if (highest != schema) + return std::unexpected{ArchiveCodecError::version}; if (body.size() - at != organisms * archived_organism_bytes) return std::unexpected{ArchiveCodecError::format}; for (std::uint64_t o = 0; o < organisms; ++o, at += archived_organism_bytes) { diff --git a/src/biology/mutation.cpp b/src/biology/mutation.cpp index db7a977..c8c922e 100644 --- a/src/biology/mutation.cpp +++ b/src/biology/mutation.cpp @@ -28,8 +28,10 @@ constexpr std::array catalog = [] { // One rule rather than a constant per capability: a hundredth of the declared // range, at least one. Granularity follows the range, which is a stated model // choice and not a claim about biology. -constexpr std::uint32_t adjustment_step(Capability kind) noexcept { - const auto& limits = capability_schema[static_cast(kind)]; +// The step comes from the genome's own schema, so a schema 1 lineage keeps its steps. +constexpr std::uint32_t adjustment_step(const std::array& table, + Capability kind) noexcept { + const auto& limits = table[static_cast(kind)]; const auto span = limits.maximum - limits.minimum; return span / 100 == 0 ? 1 : span / 100; } @@ -72,12 +74,16 @@ std::uint64_t bounded(Random& stream, std::uint64_t bound) noexcept { std::optional apply(const Genome& genome, const MutationOperator& entry, std::uint64_t id, std::uint64_t origin_event) { GenomeDraft draft; + // A successor keeps its parent's schema: its parameters are the parent's, in the + // parent's units (ADR-0099), and a lineage's offers never shift by migration. + draft.schema_version = genome.schema_version(); draft.id = id; draft.origin_event = origin_event; draft.development_version = genome.development_version(); draft.parents = {genome.id()}; draft.capabilities.assign(genome.capabilities().begin(), genome.capabilities().end()); - const auto& limits = capability_schema[static_cast(entry.target)]; + const auto& table = *capability_schema_for(genome.schema_version()); + const auto& limits = table[static_cast(entry.target)]; const auto carried = std::ranges::find(draft.capabilities, entry.target, &CapabilityRecord::kind); const auto present = carried != draft.capabilities.end(); @@ -86,7 +92,7 @@ std::optional apply(const Genome& genome, const MutationOperator& e if (!present) { return std::nullopt; } - const auto step = adjustment_step(entry.target); + const auto step = adjustment_step(table, entry.target); // Refused rather than clamped: a clamp would make two operators agree // and would offer a no-op as a real choice. if (carried->parameter > limits.maximum - step) { @@ -99,7 +105,7 @@ std::optional apply(const Genome& genome, const MutationOperator& e if (!present) { return std::nullopt; } - const auto step = adjustment_step(entry.target); + const auto step = adjustment_step(table, entry.target); if (carried->parameter < limits.minimum + step) { return std::nullopt; } diff --git a/src/processes/development_opening.cpp b/src/processes/development_opening.cpp index 0a2e077..a65b75b 100644 --- a/src/processes/development_opening.cpp +++ b/src/processes/development_opening.cpp @@ -58,6 +58,7 @@ std::unique_ptr community(world::WorldIdentity id) { } std::optional genome(std::uint64_t id, std::uint64_t parent) { biology::GenomeDraft draft; + draft.schema_version = biology::genome_schema_v1; // development-v1 is fixed (ADR-0099). draft.id = id; draft.origin_event = id; draft.development_version = 1; diff --git a/tests/development_tests.cpp b/tests/development_tests.cpp index 12a2e36..f36b46e 100644 --- a/tests/development_tests.cpp +++ b/tests/development_tests.cpp @@ -117,6 +117,30 @@ void capability_prerequisites() { CHECK(with->maintenance == without->maintenance); } +// ADR-0099: development version 1 reads genome rates per second at its 50 ms quantum. +// A schema 1 genome and its exact schema 2 translation develop identically, and a schema +// 2 rate that is not a whole number per tick rounds down (a capacity). +void schema_v2_rates_develop_identically() { + auto old = draft(); + old.capabilities.push_back({Capability::movement, 250, 0}); + old.capabilities.push_back({Capability::ingestion, 120'000, 0}); + auto now = old; + now.schema_version = genome_schema_version; + now.capabilities[3].parameter = 5'000; // 250 per tick, per second. + now.capabilities[4].parameter = 2'400'000; // 120000 per tick, per second. + for (const std::uint32_t realized : {250U, 500U, 1000U}) { + const auto a = derive_phenotype(build(old), {realized, 0}, temperate); + const auto b = derive_phenotype(build(now), {realized, 0}, temperate); + CHECK(a && b && *a == *b); + const auto pa = plan_development(build(old), {realized, 0}, temperate); + const auto pb = plan_development(build(now), {realized, 0}, temperate); + CHECK(pa && pb && *pa == *pb); + } + now.capabilities[3].parameter = 5'019; // 250.95 per tick. + const auto rounded = derive_phenotype(build(now), {1000, 0}, temperate); + CHECK(rounded && rounded->movement_speed == 250); +} + void thermal_shortfall_is_reported_not_enforced() { auto source = draft(); source.capabilities.push_back({Capability::thermal_tolerance, 5'000, 0}); @@ -272,6 +296,7 @@ int main() try { damage_is_not_genome(); identical_inputs_give_identical_phenotypes(); capability_prerequisites(); + schema_v2_rates_develop_identically(); thermal_shortfall_is_reported_not_enforced(); rule_versions_and_invalid_growth(); availability_bounds_do_not_wrap(); diff --git a/tests/genome_tests.cpp b/tests/genome_tests.cpp index cfbbb13..1168746 100644 --- a/tests/genome_tests.cpp +++ b/tests/genome_tests.cpp @@ -115,30 +115,38 @@ void capability_and_range_rules() { draft = founder(); draft.capabilities.push_back({Capability::thermal_tolerance, 1, 1U << capability_count}); reject(draft, GenomeError::unknown_capability); - // Every boundary of every declared range, from both sides. - for (std::size_t index = 0; index < capability_count; ++index) { - const auto kind = static_cast(index); - const auto& limits = capability_schema[index]; - for (const auto value : {limits.minimum, limits.maximum}) { - draft = founder(); + // Every boundary of every declared range, from both sides, in each schema's own table. + for (const auto schema : {genome_schema_v1, genome_schema_version}) { + const auto& table = *capability_schema_for(schema); + const auto fresh = [&] { + auto d = founder(); + d.schema_version = schema; + return d; + }; + for (std::size_t index = 0; index < capability_count; ++index) { + const auto kind = static_cast(index); + const auto& limits = table[index]; + for (const auto value : {limits.minimum, limits.maximum}) { + draft = fresh(); + std::erase_if(draft.capabilities, + [kind](const auto& record) { return record.kind == kind; }); + draft.capabilities.push_back({kind, value, limits.mandates}); + const auto accepted = Genome::create(draft); + CHECK(accepted && *accepted->parameter(kind) == value); + } + if (limits.minimum > 0) { + draft = fresh(); + std::erase_if(draft.capabilities, + [kind](const auto& record) { return record.kind == kind; }); + draft.capabilities.push_back({kind, limits.minimum - 1, limits.mandates}); + reject(draft, GenomeError::parameter_out_of_range); + } + draft = fresh(); std::erase_if(draft.capabilities, [kind](const auto& record) { return record.kind == kind; }); - draft.capabilities.push_back({kind, value, limits.mandates}); - const auto accepted = Genome::create(draft); - CHECK(accepted && *accepted->parameter(kind) == value); - } - if (limits.minimum > 0) { - draft = founder(); - std::erase_if(draft.capabilities, - [kind](const auto& record) { return record.kind == kind; }); - draft.capabilities.push_back({kind, limits.minimum - 1, limits.mandates}); + draft.capabilities.push_back({kind, limits.maximum + 1, limits.mandates}); reject(draft, GenomeError::parameter_out_of_range); } - draft = founder(); - std::erase_if(draft.capabilities, - [kind](const auto& record) { return record.kind == kind; }); - draft.capabilities.push_back({kind, limits.maximum + 1, limits.mandates}); - reject(draft, GenomeError::parameter_out_of_range); } } @@ -245,7 +253,7 @@ void encoded_record_rules() { CHECK(!tampered && tampered.error() == GenomeError::digest_mismatch); // An unsupported schema version fails closed before anything else is read. auto future = bytes; - future[8] = 2; + future[8] = static_cast(genome_schema_version + 1); reseal(future); const auto ahead = Genome::decode(future); CHECK(!ahead && ahead.error() == GenomeError::unsupported_version); @@ -313,6 +321,44 @@ void frozen_record_compatibility() { CHECK(*restored == *genome); } +// Schema 2 (ADR-0099): rates per second. Schema 1 records stay valid, unrewritten, and +// read as exactly twenty times their per-tick values. +void schema_v2_rates() { + auto v1 = founder(); + v1.capabilities.push_back({Capability::movement, 5'000, 0}); + v1.capabilities.push_back({Capability::ingestion, 1'000'000, 0}); // Schema 1 maximum. + const auto old = Genome::create(v1); + CHECK(old && old->schema_version() == genome_schema_v1); + CHECK(old->per_second(Capability::movement) == std::uint64_t{100'000}); + CHECK(old->per_second(Capability::ingestion) == std::uint64_t{20'000'000}); + CHECK(old->per_second(Capability::target_size) == std::uint64_t{5'000}); // Not a rate. + CHECK(!old->per_second(Capability::thermal_tolerance)); + auto v2 = v1; + v2.schema_version = genome_schema_version; + v2.capabilities[3].parameter = 100'000; // Movement, per second. + v2.capabilities[4].parameter = 20'000'000; + const auto now = Genome::create(v2); + CHECK(now && now->schema_version() == 2); + CHECK(now->per_second(Capability::movement) == old->per_second(Capability::movement)); + CHECK(now->per_second(Capability::ingestion) == old->per_second(Capability::ingestion)); + // The same meaning in another schema is another record. + CHECK(now->content_digest() != old->content_digest()); + const auto restored = Genome::decode(now->encode()); + CHECK(restored && *restored == *now && restored->schema_version() == 2); + // Ranges are per schema: a schema 2 rate may exceed schema 1's per-tick maximum. + v2.capabilities[4].parameter = 20'000'001; + reject(v2, GenomeError::parameter_out_of_range); + v1.capabilities[4].parameter = 1'000'001; + reject(v1, GenomeError::parameter_out_of_range); + auto unknown = founder(); + unknown.schema_version = 3; + reject(unknown, GenomeError::unsupported_version); + auto bytes = Genome::create(founder())->encode(); + bytes[8] = 3; // Schema field; decode refuses before the checksum matters. + CHECK(!Genome::decode(bytes) && + Genome::decode(bytes).error() == GenomeError::unsupported_version); +} + void moves_preserve_validity() { const auto original = Genome::create(founder()); const auto other = Genome::create(descendant()); @@ -349,6 +395,7 @@ int main() try { canonical_encoding(); encoded_record_rules(); frozen_record_compatibility(); + schema_v2_rates(); moves_preserve_validity(); std::cout << "PASS genome schema, ancestry, development graph and canonical encoding\n"; return 0; diff --git a/tests/lineage_tests.cpp b/tests/lineage_tests.cpp index 7177017..322adc9 100644 --- a/tests/lineage_tests.cpp +++ b/tests/lineage_tests.cpp @@ -20,8 +20,10 @@ void check(bool condition, std::source_location location = std::source_location: } } #define CHECK(expression) check(static_cast(expression)) -Genome descend(std::uint64_t id, std::uint64_t parent, std::uint32_t conversion = 400) { +Genome descend(std::uint64_t id, std::uint64_t parent, std::uint32_t conversion = 400, + std::uint32_t schema = genome_schema_v1) { GenomeDraft source; + source.schema_version = schema; source.id = id; source.origin_event = id; source.development_version = 1; @@ -380,6 +382,29 @@ void the_archive_decoder_refuses_damage_and_disorder() { CHECK(!LineageArchive::decode(std::span{bytes}.first(39))); } +// ADR-0099: the header names the highest genome schema held, so an archive of schema 1 +// records keeps its exact bytes, and a mixed archive round-trips. +void schemas_mix_without_rewriting() { + const auto old = with_history().encode(); + CHECK(field(old, 12, 4) == genome_schema_v1); + auto archive = branching(); + CHECK(archive.admit(descend(6, 4, 400, genome_schema_version))); + const auto bytes = archive.encode(); + CHECK(field(bytes, 12, 4) == genome_schema_version); + const auto decoded = LineageArchive::decode(bytes); + CHECK(decoded && decoded->encode() == bytes); + CHECK(decoded->find(6)->schema_version() == 2 && decoded->find(4)->schema_version() == 1); + // A header that understates its records is refused. + auto understated = bytes; + set(understated, 12, genome_schema_v1, 4); + reseal(understated); + CHECK(!LineageArchive::decode(understated)); + auto unknown = old; + set(unknown, 12, 3, 4); + reseal(unknown); + CHECK(!LineageArchive::decode(unknown)); +} + void a_full_archive_round_trips() { LineageArchive archive; CHECK(archive.admit(descend(1, 0))); @@ -411,6 +436,7 @@ int main() try { the_archive_round_trips_canonically(); the_archive_decoder_refuses_damage_and_disorder(); a_full_archive_round_trips(); + schemas_mix_without_rewriting(); std::cout << "PASS lineage retention, branching ancestry and provisional absence\n"; return 0; } catch (...) { diff --git a/tests/mutation_tests.cpp b/tests/mutation_tests.cpp index 78f2765..bb50131 100644 --- a/tests/mutation_tests.cpp +++ b/tests/mutation_tests.cpp @@ -137,7 +137,8 @@ void only_legitimate_operators_are_offered() { CHECK(offered); for (const auto& candidate : offered->candidates) { // A required capability can never be shed, however it is drawn. - const auto& limits = capability_schema[static_cast(candidate.target)]; + const auto& limits = (*capability_schema_for( + genome.schema_version()))[static_cast(candidate.target)]; CHECK(!(candidate.effect == MutationEffect::shed && limits.required)); // Acquiring only ever applies to something the parent lacks, and // adjusting only to something it carries. @@ -320,6 +321,37 @@ void invalid_contexts_are_refused() { CHECK(mutation_catalog().size() == 4 * capability_count); } } // namespace +// ADR-0099: a successor keeps its parent's schema and that schema's adjustment step, so +// a schema 1 lineage is never migrated by mutating, and a schema 2 one steps per second. +void successors_keep_their_parents_schema() { + for (const auto schema : {genome_schema_v1, genome_schema_version}) { + auto source = draft(); + source.schema_version = schema; + source.capabilities.push_back( + {Capability::movement, schema == genome_schema_v1 ? 50'000U : 1'000'000U, 0}); + const auto parent = build(source); + const auto step = schema == genome_schema_v1 ? 1'000U : 20'000U; // 1/100 of the range. + bool adjusted = false; + // Offers are seeded; search seeds (deterministically) for the movement adjustment. + for (std::uint64_t seed = 1; seed <= 200 && !adjusted; ++seed) { + const auto offered = mutation_candidates(parent, context(seed, 8)); + CHECK(offered); + for (const auto& candidate : offered->candidates) { + const auto successor = select_mutation(parent, context(seed, 8), *offered, + candidate.operator_id, 99, 42); + CHECK(successor && successor->schema_version() == schema); + if (candidate.target == Capability::movement && + candidate.effect == MutationEffect::adjust_up) { + CHECK(*successor->parameter(Capability::movement) == + *parent.parameter(Capability::movement) + step); + adjusted = true; + } + } + } + CHECK(adjusted); + } +} + int main() try { the_same_context_always_offers_the_same_candidates(); offers_are_canonical_and_unranked(); @@ -330,6 +362,7 @@ int main() try { reported_cost_is_what_the_budget_charges(); invalid_contexts_are_refused(); every_eligible_operator_is_offered_and_none_is_favoured(); + successors_keep_their_parents_schema(); std::cout << "PASS mutation catalog, uniform seeded offers and guarded selection\n"; return 0; } catch (...) {