homemaker-layout/tests/test_programme.py
Claude 7ec4e5d121
§39.4: tighten generic-type matching, reverting the harbor rename
Supersedes the previous commit's approach. Renaming harbor's four colliding
codes fixed one programme; tightening the matching rule fixes the rule, so a
room may be called anything. cr1/of/st1/st2 are restored and the examples are
byte-identical to their pre-§39 state -- which also means existing .dom
artefacts (evolved-3M*) stay valid, so migrate_ju3_rename.py is deleted.

The rule: Urb has exactly three GENERIC structural types (get_space_types:
qw/C O S/), the leaves the search creates. Measured across the corpus: 154 C,
110 O, 1 S, not one lowercase generic -- while every programme code is
lowercase, including single-character ones (r, t, m, n). Case is the
discriminator, not length. Every generic test was type[0].lower() in (...), a
case-insensitive PREFIX that swept up any programme code starting with those
letters; they now match the generic set exactly. 30 sites across dom, fitness,
graph, operators, programme, shapecurve and bubble.

NOT applied to the SEMANTIC prefixes: l/k/b/t classify programme codes by first
letter (graph.py builds bedroom<->toilet and kitchen<->living relations from
them) and stay prefix-based. Where the namespaces were mixed in one expression
they were split -- has_circulation's ("b","l","k","c") is three semantic
prefixes plus dom.is_circulation; access()'s ("l","c","s") is semantic l plus
the generic circulation set.

New: dom.GENERIC_{CIRCULATION,OUTSIDE,TYPES} + is_generic(); fitness.
_generic_class(), replacing the _t0 dispatch in quality_size/quality_width/
quality_proportion/value_rate -- the four terms that mattered most and that a
first sweep missed, since they dispatch through a t0 variable rather than an
inline test. graph._adjacency_target resolves a generic adjacency requirement
(programmes write "adjacency: [c, o]") to the generic set while every other
requirement keeps Perl's prefix semantics.

Two subtleties: S is in both generic sets but takes the OUTSIDE parameter
families -- a first translation tested circulation first and silently gave S
the circulation params, caught by test_get_space_params_sahn_proportion. And
validate_codes survives, narrowed to a code spelled exactly C/O/S, which is a
genuine ambiguity; merely starting with c/o/s is now fine.

Invariant asserted as a test: test_scoring_is_invariant_under_programme_code_
spelling relabels one tree and its config together and re-scores. Bit-identical
across 12 comparisons (6 seeds x collapse on/off).

Re-baseline (seed 1, 20k, original names): 58 fails (15 hard / 43 soft) against
the real 37-instance programme, with cr1 at 79.1 m2 vs declared 80 (was 32.9
and 17.1), of/st1/st2 all present and in band, and one fail naming any of them.
57 -> 58 on a 5-instance-harder programme is within noise: "did not regress".

Fallout (§39.5): 2g7.5's CP-SAT seeder win does not survive. Over 6 seeds --
harbor real 102/114 (cpsat loses), harbor old-effective 98/99 (tie, so the win
was already marginal), maple-court 156/144 (cpsat wins). maple is the control:
the solver did not regress, harbor's programme changed. Test xfail'd with that
reason plus a maple companion; both assign_solver flags stay default off.
Filed homemaker-py-w6x to re-check other narrow-margin harbor A/Bs.

345 passed, 1 xfailed, same 7 pre-existing fixture failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MJ84Feep79Hhm3E4zZJmnB
2026-08-26 09:45:28 +00:00

128 lines
5.7 KiB
Python

