M11.1: genome schema 2 with per-second rates (ADR-0099, segment 2)
Foundation / checks (deploy) (push) Successful in 3m32s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (client) (push) Successful in 12m56s
Foundation / checks (network) (push) Successful in 13m46s
Foundation / checks (tsan) (push) Successful in 7m59s
Foundation / checks (checks) (push) Successful in 33m36s
Foundation / checks (postgres) (push) Successful in 45m30s
Foundation / postgres-tools (push) Successful in 20s
Foundation / server-image (push) Successful in 25s
Foundation / image (push) Successful in 2m36s
Foundation / checks (deploy) (push) Successful in 3m32s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (client) (push) Successful in 12m56s
Foundation / checks (network) (push) Successful in 13m46s
Foundation / checks (tsan) (push) Successful in 7m59s
Foundation / checks (checks) (push) Successful in 33m36s
Foundation / checks (postgres) (push) Successful in 45m30s
Foundation / postgres-tools (push) Successful in 20s
Foundation / server-image (push) Successful in 25s
Foundation / image (push) Successful in 2m36s
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 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "evo/biology/development.hpp"
|
||||
|
||||
#include "evo/accounting/rates.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
@@ -96,13 +98,23 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi
|
||||
phenotype.conversion_per_thousand =
|
||||
static_cast<std::uint32_t>(*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<std::uint32_t>(*genome.parameter(Capability::movement));
|
||||
const auto speed = rate_per_tick(*genome.per_second(Capability::movement));
|
||||
if (!speed || *speed > std::numeric_limits<std::uint32_t>::max()) {
|
||||
return std::unexpected{DevelopmentError::overflow};
|
||||
}
|
||||
phenotype.movement_speed = static_cast<std::uint32_t>(*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;
|
||||
|
||||
+40
-12
@@ -1,5 +1,7 @@
|
||||
#include "evo/biology/genome.hpp"
|
||||
|
||||
#include "evo/accounting/rates.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
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<std::size_t>(kind)];
|
||||
}
|
||||
|
||||
std::expected<void, GenomeError> validate(std::uint64_t id, std::uint64_t origin,
|
||||
std::uint32_t development,
|
||||
std::expected<void, GenomeError> validate(std::uint32_t schema, std::uint64_t id,
|
||||
std::uint64_t origin, std::uint32_t development,
|
||||
std::span<const std::uint64_t> parents,
|
||||
std::span<const CapabilityRecord> 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<std::size_t>(kind)];
|
||||
};
|
||||
if (id == 0 || origin == 0) {
|
||||
return std::unexpected{GenomeError::invalid_identity};
|
||||
}
|
||||
@@ -112,7 +117,7 @@ std::expected<void, GenomeError> validate(std::uint64_t id, std::uint64_t origin
|
||||
}
|
||||
for (std::size_t index = 0; index < capability_count; ++index) {
|
||||
const auto kind = static_cast<Capability>(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<std::uint8_t> Genome::body() const {
|
||||
std::vector<std::uint8_t> 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, GenomeError> 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, GenomeError> Genome::decode(std::span<const std::uint8_t>
|
||||
}
|
||||
Reader reader{bytes};
|
||||
static_cast<void>(reader.read(magic.size()));
|
||||
if (reader.read(4) != genome_schema_version) {
|
||||
const auto schema = static_cast<std::uint32_t>(reader.read(4));
|
||||
if (capability_schema_for(schema) == nullptr) {
|
||||
return std::unexpected{GenomeError::unsupported_version};
|
||||
}
|
||||
const auto development = static_cast<std::uint32_t>(reader.read(4));
|
||||
@@ -254,10 +261,12 @@ std::expected<Genome, GenomeError> Genome::decode(std::span<const std::uint8_t>
|
||||
const auto prerequisites = static_cast<std::uint32_t>(reader.read(4));
|
||||
capabilities.push_back({static_cast<Capability>(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<std::uint32_t, GenomeError> Genome::parameter(Capability kind) con
|
||||
}
|
||||
return std::unexpected{GenomeError::missing_capability};
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, GenomeError> 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<std::size_t>(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
|
||||
|
||||
+14
-3
@@ -234,7 +234,11 @@ std::vector<std::uint8_t> 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, ArchiveCodecError>
|
||||
LineageArchive::decode(std::span<const std::uint8_t> 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<const std::uint8_t> 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<const std::uint8_t> 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) {
|
||||
|
||||
@@ -28,8 +28,10 @@ constexpr std::array<MutationOperator, 4 * capability_count> 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<std::size_t>(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<CapabilitySpec, capability_count>& table,
|
||||
Capability kind) noexcept {
|
||||
const auto& limits = table[static_cast<std::size_t>(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<GenomeDraft> 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<std::size_t>(entry.target)];
|
||||
const auto& table = *capability_schema_for(genome.schema_version());
|
||||
const auto& limits = table[static_cast<std::size_t>(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<GenomeDraft> 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<GenomeDraft> 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;
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ std::unique_ptr<EnvironmentalCommunity> community(world::WorldIdentity id) {
|
||||
}
|
||||
std::optional<biology::Genome> 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;
|
||||
|
||||
Reference in New Issue
Block a user