revert no longer undoes moves between levels; it names the command back
A change that moves rules between levels (sisi rules move, or an organize that moves lines across levels) prints the exact reverse command when it succeeds, and revert after it changes nothing and prints the same command. Revert keeps undoing learn, retro and single-level organize. The multi-file undo record and its restore loop are gone. Fixes the five findings of the fix review: a store with no .git starts its history with the rules as they are (no undo goes past them); revert reads the undo record even when its bytes did not change; a trailing slash in SISI_DATA is still the store; organize takes the user lock and refuses a linked drops.jsonl. Also: rules usage names canon; a non-UTF-8 file is named. Product code 2427 -> 2426 lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
064cd6f5ca
commit
2a68ead05f
5 files changed
+192
-83
No files matched your search
+119
-9
@@ -3,7 +3,7 @@
|
||||
Learning: work is done, the retro is forced, its improvement is a rule in the next session, the rules are
|
||||
organized and the change can be undone. Delegation: the lead is stopped when it should be delegating, and the
|
||||
models table is researched, read and kept fresh."""
|
||||
import glob, hashlib, json, os, random, re, shutil, subprocess, sys, tempfile, time, unittest
|
||||
import glob, hashlib, json, os, random, re, shlex, shutil, subprocess, sys, tempfile, time, unittest
|
||||
from unittest import mock
|
||||
|
||||
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
@@ -1024,7 +1024,7 @@ class Core(unittest.TestCase):
|
||||
self.assertEqual(len(out.strip().splitlines()), 1, (label, out))
|
||||
self.assertEqual(self.read(target), "keep me intact\n", label)
|
||||
self.assertEqual(subprocess.run(["git", "log", "--oneline"], cwd=self.data, capture_output=True, text=True, check=True).stdout, log_before, label)
|
||||
self.assertEqual(subprocess.run(["git", "status", "--porcelain"], cwd=self.data, capture_output=True, text=True, check=True).stdout, "", label)
|
||||
self.assertEqual(subprocess.run(["git", "status", "--porcelain", "--", "rules"], cwd=self.data, capture_output=True, text=True, check=True).stdout, "", label)
|
||||
|
||||
def test_revert_declines_relative_dotdot_and_dotgit_paths(self):
|
||||
self.mkdir(self.data, "rules")
|
||||
@@ -1052,7 +1052,7 @@ class Core(unittest.TestCase):
|
||||
self.assertIn("cannot be undone", out, label)
|
||||
self.assertEqual(self.read(real_file), content, label)
|
||||
self.assertEqual(subprocess.run(["git", "log", "--oneline"], cwd=self.data, capture_output=True, text=True, check=True).stdout, log_before, label)
|
||||
self.assertEqual(subprocess.run(["git", "status", "--porcelain"], cwd=self.data, capture_output=True, text=True, check=True).stdout, "", label)
|
||||
self.assertEqual(subprocess.run(["git", "status", "--porcelain", "--", "rules"], cwd=self.data, capture_output=True, text=True, check=True).stdout, "", label)
|
||||
|
||||
def test_hostile_project_rules_and_agents_files_do_not_hang_or_leak(self):
|
||||
import stat
|
||||
@@ -1213,7 +1213,7 @@ class Core(unittest.TestCase):
|
||||
self.sisi("learn", "--project", "Initial rule before race")
|
||||
rules_file = os.path.join(self.proj, ".sisi", "rules.md")
|
||||
started, second_done, errs = threading.Event(), threading.Event(), []
|
||||
real_commit, count = rules._commit, [0]
|
||||
real_commit, count = store.commit, [0]
|
||||
def wrapped_commit(*a, **kw):
|
||||
count[0] += 1
|
||||
if count[0] == 1:
|
||||
@@ -1233,7 +1233,7 @@ class Core(unittest.TestCase):
|
||||
os.chdir(self.tmp)
|
||||
try:
|
||||
with mock.patch.object(store, "DATA", self.data), mock.patch.object(store, "STATE", os.path.join(self.tmp, "state")), \
|
||||
mock.patch.object(store, "LIB", os.path.join(self.tmp, "lib")), mock.patch.object(rules, "_commit", wrapped_commit):
|
||||
mock.patch.object(store, "LIB", os.path.join(self.tmp, "lib")), mock.patch.object(store, "commit", wrapped_commit):
|
||||
t1 = threading.Thread(target=worker, args=("First concurrent project rule",))
|
||||
t1.start()
|
||||
self.assertTrue(started.wait(15))
|
||||
@@ -4115,7 +4115,7 @@ cli.main(["tick"])
|
||||
def sides(self, *paths):
|
||||
return [self.read(p) if os.path.exists(p) else None for p in paths]
|
||||
|
||||
def test_one_move_is_one_undo_in_every_direction(self): # finding 1, invariant 2
|
||||
def test_a_move_names_the_command_that_moves_it_back_in_every_direction(self): # finding 1, invariant 2; fix review: revert does not undo a move
|
||||
other = self.project("beta")
|
||||
user, core = os.path.join(self.data, "rules", "user.md"), os.path.join(self.data, "rules", "core.md")
|
||||
here, there = os.path.join(self.proj, ".sisi", "rules.md"), os.path.join(other, ".sisi", "rules.md")
|
||||
@@ -4135,8 +4135,11 @@ cli.main(["tick"])
|
||||
moved = self.sides(src, dst)
|
||||
self.assertEqual((moved[0] or "").count(text), 0, (frm, to))
|
||||
self.assertEqual(moved[1].count(text), 1, (frm, to))
|
||||
rc, out, _ = self.sisi("rules", "revert", cwd=lcwd)
|
||||
self.assertEqual(rc, 0, out)
|
||||
rc, back, _ = self.sisi("rules", "revert", cwd=lcwd)
|
||||
self.assertEqual(rc, 1, back)
|
||||
self.assertIn("Nothing was changed", back)
|
||||
self.assertEqual(self.sides(src, dst), moved, (frm, to))
|
||||
self.assertEqual(self.run_back(out), 1)
|
||||
self.assertEqual(self.sides(src, dst)[0], before[0], (frm, to))
|
||||
self.assertEqual((self.sides(src, dst)[1] or "").count(text), 0, (frm, to))
|
||||
|
||||
@@ -4243,6 +4246,10 @@ cli.main(["tick"])
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertEqual(self.read(self.data, "rules", "core.md"), "- Kept user rule [owner 2026-11-20]\n")
|
||||
self.assertEqual(self.read(self.data, "rules", "user.md"), "")
|
||||
rc, back, _ = self.sisi("rules", "revert") # fix review: a move between levels is not undone; its command back is named
|
||||
self.assertEqual((rc, self.read(self.data, "rules", "user.md")), (1, ""), back)
|
||||
self.assertEqual(self.run_back(back), 1)
|
||||
self.assertEqual((self.read(self.data, "rules", "core.md"), self.read(self.data, "rules", "user.md")), ("", "- Kept user rule [owner 2026-11-20]\n"))
|
||||
|
||||
def test_outside_git_the_user_rules_are_organized_and_audited(self): # finding 7, invariant 4
|
||||
notes = self.mkdir(self.tmp, "notes")
|
||||
@@ -4304,9 +4311,112 @@ cli.main(["tick"])
|
||||
self.assertFalse(os.path.exists(os.path.join(self.tmp, ".sisi")))
|
||||
self.assertFalse(os.path.exists(os.path.join(self.tmp, "AGENTS.md")))
|
||||
|
||||
# the fix review (fix-review.md): a change across files is not undone by revert, which names the command that moves it back
|
||||
def run_back(self, out):
|
||||
"""Run each reverse command an output names (`sisi rules move ...`, maybe after `cd DIR &&`); returns how many."""
|
||||
cmds = [l.strip() for l in out.splitlines() if l.startswith(" ") and "sisi rules move " in l]
|
||||
for c in cmds:
|
||||
words = shlex.split(c)
|
||||
cwd = words[1] if words[0] == "cd" else None
|
||||
words = words[words.index("sisi") + 1:]
|
||||
rc, o, e = self.sisi(*words, cwd=cwd)
|
||||
self.assertEqual((rc, e), (0, ""), (c, o))
|
||||
return len(cmds)
|
||||
|
||||
def test_revert_after_a_move_in_a_store_without_history_loses_nothing(self): # fix review 1
|
||||
user, proj = os.path.join(self.data, "rules", "user.md"), os.path.join(self.proj, ".sisi", "rules.md")
|
||||
self.user_file("- Manual user rule [owner 2026-11-20]\n")
|
||||
rc, out, err = self.sisi("rules", "move", "Manual user rule", "project")
|
||||
self.assertEqual((rc, err), (0, ""), out)
|
||||
self.assertIn("sisi rules move", out)
|
||||
rc, out, _ = self.sisi("rules", "revert")
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("Nothing was changed", out)
|
||||
self.assertEqual((self.read(user), self.read(proj)), ("", "- Manual user rule [owner 2026-11-20]\n"))
|
||||
self.assertEqual(self.run_back(out), 1)
|
||||
self.assertEqual((self.read(user), self.read(proj)), ("- Manual user rule [owner 2026-11-20]\n", ""))
|
||||
shutil.rmtree(os.path.join(self.data, ".git")) # a first learn into a store with no history: its revert keeps what was there
|
||||
self.assertEqual(self.sisi("learn", "--level", "user", "A new user rule after the manual one")[0], 0)
|
||||
self.assertEqual(self.sisi("rules", "revert")[0], 0)
|
||||
self.assertEqual(self.read(user), "- Manual user rule [owner 2026-11-20]\n")
|
||||
|
||||
def test_revert_after_two_organizes_that_leave_the_project_list_alone(self): # fix review 2
|
||||
user, proj = os.path.join(self.data, "rules", "user.md"), os.path.join(self.mkdir(self.proj, ".sisi"), "rules.md")
|
||||
self.write(proj, "- Keep project rule [owner 2026-11-20]\n")
|
||||
self.user_file("- User rule one [retro 2026-11-20]\n- User rule two [retro 2026-11-20]\n")
|
||||
for n, users in enumerate(("- User rule one merged [retro 2026-11-20]\n", "- User rule one merged again [retro 2026-11-20]\n")):
|
||||
ans = self.write(os.path.join(self.tmp, "a%d.md" % n), "## user\n" + users + "## project\n- Keep project rule [owner 2026-11-20]\n")
|
||||
self.assertEqual(self.sisi("rules", "organize")[0], 0)
|
||||
rc, out, _ = self.sisi("rules", "organize", "--record", ans)
|
||||
self.assertEqual(rc, 0, out)
|
||||
rc, out, _ = self.sisi("rules", "revert")
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertEqual(self.read(user), "- User rule one merged [retro 2026-11-20]\n")
|
||||
self.assertEqual(self.read(proj), "- Keep project rule [owner 2026-11-20]\n")
|
||||
|
||||
def test_a_store_path_with_a_trailing_slash_is_still_the_store(self): # fix review 3
|
||||
self.env["SISI_DATA"] = self.data + os.sep
|
||||
self.assertEqual(self.sisi("learn", "--level", "user", "Trailing slash rule stays one rule")[0], 0)
|
||||
rc, out, err = self.sisi("rules", "move", "Trailing slash rule", "project")
|
||||
self.assertEqual((rc, err), (0, ""), out)
|
||||
self.assertEqual(subprocess.run(["git", "status", "--porcelain", "--", "rules"], cwd=self.data, capture_output=True, text=True).stdout, "")
|
||||
rc, out, _ = self.sisi("rules", "revert")
|
||||
self.assertIn("sisi rules move", out)
|
||||
self.assertEqual(self.read(self.proj, ".sisi", "rules.md").count("Trailing slash rule"), 1)
|
||||
self.assertEqual(self.sisi("learn", "--level", "user", "Another rule with the slash")[0], 0)
|
||||
self.assertEqual(self.sisi("rules", "revert")[0], 0)
|
||||
self.assertNotIn("Another rule", self.read(self.data, "rules", "user.md"))
|
||||
|
||||
def test_revert_after_a_move_between_projects_changes_neither(self): # fix review 4
|
||||
from sisi import rules, store
|
||||
pf, pe = self.project("pf"), self.project("pe")
|
||||
self.assertEqual(self.sisi("learn", "--level", "project", "Rule E already there", cwd=pe)[0], 0)
|
||||
self.assertEqual(self.sisi("learn", "--level", "project", "Rule F to E revert fail", cwd=pf)[0], 0)
|
||||
rc, out, err = self.sisi("rules", "move", "Rule F to E", "project", "--project", pe, cwd=pf)
|
||||
self.assertEqual((rc, err), (0, ""), out)
|
||||
fpath, epath = os.path.join(pf, ".sisi", "rules.md"), os.path.join(pe, ".sisi", "rules.md")
|
||||
before, real = self.sides(fpath, epath), store.write
|
||||
with mock.patch.object(store, "write", lambda p, t: (_ for _ in ()).throw(OSError("disk full")) if p == epath else real(p, t)):
|
||||
why, out = self.in_process(rules.revert)
|
||||
self.assertIn("sisi rules move", out)
|
||||
self.assertEqual(self.sides(fpath, epath), before)
|
||||
self.assertEqual(self.run_back(out), 1)
|
||||
self.assertEqual([(s or "").count("Rule F to E") for s in self.sides(fpath, epath)], [1, 0])
|
||||
|
||||
def test_organize_takes_the_user_lock_and_refuses_a_linked_drops_file(self): # fix review 5
|
||||
from sisi import rules, store
|
||||
self.user_file("- User rule to drop [retro 2026-11-20]\n- User rule kept [owner 2026-11-20]\n")
|
||||
real_lock, keys = store.lock, []
|
||||
ans = self.write(os.path.join(self.tmp, "a.md"), "## user\n- User rule kept [owner 2026-11-20]\n## project\n")
|
||||
self.in_process(rules.organize_brief, self.proj)
|
||||
with mock.patch.object(store, "lock", lambda key, wait=True: keys.append(key) or real_lock(key, wait=wait)):
|
||||
why, out = self.in_process(rules.organize_record, ans, self.proj)
|
||||
self.assertIsNone(why, out)
|
||||
self.assertIn("user", keys)
|
||||
self.assertEqual(self.sisi("rules", "revert")[0], 0)
|
||||
ext = self.write(os.path.join(self.tmp, "elsewhere.jsonl"), '{"outside": 1}\n')
|
||||
dp = os.path.join(self.data, "rules", "drops.jsonl")
|
||||
os.path.lexists(dp) and os.remove(dp)
|
||||
os.symlink(ext, dp)
|
||||
self.assertEqual(self.sisi("rules", "organize")[0], 0)
|
||||
rc, out, err = self.sisi("rules", "organize", "--record", ans)
|
||||
self.assertEqual((rc, err), (1, ""), out)
|
||||
self.assertIn("symlink", out)
|
||||
self.assertTrue(os.path.islink(dp))
|
||||
self.assertEqual(self.read(ext), '{"outside": 1}\n')
|
||||
self.assertIn("User rule to drop", self.read(self.data, "rules", "user.md"))
|
||||
|
||||
def test_rules_usage_names_canon_and_a_bad_file_is_named(self): # fix review 7
|
||||
self.assertIn("|canon", self.sisi("rules", "nonsense")[1])
|
||||
with open(self.user_file(""), "wb") as f:
|
||||
f.write(b"- bad \xff byte [retro 2026-11-20]\n")
|
||||
rc, out, _ = self.sisi("rules", "move", "bad", "project")
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("user.md is not valid UTF-8", out)
|
||||
|
||||
def test_the_product_stays_small(self):
|
||||
code = sum(len(self.read(p).splitlines()) for p in glob.glob(os.path.join(REPO, "sisi", "*.py")))
|
||||
self.assertLessEqual(code, 2427, "sisi is two loops. If it needs more than this, find the simpler shape.")
|
||||
self.assertLessEqual(code, 2426, "sisi is two loops. If it needs more than this, find the simpler shape.")
|
||||
rulebook = self.read(REPO, "canon", "core.md")
|
||||
self.assertLess(len(rulebook.splitlines()), 139)
|
||||
self.assertLessEqual(len(rulebook), 10711)
|
||||
|
||||
Reference in new issue
Block a user