Files
sisi/tests/test_plan.py
T
swaitsandClaude Fable 5.2 3a349a3859
ci / test (push) Waiting to run
Planner prefers many small exact nodes; slow checks wait for a downstream join
The planning brief now asks for many small, exactly specified nodes a near-free
model can do well and fast, each proven by the quick test of its own change, with
the full suite deferred to a join node that merges its needs, runs it once and
fixes what broke. The pick header names each needed node's branch so a join
knows what to merge. Rule 5 says the same in one character less.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 17:19:17 -07:00

612 lines
31 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, shutil, sys, unittest
from unittest import mock
try: # the module, not its class: its tests must not run twice
from tests import test_core
except ImportError:
import test_core
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_a_final_join_that_is_not_the_leads(self):
self.refused(PLAN.replace("| join | lead | c | - | - |", "| join | hard | c | p-j | docs |"), "the final join join must be class lead")
def test_refuses_a_worker_node_without_files_or_check(self):
self.refused(PLAN.replace("| sisi/b.py |", "| - |"), "worker node b needs files it may change and a check")
self.refused(PLAN.replace("| test b passes |", "| - |"), "worker node b needs files it may change and a check")
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(), {"a": "running"}) # a running node keeps its state by id
rc, out, _ = self.record(PLAN.replace("| sisi/a.py |", "| sisi/a2.py |"))
self.assertEqual(rc, 1, out)
self.assertIn("node a is running", out)
self.assertIn("a", self.nodes())
rc, out, _ = self.record(PLAN.replace("| a | routine | - |", "| a2 | routine | - |").replace("| a, b |", "| a2, b |"))
self.assertEqual(rc, 1, out)
self.assertIn("node a is running", out)
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.assertRegex(out.splitlines()[0], r"^sisi chore: plan [0-9a-f]{8}$")
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)",
"prefer many small nodes", "quick test of its own change", "merges its needs, runs them once and fixes what broke"):
self.assertIn(want, out)
def test_a_mid_graph_join_takes_the_slow_check_and_its_header_names_the_branches_to_merge(self):
self.ready_plan(text=COLS + (
"| a | routine | - | p-a | sisi/a.py | test a passes | exact spec |\n"
"| b | routine | - | p-b | sisi/b.py | test b passes | exact spec |\n"
"| merge | ordinary | a, b | p-m | sisi/a.py, sisi/b.py | full suite passes | merge, run once, fix |\n"
"| join | lead | merge | - | - | released | the lead releases |\n"))
for n in "ab":
self.assertEqual(self.sisi("plan", "--done", n, "test %s ok" % n, sid="s1")[0], 0)
self.assertIn("needs done: a on p-a (test a ok), b on p-b (test b ok)", self.pick("merge"))
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()
brief = self.sisi("plan", "brief", sid="s1")[1]
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.assertEqual(self.decision(self.agent("gamma", brief)), {})
self.assertEqual(self.state().get("chores"), []) # spent
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.denied(self.agent("gamma", brief), "is not one sisi printed in this session")
def test_a_chore_line_must_carry_the_printed_brief(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.denied(self.tool("Agent", tool_input={"model": "gamma", "description": "sisi chore: plan", "prompt": "Arbitrary unplanned work"}),
"no plan is recorded") # the longest field is the brief: no chore line in it
self.denied(self.tool("Agent", tool_input={"model": "gamma", "description": "x", "prompt": "sisi chore: plan\nArbitrary unplanned work"}),
"is not one sisi printed in this session")
self.denied(self.agent("gamma", brief.replace("Goal:", "Goal: and also rewrite the installer")), "is not one sisi printed")
self.assertNotIn("permissionDecision", self.decision(self.agent("gamma", brief + "\nAlso: the lead's own note after it.")))
def test_the_longest_field_is_the_brief(self):
self.ready_plan()
self.pick("a")
self.denied(self.tool("Agent", tool_input={"model": "alpha", "isolation": "worktree", "description": self.pick_header("a"),
"prompt": "Unrelated work that is not in the plan at all, described at length here."}), "first line must be")
self.assertNotIn("a", self.nodes())
def test_a_shell_worker_brief_carries_its_node_or_chore_on_any_line(self):
self.ready_plan()
self.table(test_core.WORKERS)
self.write(os.path.join(self.data, "config.json"), json.dumps({"core": {"shell_workers": True}}))
self.pick("a")
d = self.decision(self.tool("Bash", tool_input={"command": 'agy --print "\n%s"' % self.pick_header("a")}))
self.assertNotIn("permissionDecision", d)
self.assertEqual(self.nodes(), {"a": "running"})
brief = self.sisi("models", "research", sid="s1")[1]
self.assertEqual(self.sisi("pick", "routine", sid="s1")[0], 0)
self.assertNotIn("permissionDecision", self.decision(self.tool("Bash", tool_input={"command": 'agy --print "\n%s"' % brief})))
self.assertEqual(self.state()["chores"], [])
def test_fail_refuses_a_running_node(self):
self.ready_plan()
self.pick("a")
self.agent("alpha", self.pick_header("a"))
rc, out, _ = self.sisi("plan", "--fail", "a", "I think it went wrong", sid="s1")
self.assertEqual(rc, 1)
self.assertIn("only its worker's verdict", out)
self.assertEqual(self.nodes(), {"a": "running"})
def test_bad_needs_and_orphan_state_do_not_crash(self):
self.ready_plan()
path = os.path.join(self.tmp, "state", "core-sessions", "s1.json")
s = self.state()
s["plan"]["nodes"][2]["needs"] = ["a", 7, None, "b"]
s["plan"]["state"] = {"ghost": {"st": "running", "worker": "alpha", "since": 1.0}}
self.write(path, json.dumps(s))
rc, out, err = self.sisi("plan", sid="s1")
self.assertEqual((rc, err), (0, ""), out)
self.assertIn("ready: a, b", out)
self.assertEqual(self.sisi("pick", "--node", "a", sid="s1")[0], 0)
self.assertEqual(self.record(PLAN)[0], 0)
def test_an_invented_chore_line_is_denied(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
why = self.denied(self.agent("gamma", "sisi chore: anything\nDo it."), "`sisi chore: anything` brief is not one sisi printed")
self.assertIn("no done is recorded", why)
def test_sisis_own_chore_briefs_carry_their_line_and_pass(self):
self.table(TABLE)
self.hook("prompt", prompt="tidy up")
for args, line in ((("rules", "organize"), "sisi chore: rules organize"), (("models", "research"), "sisi chore: models research")):
rc, brief, _ = self.sisi(*args, sid="s1")
self.assertEqual(rc, 0, brief)
self.assertRegex(brief.splitlines()[0], r"^%s [0-9a-f]{8}$" % line)
self.assertEqual(self.sisi("pick", "routine", sid="s1")[0], 0)
self.assertNotIn("permissionDecision", self.decision(self.agent("alpha", brief)), line)
self.assertEqual(self.state()["chores"], [])
def chore_id(self, brief):
return brief.splitlines()[0].rsplit(" ", 1)[-1]
def short_brief(self, brief):
return brief.splitlines()[0] + "\nRun `sisi chore show %s` and do what it says." % self.chore_id(brief)
def test_a_chore_id_is_issued_and_printed_on_the_first_line(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
cid = self.chore_id(brief)
self.assertRegex(cid, r"^[0-9a-f]{8}$")
self.assertIn(cid, [x[3] for x in self.state()["chores"]])
def test_a_short_delegation_with_the_id_passes_once(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.assertEqual(self.decision(self.agent("gamma", self.short_brief(brief))), {})
self.assertEqual(self.state().get("chores"), [])
def test_a_reused_chore_id_is_denied(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.assertEqual(self.decision(self.agent("gamma", self.short_brief(brief))), {})
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.denied(self.agent("gamma", self.short_brief(brief)), "is not one sisi printed in this session")
def test_a_wrong_chore_id_is_denied(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
cid = self.chore_id(brief)
wrong = ("0" if cid[0] != "0" else "1") + cid[1:]
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
why = self.denied(self.agent("gamma", "sisi chore: plan %s\nRun `sisi chore show %s` and do what it says." % (wrong, wrong)),
"is not one sisi printed in this session")
self.assertIn("sisi plan brief", why)
def test_another_sessions_chore_id_is_denied(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
self.hook("prompt", sid="s2", prompt="build it")
self.done(sid="s2")
self.assertEqual(self.sisi("pick", "hard", sid="s2")[0], 0)
self.denied(self.agent("gamma", self.short_brief(brief), sid="s2"), "is not one sisi printed in this session")
def test_chore_show_prints_the_brief_and_unknown_id_fails(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
cid = self.chore_id(brief)
rc, out, _ = self.sisi("chore", "show", cid, sid="s2")
self.assertEqual(rc, 0, out)
self.assertEqual(out, brief)
rc, out, _ = self.sisi("chore", "show", "deadbeef")
self.assertEqual(rc, 1, out)
self.assertIn("deadbeef", out)
def test_a_full_text_delegation_with_the_id_still_passes(self):
self.table(TABLE)
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("plan", "brief", sid="s1")[1]
self.assertEqual(self.sisi("pick", "hard", sid="s1")[0], 0)
self.assertEqual(self.decision(self.agent("gamma", brief)), {})
self.assertEqual(self.state().get("chores"), [])
def test_a_shell_worker_may_carry_the_id_line_on_any_line(self):
self.table(test_core.WORKERS)
self.write(os.path.join(self.data, "config.json"), json.dumps({"core": {"shell_workers": True, "enforce": True}}))
self.hook("prompt", prompt="build it")
self.done()
brief = self.sisi("models", "research", sid="s1")[1]
self.assertEqual(self.sisi("pick", "routine", sid="s1")[0], 0)
d = self.decision(self.tool("Bash", tool_input={"command": 'agy --print "\n%s"' % self.short_brief(brief)}))
self.assertNotIn("permissionDecision", d)
self.assertEqual(self.state()["chores"], [])
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")), "not in the models table")
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."), "first line must be `sisi node: ID`")
def test_deny_a_node_line_that_is_not_the_first_line(self):
self.ready_plan()
self.pick("a")
self.denied(self.agent("alpha", "Do this.\nsisi node: a"), "first line must be `sisi node: ID`")
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("first line must be `sisi node: ID`", 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"))
def test_a_brief_in_another_field_is_read(self):
self.ready_plan()
self.pick("a")
res = self.tool("subagent_spawn", tool_input={"objective": self.pick_header("a"), "model": "alpha"})
d = self.decision(res)
self.assertNotIn("permissionDecision", d) # a tool with no isolation field: reminded only
self.assertIn('isolation "worktree"', d["additionalContext"])
self.assertEqual(self.nodes(), {"a": "running"})
def test_a_call_with_no_text_is_reminded_not_denied(self):
self.ready_plan()
d = self.decision(self.tool("Workflow", tool_input={"steps": [1, 2]}))
self.assertNotIn("permissionDecision", d)
self.assertIn("first line must be", d["additionalContext"])
def test_a_session_with_no_request_yet_is_reminded_not_denied(self):
self.table(TABLE)
d = self.decision(self.agent("alpha", "Do it.", sid="fresh"))
self.assertNotIn("permissionDecision", d)
self.assertIn("no done is recorded", d["additionalContext"])
def test_a_node_in_sequence_needs_no_worktree(self):
seq = COLS + ("| a | routine | - | p-a | sisi/a.py | test a passes | x |\n| b | routine | a | p-b | sisi/b.py | test b passes | x |\n"
"| join | lead | b | - | - | suite passes | x |\n")
self.ready_plan(text=seq)
self.pick("a")
self.assertEqual(self.decision(self.agent("alpha", self.pick_header("a"), isolation=None)), {})
def test_done_refuses_a_running_worker_node(self):
self.ready_plan()
self.pick("a")
self.agent("alpha", self.pick_header("a"))
rc, out, _ = self.sisi("plan", "--done", "a", "it looks finished to me", sid="s1")
self.assertEqual(rc, 1)
self.assertIn("sisi judge WORKER", out)
self.assertEqual(self.nodes(), {"a": "running"})
def test_a_damaged_plan_state_does_not_crash(self):
self.ready_plan()
path = os.path.join(self.tmp, "state", "core-sessions", "s1.json")
s = self.state()
s["plan"]["state"] = {"a": "junk", "b": {"st": 5}, "c": None}
self.write(path, json.dumps(s))
rc, out, err = self.sisi("plan", sid="s1")
self.assertEqual((rc, err), (0, ""), out)
self.assertIn("ready: a, b", out)
self.assertEqual(self.sisi("pick", "--node", "a", sid="s1")[0], 0)
s["plan"]["state"] = ["junk"]
self.write(path, json.dumps(s))
self.assertEqual(self.sisi("plan", sid="s1")[0], 0)
def test_judge_moves_only_the_node_its_job_started(self):
from sisi import plan
s = {"plan": {"nodes": [{"id": "a", "cls": "routine", "needs": [], "branch": "b", "files": ["x"], "check": "c"}],
"state": {"a": {"st": "running", "worker": "alpha", "since": 100.0}}}}
plan.judged(s, "alpha", 200.0, "good")
self.assertEqual(s["plan"]["state"]["a"]["st"], "running")
plan.judged(s, "alpha", 100.0, "good")
self.assertEqual(s["plan"]["state"]["a"]["st"], "done")
# 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)
exe = "sisi" if shutil.which("sisi") else os.path.join(self.tmp, "lib", "current", "bin", "sisi")
self.assertIn("`%s pick --node a` `%s pick --node b`" % (exe, exe), 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_lets_the_turn_end_when_only_the_lead_join_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.assertNotIn("is yours and ready", out) # the lead may be waiting on its own job; unsaid checks still speak for themselves
def test_gate_still_blocks_on_a_ready_worker_node_beside_a_ready_lead_node(self):
self.ready_plan()
self.assertEqual(self.sisi("plan", "--done", "c", "done by hand and tested", sid="s1")[0], 0)
self.assertEqual(self.sisi("plan", "--done", "a", "done by hand and tested", sid="s1")[0], 0)
out = self.hook("gate")[1]
self.assertIn('"block"', out)
self.assertIn("nodes b are ready and not started", out)
if __name__ == "__main__":
unittest.main()