From 1e2a5e3909bddb5d33e3ec2a37d2146acc32c1e5 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Fri, 27 Nov 2026 14:35:06 -0500 Subject: [PATCH] Fix --codemod-owner-picks in synthesize-plan CLI and single-embedded constant_subtype_answer --- nominal_refactor_advisor/cli.py | 18 ++- .../refactors/constant_subtype_answer.py | 30 +++- tests/test_toad_bench_synthesis_fixes.py | 144 ++++++++++++++++++ 3 files changed, 185 insertions(+), 7 deletions(-) diff --git a/nominal_refactor_advisor/cli.py b/nominal_refactor_advisor/cli.py index fbfe140a..40f45453 100644 --- a/nominal_refactor_advisor/cli.py +++ b/nominal_refactor_advisor/cli.py @@ -3579,6 +3579,14 @@ class CodemodSynthesisExecutionCliCommand( ): """Shared execution surface for finding-backed synthesis commands.""" + def picked_findings(self) -> tuple[RefactorFinding, ...]: + if self.args.codemod_owner_picks is None: + return self.findings + picked, _report = OwnerPicks.read(self.args.codemod_owner_picks).applied( + self.findings + ) + return tuple(picked) + def run(self) -> int: snapshot = self.required_source_snapshot() return self.synthesis_execution(snapshot).run() @@ -3604,8 +3612,9 @@ def synthesis_execution( self, snapshot: CodemodSourceSnapshot, ) -> FindingRecipePlanSynthesisExecution: + findings = self.picked_findings() plan = snapshot.plan_from_findings( - self.findings, + findings, detector_ids=tuple(self.args.codemod_goal_detectors), ) return FindingRecipePlanSynthesisExecution( @@ -3614,7 +3623,7 @@ def synthesis_execution( CodemodPlanSequence.from_document(plan.document) ), plan_out=self.args.codemod_plan_out, - workflow_scan=CodemodWorkflowScan(modules=self.modules, findings=self.findings), + workflow_scan=CodemodWorkflowScan(modules=self.modules, findings=findings), plan=plan, ) @@ -3632,8 +3641,9 @@ def synthesis_execution( self, snapshot: CodemodSourceSnapshot, ) -> FindingRecipeClassPlanSynthesisExecution: + findings = self.picked_findings() report = codemod_class_plan_from_findings( - self.findings, + findings, root=self.roots[0], selector_context=snapshot, detector_ids=tuple(self.args.codemod_goal_detectors), @@ -3644,7 +3654,7 @@ def synthesis_execution( CodemodPlanSequence.from_document(report.finding_plan.document) ), plan_out=self.args.codemod_plan_out, - workflow_scan=CodemodWorkflowScan(modules=self.modules, findings=self.findings), + workflow_scan=CodemodWorkflowScan(modules=self.modules, findings=findings), report=report, ) diff --git a/nominal_refactor_advisor/refactors/constant_subtype_answer.py b/nominal_refactor_advisor/refactors/constant_subtype_answer.py index 888fbee7..f1eed883 100644 --- a/nominal_refactor_advisor/refactors/constant_subtype_answer.py +++ b/nominal_refactor_advisor/refactors/constant_subtype_answer.py @@ -2685,7 +2685,15 @@ def evaluate_recipe_for_finding( return self.stopped_evaluation(error) pick = finding.metrics.picked_base - consumers: list[ConstantAnswerPushConsumer] = [] + resolved_consumers: list[ + tuple[ + object, + SourceRewriteTarget, + ast.FunctionDef | ast.AsyncFunctionDef, + object, + bool, + ] + ] = [] for consumer in metrics.consumer_locations: consumer_target = SourceRewriteTarget( file_path=consumer.file_path, qualname=consumer.symbol @@ -2708,10 +2716,18 @@ def evaluate_recipe_for_finding( ) ) is_dunder = function.name.startswith("__") and function.name.endswith("__") - if raw_site.dispatch.is_whole_function and not is_dunder: + needs_pick = not (raw_site.dispatch.is_whole_function and not is_dunder) + resolved_consumers.append( + (consumer, consumer_target, function, raw_site, needs_pick) + ) + + embedded_count = sum(1 for _c, _t, _f, _s, needs_pick in resolved_consumers if needs_pick) + consumers: list[ConstantAnswerPushConsumer] = [] + for consumer, consumer_target, function, _raw_site, needs_pick in resolved_consumers: + if not needs_pick: method_name = function.name docstring: str | None = ast.get_docstring(function) - elif pick is not None and len(metrics.consumer_locations) == 1: + elif pick is not None and embedded_count == 1: method_name = pick.name docstring = pick.judgement else: @@ -2729,6 +2745,14 @@ def evaluate_recipe_for_finding( ) except ValueError as error: return self.stopped_evaluation(error) + if embedded_count > 1: + return self.stopped_evaluation( + NeedsInput( + f"multiple embedded branches ({embedded_count}) on `{metrics.member_name}` " + f"can push onto `{base_indexed.qualname}` but need distinct method names", + NeededInput.SOURCE_CHANGE, + ) + ) return self.stopped_evaluation( NeedsInput( f"embedded branch on `{metrics.member_name}` in `{consumer.symbol}` " diff --git a/tests/test_toad_bench_synthesis_fixes.py b/tests/test_toad_bench_synthesis_fixes.py index 9dd98bac..c80be4a6 100644 --- a/tests/test_toad_bench_synthesis_fixes.py +++ b/tests/test_toad_bench_synthesis_fixes.py @@ -3,12 +3,15 @@ from __future__ import annotations import importlib.util +import json +import sys from pathlib import Path from nominal_refactor_advisor.ast_tools import ( PythonSourcePathPolicy, parse_python_modules, ) +from nominal_refactor_advisor.cli import main as cli_main from nominal_refactor_advisor.codemod import CodemodPlanSequence, CodemodSourceSnapshot from nominal_refactor_advisor.detectors._base import DetectorConfig from nominal_refactor_advisor.detectors._relocations import ConstantSubtypeAnswerDetector @@ -321,3 +324,144 @@ def test_constant_subtype_compound_and_uniform_early_return_without_tail_pushes( assert mod.maybe_reveal(mod.FragmentLocation(), True, 0, 20, "H") == "fallback" assert mod.maybe_reveal(mod.UnloadedFragment(), True, 5, 20, "H") == "fallback" assert mod.maybe_reveal(mod.FragmentLocation(), False, 5, 20, "H") == "fallback" + + +def test_codemod_synthesize_plan_cli_applies_owner_picks( + tmp_path: Path, capsys, monkeypatch +) -> None: + """`--codemod-synthesize-plan --codemod-owner-picks` applies owner picks instead of + silently ignoring them while printing 'supply owner ... in --codemod-owner-picks'.""" + _write( + tmp_path, + { + "pkg/__init__.py": "", + "pkg/metrics.py": ( + "from dataclasses import dataclass\n\n\n" + "@dataclass\n" + "class RowMetrics:\n" + " extent: int | None = None\n\n\n" + "def row_extent(table: dict[int, RowMetrics], width: int) -> int:\n" + " entry = table.get(width)\n" + " return width if entry is None or entry.extent is None else entry.extent\n" + ), + }, + ) + modules = _parse(tmp_path) + with ScanCache.scope(): + (finding,) = ForeignAbsenceProbeDetector().detect(modules, DetectorConfig()) + picks_path = tmp_path / "picks.json" + picks_path.write_text( + json.dumps( + { + "picks": [ + { + "finding": finding.stable_id, + "owner": "row_extent", + "because": "RowMetrics owns its effective extent.", + "name": "extent_or", + "judgement": "Return recorded extent or fallback width.", + } + ] + } + ), + encoding="utf-8", + ) + monkeypatch.setattr( + sys, + "argv", + [ + "nra", + "--no-cache", + "--codemod-synthesize-plan", + "--codemod-goal-detector", + "foreign_absence_probe", + "--codemod-owner-picks", + str(picks_path), + "--json", + str(tmp_path / "pkg"), + ], + ) + rc = cli_main() + assert rc == 0 + payload = json.loads(capsys.readouterr().out) + assert payload["synthesis_report"]["candidate_count"] == 1 + assert payload["expected_removed_finding_ids"] == [finding.stable_id] + + +def test_constant_subtype_single_embedded_plus_whole_function_uses_owner_pick( + tmp_path: Path, +) -> None: + """When one consumer is a whole-function dispatch and only one consumer is embedded + (Toad `SidebarModelState.shows_model`: `ChannelTreeSidebar.sync` + `.present`), a single + `OwnerPick` names the embedded consumer instead of being ignored because total consumers > 1.""" + _write( + tmp_path, + { + "pkg/__init__.py": "", + "pkg/sidebar.py": ( + "from dataclasses import dataclass\n\n\n" + "class SidebarModelState:\n" + " def shows_model(self) -> bool:\n" + " return False\n\n\n" + "@dataclass\n" + "class Current(SidebarModelState):\n" + " shown: str\n\n" + " def shows_model(self) -> bool:\n" + " return True\n\n\n" + "class ChannelTreeSidebar:\n" + " model_state: SidebarModelState\n\n" + " def __init__(self, state: SidebarModelState) -> None:\n" + " self.model_state = state\n" + " self.events: list[str] = []\n\n" + " def present(self) -> str | None:\n" + " model = self.model_state.shown if self.model_state.shows_model() else None\n" + " self.events.append(f'present:{model}')\n" + " return model\n\n" + " def sync(self, tag: str) -> None:\n" + " if not self.model_state.shows_model():\n" + " return\n" + " self.events.append(f'sync:{self.model_state.shown}:{tag}')\n" + ), + }, + ) + modules = _parse(tmp_path) + snap = CodemodSourceSnapshot.from_modules(modules) + with ScanCache.scope(): + (finding,) = ConstantSubtypeAnswerDetector().detect(modules, DetectorConfig()) + picks = OwnerPicks( + "picks.json", + "", + ( + OwnerPick( + finding.stable_id, + finding.metrics.consumer_symbol, + "SidebarModelState owns whether a model is shown.", + "shown_model", + "Return the shown model when present.", + ), + ), + ) + (picked,), report = picks.applied((finding,)) + assert not report.unmatched + evaluation = ConstantSubtypeAnswerDetector().evaluate_recipe_for_finding(picked, snap) + assert evaluation.rejection_reason == "" + sim = CodemodPlanSequence.from_operations( + evaluation.required_recipe.operations + ).simulate(snap) + assert sim.is_clean + sim.apply() + + with ScanCache.scope(): + assert ConstantSubtypeAnswerDetector().detect(_parse(tmp_path), DetectorConfig()) == [] + spec = importlib.util.spec_from_file_location("pkg_sidebar", tmp_path / "pkg/sidebar.py") + assert spec is not None and spec.loader is not None + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + empty = mod.ChannelTreeSidebar(mod.SidebarModelState()) + assert empty.present() is None + empty.sync("a") + assert empty.events == ["present:None"] + cur = mod.ChannelTreeSidebar(mod.Current("m1")) + assert cur.present() == "m1" + cur.sync("a") + assert cur.events == ["present:m1", "sync:m1:a"]