Repository navigation
decision_context: single-source the successful-read status set #8631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2430468136
wants to merge
1
commit into
loopx-project:main
Choose a base branch
from
2430468136:codex/decision-context-success-status-owner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+243
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
tests/architecture/test_decision_context_success_status_owner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| """Guard one owner for the decision-context successful-read status set (#4447). | ||
|
|
||
| ``_SUCCESS_STATUSES`` in ``freshness.py`` decides which scan/attempt statuses | ||
| count as a successful read (``completed`` / ``no_change``). The same two-member | ||
| set was restated across the package: ``capture.py`` bound it under a different | ||
| name (``_READ_SUCCESS_STATUSES``) *and* inlined it once, while ``assembler.py`` | ||
| inlined it four times with no name at all -- seven sites folding to one value. | ||
| Both consumers *already import* from ``freshness``, so borrowing the owner object | ||
| adds no new import edge and cannot introduce a cycle. Every site now references | ||
| the single owner and the restated name is retired. | ||
|
|
||
| The by-name semantic inventory cannot see this: the owner and the retired | ||
| capture name are private ``_``-bindings (the inventory only catalogues | ||
| ``UPPER_NAME`` carriers), they differ by name, and four of the seven sites had no | ||
| name to key on. This guard keys on the **folded value** instead, so a renamed or | ||
| anonymous inline restatement is caught, while the genuinely different neighbours | ||
| -- the four-member ``_SCAN_STATUSES`` vocabulary and the ordered four-status | ||
| report tuple -- are never swept. | ||
|
|
||
| Two layers: a whole-tree folded-value census (only the owner states the set), and | ||
| behavioural parity through the owner's own ``source_freshness_row`` gate, so a | ||
| migration that silently changed which statuses count as success fails here. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import ast | ||
| import pathlib | ||
| from datetime import datetime, timezone | ||
|
|
||
| import pytest | ||
|
|
||
| from loopx.capabilities.decision_context import ( | ||
| assembler as assembler_mod, | ||
| capture as capture_mod, | ||
| freshness as owner, | ||
| sources as sources_mod, | ||
| ) | ||
|
|
||
| REPO_ROOT = pathlib.Path(__file__).resolve().parents[2] | ||
| PACKAGE_ROOT = REPO_ROOT / "loopx" | ||
| OWNER_PATH = "loopx/capabilities/decision_context/freshness.py" | ||
| CAPTURE_PATH = "loopx/capabilities/decision_context/capture.py" | ||
| ASSEMBLER_PATH = "loopx/capabilities/decision_context/assembler.py" | ||
|
|
||
| SUCCESS = frozenset({"completed", "no_change"}) | ||
| FAILURE = frozenset({"provider_failed", "failed", "unavailable"}) | ||
| SCAN_VOCABULARY = frozenset({"completed", "failed", "no_change", "unavailable"}) | ||
|
|
||
|
|
||
| def _members(node: ast.expr) -> frozenset[str] | None: | ||
| """Fold a set / frozenset / list / tuple of plain string literals to members.""" | ||
|
|
||
| if isinstance(node, ast.Call): | ||
| fname = getattr(node.func, "id", None) or getattr(node.func, "attr", None) | ||
| if fname in {"frozenset", "set"} and len(node.args) == 1: | ||
| return _members(node.args[0]) | ||
| return None | ||
| if isinstance(node, (ast.Set, ast.List, ast.Tuple)): | ||
| values = [ | ||
| element.value | ||
| for element in node.elts | ||
| if isinstance(element, ast.Constant) and isinstance(element.value, str) | ||
| ] | ||
| if len(values) == len(node.elts) and values: | ||
| return frozenset(values) | ||
| return None | ||
|
|
||
|
|
||
| def _status_literal_sites(source: str) -> list[int]: | ||
| """Every collection literal in the source folding to SUCCESS, counted once. | ||
|
|
||
| ``frozenset({...})`` is walked as both the call and its inner set; the inner | ||
| node is skipped so one restatement is one site, not two. | ||
| """ | ||
|
|
||
| tree = ast.parse(source) | ||
| wrapped = { | ||
| id(node.args[0]) | ||
| for node in ast.walk(tree) | ||
| if isinstance(node, ast.Call) | ||
| and (getattr(node.func, "id", None) or getattr(node.func, "attr", None)) | ||
| in {"frozenset", "set"} | ||
| and len(node.args) == 1 | ||
| } | ||
| return [ | ||
| node.lineno | ||
| for node in ast.walk(tree) | ||
| if id(node) not in wrapped and _members(node) == SUCCESS | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def tree_offenders() -> dict[str, list[int]]: | ||
| offenders: dict[str, list[int]] = {} | ||
| for path in sorted(PACKAGE_ROOT.rglob("*.py")): | ||
| relative = path.relative_to(REPO_ROOT).as_posix() | ||
| if relative == OWNER_PATH: | ||
| continue | ||
| sites = _status_literal_sites(path.read_text(encoding="utf-8")) | ||
| if sites: | ||
| offenders[relative] = sites | ||
| return offenders | ||
|
|
||
|
|
||
| def test_only_the_owner_states_the_success_set(tree_offenders) -> None: | ||
| assert tree_offenders == {}, ( | ||
| f"a restatement of the successful-read statuses remains: {tree_offenders}" | ||
| ) | ||
|
|
||
|
|
||
| def test_the_owner_states_the_recorded_members_once() -> None: | ||
| assert frozenset(owner._SUCCESS_STATUSES) == SUCCESS | ||
| source = (REPO_ROOT / OWNER_PATH).read_text(encoding="utf-8") | ||
| assert len(_status_literal_sites(source)) == 1 | ||
|
|
||
|
|
||
| def test_capture_borrows_the_owner_object() -> None: | ||
| assert capture_mod._SUCCESS_STATUSES is owner._SUCCESS_STATUSES | ||
|
|
||
|
|
||
| def test_assembler_borrows_the_owner_object() -> None: | ||
| assert assembler_mod._SUCCESS_STATUSES is owner._SUCCESS_STATUSES | ||
|
|
||
|
|
||
| def test_capture_retired_its_restated_name() -> None: | ||
| assert not hasattr(capture_mod, "_READ_SUCCESS_STATUSES") | ||
| source = (REPO_ROOT / CAPTURE_PATH).read_text(encoding="utf-8") | ||
| assert _status_literal_sites(source) == [] | ||
| assert "_SUCCESS_STATUSES" in source | ||
|
|
||
|
|
||
| def test_assembler_has_no_inline_restatement() -> None: | ||
| source = (REPO_ROOT / ASSEMBLER_PATH).read_text(encoding="utf-8") | ||
| assert _status_literal_sites(source) == [] | ||
| assert "_SUCCESS_STATUSES" in source | ||
|
|
||
|
|
||
| def test_the_failure_neighbour_is_a_different_set() -> None: | ||
| # capture keeps a distinct failure set; single-sourcing success must not | ||
| # collapse or delete it. | ||
| assert frozenset(capture_mod._READ_FAILURE_STATUSES) == FAILURE | ||
| assert SUCCESS.isdisjoint(FAILURE) | ||
|
|
||
|
|
||
| def test_the_scan_vocabulary_is_a_different_set() -> None: | ||
| # The full status vocabulary is a superset, not the success set; it stays | ||
| # its own owner in sources.py and is deliberately left untouched. | ||
| assert frozenset(sources_mod._SCAN_STATUSES) == SCAN_VOCABULARY | ||
| assert SUCCESS < SCAN_VOCABULARY | ||
| assert SUCCESS != SCAN_VOCABULARY | ||
|
|
||
|
|
||
| # --- behavioural parity through the owner's own freshness gate -------------------------- | ||
|
|
||
| OBSERVED_AT = datetime(2026, 7, 25, 17, 30, tzinfo=timezone.utc) | ||
|
|
||
|
|
||
| def _spec() -> sources_mod.DecisionSourceSpec: | ||
| return sources_mod.DecisionSourceSpec( | ||
| source_id="source:owner", | ||
| provider_id="source-provider", | ||
| source_kind="person", | ||
| priority="p0", | ||
| evidence_level="s2", | ||
| objectives=("adoption",), | ||
| scan_mode="incremental", | ||
| exact_read_policy="material_change", | ||
| freshness_seconds=3600, | ||
| private_locator="private-owner-locator", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("status", sorted(SUCCESS)) | ||
| def test_a_successful_attempt_does_not_alert(status: str) -> None: | ||
| row = owner.source_freshness_row( | ||
| source=_spec(), | ||
| observed_at=OBSERVED_AT, | ||
| last_read_at=OBSERVED_AT.isoformat(), | ||
| last_attempt_status=status, | ||
| failure_streak=0, | ||
| ) | ||
| assert row["status"] == "fresh" | ||
| assert row["alert"] is False | ||
| assert row["alert_reasons"] == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("status", sorted(capture_mod._READ_FAILURE_STATUSES)) | ||
| def test_a_failed_attempt_alerts_with_the_status(status: str) -> None: | ||
| # The read time is fresh, yet a non-success attempt still raises an alert: | ||
| # this is exactly the owner gate the consumers now share by identity. | ||
| row = owner.source_freshness_row( | ||
| source=_spec(), | ||
| observed_at=OBSERVED_AT, | ||
| last_read_at=OBSERVED_AT.isoformat(), | ||
| last_attempt_status=status, | ||
| failure_streak=0, | ||
| ) | ||
| assert row["status"] == "fresh" | ||
| assert row["alert"] is True | ||
| assert row["alert_reasons"] == [f"last_attempt_{status}"] | ||
|
|
||
|
|
||
| # --- the census bites every restatement form and leaves the others ---------------------- | ||
|
|
||
| POISON_CORPUS = ( | ||
| ("bare set restatement", 'X = {"completed", "no_change"}\n', 1), | ||
| ("frozenset call", 'X = frozenset({"completed", "no_change"})\n', 1), | ||
| ("set call over a list", 'X = set(["completed", "no_change"])\n', 1), | ||
| ("list literal", 'X = ["completed", "no_change"]\n', 1), | ||
| ("tuple literal", 'X = ("completed", "no_change")\n', 1), | ||
| ("reordered members fold the same", 'X = {"no_change", "completed"}\n', 1), | ||
| ("renamed binding is still caught", '_ANY = {"completed", "no_change"}\n', 1), | ||
| ("nested inside a function", 'def f():\n return {"completed", "no_change"}\n', 1), | ||
| ("importing the owner is not a restatement", "from .m import _SUCCESS_STATUSES\n", 0), | ||
| ("alias rebind is not a restatement", "_READ_SUCCESS_STATUSES = _SUCCESS_STATUSES\n", 0), | ||
| ("the full scan vocabulary is not this set", '_SCAN = {"completed", "failed", "no_change", "unavailable"}\n', 0), | ||
| ("the ordered report tuple is not this set", 'X = ("completed", "no_change", "failed", "unavailable")\n', 0), | ||
| ("a superset is not this set", 'X = {"completed", "no_change", "failed"}\n', 0), | ||
| ("a single status is not this set", 'X = {"completed"}\n', 0), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "label,source,expected", POISON_CORPUS, ids=[case[0] for case in POISON_CORPUS] | ||
| ) | ||
| def test_the_census_names_every_form_and_leaves_the_others( | ||
| label: str, source: str, expected: int | ||
| ) -> None: | ||
| assert len(_status_literal_sites(source)) == expected, label | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] 只在已知的成功判定位置阻止重述。 将 _source_coverage 的报表顺序写成 (*("completed", "no_change"), "failed", "unavailable") 后,真实 prepare-evidence 的键顺序、计数、覆盖和 freshness 保持一致,新增测试却退出 1,报 assembler.py:176。请把硬检查限定到状态判定,保留有序报表数据的反例。