344 lines
17 KiB
Python
344 lines
17 KiB
Python
"""The plan: a dependency graph recorded before any work is handed out, and the hooks that hold the lead to it."""
|
|
import io, json, os, sys, unittest
|
|
from unittest import mock
|
|
import test_core # the module, not its class: its tests must not run twice
|
|
sys.path.insert(0, test_core.REPO)
|
|
|
|
TABLE = test_core.HEADER + (
|
|
"| alpha | host subagent model `alpha` | routine | free | quick | |\n"
|
|
"| beta | host subagent model `beta` | ordinary | free | edits | |\n"
|
|
"| gamma | host subagent model `gamma` | hard | per use | everything | |\n")
|
|
COLS = "| node | class | needs | branch | files | check | why |\n|---|---|---|---|---|---|---|\n"
|
|
PLAN = COLS + (
|
|
"| a | routine | - | p-a | sisi/a.py | test a passes | exact spec, test decides |\n"
|
|
"| b | ordinary | - | p-b | sisi/b.py | test b passes | some judgement |\n"
|
|
"| c | hard | a, b | p-c | sisi/c.py | test c passes | design judgement, redo costly |\n"
|
|
"| join | lead | c | - | - | full suite passes | the lead merges |\n")
|
|
|
|
|
|
class Plan(unittest.TestCase):
|
|
ENFORCE = True
|
|
setUp, project, sisi, hook, tool, done, table, state, read, write, in_process = (
|
|
test_core.Core.setUp, test_core.Core.project, test_core.Core.sisi, test_core.Core.hook, test_core.Core.tool, test_core.Core.done,
|
|
test_core.Core.table, test_core.Core.state, test_core.Core.read, test_core.Core.write, test_core.Core.in_process)
|
|
|
|
def record(self, text, sid="s1"):
|
|
return self.sisi("plan", "--record", self.write(os.path.join(self.tmp, "plan.md"), text), sid=sid)
|
|
|
|
def refused(self, text, why):
|
|
rc, out, _ = self.record(text)
|
|
self.assertEqual(rc, 1, out)
|
|
self.assertIn(why, out)
|
|
self.assertIn("Nothing was recorded", out)
|
|
self.assertNotIn("plan", self.state() if os.path.exists(os.path.join(self.tmp, "state", "core-sessions", "s1.json")) else {})
|
|
|
|
def ready_plan(self, sid="s1", text=PLAN):
|
|
self.table(TABLE)
|
|
self.hook("prompt", sid=sid, prompt="build it")
|
|
self.done(sid)
|
|
rc, out, _ = self.record(text, sid)
|
|
self.assertEqual(rc, 0, out)
|
|
return out
|
|
|
|
def pick(self, node, sid="s1"):
|
|
rc, out, _ = self.sisi("pick", "--node", node, sid=sid)
|
|
self.assertEqual(rc, 0, out)
|
|
return out
|
|
|
|
def agent(self, model, brief, sid="s1", **extra):
|
|
return self.tool("Agent", sid=sid, tool_input=dict({"model": model, "prompt": brief, "isolation": "worktree"}, **extra))
|
|
|
|
def decision(self, res):
|
|
rc, out, err = res
|
|
self.assertEqual((rc, err), (0, ""), res)
|
|
return json.loads(out)["hookSpecificOutput"] if out.strip() else {}
|
|
|
|
def denied(self, res, why):
|
|
d = self.decision(res)
|
|
self.assertEqual(d.get("permissionDecision"), "deny", d)
|
|
self.assertIn(why, d["permissionDecisionReason"])
|
|
return d["permissionDecisionReason"]
|
|
|
|
def nodes(self, sid="s1"):
|
|
return {i: v.get("st") for i, v in self.state(sid)["plan"]["state"].items()}
|
|
|
|
# recording: each refusal
|
|
|
|
def test_refuses_a_bad_node_id(self):
|
|
self.refused(PLAN.replace("| a |", "| A_1 |"), "node id 'A_1' is not")
|
|
|
|
def test_refuses_a_duplicate_node(self):
|
|
self.refused(PLAN.replace("| b | ordinary | - | p-b | sisi/b.py", "| a | ordinary | - | p-b | sisi/b.py"), "node a appears twice")
|
|
|
|
def test_refuses_a_missing_need(self):
|
|
self.refused(PLAN.replace("| a, b |", "| a, zz |"), "node c needs zz, which is not a node")
|
|
|
|
def test_refuses_a_cycle(self):
|
|
self.refused(PLAN.replace("| a | routine | - |", "| a | routine | c |"), "cycle")
|
|
|
|
def test_refuses_an_unknown_class(self):
|
|
self.refused(PLAN.replace("| routine |", "| cheap |"), "class 'cheap'")
|
|
|
|
def test_refuses_two_worker_nodes_on_one_branch(self):
|
|
self.refused(PLAN.replace("| p-b |", "| p-a |"), "branch p-a is given to more than one node")
|
|
|
|
def test_refuses_a_worker_node_without_a_branch(self):
|
|
self.refused(PLAN.replace("| p-b |", "| - |"), "node b needs a branch of its own")
|
|
|
|
def test_refuses_parallel_nodes_that_share_files(self):
|
|
for a, b in (("sisi/a.py", "sisi/a.py"), ("sisi", "sisi/b.py"), ("sisi/*.py", "sisi/b.py")):
|
|
self.refused(PLAN.replace("| sisi/a.py |", "| %s |" % a).replace("| sisi/b.py |", "| %s |" % b),
|
|
"nodes a and b can run at the same time")
|
|
|
|
def test_nodes_in_sequence_may_share_files(self):
|
|
self.ready_plan(text=PLAN.replace("| sisi/c.py |", "| sisi/a.py, sisi/b.py |"))
|
|
|
|
def test_refuses_more_than_one_sink(self):
|
|
self.refused(PLAN.replace("| c | hard | a, b |", "| c | hard | a |"), "exactly one final join")
|
|
|
|
def test_refuses_no_table(self):
|
|
self.refused("just words", "no table found")
|
|
|
|
def test_record_prints_waves_critical_path_dispatch_line_why_and_record(self):
|
|
out = self.ready_plan()
|
|
self.assertIn("critical path 3 wave(s)", out)
|
|
self.assertIn("wave 1: a (routine: exact spec, test decides), b (ordinary: some judgement)", out)
|
|
self.assertIn("dispatch wave 1 now, all in one message: a, b", out)
|
|
self.assertIn("record (last 20 judged per class): routine none judged yet", out)
|
|
self.assertEqual(self.nodes(), {})
|
|
|
|
def test_a_table_without_the_why_column_records(self):
|
|
six = "\n".join("|".join(l.split("|")[:-2]) + "|" if l.startswith("|") else l for l in PLAN.splitlines()) + "\n"
|
|
self.assertNotIn("why", six)
|
|
out = self.ready_plan(text=six)
|
|
self.assertIn("wave 1: a (routine), b (ordinary)", out)
|
|
|
|
def test_record_line_counts_the_last_twenty_judged_per_class(self):
|
|
self.table(TABLE)
|
|
line = lambda w, v: json.dumps({"ts": "2026-11-20T10:00:00", "worker": w, "verdict": v, "why": "did it fine"})
|
|
self.write(os.path.join(self.data, "outcomes.jsonl"), "\n".join(
|
|
[line("alpha", "bad")] * 5 + [line("alpha", "good")] * 18 + [line("beta", "bad")] + [line("gamma", "out")] + [line("nobody", "good")]) + "\n")
|
|
from sisi import plan
|
|
self.assertEqual(self.in_process(plan.record_line)[0],
|
|
"record (last 20 judged per class): routine 18/20 good, ordinary 0/1 good, hard none judged yet")
|
|
|
|
def test_rerecording_keeps_the_state_of_unchanged_nodes(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.assertEqual(self.decision(self.agent("alpha", self.pick_header("a"))), {})
|
|
self.assertEqual(self.nodes(), {"a": "running"})
|
|
self.assertEqual(self.record(PLAN.replace("test b passes", "test b and b2 pass"))[0], 0)
|
|
self.assertEqual(self.nodes(), {"a": "running"})
|
|
self.assertEqual(self.record(PLAN.replace("test a passes", "test a2 passes"))[0], 0)
|
|
self.assertEqual(self.nodes(), {})
|
|
|
|
def pick_header(self, node, sid="s1"):
|
|
st = self.state(sid)
|
|
n = next(x for x in st["plan"]["nodes"] if x["id"] == node)
|
|
return "sisi node: %s\nbranch: %s\nYou are a worker.\nDo it." % (node, n["branch"])
|
|
|
|
def test_plan_brief_is_a_chore_built_from_done_with_the_total_cost_rule(self):
|
|
self.ready_plan()
|
|
rc, out, _ = self.sisi("plan", "brief", sid="s1")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertEqual(out.splitlines()[0], "sisi chore: plan")
|
|
for want in ("export every row without losing any", "1. row counts match", "| node | class | needs | branch | files | check | why |",
|
|
"Class by total cost", "routine nodes whenever their", "record (last 20 judged per class)"):
|
|
self.assertIn(want, out)
|
|
|
|
def test_a_retro_clears_the_plan(self):
|
|
self.ready_plan()
|
|
self.assertEqual(self.sisi("retro", "--no-gap", "every check passed with the tests shown", *test_core.NG, sid="s1")[0], 0)
|
|
self.assertNotIn("plan", self.state())
|
|
|
|
# pick --node
|
|
|
|
def test_pick_node_takes_class_and_branch_and_prints_the_header(self):
|
|
self.ready_plan()
|
|
out = self.pick("a")
|
|
self.assertIn("worker: alpha", out)
|
|
for want in ("sisi node: a", "branch: p-a (its own worktree and build folder)", "may change: sisi/a.py", "check: test a passes", "needs done: -"):
|
|
self.assertIn(want, out)
|
|
self.assertIn("record (last 20 judged per class)", out)
|
|
self.assertEqual(self.state()["pending"]["alpha"], [{"limit": None, "branch": "p-a", "node": "a"}])
|
|
|
|
def test_pick_node_refuses_a_waiting_or_lead_node(self):
|
|
self.ready_plan()
|
|
rc, out, _ = self.sisi("pick", "--node", "c", sid="s1")
|
|
self.assertEqual(rc, 1)
|
|
self.assertIn("node c is not ready (needs not done: a, b)", out)
|
|
rc, out, _ = self.sisi("pick", "--node", "join", sid="s1")
|
|
self.assertEqual(rc, 1)
|
|
self.assertIn('sisi plan --done join', out)
|
|
|
|
# the hook: each reason to deny
|
|
|
|
def test_deny_without_done(self):
|
|
self.table(TABLE)
|
|
self.hook("prompt", prompt="build it")
|
|
self.denied(self.agent("alpha", "Do it."), "no done is recorded yet")
|
|
|
|
def test_deny_without_a_plan(self):
|
|
self.table(TABLE)
|
|
self.hook("prompt", prompt="build it")
|
|
self.done()
|
|
self.assertEqual(self.sisi("pick", "routine", sid="s1")[0], 0)
|
|
why = self.denied(self.agent("alpha", "Do it."), "no plan is recorded")
|
|
self.assertIn("sisi plan brief", why)
|
|
self.assertEqual(self.state().get("delegations_since", 0), 0) # a denied call is no delegation
|
|
self.assertEqual(self.state()["picked"], ["alpha"]) # and spends no pick
|
|
|
|
def test_a_chore_brief_passes_without_a_plan(self):
|
|
self.table(TABLE)
|
|
self.hook("prompt", prompt="build it")
|
|
self.done()
|
|
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
|
|
self.assertEqual(self.decision(self.agent("gamma", "sisi chore: plan\nYou are a worker.")), {})
|
|
|
|
def test_deny_an_unpicked_model(self):
|
|
self.ready_plan()
|
|
self.denied(self.agent("alpha", self.pick_header("a")), "the wheel did not give you this worker")
|
|
|
|
def test_deny_a_call_with_no_model(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
res = self.tool("Agent", tool_input={"prompt": self.pick_header("a"), "isolation": "worktree"})
|
|
self.denied(res, "no model is named")
|
|
|
|
def test_deny_a_model_not_in_the_table(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.denied(self.agent("zeta", self.pick_header("a")), "sisi pick --node ID")
|
|
|
|
def test_deny_a_brief_without_a_node_line(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.denied(self.agent("alpha", "You are a worker. Do it."), "names no plan node")
|
|
|
|
def test_deny_a_node_that_is_not_ready(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.denied(self.agent("alpha", "sisi node: c\nDo it."), "node c is not ready (needs not done: a, b)")
|
|
|
|
def test_deny_a_node_already_running(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.assertEqual(self.decision(self.agent("alpha", self.pick_header("a"))), {})
|
|
self.assertEqual(self.sisi("pick", "routine", sid="s1")[0], 0)
|
|
self.denied(self.agent("alpha", self.pick_header("a")), "node a is not ready (already running)")
|
|
|
|
def test_deny_a_worker_picked_for_another_node(self):
|
|
self.ready_plan(text=PLAN.replace("| b | ordinary |", "| b | routine |"))
|
|
self.pick("a")
|
|
self.denied(self.agent("alpha", "sisi node: b\nDo it."), "picked for node a, not b")
|
|
|
|
def test_two_picks_of_one_worker_are_spent_by_node_not_by_order(self):
|
|
self.ready_plan(text=PLAN.replace("| b | ordinary |", "| b | routine |"))
|
|
self.pick("a")
|
|
self.pick("b")
|
|
self.assertEqual(self.decision(self.agent("alpha", self.pick_header("b"))), {})
|
|
self.assertEqual(self.decision(self.agent("alpha", self.pick_header("a"))), {})
|
|
self.assertEqual(self.nodes(), {"a": "running", "b": "running"})
|
|
|
|
def test_deny_a_shell_out_to_a_worker(self):
|
|
self.table(test_core.WORKERS)
|
|
self.hook("prompt", prompt="build it")
|
|
self.done()
|
|
self.denied(self.tool("Bash", tool_input={"command": 'agy --print "sisi chore: x"'}), "do not shell out to a worker")
|
|
|
|
def test_deny_a_parallel_node_without_its_own_worktree(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.denied(self.agent("alpha", self.pick_header("a"), isolation=None), 'isolation "worktree"')
|
|
self.pick("b")
|
|
res = self.tool("mcp__t3-code__delegate_task", tool_input={"task": self.pick_header("b"), "target": {"model": "beta"}, "isolation": "shared"})
|
|
self.denied(res, 'isolation "worktree"')
|
|
|
|
def test_the_deny_is_the_decision_not_a_reminder(self):
|
|
"""Fails if watch stops printing a deny: the call would go through with only a note."""
|
|
self.ready_plan()
|
|
d = self.decision(self.agent("alpha", "Do it."))
|
|
self.assertEqual(d.get("permissionDecision"), "deny")
|
|
self.assertNotIn("additionalContext", d)
|
|
|
|
def test_the_planned_delegation_passes_and_runs_its_node(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.pick("b")
|
|
self.assertEqual(self.decision(self.agent("alpha", self.pick_header("a"))), {})
|
|
res = self.tool("mcp__t3-code__delegate_task", tool_input={"task": self.pick_header("b"), "target": {"model": "beta"}, "isolation": "worktree"})
|
|
self.assertEqual(self.decision(res), {})
|
|
self.assertEqual(self.nodes(), {"a": "running", "b": "running"})
|
|
self.assertEqual(self.state()["delegations_since"], 2)
|
|
|
|
def test_sisi_commands_and_direct_edits_are_never_denied(self):
|
|
self.ready_plan()
|
|
self.assertEqual(self.decision(self.tool("Bash", tool_input={"command": "sisi plan"})), {})
|
|
d = self.decision(self.tool("Edit", tool_input={"file_path": "x"}))
|
|
self.assertNotIn("permissionDecision", d)
|
|
|
|
def test_workers_are_never_denied(self):
|
|
self.table(TABLE)
|
|
self.assertEqual(self.decision(self.tool("Agent", sid="w1", agent_id="sub1", tool_input={"prompt": "Do it."})), {})
|
|
self.hook("prompt", sid="w2", prompt="You are a worker. Do this task yourself.")
|
|
self.assertEqual(self.decision(self.tool("Agent", sid="w2", tool_input={"prompt": "Do it."})), {})
|
|
|
|
def test_enforce_false_gives_reminders(self):
|
|
self.ready_plan()
|
|
self.write(os.path.join(self.data, "config.json"), json.dumps({"core": {"enforce": False}}))
|
|
d = self.decision(self.agent("alpha", "Do it."))
|
|
self.assertNotIn("permissionDecision", d)
|
|
self.assertIn("names no plan node", d["additionalContext"])
|
|
self.assertIn("the wheel did not give you", d["additionalContext"])
|
|
self.assertEqual(self.state()["delegations_since"], 1)
|
|
|
|
def test_an_error_inside_watch_lets_the_call_through(self):
|
|
self.ready_plan()
|
|
from sisi import cli, plan
|
|
stdin = io.StringIO(json.dumps({"session_id": "s1", "cwd": self.proj, "hook_event_name": "PreToolUse", "tool_name": "Agent",
|
|
"tool_input": {"model": "alpha", "prompt": "Do it."}}))
|
|
with mock.patch.object(plan, "deny", side_effect=RuntimeError("boom")), mock.patch.object(sys, "stdin", stdin):
|
|
rc, out = self.in_process(cli.main, ["watch"])
|
|
self.assertEqual((rc, out), (0, ""))
|
|
self.assertIn("boom", self.read(self.tmp, "state", "core-errors.log"))
|
|
|
|
# judge and the end of the turn
|
|
|
|
def test_judge_good_marks_done_and_bad_reopens(self):
|
|
self.ready_plan()
|
|
self.pick("a")
|
|
self.pick("b")
|
|
self.agent("alpha", self.pick_header("a"))
|
|
self.tool("Agent", tool_input={"model": "beta", "prompt": self.pick_header("b"), "isolation": "worktree"})
|
|
self.assertEqual(self.sisi("judge", "alpha", "good", "did the work well", sid="s1")[0], 0)
|
|
self.assertEqual(self.sisi("judge", "beta", "bad", "broke the tests badly", sid="s1")[0], 0)
|
|
self.assertEqual(self.nodes(), {"a": "done", "b": "failed"})
|
|
out = self.sisi("plan", sid="s1")[1]
|
|
self.assertIn("ready: b", out)
|
|
self.assertIn("a [routine] done", out)
|
|
|
|
def test_gate_sends_the_lead_back_while_nodes_are_ready_and_not_started(self):
|
|
self.ready_plan()
|
|
rc, out, _ = self.hook("gate")
|
|
self.assertIn('"block"', out)
|
|
self.assertIn("nodes a, b are ready and not started", out)
|
|
self.assertIn("`sisi pick --node a` `sisi pick --node b`", out)
|
|
|
|
def test_gate_lets_the_turn_end_while_nodes_only_run(self):
|
|
self.ready_plan()
|
|
for n, m in (("a", "alpha"), ("b", "beta")):
|
|
self.pick(n)
|
|
self.assertEqual(self.decision(self.agent(m, self.pick_header(n))), {})
|
|
self.assertEqual(self.hook("gate")[1], "")
|
|
|
|
def test_gate_names_the_lead_join_when_only_it_is_ready(self):
|
|
self.ready_plan()
|
|
for n in ("a", "b", "c"):
|
|
self.assertEqual(self.sisi("plan", "--done", n, "done by hand and tested", sid="s1")[0], 0)
|
|
out = self.hook("gate")[1]
|
|
self.assertIn("node join is yours and ready: full suite passes", out)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|