ADR-0062 adds a canonical LineageArchive codec (parents before children, organisms by id, FNV) that decodes by replaying admit/record_birth/ record_death and requires byte-identical re-encoding; it never overwrites an existing archive. LineageRegion binds the environmental producer and consumer accounts to archived organisms with identity high-waters, enforces closure (lifecycle vs deaths, exact living set, tick bounds) and records deaths in the tick that causes them. Checkpoint profile 2 (environmental-lineage-v1, history profile 1) publishes that container; schema v4 admits it with a 4,268,144-byte cap and requires each commit to keep its predecessor's profile, RNG, history and durability declarations. Linux adds provision/publish/recover for the profile on the shared publication path. Qualification found WSL2 stepping CLOCK_REALTIME back ~0.5 s every ~32 s, which made two 1 s-lease tests flaky under ASan. Tests now wait on database-time expiry; ADR-0060 records the slewed-clock requirement. Validated: Windows CTest 57/57; Linux GCC Debug/Release, clang-tidy and ASan/UBSan CTest 57/57 each; PostgreSQL suites 2x Release, 2x analysis, 5x ASan; SQL and C++ mutations each caught. No Docker/Gitea CI, remote store, process kill or deployment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
7.0 KiB
PL/PgSQL
132 lines
7.0 KiB
PL/PgSQL
-- M7.4b history-capable profile (ADR-0062). Admits environmental-lineage-v1
|
|
-- (profile 2, history profile 1: lineage archive 1 + genome schema 1) beside
|
|
-- environmental-v1, with per-profile payload caps, and makes publication require the
|
|
-- predecessor's exact profile, RNG, history and durability declarations.
|
|
-- Grants are unchanged: CREATE OR REPLACE keeps each function's owner and ACL.
|
|
|
|
-- Mirrors decode_manifest + supported() in src/persistence/region_checkpoint.cpp.
|
|
-- 22023: malformed; EVL06: parsable but unsupported by this schema.
|
|
CREATE OR REPLACE FUNCTION evolution.parse_checkpoint_manifest(m bytea)
|
|
RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE
|
|
SET search_path = pg_catalog, pg_temp AS $$
|
|
DECLARE
|
|
r evolution.checkpoint_manifest;
|
|
zero16 constant bytea := '\x00000000000000000000000000000000';
|
|
zero32 constant bytea := decode(repeat('00', 32), 'hex');
|
|
profile numeric;
|
|
history numeric;
|
|
cap numeric;
|
|
BEGIN
|
|
IF m IS NULL OR octet_length(m) <> 280 OR substring(m FROM 1 FOR 8) <> '\x4145564f52434d31' THEN
|
|
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
|
|
END IF;
|
|
profile := evolution.manifest_uint(m, 12, 4);
|
|
history := evolution.manifest_uint(m, 232, 4);
|
|
IF evolution.manifest_uint(m, 8, 4) <> 1
|
|
OR NOT ((profile = 1 AND history = 0) OR (profile = 2 AND history = 1))
|
|
OR substring(m FROM 161 FOR 36) <> '\x020000000200000001000000010000000100000001000000010000000100000001000000'
|
|
OR evolution.manifest_uint(m, 228, 4) <> 0 OR evolution.manifest_uint(m, 236, 4) <> 1 THEN
|
|
RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06';
|
|
END IF;
|
|
-- Payload caps: 1 MiB envelope; 72 + 1 MiB + lineage archive cap (3219496) container.
|
|
cap := CASE profile WHEN 1 THEN 1048576 ELSE 4268144 END;
|
|
r.world_id := evolution.manifest_uint(m, 16, 8);
|
|
r.region_id := evolution.manifest_uint(m, 24, 8);
|
|
r.frame_id := evolution.manifest_uint(m, 32, 8);
|
|
r.commit_id := encode(substring(m FROM 41 FOR 16), 'hex')::uuid;
|
|
r.commit_sequence := evolution.manifest_uint(m, 56, 8);
|
|
r.predecessor_commit := encode(substring(m FROM 65 FOR 16), 'hex')::uuid;
|
|
r.predecessor_digest := substring(m FROM 81 FOR 32);
|
|
r.begin_tick := evolution.manifest_uint(m, 112, 8);
|
|
r.end_tick := evolution.manifest_uint(m, 120, 8);
|
|
r.holder := encode(substring(m FROM 129 FOR 16), 'hex')::uuid;
|
|
r.epoch := evolution.manifest_uint(m, 144, 8);
|
|
r.tick_microseconds := evolution.manifest_uint(m, 152, 8);
|
|
r.versions := substring(m FROM 161 FOR 36);
|
|
r.build_identity := substring(m FROM 197 FOR 32);
|
|
r.payload_bytes := evolution.manifest_uint(m, 240, 8);
|
|
r.payload_sha256 := substring(m FROM 249 FOR 32);
|
|
IF r.world_id = 0 OR r.region_id = 0 OR r.frame_id = 0
|
|
OR substring(m FROM 41 FOR 16) = zero16 OR r.tick_microseconds = 0
|
|
OR r.build_identity = zero32 OR r.payload_bytes < 1 OR r.payload_bytes > cap
|
|
OR r.payload_sha256 = zero32 THEN
|
|
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
|
|
END IF;
|
|
IF r.commit_sequence = 0 THEN
|
|
IF substring(m FROM 65 FOR 16) <> zero16 OR r.predecessor_digest <> zero32
|
|
OR r.begin_tick <> 0 OR r.end_tick <> 0 OR substring(m FROM 129 FOR 16) <> zero16
|
|
OR r.epoch <> 0 THEN
|
|
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
|
|
END IF;
|
|
r.predecessor_commit := NULL;
|
|
r.holder := NULL;
|
|
ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_commit = r.commit_id
|
|
OR r.predecessor_digest = zero32 OR substring(m FROM 129 FOR 16) = zero16
|
|
OR r.epoch = 0 OR r.end_tick <= r.begin_tick OR r.end_tick - r.begin_tick > 64 THEN
|
|
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
|
|
END IF;
|
|
RETURN r;
|
|
END $$;
|
|
|
|
CREATE OR REPLACE FUNCTION evolution.publish_region_checkpoint(p_manifest bytea)
|
|
RETURNS TABLE (region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid,
|
|
head_digest text, head_tick evolution.u64)
|
|
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
DECLARE
|
|
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
|
|
h evolution.region_heads%ROWTYPE;
|
|
p evolution.region_commits%ROWTYPE;
|
|
frame evolution.u64;
|
|
d bytea := sha256(p_manifest);
|
|
BEGIN
|
|
IF c.commit_sequence = 0 THEN
|
|
RAISE EXCEPTION 'genesis is provisioned, not published' USING ERRCODE = '22023';
|
|
END IF;
|
|
PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch);
|
|
SELECT * INTO h FROM evolution.region_heads r
|
|
WHERE r.world_id = c.world_id AND r.region_id = c.region_id FOR UPDATE;
|
|
IF NOT FOUND THEN
|
|
RAISE EXCEPTION 'region not provisioned' USING ERRCODE = 'EVL04';
|
|
END IF;
|
|
IF h.commit_sequence = 18446744073709551615 THEN
|
|
RAISE EXCEPTION 'commit sequences exhausted' USING ERRCODE = 'EVL05';
|
|
END IF;
|
|
IF c.commit_sequence <> h.commit_sequence + 1 OR c.predecessor_commit <> h.commit_id
|
|
OR c.predecessor_digest <> h.digest OR c.begin_tick <> h.end_tick THEN
|
|
RAISE EXCEPTION 'region head moved' USING ERRCODE = 'EVL03';
|
|
END IF;
|
|
SELECT l.frame_id INTO frame FROM evolution.region_leases l
|
|
WHERE l.world_id = c.world_id AND l.region_id = c.region_id;
|
|
IF c.frame_id <> frame THEN
|
|
RAISE EXCEPTION 'foreign frame' USING ERRCODE = '22023';
|
|
END IF;
|
|
SELECT * INTO p FROM evolution.region_commits r
|
|
WHERE r.world_id = c.world_id AND r.region_id = c.region_id
|
|
AND r.commit_sequence = h.commit_sequence;
|
|
-- Profile, RNG/history/durability declarations, rules, formats and quantum are
|
|
-- fixed for a region; changing any of them is a migration, never a commit.
|
|
IF c.versions <> p.versions OR c.tick_microseconds <> p.tick_microseconds
|
|
OR substring(p_manifest FROM 13 FOR 4) <> substring(p.manifest FROM 13 FOR 4)
|
|
OR substring(p_manifest FROM 229 FOR 12) <> substring(p.manifest FROM 229 FOR 12)
|
|
OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b
|
|
WHERE b.build_identity = c.build_identity) THEN
|
|
RAISE EXCEPTION 'unsupported checkpoint' USING ERRCODE = 'EVL06';
|
|
END IF;
|
|
IF EXISTS (SELECT 1 FROM evolution.region_commits r WHERE r.commit_id = c.commit_id) THEN
|
|
RAISE EXCEPTION 'commit identity reused' USING ERRCODE = 'EVL07';
|
|
END IF;
|
|
INSERT INTO evolution.region_commits(world_id, region_id, commit_sequence, commit_id,
|
|
commit_digest, manifest, begin_tick, end_tick, published_epoch, tick_microseconds,
|
|
versions, build_identity, payload_bytes, payload_sha256)
|
|
VALUES (c.world_id, c.region_id, c.commit_sequence, c.commit_id, d, p_manifest,
|
|
c.begin_tick, c.end_tick, c.epoch, c.tick_microseconds, c.versions,
|
|
c.build_identity, c.payload_bytes, c.payload_sha256);
|
|
UPDATE evolution.region_heads r
|
|
SET commit_sequence = c.commit_sequence, commit_id = c.commit_id, digest = d,
|
|
end_tick = c.end_tick, published_epoch = c.epoch
|
|
WHERE r.world_id = c.world_id AND r.region_id = c.region_id;
|
|
RETURN QUERY SELECT frame, c.commit_sequence, c.commit_id, encode(d, 'hex'), c.end_tick;
|
|
END $$;
|
|
|
|
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (4, :'migration_sha_4');
|