"""Tests for programme.py parsing and validation."""
from pathlib import Path
import pytest
from homemaker_layout import dom, fitness, programme
# --------------------------------------------------------------------------- #
# homemaker-py-ju3 / DESIGN.md §39.2 — reserved generic type prefixes
# --------------------------------------------------------------------------- #
def test_validate_codes_accepts_codes_that_merely_start_with_c_o_s():
"""§39.4: the generic-type tests match C/O/S EXACTLY, so a programme code
may start with any letter. This used to raise — that was the bug, not the
rule."""
programme.validate_codes(["cr1", "of", "st1", "st2", "b1", "k1", "la1"])
@pytest.mark.parametrize("code", ["C", "O", "S"])
def test_validate_codes_rejects_exact_generic_types(code):
"""A code spelled exactly like a generic structural type is a genuine
ambiguity no matching rule can resolve, so it still fails loudly."""
with pytest.raises(ValueError, match="generic structural types"):
programme.validate_codes([code])
def test_exact_generic_rejected_by_both_parse_paths():
"""programme._parse_spaces and fitness.Fitness._load_programme parse
conf["spaces"] independently — validating only one would leave the other
door open."""
conf = {"spaces": {"C": {"size": [80.0, 10.0]}}}
with pytest.raises(ValueError, match="generic structural types"):
programme._parse_spaces(conf)
with pytest.raises(ValueError, match="generic structural types"):
fitness.Fitness(conf=conf)
def test_colliding_code_is_a_full_requirement_not_a_generic():
"""The §39.2 damage in one assertion: a c-prefixed code must keep its
declared targets and stay in the required set."""
conf = {"spaces": {"cr1": {"size": [80.0, 10.0], "width": [6.0, 1.5],
"proportion": [2.0, 0.5], "count": 1}}}
fit = fitness.Fitness(conf=conf)
assert fit.get_space_params("cr1", "size") == [80.0, 10.0]
assert fit.get_space_params("cr1", "width") == [6.0, 1.5]
assert not dom.is_generic("cr1")
assert not dom.is_circulation(dom.Node(type="cr1"))
assert not dom.is_outside(dom.Node(type="of"))
# ...while the genuine generics still classify as before
assert dom.is_circulation(dom.Node(type="C"))
assert dom.is_outside(dom.Node(type="O"))
assert dom.is_outside(dom.Node(type="S")) and dom.is_circulation(dom.Node(type="S"))
def test_semantic_but_unreserved_prefixes_are_allowed():
"""l/k/b/t carry adjacency semantics but never discard a requirement, so
programme codes may use them freely — only c/o/s are reserved."""
reqs = programme._parse_spaces({"spaces": {
"l1": {"size": [20.0, 4.0]}, "k1": {"size": [12.0, 3.0]},
"b1": {"size": [16.0, 4.0]}, "t1": {"size": [3.0, 1.0]},
}})
assert sorted(reqs) == ["b1", "k1", "l1", "t1"]
def test_corpus_programmes_are_namespace_clean():
"""Every checked-in example must load — a regression here means a corpus
programme reintroduced a colliding code."""
for d in sorted(Path("examples").iterdir()):
if (d / "patterns.config").is_file():
programme.load_programme_dir(str(d))
def test_scoring_is_invariant_under_programme_code_spelling(tmp_path):
"""§39.4's headline invariant: renaming a programme code must not change
what a layout scores.
Builds one layout from harbor-house (whose codes ``cr1``/``of``/``st1``/
``st2`` all begin with a reserved generic letter), then relabels that exact
tree AND its config together and re-scores. Same geometry, same topology,
only the spelling differs — so any difference is the generic-type rule
leaking into the programme namespace, which is the bug this guards.
"""
import copy
import re
import shutil
import numpy as np
from homemaker_layout import driver, operators
rename = {"cr1": "fr1", "of": "ao", "st1": "gs1", "st2": "gs2"}
src = Path("examples/harbor-house")
shutil.copytree(src, tmp_path / "hh")
cfg = tmp_path / "hh" / "patterns.config"
text = cfg.read_text()
for old, new in rename.items():
text = re.sub(rf"^( ){re.escape(old)}:$", rf"\g<1>{new}:", text, flags=re.M)
cfg.write_text(text)
def evaluator(directory):
overrides = driver._overrides_for(True, False, None, False, True, False)
conf, cost = fitness.load_config(str(directory), overrides=dict(overrides or {}))
return fitness.Fitness(conf, cost)
def relabel(root):
for lvl in dom.levels(root):
for leaf in lvl.leaves():
leaf.type = rename.get(leaf.type, leaf.type)
leaf.share_type = rename.get(leaf.share_type, leaf.share_type)
return root
reqs = programme.load_programme_dir(str(src))
before, after = evaluator(src), evaluator(tmp_path / "hh")
for seed in range(3):
root = operators.constructive_topology(
dom.load(str(src / "init.dom")), reqs, np.random.default_rng(seed),
sorted(reqs) + ["C", "O"],
min_storeys=programme.storey_minimum(str(src)),
adjacency_aware=True, proportion_aware=True, circ_divisor=3,
leaf_sharing=True, leaf_share_factor=3, depth_balanced=True,
interior_outside=True, outside_divisor=3)
score_a, fails_a = before.score_with_fails(copy.deepcopy(root))
score_b, fails_b = after.score_with_fails(relabel(copy.deepcopy(root)))
normalised = tuple(sorted(
re.sub(r"\b(%s)\b" % "|".join(rename), lambda m: rename[m.group(1)], f)
for f in fails_a))
assert f"{score_a:.12g}" == f"{score_b:.12g}", f"seed {seed}: score differs"
assert normalised == tuple(sorted(fails_b)), f"seed {seed}: fails differ"