[PR #1506] world_grid_atlas: harden geometry, alignment, linear_fit, DDS/MHIU bounds, manifests, and strict JSON (WORLD-GRID-ATLAS-VALIDATION-HARDENING) #1537

Open
opened 2026-11-26 20:21:35 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1506
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/world-grid-atlas-hardening


📝 Commits (1)

  • 6b984f2 world_grid_atlas: harden geometry, alignment, linear_fit, DDS/MHIU bounds, manifests, and strict JSON (WORLD-GRID-ATLAS-VALIDATION-HARDENING)

📊 Changes

2 files changed (+227 additions, -30 deletions)

View changed files

📝 tests/test_world_grid_atlas.py (+116 -0)
📝 tools/world_grid_atlas.py (+111 -30)

📄 Description

Claimed in the coordination issue as: WORLD-GRID-ATLAS-VALIDATION-HARDENING (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7150859873)

What and why

Hardens tools/world_grid_atlas.py against malformed geometry, alignment, regression-fit, DDS, MHIU, manifest, and JSON inputs:

  • geometry(): rejects width <= 0, height <= 0, bool dimensions/values, non-3-tuple or non-finite origin, and negative or non-finite zInflation (previously width=0/-512 passed % tile and triggered ZeroDivisionError/inverted boxes, and negative zInflation produced z_min > z_max).
  • cell_box(): rejects bool / non-int cells (previously 1.5 or True silently returned a box) and non-positive grid dimensions.
  • fill_by_order(): rejects negative/bool n_tiles, duplicate cells in order, out-of-range/bool anchor tile indices, and anchor cells absent from order (raising ValueError instead of KeyError or out-of-range placement keys).
  • align(): rejects ragged score rows and non-finite/bool scores, and verifies backtrack indices are valid (previously NaN/-inf scores left arg[j] == -1, silently returning -1 and indexing order[-1]).
  • linear_fit(): checks len(xs) == len(ys) (previously my = fsum(ys) / len(xs) silently computed wrong means and regression parameters on mismatched inputs), rejects non-finite/bool points, and clamps Pearson r to [-1.0, 1.0].
  • read_image(): checks width > 0, height > 0, and len(data) >= DX10_HEADER before struct.unpack_from(..., 128) so a truncated DDS / DX10 file raises a clean ValueError instead of an unhandled struct.error traceback.
  • split_cells() / tile_step(): reject non-positive rows, columns, w, h, and p with ValueError instead of ZeroDivisionError.
  • water_fit() & run(): skip degenerate zero-span / non-finite / truncated MHIU heightmaps cleanly without divide-by-zero warnings, and run water_fit only on WaterSystemHeightMap itself rather than every grid sharing a dictionary set.
  • load_manifests() & write_json(): validate JSON manifest root/items/textures structure (clean ValueError instead of AttributeError) and pass allow_nan=False to json.dump().

Tested

  • python3 -I tests/test_world_grid_atlas.py: 42/42 (26 stdlib tests run and OK; 16 numpy/PIL tests skipped under -I).
  • python3 tests/test_world_grid_atlas.py: 42/42 OK (all stdlib + numpy/PIL forward tests pass).
  • Mutation check against unpatched main (3ff2a2adc): all 10 new regression tests fail (failures=19, errors=5 plus RuntimeWarning: divide by zero on water_fit).
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
  • python3 -I scripts/check_no_game_data.py tools/world_grid_atlas.py tests/test_world_grid_atlas.py: clean.
  • python3 -m compileall -q tools/world_grid_atlas.py tests/test_world_grid_atlas.py: clean.

GPUs tested: n/a (Python atlas/analysis tool only)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — no translator rule changed)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1506 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/world-grid-atlas-hardening` --- ### 📝 Commits (1) - [`6b984f2`](https://github.com/ubiquitarianism/leonida/commit/6b984f2882d0ee4eadaa4737c159030dc10e5c78) world_grid_atlas: harden geometry, alignment, linear_fit, DDS/MHIU bounds, manifests, and strict JSON (WORLD-GRID-ATLAS-VALIDATION-HARDENING) ### 📊 Changes **2 files changed** (+227 additions, -30 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_world_grid_atlas.py` (+116 -0) 📝 `tools/world_grid_atlas.py` (+111 -30) </details> ### 📄 Description Claimed in the coordination issue as: WORLD-GRID-ATLAS-VALIDATION-HARDENING (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7150859873) ### What and why Hardens `tools/world_grid_atlas.py` against malformed geometry, alignment, regression-fit, DDS, MHIU, manifest, and JSON inputs: - **`geometry()`**: rejects `width <= 0`, `height <= 0`, `bool` dimensions/values, non-3-tuple or non-finite `origin`, and negative or non-finite `zInflation` (previously `width=0`/`-512` passed `% tile` and triggered `ZeroDivisionError`/inverted boxes, and negative `zInflation` produced `z_min > z_max`). - **`cell_box()`**: rejects `bool` / non-`int` cells (previously `1.5` or `True` silently returned a box) and non-positive grid dimensions. - **`fill_by_order()`**: rejects negative/`bool` `n_tiles`, duplicate cells in `order`, out-of-range/`bool` anchor tile indices, and anchor cells absent from `order` (raising `ValueError` instead of `KeyError` or out-of-range placement keys). - **`align()`**: rejects ragged score rows and non-finite/`bool` scores, and verifies backtrack indices are valid (previously `NaN`/`-inf` scores left `arg[j] == -1`, silently returning `-1` and indexing `order[-1]`). - **`linear_fit()`**: checks `len(xs) == len(ys)` (previously `my = fsum(ys) / len(xs)` silently computed wrong means and regression parameters on mismatched inputs), rejects non-finite/`bool` points, and clamps Pearson `r` to `[-1.0, 1.0]`. - **`read_image()`**: checks `width > 0`, `height > 0`, and `len(data) >= DX10_HEADER` before `struct.unpack_from(..., 128)` so a truncated `DDS ` / `DX10` file raises a clean `ValueError` instead of an unhandled `struct.error` traceback. - **`split_cells()` / `tile_step()`**: reject non-positive `rows`, `columns`, `w`, `h`, and `p` with `ValueError` instead of `ZeroDivisionError`. - **`water_fit()` & `run()`**: skip degenerate zero-span / non-finite / truncated MHIU heightmaps cleanly without divide-by-zero warnings, and run `water_fit` only on `WaterSystemHeightMap` itself rather than every grid sharing a dictionary set. - **`load_manifests()` & `write_json()`**: validate JSON manifest root/items/textures structure (clean `ValueError` instead of `AttributeError`) and pass `allow_nan=False` to `json.dump()`. ### Tested - `python3 -I tests/test_world_grid_atlas.py`: 42/42 (26 stdlib tests run and OK; 16 numpy/PIL tests skipped under `-I`). - `python3 tests/test_world_grid_atlas.py`: 42/42 OK (all stdlib + numpy/PIL forward tests pass). - Mutation check against unpatched `main` (`3ff2a2adc`): all 10 new regression tests fail (`failures=19, errors=5` plus `RuntimeWarning: divide by zero` on `water_fit`). - `python3 -I tests/test_text_write_line_endings.py`: 3/3 OK. - `python3 -I scripts/check_no_game_data.py tools/world_grid_atlas.py tests/test_world_grid_atlas.py`: clean. - `python3 -m compileall -q tools/world_grid_atlas.py tests/test_world_grid_atlas.py`: clean. GPUs tested: n/a (Python atlas/analysis tool only) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — no translator rule changed) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1537
No description provided.