Files
aevolution/migrations/0022_cellular_birth_trace_bound.sql
T
dingo b4fe9e7628
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 2m0s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / checks (network) (push) Failing after 3m4s
Foundation / checks (client) (push) Failing after 4m22s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Record cellular births in replay traces
2026-10-11 00:28:49 -04:00

105 lines
5.5 KiB
PL/PgSQL

-- M11.3d-7h, ADR-0114: allow canonical cellular birth records in bounded traces.
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');
history numeric;
cap numeric;
ticks numeric;
BEGIN
IF m IS NULL OR octet_length(m) < 12 OR substring(m FROM 1 FOR 8) <> '\x4145564f52434d31' THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
IF evolution.manifest_uint(m, 8, 4) <> 2 THEN
RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06';
END IF;
IF octet_length(m) <> 360 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
r.profile := evolution.manifest_uint(m, 12, 4);
history := evolution.manifest_uint(m, 232, 4);
IF NOT (
(((r.profile = 1 AND history = 0) OR (r.profile = 2 AND history = 1))
AND substring(m FROM 161 FOR 36) = '\x020000000200000001000000010000000100000001000000010000000100000001000000')
OR (r.profile = 3 AND history = 2
AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000000000000000000001000000010000000100000001000000')
OR (r.profile = 4 AND history = 3
AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000010000000100000002000000010000000100000000000000')
)
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;
cap := CASE r.profile WHEN 1 THEN 1048576 WHEN 2 THEN 4268144
WHEN 4 THEN 16777216 ELSE 3266800 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);
r.trace_bytes := evolution.manifest_uint(m, 280, 8);
r.trace_sha256 := substring(m FROM 289 FOR 32);
r.decision_count := evolution.manifest_uint(m, 320, 4);
r.decisions_sha256 := substring(m FROM 329 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 OR evolution.manifest_uint(m, 324, 4) <> 0 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
IF r.profile IN (3, 4) AND (r.tick_microseconds <> 50000 OR
(r.profile = 3 AND (r.decision_count <> 0 OR r.decisions_sha256 <> zero32)) OR
(r.commit_sequence = 0 AND
(substring(m FROM 65 FOR 16) <> zero16 OR r.predecessor_digest <> zero32))) THEN
RAISE EXCEPTION 'invalid lifecycle checkpoint' USING ERRCODE = '22023';
END IF;
IF r.commit_sequence = 0 THEN
IF substring(m FROM 129 FOR 16) <> zero16 OR r.epoch <> 0 OR r.begin_tick <> r.end_tick
OR r.trace_bytes <> 0 OR r.trace_sha256 <> zero32 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
IF substring(m FROM 65 FOR 16) = zero16 AND r.predecessor_digest = zero32 THEN
IF r.end_tick <> 0 OR r.decision_count <> 0 OR r.decisions_sha256 <> zero32 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
r.predecessor_commit := NULL;
ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_digest = zero32
OR r.predecessor_commit = r.commit_id OR r.profile IN (3, 4)
OR r.decision_count > 8
OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
r.holder := NULL;
ELSE
ticks := r.end_tick - r.begin_tick;
IF 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 ticks > 64
OR (r.profile IN (1, 2) AND r.trace_bytes <> 96 + 304 * ticks)
OR (r.profile = 3 AND (r.trace_bytes < 112 OR r.trace_bytes > 12400
OR mod(r.trace_bytes - 112, 48) <> 0))
OR (r.profile = 4 AND (r.trace_bytes < 112 + 4 * ticks
OR r.trace_bytes > 112 + 16400 * ticks))
OR r.trace_sha256 = zero32 OR r.decision_count > 8
OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
END IF;
RETURN r;
END $$;
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (22, :'migration_sha_22');