From 1630d566cd75410a1b7efd41b15480c2031d0bf8 Mon Sep 17 00:00:00 2001 From: Polichinl Date: Sun, 29 Nov 2026 00:32:44 +0100 Subject: [PATCH] refactor: one store factory per partner (#362) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Story 3 of epic #359. Pure restructuring — Fowler's two hats, no behaviour change, no new behaviour tests. The same 636 tests pass before and after. A seam you cannot switch from outside the code is not a seam (Feathers, ch. 4). The store was constructed in six places, three per partner, so there was no single point at which a later change could choose a different implementation. Each partner now has one `_datastore(config, *, keep_model_path)`. `keep_model_path` has no default, deliberately. The two settings are two different query behaviours and the wrong one is silent: - False clears model_path, suppressing pipeline-core's automatic name == model_name filter, which OVERWRITES a name the caller supplied rather than narrowing it (C-77, measured on the real store in #361) - True keeps it, which only the two partner WRITE stores want, because upload_data falls back to model_path.model_name when name is None So adding a fourth store means deciding which it is, at the call site. The two partners are NOT unified, as the story requires: `_datastore` is defined separately in each package, nothing imports it across the boundary, and both copies of store_port.py are untouched and still byte-identical. Acceptance, all checked: - `grep -rn "DatastoreModule(" views_postprocessing` returns TWO sites, one per partner, down from six - full suite 636 passed / 1 failed / 3 skipped / 41 xfailed, the same counts as before the refactor. The failure is test_the_installed_runtime_dependencies_match_the_lockfile, pre-existing and failing identically on clean development (views-models#757) - ruff 0.14.13 clean - four mutations through the new factory, each caught by exactly the right test and the right partner: the factory never suppressing; the shelf reader asking to keep; the write store asking to clear; the read-back asking to keep - no new abstraction crosses the two partners Two consequences handled rather than worked around: - tests/test_findability.py asserted this property by searching the builder's SOURCE TEXT for `store.model_path = None`. Moving that line into the factory broke the check while changing no behaviour, which is the weakness of a textual guard. #361 already added a behavioural, mutation- verified test of the same property, so the textual assertion is replaced by a structural one on the call's argument — and that one fires where the behavioural test skips, which /code-review high pointed out would otherwise have left C-77 unguarded in an environment without pipeline-core. - the factory's docstring was condensed to stay inside the manager line budget rather than raising the budget a third time this month. The reasoning it used to repeat lives in tests/test_store_construction.py, which is where it is measured. Net lines are +33, not the "slightly negative" the story expected: the epic requires the two partners to share nothing, so the factory and its explanation exist twice. Said rather than massaged. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01ANY1CCy9Xo7zjMY4XJ69v9 --- tests/test_findability.py | 41 +++++++++++++++++--- views_postprocessing/crafd/managers/crafd.py | 34 +++++++++++----- views_postprocessing/unfao/managers/unfao.py | 34 +++++++++++----- 3 files changed, 83 insertions(+), 26 deletions(-) diff --git a/tests/test_findability.py b/tests/test_findability.py index dc4863f..d67e8f9 100644 --- a/tests/test_findability.py +++ b/tests/test_findability.py @@ -133,12 +133,41 @@ def test_the_preflight_queries_the_declared_name_not_the_path_managers(partner): f"{partner} lost the read-back store builder; without it " "`get_latest_file_id` merges the path manager's model name into the query" ) - builder = source[source.index("def _build_partner_read_store"):source.index("class ")] - assert "store.model_path = None" in builder, ( - f"{partner}'s read-back store no longer suppresses pipeline-core's automatic " - "`name == model_name` filter, so the preflight verifies the views-models " - "directory name instead of the declared consumer name. A rename there would " - "leave this check green while the delivery went dark (C-77)." + # The suppression used to be asserted here by searching this builder's source for + # the literal `store.model_path = None`. #362 moved that line into the `_datastore` + # factory, which broke the check without changing any behaviour — the weakness of a + # textual guard, which fails on a harmless rewrite and would equally have stayed + # green had the line moved somewhere it never ran. + # + # The property is now asserted BEHAVIOURALLY, on the store the builder actually + # returns, by + # tests/test_store_construction.py::test_the_partner_read_back_store_is_built_with_the_name_filter_suppressed + # which is mutation-verified: deleting the suppression, or flipping this call's + # argument, reddens it for both partners. + # + # That test needs pipeline-core, so it SKIPS where pipeline-core is absent, and this + # file's checks do not. Review found that removing the textual assertion therefore + # left C-77 guarded by nothing at all in an environment without the dependency. So a + # structural check stays here as the cheap local backstop — on the call's argument, + # not on a line of text, so the move that broke the old one cannot break this one. + builder_ast = next( + n for n in ast.walk(ast.parse(source)) + if isinstance(n, ast.FunctionDef) and n.name == "_build_partner_read_store" + ) + suppressed = [ + kw for call in ast.walk(builder_ast) + if isinstance(call, ast.Call) and getattr(call.func, "id", None) == "_datastore" + for kw in call.keywords + if kw.arg == "keep_model_path" + and isinstance(kw.value, ast.Constant) + and kw.value.value is False + ] + assert suppressed, ( + f"{partner}'s read-back store is no longer built with keep_model_path=False, so " + "pipeline-core's automatic `name == model_name` filter is live and the preflight " + "verifies the views-models directory name instead of the declared consumer name " + "(C-77). The behavioural proof is in tests/test_store_construction.py; this is " + "the structural backstop for environments where that file skips." ) preflight = _function_source(source, "_assert_delivery_is_findable") diff --git a/views_postprocessing/crafd/managers/crafd.py b/views_postprocessing/crafd/managers/crafd.py index 99b5f5c..a5c8531 100644 --- a/views_postprocessing/crafd/managers/crafd.py +++ b/views_postprocessing/crafd/managers/crafd.py @@ -24,6 +24,25 @@ +def _datastore(config: AppwriteConfig, *, keep_model_path: bool) -> DatastoreModule: + """The ONE place this package constructs a prediction store (#362, epic #359). + + *A seam you cannot switch from outside the code is not a seam* (Feathers, ch. 4). + + ``keep_model_path`` has no default: the two settings are two different query + behaviours and the wrong one is silent. ``False`` clears ``model_path``, suppressing + pipeline-core's automatic ``name == model_name`` filter, which **overwrites** a name + the caller supplied rather than narrowing it (C-77). ``True`` keeps it — only the + partner WRITE store wants that, because ``upload_data`` falls back to + ``model_path.model_name`` when ``name`` is ``None``. Both measured on the real store + in ``tests/test_store_construction.py``, which is where that reasoning lives. + """ + store = DatastoreModule(appwrite_file_manager_config=config) + if not keep_model_path: + store.model_path = None + return store + + def _build_prod_forecasts_store(ensemble_name: str | None) -> DatastoreModule: """The shared internal store (ADR-013's "shared shelf"), built from the launcher-assembled environment (validated fail-loud; þing-01 #134 — no dotenv is @@ -72,12 +91,8 @@ def _build_prod_forecasts_store(ensemble_name: str | None) -> DatastoreModule: database_id=os.getenv("APPWRITE_METADATA_DATABASE_ID"), database_name=os.getenv("APPWRITE_METADATA_DATABASE_NAME"), ) - datastore = DatastoreModule(appwrite_file_manager_config=appwrite_config) - # Suppress the automatic name==model_name filter (see docstring). model_path is used - # by DatastoreModule only for that injection and for uploads; the contract read - # neither uploads nor performs any model-scoped lookup. - datastore.model_path = None - return datastore + # The contract read neither uploads nor looks anything up by model name. + return _datastore(appwrite_config, keep_model_path=False) def _build_partner_store(*, config: AppwriteConfig) -> DatastoreModule: @@ -86,7 +101,8 @@ def _build_partner_store(*, config: AppwriteConfig) -> DatastoreModule: Takes the config, not the path manager, so one delivery resolves the audience connection once and the C-94 read-back verifies what the write used (vpp_018 §3). """ - return DatastoreModule(appwrite_file_manager_config=config) + # The one store in this package that keeps its model_path; `_datastore` says why. + return _datastore(config, keep_model_path=True) def _partner_appwrite_config(model_path) -> AppwriteConfig: @@ -138,9 +154,7 @@ def _build_partner_read_store(*, config: AppwriteConfig) -> DatastoreModule: views-models directory name, which equals the declared consumer name only by coincidence (**C-77**). C-94 records why it reuses the write key. """ - store = DatastoreModule(appwrite_file_manager_config=config) - store.model_path = None - return store + return _datastore(config, keep_model_path=False) def _assert_delivery_is_findable( diff --git a/views_postprocessing/unfao/managers/unfao.py b/views_postprocessing/unfao/managers/unfao.py index bd6f2a3..5e62bf2 100644 --- a/views_postprocessing/unfao/managers/unfao.py +++ b/views_postprocessing/unfao/managers/unfao.py @@ -24,6 +24,25 @@ +def _datastore(config: AppwriteConfig, *, keep_model_path: bool) -> DatastoreModule: + """The ONE place this package constructs a prediction store (#362, epic #359). + + *A seam you cannot switch from outside the code is not a seam* (Feathers, ch. 4). + + ``keep_model_path`` has no default: the two settings are two different query + behaviours and the wrong one is silent. ``False`` clears ``model_path``, suppressing + pipeline-core's automatic ``name == model_name`` filter, which **overwrites** a name + the caller supplied rather than narrowing it (C-77). ``True`` keeps it — only the + partner WRITE store wants that, because ``upload_data`` falls back to + ``model_path.model_name`` when ``name`` is ``None``. Both measured on the real store + in ``tests/test_store_construction.py``, which is where that reasoning lives. + """ + store = DatastoreModule(appwrite_file_manager_config=config) + if not keep_model_path: + store.model_path = None + return store + + def _build_prod_forecasts_store(ensemble_name: str | None) -> DatastoreModule: """The shared internal store (ADR-013's "shared shelf"), built from the launcher-assembled environment (validated fail-loud; þing-01 #134 — no dotenv is @@ -72,12 +91,8 @@ def _build_prod_forecasts_store(ensemble_name: str | None) -> DatastoreModule: database_id=os.getenv("APPWRITE_METADATA_DATABASE_ID"), database_name=os.getenv("APPWRITE_METADATA_DATABASE_NAME"), ) - datastore = DatastoreModule(appwrite_file_manager_config=appwrite_config) - # Suppress the automatic name==model_name filter (see docstring). model_path is used - # by DatastoreModule only for that injection and for uploads; the contract read - # neither uploads nor performs any model-scoped lookup. - datastore.model_path = None - return datastore + # The contract read neither uploads nor looks anything up by model name. + return _datastore(appwrite_config, keep_model_path=False) def _build_partner_store(model_path) -> DatastoreModule: @@ -87,7 +102,8 @@ def _build_partner_store(model_path) -> DatastoreModule: reached for. That is the whole difference between this and a method, and it is what lets the environment contract be checked without standing up a manager. """ - return DatastoreModule(appwrite_file_manager_config=_partner_appwrite_config(model_path)) + # The one store in this package that keeps its model_path; `_datastore` says why. + return _datastore(_partner_appwrite_config(model_path), keep_model_path=True) def _partner_appwrite_config(model_path) -> AppwriteConfig: @@ -119,9 +135,7 @@ def _build_partner_read_store(model_path) -> DatastoreModule: views-models directory name, which equals the declared consumer name only by coincidence (**C-77**). C-94 records why it reuses the write key. """ - store = DatastoreModule(appwrite_file_manager_config=_partner_appwrite_config(model_path)) - store.model_path = None - return store + return _datastore(_partner_appwrite_config(model_path), keep_model_path=False) def _assert_delivery_is_findable(