From cda61338531a0e8a2ad45819bb7cb7f402452f3b Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 15:20:53 -0700 Subject: [PATCH] Integration points 1-6: one refusal at a time, routing decision in the meter, ledger kinds, retry-of on retry_choice, router lines at difficulty 1 and 2, rule statements name their code. done: points 1-6 / next: fixes A-D, tests --- canon/guard.json | 1 + sisi/rulecmd.py | 61 ++++++++++++++++++++++------------------- tests/test_meter.py | 30 +++++++++++++------- tests/test_unitguard.py | 8 +++--- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/canon/guard.json b/canon/guard.json index 28d22b7..00f4e07 100644 --- a/canon/guard.json +++ b/canon/guard.json @@ -65,6 +65,7 @@ "without changing", "without modifying", "no changes", + "files you may change: none", "report only", "only report", "do not create" diff --git a/sisi/rulecmd.py b/sisi/rulecmd.py index 9c8b3d8..2ef1fff 100644 --- a/sisi/rulecmd.py +++ b/sisi/rulecmd.py @@ -86,51 +86,51 @@ def _own_the_outcome(): def _size_by_risk(): return _say(enforced=["the end-of-turn gate computes the risk floor of the change, takes the profile the contract declares " - "(lowering it needs a reason) and sends the agent back for whatever that profile requires" + "(lowering it needs a reason) and sends the agent back for whatever that profile requires (sisi/gate.py, sisi/risk.py, sisi/assure.py)" if _gate_wired() and _has("risk", "assess", "requirements_for") and _has("assure", "effective_profile", "missing") else None, - "a contract without a declared `risk.profile` is invalid" + "a contract without a declared `risk.profile` is invalid (sisi/contract.py)" if _has("contract", "RISK_PROFILES") and _has("assure", "missing") else None, "the before-tool hook works out the risk floor of the path about to change (with what this session " "already changed) and refuses %s unless a valid unit stands behind it; the floor wins over a lower " - "declared profile without a reason" % _guard_scope() if _guard_scope() else None], - measured=["`sisi risk` prints the floor and what the profile requires, the unit record keeps the profile" + "declared profile without a reason (sisi/unitguard.py, sisi/watch.py)" % _guard_scope() if _guard_scope() else None], + measured=["`sisi risk` prints the floor and what the profile requires, the unit record keeps the profile (sisi/risk.py, sisi/unit.py)" if _has("risk", "assess") else None], judgement=["raising the profile when you know better; lowering it needs a reason"]) def _define_done(): - return _say(enforced=["contract check refuses a check with no command or one that cannot fail (vacuous)" + return _say(enforced=["contract check refuses a check with no command or one that cannot fail (vacuous) (sisi/contract.py)" if _has("contract", "vacuous_reason") else None, "end-of-turn gate sends the agent back when the profile needs a contract and there is none, " - "or when a check lacks what it covers, invariants or the requirement list are missing" + "or when a check lacks what it covers, invariants or the requirement list are missing (sisi/gate.py, sisi/assure.py)" if _gate_wired() and _has("assure", "missing") else None, "%s is refused before it runs unless this session holds a valid unit: opened or continued by it " "(`sisi unit continue`), not stale (`unit.stale_hours`), `sisi contract check` passing with no placeholder, " - "and carrying what its risk profile needs before building" % _guard_scope() + "and carrying what its risk profile needs before building (sisi/unitguard.py, called from sisi/watch.py)" % _guard_scope() if _guard_scope() else None, "`sisi unit pause` is refused without such a unit (a pause defers waiting checks and the retro, never the " - "contract), and a pause silences the gate only for the session holding the unit" + "contract), and a pause silences the gate only for the session holding the unit (sisi/unitguard.py, sisi/unit.py)" if _has("unitguard", "pause_refusal") else None, "the gate never judges a session's changes against a unit the session does not hold: it sends back " - "to continue, park or init" if _gate_wired() and _has("unitguard", "bound_fresh") else None, - "every refusal is kept in the unit record and counted by `sisi status`" + "to continue, park or init (sisi/gate.py)" if _gate_wired() and _has("unitguard", "bound_fresh") else None, + "every refusal is kept in the unit record and counted by `sisi status` (sisi/unitguard.py, sisi/scorecard.py)" if _has("unitguard", "record_refusal") else None], judgement=["the goal in the person's words, one check on the purpose, invariants captured before the first edit"]) def _verify_enough(): - return _say(enforced=["`sisi verify` runs the checks with five statuses (passed, failed, skipped, not-applicable, not-run), nothing unrun passes" + return _say(enforced=["`sisi verify` runs the checks with five statuses (passed, failed, skipped, not-applicable, not-run), nothing unrun passes (sisi/verify.py)" if _has("verify", "STATUSES") else None, - "the end-of-turn gate runs the fast checks and sends the agent back on a failure, with the failing evidence lines" + "the end-of-turn gate runs the fast checks and sends the agent back on a failure, with the failing evidence lines (sisi/gate.py)" if _gate_wired() and _has("assure", "failure_lines") else None, "high-risk profiles need evidence from someone or something other than the author: a check marked " "independent does not count when `by` is empty, the session's own model or a self-reference, and a " - "review counts only from a worker other than the author's model, judged with a check stronger than none" + "review counts only from a worker other than the author's model, judged with a check stronger than none (sisi/assure.py, sisi/unitguard.py)" if _has("assure", "independent_evidence") and _has("unitguard", "is_self_reference") else - ("high-risk profiles need evidence from someone or something other than the author" + ("high-risk profiles need evidence from someone or something other than the author (sisi/assure.py)" if _has("assure", "independent_evidence") else None)], - measured=["a commit or merge with no passing `sisi verify` for the tree gets a notice, never a block" + measured=["a commit or merge with no passing `sisi verify` for the tree gets a notice, never a block (sisi/watch.py)" if _has("watch", "commit_notice") else None], judgement=["choosing the technique, which checks the change can affect, watching a test fail once"]) @@ -152,37 +152,42 @@ def _pre_tool_wired(): def _orchestrate(): meter = _pre_tool_wired() and _has("orchestration", "before", "cmd_direct") - return _say(enforced=["a delegation brief without outcome, acceptance or files it may change is refused (`sisi delegate`)" + return _say(enforced=["a delegation brief without outcome, acceptance or files it may change is refused (`sisi delegate`; sisi/brief.py, sisi/workers.py)" if _has("workers", "cmd_delegate") and _has("brief", "missing_required") else None, "the before-tool hook counts the main thread's direct tool calls and result characters for every tool, and " "refuses the next one past the budget (`orchestrate.direct_calls`, `orchestrate.direct_chars`), naming the router's " - "workers; `sisi unit direct \"\"` extends the turn with a specific reason (a generic one is refused)" + "workers, asked at difficulty 1 for reading and 2 for building; `sisi unit direct \"\"` extends the turn with a specific reason " + "(a generic one is refused) (sisi/orchestration.py)" if meter else None, "a delegation that names no model, or the session's own model, is refused with the router's pick; an allowed " - "one has its brief checked and is written to the ledger as a run" + "one has its brief checked and is written to the ledger as a run (sisi/orchestration.py)" if meter and _has("orchestration", "_before_delegation") else None, - "the end-of-turn gate asks for the judgement of each finished delegation, once per set of runs" + "the end-of-turn gate asks for the judgement of each finished delegation, once per set of runs (sisi/orchestration.py, sisi/gate.py)" if _gate_wired() and _has("orchestration", "judgement_sendback") else None, - "a shell command that failed three times in a row with no file changed is refused once (rule 5)" + "a shell command that failed three times in a row with no file changed is refused once (rule 5) (sisi/orchestration.py)" if meter else None, "an unavailable worker (a limit, quota, login or connection failure arriving as a delegation result or as " "a later notice) is recorded down with the reset time parsed from the message, the router's next worker " "is handed over for the same brief; a question-dialog call right after it is refused once, ending the turn with no fallback " - "started is sent back once, and so is a final message that names the unavailable worker" + "started is sent back once, and so is a final message that names the unavailable worker (sisi/fallback.py, sisi/orchestration.py)" if meter and _has("fallback", "handle", "fallback_text") else None, "a delegation with no `sisi route` decision in the last minutes (`orchestrate.route_window_min`) is refused once; " - "the run keeps the kind and difficulty guess, and a worker other than the router's pick is recorded as an override" + "the run keeps the kind (one of the ledger's, else `sisi route` refuses it) and difficulty guess, and a worker other than the router's pick " + "is recorded as an override (sisi/orchestration.py, sisi/workers.py)" if meter and _has("orchestration", "record_route") else None, - "`sisi route --retry-of ID` keeps the worker with a better brief after a first rejection and moves to a stronger " - "one after a second" - if _has("workers", "retry_context", "stronger_choice") else None], - measured=["the delegation ledger and `sisi delegate judge`, `sisi route` recommends the worker from that record" + "`sisi route --retry-of ID` keeps the worker with a better brief after a first rejection, then moves to a worker whose " + "evidence is strictly better (sisi/routing.py retry_choice, sisi/workers.py)" + if _has("workers", "retry_context") and _has("routing", "retry_choice") else None, + "the before-tool hook answers one refusal at a time, in a fixed order: workers are exempt, then the contract " + "guard, then the delegation guard, then the direct-work budget (sisi/watch.py)" + if meter and _guard_scope() else None], + measured=["the delegation ledger and `sisi delegate judge`, `sisi route` recommends the worker from that record (sisi/routing.py, sisi/workers.py)" if _has("routing", "choose") and _has("workers", "cmd_delegate") else None, "the unit record and `sisi status` show direct calls and characters, refusals, extensions with their reasons, " - "delegations started, judged and unjudged, and the share of tool calls delegated" + "delegations started, judged and unjudged, and the share of tool calls delegated (sisi/orchestration.py, sisi/scorecard.py)" if _has("orchestration", "telemetry") else None, "`sisi workers` shows how often the router was overridden and how the overrides turned out; a model discovery " - "has not seen before is named once at session start as unproven" + "has not seen before is named once at session start as unproven (sisi/workers.py)" if _has("workers", "override_line", "new_worker_line") else None], judgement=["who gets the task, how much to check what comes back, the quality of a brief"]) diff --git a/tests/test_meter.py b/tests/test_meter.py index a08c568..ace6c2f 100644 --- a/tests/test_meter.py +++ b/tests/test_meter.py @@ -11,7 +11,7 @@ from sisi import orchestration, store, workers def seed_workers(case, names=("alpha-small", "beta-large")): """A ledger with two usable workers (host-native, judged), the first the cheaper.""" for i, w in enumerate(names): - for t in ("research", "implementation"): + for t in ("read", "impl"): workers.append_record({"kind": "attempt", "id": "%s%s" % (w[:3], t[:2]), "run_id": None, "ts": 1, "project": "p", "type": t, "difficulty": 1, "worker": w, "outcome": "accepted", "check": "read"}) @@ -532,7 +532,7 @@ class FallbackTests(MeterBase): json.dump({"models": {"provx/model-1": {"cost_per_million_tokens": 0.1}, "alpha-small": {"cost_per_million_tokens": 1}, "beta-large": {"cost_per_million_tokens": 9}}}, f) - for t in ("research", "implementation"): + for t in ("read", "impl"): workers.append_record({"kind": "attempt", "id": "px" + t[:2], "run_id": None, "ts": 1, "project": "p", "type": t, "difficulty": 1, "worker": "provx/model-1", "outcome": "accepted", "check": "read"}) @@ -571,6 +571,15 @@ class FallbackTests(MeterBase): self.assertNotIn("tell them", ctx.lower()) # worker availability is internal (SPEC 9.7, 17) self.assertIn("Cancel or interrupt", ctx) + def test_a_failure_notice_gets_the_fallback_and_no_recipe(self): + """The notice is a machine's message: the recipe matcher does not run on it, the fallback still goes through.""" + self.prompt() + self.launch() + ctx = self.context(self.prompt(self.notice(self.QUOTA + " (task: cut a new release and tag it)"))) + self.assertIn("provx/model-1 is unavailable", ctx) + self.assertNotIn("recipe", ctx) + self.assertNotIn("Done means", ctx) + def test_the_same_through_a_delegation_tool_result(self): self.prompt() self.pre("Agent", {"description": "d", "prompt": "Outcome: x", "model": "provx/model-1"}, tool_use_id="t9") @@ -832,17 +841,17 @@ class RouteDecisionTests(MeterBase): def test_after_sisi_route_the_delegation_is_allowed_and_its_run_carries_the_guess(self): self.prompt(route=False) - self.route("implementation", "2") + self.route("review", "2") denied, ctx, _ = self.deleg() self.assertFalse(denied) - self.assertIn("(implementation, difficulty 2)", ctx) + self.assertIn("(review, difficulty 2)", ctx) self.assertNotIn("--type", ctx) # judge needs only the outcome and the check run = self.finish("d1") self.assertEqual((run["type"], run["difficulty"], run["route_worker"], run["chosen_against_route"]), - ("implementation", 2, "alpha-small", False)) + ("review", 2, "alpha-small", False)) self.judge(run, "accepted") att = [r for r in workers.read_records() if r.get("kind") == "attempt" and r.get("run_id") == run["id"]][0] - self.assertEqual((att["type"], att["difficulty"]), ("implementation", 2)) + self.assertEqual((att["type"], att["difficulty"]), ("review", 2)) def test_a_stale_decision_is_no_decision(self): self.prompt(route=False) @@ -907,9 +916,9 @@ class RouteDecisionTests(MeterBase): att2 = self.judge(second, "rejected")[0] r = self.sisi("route", "--retry-of", att2, env={"SISI_SESSION_ID": self.sid}) self.assertEqual(r.stdout.splitlines()[0], "beta-large") # the second rejection: excluded, the next one up - self.assertIn("alpha-small was rejected twice", r.stdout) + self.assertIn("retry after 2 rejection(s) by alpha-small", r.stdout) - def test_retry_of_uses_the_benchmark_standing_when_the_record_is_thin(self): + def test_retry_of_after_two_rejections_takes_the_cheapest_worker_with_strictly_better_evidence(self): with open(os.path.join(self.data, "model-strength.json"), "w") as f: json.dump({"models": {"alpha-small": {"score": 50, "source": "s", "date": "2026-01-01"}, "beta-large": {"score": 70, "source": "s", "date": "2026-01-01"}, @@ -921,8 +930,9 @@ class RouteDecisionTests(MeterBase): "difficulty": 1, "worker": w, "outcome": "rejected", "check": "read", "retry_of": "rj0" if i else None}) r = self.sisi("route", "--retry-of", "rj1", env={"SISI_SESSION_ID": self.sid}) - self.assertEqual(r.stdout.splitlines()[0], "gamma-mid") - self.assertIn("stronger", r.stdout) + # routing.retry_choice: not the rejected worker; of those with better evidence, the cheapest (not the top standing) + self.assertEqual(r.stdout.splitlines()[0], "beta-large") + self.assertIn("strictly better evidence", r.stdout) def test_retry_of_an_unknown_or_unjudged_run_is_an_error(self): r = self.sisi("route", "--retry-of", "nope") diff --git a/tests/test_unitguard.py b/tests/test_unitguard.py index e3145c2..86200c8 100644 --- a/tests/test_unitguard.py +++ b/tests/test_unitguard.py @@ -10,7 +10,7 @@ The abuse cases are written from the side of an agent that wants to skip.""" import contextlib, io, json, os, subprocess, sys, time, unittest from unittest import mock from support import * -from sisi import assure, contract as C, gate, gitutil, store, unit as U, unitguard as G, watch +from sisi import assure, contract as C, gate, gitutil, orchestration, store, unit as U, unitguard as G, watch SRC = "src/app.py" @@ -477,9 +477,9 @@ class DelegationTests(GuardCase): return self.before(tool, {"description": "worker", "prompt": prompt, "subagent_type": "general-purpose"}, **kw) def route(self, kind): - s = store.load_session(self.sid) - s["route"] = {"kind": kind} - store.save_session(self.sid, s) + """A routing decision as `sisi route` records it: in the orchestration meter, the one place it lives.""" + with orchestration.Meter(self.sid) as m: + m["route"] = {"kind": kind, "difficulty": 2, "worker": "w", "ts": time.time()} def test_delegating_a_build_with_no_unit_is_refused_and_says_where_the_acceptance_comes_from(self): for tool in ("Agent", "Task"):