diff --git a/sisi/workers.py b/sisi/workers.py index 49810a4..4b81cc5 100644 --- a/sisi/workers.py +++ b/sisi/workers.py @@ -656,6 +656,21 @@ def load_prefs(): return out +def effective_prefs(kind, difficulty, project): + """The user's avoid/prefer patterns plus the routing adjustments a learning cycle wrote that apply in + this project for this kind (and difficulty, when the adjustment names one). A learned entry carries + its learning id as its reason, so `sisi route --explain` shows where it came from.""" + from . import cycle + prefs = load_prefs() + for a in cycle.routing_adjustments(project): + if a.get("kind") != kind or a.get("difficulty") not in (None, difficulty): + continue + for key in ("avoid", "prefer"): + if a.get(key): + prefs[key].append({"pattern": str(a[key]), "why": "learning %s" % a.get("learning")}) + return prefs + + def _fact_dates(): dates = [] prices = load_prices() @@ -994,9 +1009,9 @@ def run_delegation(candidates, kind, difficulty, prompt, cwd, explicit=None, rev timeout = DEFAULT_RUN_TIMEOUT_S tried = [] cand = explicit - prefs = load_prefs() while True: if cand is None: + prefs = effective_prefs(kind, difficulty, cwd) result = routing.choose(candidates, read_records(), read_availability(), kind, difficulty, review_of=review_of, now=time.time(), exclude=tried, avoid=prefs["avoid"], prefer=prefs["prefer"]) @@ -1303,7 +1318,7 @@ def cmd_route(args=()): kind, difficulty = setup records = read_records() candidates, cold, excluded = routable_candidates(build_candidates(), records) - prefs = load_prefs() + prefs = effective_prefs(kind, difficulty, os.getcwd()) result = routing.choose(candidates, records, read_availability(), kind, difficulty, review_of=_opt(args, "--review-of"), now=time.time(), avoid=prefs["avoid"], prefer=prefs["prefer"]) diff --git a/tests/test_learning_loop.py b/tests/test_learning_loop.py index f78f806..0b5e179 100644 --- a/tests/test_learning_loop.py +++ b/tests/test_learning_loop.py @@ -542,6 +542,34 @@ class Mechanisms(Base): self.assertEqual(len(cycle.routing_adjustments(self.proj)), 1) self.assertEqual(cycle.routing_adjustments(self.other_project()), []) + def test_a_promoted_routing_adjustment_changes_sisi_route_for_its_project_only(self): + """R-11.1-7, R-7.1-1: the reader of routing-adjustments.json is `sisi route`: a cycle's avoid entry + moves the choice in its own project, names its learning in --explain, and leaves another project alone.""" + bindir = os.path.join(self.tmp, "bin") + os.makedirs(bindir) + tool = os.path.join(bindir, "muse") + with open(tool, "w") as f: + f.write('#!/bin/sh\nif [ "$1 $2" = "exec --list-models" ]; then echo beta-1; echo beta-2; fi\nexit 0\n') + os.chmod(tool, 0o755) + env = {"PATH": bindir} + for m, price in (("beta-1", "1"), ("beta-2", "2")): + r = self.sisi("workers", "fact", "muse/" + m, "--price", price, "--source", "fixture", env=env) + self.assertEqual(r.returncode, 0, r.stderr) + other = self.other_project() + route = ("route", "--kind", "impl", "--difficulty", "2", "--explain") + before, before_other = self.sisi(*route, env=env), self.sisi(*route, cwd=other, env=env) + self.assertEqual(before.stdout.splitlines()[0], "muse/beta-1", before.stdout) + self.assertEqual(before_other.stdout, before.stdout) + self.propose() + self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "routing", + "kind": "impl", "avoid": "beta-1"}]).returncode, 0) + after, after_other = self.sisi(*route, env=env), self.sisi(*route, cwd=other, env=env) + self.assertEqual(after.stdout.splitlines()[0], "muse/beta-2", after.stdout) + self.assertIn("learning L-0001", after.stdout) + self.assertEqual(after_other.stdout, before_other.stdout) + unrelated = self.sisi("route", "--kind", "review", "--difficulty", "2", env=env) + self.assertEqual(unrelated.stdout.splitlines()[0], "muse/beta-1") + def test_earned_scope_widens_what_a_cycle_may_apply(self): """R-11.6-2: once a learning has earned scope (two units, two projects) its risk adjustment reaches both projects and a rule it motivates needs no approval for reach alone."""