Merge PR #144: backfill founding_age_years for inhabited bodies (#1000)

Reviewed-by: Hoshe (data QA), Tyre (pipeline) — both APPROVE.
This commit is contained in:
2026-05-30 22:50:17 +02:00
3 changed files with 47 additions and 1 deletions
+1 -1
View File
@@ -1205,7 +1205,7 @@ Technical foundation decisions that constrain implementation: engine, client-ser
- **Ticket:** #922
- **Raised by:** Generation cascade workshop (#897)
- **Cross-reference:** D-096 (DistrictLayoutMode::Organic — consumes block_irregularity), D-194 (district mix — founding_age is an input), D-214 (PoliticalArchetype — archetype_step source)
- **Amendment (2026-05-26 — `founding_age_years` backfill, ticket #1000):** `founding_age_years` is backfilled on every inhabited body in `tooling/economy-db/import_economics.py` MIGRATION_SQL via `COALESCE(events-first, wave-fallback)`. **Events-first:** the system's authored `historical_events.age_years` for an `event_type ∈ {founding, first_settlement, colonization}` — currently empty in the live DB, so it is forward-compatible scaffolding only. **Wave-fallback** — canonical **founding-edge** of each wave's range (`docs/design/systems-framework-final-miri.md:495499`):
- **Amendment (2026-05-26 — `founding_age_years` backfill, ticket #1000):** `founding_age_years` is backfilled on every inhabited body in `tooling/economy-db/import_economics.py` MIGRATION_SQL via `COALESCE(events-first, wave-fallback)`. **Events-first:** the system's authored `historical_events.age_years` for `event_type = 'colonial_charter'` — the canonical founding event authored during the economics built-world workshop. 9 systems in the live DB carry a colonial_charter row, driving 11 inhabited body values; 5 of those diverge meaningfully from their wave fallback (e.g. GJ 144 = 580 vs wave-1 fallback 600, GJ 338B = 590, GJ 380 = 480). **Wave-fallback** — canonical **founding-edge** of each wave's range (`docs/design/systems-framework-final-miri.md:495499`):
| `settlement_wave` | era | range (years ago) | founding-edge fallback |
|---|---|---|---|
Binary file not shown.
+46
View File
@@ -358,6 +358,52 @@ UPDATE bodies SET economic_role = 'extraction' WHERE economic_role IN ('minin
UPDATE bodies SET economic_role = 'transit_hub' WHERE economic_role = 'transit';
UPDATE bodies SET economic_role = 'service_mixed' WHERE economic_role IN ('commercial', 'coordination');
UPDATE bodies SET economic_role = 'residential' WHERE economic_role = 'frontier';
-- Backfill bodies.founding_age_years for all inhabited bodies in player scope (D-216 amendment,
-- ticket #1000). Idempotent: WHERE clause limits to NULL rows, so a re-run is a no-op.
--
-- Strategy: COALESCE(events-first, wave-fallback)
-- events-first: the system's colonial_charter event age_years (authored; 9 systems in live DB).
-- system_history is keyed PRIMARY KEY on system_id, so the wave subquery returns
-- at most one row; LIMIT 1 is used on historical_events for safety (max one
-- colonial_charter per system in the data).
-- wave-fallback: canonical founding-edge of each settlement_wave range per D-216:
-- wave_1=600, wave_2=500, wave_3=300, wave_4=100, wave_5=40.
--
-- Exclusions (founding_age_years stays NULL):
-- origin — Sol; out of player scope per D-236.
-- unsettled — 24 systems with inhabited=0; the EXISTS guard also excludes them.
UPDATE bodies
SET founding_age_years = COALESCE(
(
SELECT he.age_years
FROM historical_events he
WHERE he.system_id = bodies.system_id
AND he.event_type = 'colonial_charter'
ORDER BY he.sort_order ASC
LIMIT 1
),
(
SELECT CASE sh.settlement_wave
WHEN 'wave_1' THEN 600
WHEN 'wave_2' THEN 500
WHEN 'wave_3' THEN 300
WHEN 'wave_4' THEN 100
WHEN 'wave_5' THEN 40
ELSE NULL -- unexpected settlement_wave: add a WHEN above
END
FROM system_history sh
WHERE sh.system_id = bodies.system_id
)
)
WHERE founding_age_years IS NULL
AND inhabited = 1
AND EXISTS (
SELECT 1
FROM system_history sh2
WHERE sh2.system_id = bodies.system_id
AND sh2.settlement_wave NOT IN ('origin', 'unsettled')
);
"""
# Columns to add to existing tables (ALTER TABLE is idempotent via try/except)