Rules: protect [owner] lines during organize, claim notices for 30m, refuse stale records
ci / test (push) Canceled after 0s
ci / test (push) Canceled after 0s
- Keep [owner ...] lines verbatim during organize --record (allow reordering, refuse missing/edited lines naming the first one). - Exclude [owner ...] lines from rules_lines/rules_chars budgets (due, organize --record, and display _fit trimming so owner rules are never trimmed out in favour of retro rules). - Update organize prompt text to instruct workers to copy [owner ...] lines verbatim. - Claim organize notices per scope for 30 minutes under a non-blocking lock (locks/organize.json) so session start never waits and only one organizer runs at a time; clear on record. - Snapshot live user+project lists in organize_brief and refuse organize --record if rules changed in between. Size: +25 lines in sisi/rules.py (within 30-line budget), raising the sisi/*.py ceiling in test_the_product_stays_small from 1,923 to 1,948 for owner-rule protection, 30m organize claim, and stale-record guard.
This commit is contained in:
1 parent
5f488476e8
commit
17bd7f82a4
3 files changed
+138
-43
No files matched your search
+1
-1
@@ -122,7 +122,7 @@ def tick(p):
|
|||||||
store.append(env, "export SISI_SESSION_ID=%s" % sid) # so `sisi retro` knows its session
|
store.append(env, "export SISI_SESSION_ID=%s" % sid) # so `sisi retro` knows its session
|
||||||
if not os.path.exists(os.path.join(store.DATA, "generated", "core.md")):
|
if not os.path.exists(os.path.join(store.DATA, "generated", "core.md")):
|
||||||
rules.build_rulebook()
|
rules.build_rulebook()
|
||||||
text = "\n".join(x for x in (rules.project_text(cwd), rules.due(cwd), models.stale(), "" if worker else _faults(cwd, False, free)) if x)
|
text = "\n".join(x for x in (rules.project_text(cwd), rules.due(cwd, claim=True), models.stale(), "" if worker else _faults(cwd, False, free)) if x)
|
||||||
if text:
|
if text:
|
||||||
print(text)
|
print(text)
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
+52
-27
@@ -3,10 +3,13 @@
|
|||||||
A rule is one line. It is in force as soon as it is added: rules for all of the person's work become the last
|
A rule is one line. It is in force as soon as it is added: rules for all of the person's work become the last
|
||||||
section of the rulebook every session loads, and a project's rules live in `.sisi/rules.md` at its root.
|
section of the rulebook every session loads, and a project's rules live in `.sisi/rules.md` at its root.
|
||||||
Now and then a cheap worker audits and organizes them. Store changes are committed and can be undone."""
|
Now and then a cheap worker audits and organizes them. Store changes are committed and can be undone."""
|
||||||
import json, os, re, time
|
import contextlib, hashlib, json, os, re, time
|
||||||
from . import store
|
from . import store
|
||||||
|
|
||||||
USER, PROJECT = "user", "project"
|
USER, PROJECT = "user", "project"
|
||||||
|
OWNER = re.compile(r"\[owner\b[^\]]*\]")
|
||||||
|
_owner, _retro = lambda l: bool(OWNER.search(l)), lambda ls: [l for l in ls if not _owner(l)]
|
||||||
|
_snap = lambda u, p: hashlib.sha1(("\n".join(u) + "\0" + "\n".join(p)).encode()).hexdigest()[:16]
|
||||||
HEAD_USER = "## Learned here\n\nAdded by retros and by the person. These add to the rules above and never relax them.\n"
|
HEAD_USER = "## Learned here\n\nAdded by retros and by the person. These add to the rules above and never relax them.\n"
|
||||||
HEAD_PROJECT = "sisi: learned in this project (these add to the rules and never relax them):"
|
HEAD_PROJECT = "sisi: learned in this project (these add to the rules and never relax them):"
|
||||||
|
|
||||||
@@ -24,11 +27,11 @@ Rewrite both lists so they serve that objective best:
|
|||||||
- Word each rule as a direct instruction with its reason, on one line, standing on its own.
|
- Word each rule as a direct instruction with its reason, on one line, standing on its own.
|
||||||
- A rule seen in more than one project, or about how the person wants to be worked with, belongs in the
|
- A rule seen in more than one project, or about how the person wants to be worked with, belongs in the
|
||||||
first list. A rule about this project's own code belongs in the second.
|
first list. A rule about this project's own code belongs in the second.
|
||||||
- Lines marked [owner ...] are the person's own words. Keep what each one asks for. You may tighten the
|
- Lines marked [owner ...] are the person's own words: copy every one of them verbatim, unchanged by even
|
||||||
wording or merge them, and the result stays marked [owner <date>].
|
one character (you may reorder them). Only [retro ...] lines may be merged, reworded or dropped.
|
||||||
- Keep the [source date] mark at the end of every line.
|
- Keep the [source date] mark at the end of every line.
|
||||||
- A `better` that recurs across retros has earned a rule: add it, worded as an instruction, marked [retro <date>].
|
- A `better` that recurs across retros has earned a rule: add it, worded as an instruction, marked [retro <date>].
|
||||||
- Each list: at most %(lines)d lines and %(chars)d characters. Fewer is better.
|
- Non-owner ([retro ...]) lines in each list: at most %(lines)d lines and %(chars)d characters. Fewer is better.
|
||||||
|
|
||||||
Answer with exactly this shape and nothing else:
|
Answer with exactly this shape and nothing else:
|
||||||
## user
|
## user
|
||||||
@@ -196,13 +199,14 @@ def add(text, scope, source, cwd=None, log=None):
|
|||||||
|
|
||||||
|
|
||||||
def _fit(ls):
|
def _fit(ls):
|
||||||
cfg, out, chars = store.config(), [], 0
|
cfg, keep, n, chars = store.config(), set(), 0, 0
|
||||||
for line in reversed(ls):
|
for i in range(len(ls) - 1, -1, -1):
|
||||||
if len(out) >= cfg["rules_lines"] or chars + len(line) > cfg["rules_chars"]:
|
if not _owner(ls[i]):
|
||||||
break
|
if n >= cfg["rules_lines"] or chars + len(ls[i]) > cfg["rules_chars"]:
|
||||||
out.append(line)
|
break
|
||||||
chars += len(line)
|
keep.add(i)
|
||||||
return out[::-1]
|
n, chars = n + 1, chars + len(ls[i])
|
||||||
|
return [l for i, l in enumerate(ls) if _owner(l) or i in keep]
|
||||||
|
|
||||||
|
|
||||||
def delivered(p):
|
def delivered(p):
|
||||||
@@ -237,9 +241,9 @@ def objective():
|
|||||||
return " ".join(m.group(1).split()) if m else ""
|
return " ".join(m.group(1).split()) if m else ""
|
||||||
|
|
||||||
|
|
||||||
def _organized():
|
def _organized(rel=("rules", "organized.json")):
|
||||||
try:
|
try:
|
||||||
return json.loads(store.read(store.data("rules", "organized.json")) or "{}")
|
return json.loads(store.read(os.path.join(store.DATA, *rel)) or "{}")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -248,30 +252,41 @@ def _mark_key(scope, cwd=None):
|
|||||||
return "rules/user.md" if scope == USER else os.path.realpath(path(PROJECT, cwd))
|
return "rules/user.md" if scope == USER else os.path.realpath(path(PROJECT, cwd))
|
||||||
|
|
||||||
|
|
||||||
def due(cwd):
|
def due(cwd, claim=False):
|
||||||
"""One line when the learned rules need organizing, else ""."""
|
"""One line when the learned rules need organizing, else ""."""
|
||||||
cfg, seen, why = store.config(), _organized(), []
|
cfg, seen, cl, now, need = store.config(), _organized(), _organized(("locks", "organize.json")), time.time(), []
|
||||||
|
active = lambda c, k: type(c.get("claim:" + k)) in (int, float) and 0 <= now - c["claim:" + k] < 1800
|
||||||
for scope in (USER, PROJECT):
|
for scope in (USER, PROJECT):
|
||||||
ls, k = scope_lines(scope, cwd, migrate=False), _mark_key(scope, cwd)
|
ls, k = _retro(scope_lines(scope, cwd, migrate=False)), _mark_key(scope, cwd)
|
||||||
prev = int(seen.get(k, seen.get("rules/projects/%s.md" % store.project_key(cwd), 0) if scope == PROJECT else 0))
|
prev = int(seen.get(k, seen.get("rules/projects/%s.md" % store.project_key(cwd), 0) if scope == PROJECT else 0))
|
||||||
over = len(ls) > cfg["rules_lines"] or sum(map(len, ls)) > cfg["rules_chars"]
|
over = len(ls) > cfg["rules_lines"] or sum(map(len, ls)) > cfg["rules_chars"]
|
||||||
added = len(ls) - prev
|
if not active(cl, k) and (over or len(ls) - prev >= cfg["organize_after"]):
|
||||||
if over or added >= cfg["organize_after"]:
|
need.append((k, "%s rules: %d lines, %s" % (scope, len(ls), "over the budget" if over else "%d new" % (len(ls) - prev))))
|
||||||
why.append("%s rules: %d lines, %s" % (scope, len(ls), "over the budget" if over else "%d new" % added))
|
if not need:
|
||||||
if not why:
|
|
||||||
return ""
|
return ""
|
||||||
|
if claim:
|
||||||
|
with store.lock("organize", wait=False) as ok:
|
||||||
|
cl = _organized(("locks", "organize.json")) if ok else {}
|
||||||
|
if not ok or not (need := [(k, m) for k, m in need if not active(cl, k)]):
|
||||||
|
return ""
|
||||||
|
cl.update({"claim:" + k: now for k, _ in need})
|
||||||
|
with contextlib.suppress(OSError):
|
||||||
|
store.write(store.data("locks", "organize.json"), json.dumps(cl))
|
||||||
return ("sisi: the learned rules need organizing (%s). In the background, give the output of "
|
return ("sisi: the learned rules need organizing (%s). In the background, give the output of "
|
||||||
"`sisi rules organize` to a cheap worker and record its answer with `sisi rules organize --record FILE`."
|
"`sisi rules organize` to a cheap worker and record its answer with `sisi rules organize --record FILE`."
|
||||||
% "; ".join(why))
|
% "; ".join(m for _, m in need))
|
||||||
|
|
||||||
|
|
||||||
def organize_brief(cwd):
|
def organize_brief(cwd):
|
||||||
cfg = store.config()
|
cfg, u, p = store.config(), lines(path(USER)), scope_lines(PROJECT, cwd)
|
||||||
|
with contextlib.suppress(OSError), store.lock("organize"):
|
||||||
|
cl = _organized(("locks", "organize.json"))
|
||||||
|
cl["snap:" + _mark_key(PROJECT, cwd)] = _snap(u, p)
|
||||||
|
store.write(store.data("locks", "organize.json"), json.dumps(cl))
|
||||||
return ORGANIZE % {
|
return ORGANIZE % {
|
||||||
"name": os.path.basename(store.project_root(cwd)), "objective": objective(),
|
"name": os.path.basename(store.project_root(cwd)), "objective": objective(),
|
||||||
"lines": cfg["rules_lines"], "chars": cfg["rules_chars"],
|
"lines": cfg["rules_lines"], "chars": cfg["rules_chars"],
|
||||||
"user": "\n".join(lines(path(USER))) or "(none)",
|
"user": "\n".join(u) or "(none)", "project": "\n".join(p) or "(none)",
|
||||||
"project": "\n".join(scope_lines(PROJECT, cwd)) or "(none)",
|
|
||||||
"retros": "\n".join(store.read(store.data("retro.jsonl")).splitlines()[-40:]) or "(none)"}
|
"retros": "\n".join(store.read(store.data("retro.jsonl")).splitlines()[-40:]) or "(none)"}
|
||||||
|
|
||||||
|
|
||||||
@@ -284,9 +299,10 @@ def organize_record(answer_file, cwd):
|
|||||||
if set(got) != {USER, PROJECT}:
|
if set(got) != {USER, PROJECT}:
|
||||||
return "the answer needs a `## user` section and a `## project` section, each a list of `- ` lines"
|
return "the answer needs a `## user` section and a `## project` section, each a list of `- ` lines"
|
||||||
for scope, ls in got.items():
|
for scope, ls in got.items():
|
||||||
if len(ls) > cfg["rules_lines"] or sum(map(len, ls)) > cfg["rules_chars"]:
|
r = _retro(ls)
|
||||||
|
if len(r) > cfg["rules_lines"] or sum(map(len, r)) > cfg["rules_chars"]:
|
||||||
return "the %s list is over the budget of %d lines and %d characters (it has %d and %d)" % (
|
return "the %s list is over the budget of %d lines and %d characters (it has %d and %d)" % (
|
||||||
scope, cfg["rules_lines"], cfg["rules_chars"], len(ls), sum(map(len, ls)))
|
scope, cfg["rules_lines"], cfg["rules_chars"], len(r), sum(map(len, r)))
|
||||||
up, pp = path(USER), path(PROJECT, cwd)
|
up, pp = path(USER), path(PROJECT, cwd)
|
||||||
if not pp or os.path.islink(os.path.dirname(pp)):
|
if not pp or os.path.islink(os.path.dirname(pp)):
|
||||||
return _migrate(cwd)[1].removeprefix("sisi: ")
|
return _migrate(cwd)[1].removeprefix("sisi: ")
|
||||||
@@ -295,6 +311,11 @@ def organize_record(answer_file, cwd):
|
|||||||
if msg:
|
if msg:
|
||||||
return msg.removeprefix("sisi: ")
|
return msg.removeprefix("sisi: ")
|
||||||
had = {USER: lines(up), PROJECT: scope_lines(PROJECT, cwd, migrate=False)}
|
had = {USER: lines(up), PROJECT: scope_lines(PROJECT, cwd, migrate=False)}
|
||||||
|
if (want := _organized(("locks", "organize.json")).get("snap:" + _mark_key(PROJECT, cwd))) and _snap(had[USER], had[PROJECT]) != want:
|
||||||
|
return "the rules changed since `sisi rules organize` ran; run `sisi rules organize` again"
|
||||||
|
for scope in (USER, PROJECT):
|
||||||
|
if miss := [l for l in had[scope] if _owner(l) and got[scope].count(l) < had[scope].count(l)]:
|
||||||
|
return "owner rule missing or changed (%s): %s" % (scope, miss[0])
|
||||||
if not any(got.values()) and any(had.values()):
|
if not any(got.values()) and any(had.values()):
|
||||||
return "the answer holds no rules at all"
|
return "the answer holds no rules at all"
|
||||||
try:
|
try:
|
||||||
@@ -311,7 +332,7 @@ def organize_record(answer_file, cwd):
|
|||||||
store.write(up, "".join(l + "\n" for l in got[USER]))
|
store.write(up, "".join(l + "\n" for l in got[USER]))
|
||||||
wrote_u = True
|
wrote_u = True
|
||||||
seen = _organized()
|
seen = _organized()
|
||||||
seen.update({_mark_key(USER): len(got[USER]), _mark_key(PROJECT, cwd): len(got[PROJECT])})
|
seen.update({_mark_key(USER): len(_retro(got[USER])), _mark_key(PROJECT, cwd): len(_retro(got[PROJECT]))})
|
||||||
seen.pop("rules/projects/%s.md" % store.project_key(cwd), None)
|
seen.pop("rules/projects/%s.md" % store.project_key(cwd), None)
|
||||||
marks = store.data("rules", "organized.json")
|
marks = store.data("rules", "organized.json")
|
||||||
store.write(marks, json.dumps(seen))
|
store.write(marks, json.dumps(seen))
|
||||||
@@ -322,6 +343,10 @@ def organize_record(answer_file, cwd):
|
|||||||
return "could not write %s (%s)" % (up, e)
|
return "could not write %s (%s)" % (up, e)
|
||||||
print("the organized rules are in %s; they could not be recorded for undo: %s" % (pp if wrote else up, e))
|
print("the organized rules are in %s; they could not be recorded for undo: %s" % (pp if wrote else up, e))
|
||||||
return None
|
return None
|
||||||
|
with contextlib.suppress(OSError), store.lock("organize"):
|
||||||
|
cl = {k: v for k, v in _organized(("locks", "organize.json")).items()
|
||||||
|
if k not in ("claim:" + _mark_key(USER), "claim:" + _mark_key(PROJECT, cwd), "snap:" + _mark_key(PROJECT, cwd))}
|
||||||
|
store.write(store.data("locks", "organize.json"), json.dumps(cl))
|
||||||
build_rulebook()
|
build_rulebook()
|
||||||
for scope in (USER, PROJECT):
|
for scope in (USER, PROJECT):
|
||||||
print("%s rules: %d lines (were %d)" % (scope, len(got[scope]), len(had[scope])))
|
print("%s rules: %d lines (were %d)" % (scope, len(got[scope]), len(had[scope])))
|
||||||
|
|||||||
+85
-15
@@ -217,7 +217,7 @@ class Core(unittest.TestCase):
|
|||||||
|
|
||||||
def test_rules_are_organized_by_a_worker_and_the_change_can_be_undone(self):
|
def test_rules_are_organized_by_a_worker_and_the_change_can_be_undone(self):
|
||||||
for i in range(9):
|
for i in range(9):
|
||||||
self.assertEqual(self.sisi("learn", "--project", "Always run the map tests number %d before a commit" % i)[0], 0)
|
self.assertEqual(self.sisi("retro", *GAP, "--improve", "Always run the map tests number %d before a commit" % i)[0], 0)
|
||||||
self.assertIn("need organizing", self.hook("tick")[1])
|
self.assertIn("need organizing", self.hook("tick")[1])
|
||||||
brief = self.sisi("rules", "organize")[1]
|
brief = self.sisi("rules", "organize")[1]
|
||||||
self.assertIn("map tests number 8", brief)
|
self.assertIn("map tests number 8", brief)
|
||||||
@@ -226,13 +226,13 @@ class Core(unittest.TestCase):
|
|||||||
self.assertEqual(glob.glob(os.path.join(self.data, "rules", "projects", "*.md")), [])
|
self.assertEqual(glob.glob(os.path.join(self.data, "rules", "projects", "*.md")), [])
|
||||||
before = self.read(path)
|
before = self.read(path)
|
||||||
answer = os.path.join(self.tmp, "answer.md")
|
answer = os.path.join(self.tmp, "answer.md")
|
||||||
for bad in ("no sections at all", "## user\n## project\n" + "- one more rule\n" * 31, "## user\n## project\n"):
|
for bad in ("no sections at all", "## user\n## project\n" + "- one more rule [retro 2026-01-01]\n" * 31, "## user\n## project\n"):
|
||||||
self.assertEqual(self.sisi("rules", "organize", "--record", self.write(answer, bad))[0], 1, bad[:20])
|
self.assertEqual(self.sisi("rules", "organize", "--record", self.write(answer, bad))[0], 1, bad[:20])
|
||||||
self.assertEqual(self.read(path), before)
|
self.assertEqual(self.read(path), before)
|
||||||
self.write(answer, "## user\n- Work from the checklist the person gave [owner 2026-01-01]\n"
|
self.write(answer, "## user\n- Work from the checklist the person gave [owner 2026-01-01]\n"
|
||||||
"## project\n- Run the map tests before a commit [owner 2026-01-01]\n")
|
"## project\n- Run the map tests before a commit [retro 2026-01-01]\n")
|
||||||
self.assertEqual(self.sisi("rules", "organize", "--record", answer)[0], 0)
|
self.assertEqual(self.sisi("rules", "organize", "--record", answer)[0], 0)
|
||||||
self.assertEqual(self.read(path), "- Run the map tests before a commit [owner 2026-01-01]\n")
|
self.assertEqual(self.read(path), "- Run the map tests before a commit [retro 2026-01-01]\n")
|
||||||
self.assertIn("Work from the checklist", self.read(self.data, "generated", "core.md"))
|
self.assertIn("Work from the checklist", self.read(self.data, "generated", "core.md"))
|
||||||
self.assertNotIn("need organizing", self.hook("tick", sid="s2")[1])
|
self.assertNotIn("need organizing", self.hook("tick", sid="s2")[1])
|
||||||
self.assertIn("Run the map tests", self.sisi("rules")[1])
|
self.assertIn("Run the map tests", self.sisi("rules")[1])
|
||||||
@@ -255,8 +255,8 @@ class Core(unittest.TestCase):
|
|||||||
self.assertFalse(os.path.lexists(os.path.join(self.proj, "pkg", ".sisi")))
|
self.assertFalse(os.path.lexists(os.path.join(self.proj, "pkg", ".sisi")))
|
||||||
self.assertIn("Always check subfolder paths before writing", self.hook("tick", cwd=sub)[1])
|
self.assertIn("Always check subfolder paths before writing", self.hook("tick", cwd=sub)[1])
|
||||||
self.assertIn("0 for all projects, 1 for this one", self.sisi("doctor", cwd=sub)[1])
|
self.assertIn("0 for all projects, 1 for this one", self.sisi("doctor", cwd=sub)[1])
|
||||||
# display trimming to budget (30 lines) and over-budget due check
|
# display trimming to budget (30 lines) and over-budget due check (non-owner rules)
|
||||||
self.write(rules_file, "".join("- Rule line number %02d [owner 2026-11-24]\n" % i for i in range(35)))
|
self.write(rules_file, "".join("- Rule line number %02d [retro 2026-11-24]\n" % i for i in range(35)))
|
||||||
tick = self.hook("tick", sid="trim", cwd=sub)[1]
|
tick = self.hook("tick", sid="trim", cwd=sub)[1]
|
||||||
self.assertNotIn("Rule line number 00", tick)
|
self.assertNotIn("Rule line number 00", tick)
|
||||||
self.assertIn("Rule line number 05", tick)
|
self.assertIn("Rule line number 05", tick)
|
||||||
@@ -379,7 +379,7 @@ class Core(unittest.TestCase):
|
|||||||
before = self.read(path)
|
before = self.read(path)
|
||||||
answer = self.write(os.path.join(self.tmp, "org.md"),
|
answer = self.write(os.path.join(self.tmp, "org.md"),
|
||||||
"## user\n- Keep things simple [owner 2026-11-24]\n"
|
"## user\n- Keep things simple [owner 2026-11-24]\n"
|
||||||
"## project\n- Check repo cleanliness always [owner 2026-11-24]\n")
|
"## project\n" + before + "- Check repo cleanliness always [retro 2026-11-24]\n")
|
||||||
self.assertEqual(self.sisi("rules", "organize", "--record", answer)[0], 0)
|
self.assertEqual(self.sisi("rules", "organize", "--record", answer)[0], 0)
|
||||||
self.assertEqual(len(git_out("log", "--oneline").splitlines()), commits_before)
|
self.assertEqual(len(git_out("log", "--oneline").splitlines()), commits_before)
|
||||||
self.assertEqual([l for l in git_out("status", "--porcelain").splitlines() if not l.startswith("??")], [])
|
self.assertEqual([l for l in git_out("status", "--porcelain").splitlines() if not l.startswith("??")], [])
|
||||||
@@ -599,7 +599,7 @@ class Core(unittest.TestCase):
|
|||||||
def ro_proj(name):
|
def ro_proj(name):
|
||||||
p = self.project(name)
|
p = self.project(name)
|
||||||
self.mkdir(p, ".sisi")
|
self.mkdir(p, ".sisi")
|
||||||
self.write(os.path.join(p, ".sisi", "rules.md"), "- existing rule [owner 2026-11-24]\n")
|
self.write(os.path.join(p, ".sisi", "rules.md"), "- existing rule [retro 2026-11-24]\n")
|
||||||
os.chmod(p, 0o555)
|
os.chmod(p, 0o555)
|
||||||
self.addCleanup(os.chmod, p, 0o755)
|
self.addCleanup(os.chmod, p, 0o755)
|
||||||
return p
|
return p
|
||||||
@@ -819,7 +819,10 @@ class Core(unittest.TestCase):
|
|||||||
self.assertEqual(self.read(wt, ".sisi", "rules.md"), tracked)
|
self.assertEqual(self.read(wt, ".sisi", "rules.md"), tracked)
|
||||||
|
|
||||||
def organized(self, cwd, *rules_):
|
def organized(self, cwd, *rules_):
|
||||||
answer = self.write(os.path.join(self.tmp, "answer-%d.md" % len(rules_)), "## user\n- A user rule kept [owner 2026-11-20]\n## project\n" + "".join(r + "\n" for r in rules_))
|
keep = lambda p: [l for l in (self.read(p).splitlines() if os.path.isfile(p) else []) if "[owner" in l]
|
||||||
|
u = keep(os.path.join(self.data, "rules", "user.md")) + ["- A user rule kept [owner 2026-11-20]"]
|
||||||
|
p = keep(os.path.join(cwd, ".sisi", "rules.md")) + list(rules_)
|
||||||
|
answer = self.write(os.path.join(self.tmp, "answer-%d.md" % len(rules_)), "## user\n" + "".join(r + "\n" for r in u) + "## project\n" + "".join(r + "\n" for r in p))
|
||||||
self.assertEqual(self.sisi("rules", "organize", "--record", answer, cwd=cwd)[0], 0)
|
self.assertEqual(self.sisi("rules", "organize", "--record", answer, cwd=cwd)[0], 0)
|
||||||
|
|
||||||
def test_revert_undoes_exactly_the_last_rule_change_of_any_kind(self):
|
def test_revert_undoes_exactly_the_last_rule_change_of_any_kind(self):
|
||||||
@@ -1068,16 +1071,16 @@ class Core(unittest.TestCase):
|
|||||||
def test_concurrent_store_commits_never_collide(self):
|
def test_concurrent_store_commits_never_collide(self):
|
||||||
pa, pb, pc = self.proj, self.project("beta"), self.project("gamma")
|
pa, pb, pc = self.proj, self.project("beta"), self.project("gamma")
|
||||||
self.sisi("learn", "--project", "Warm up store and alpha", cwd=pa)
|
self.sisi("learn", "--project", "Warm up store and alpha", cwd=pa)
|
||||||
self.sisi("learn", "--project", "Warm up beta", cwd=pb)
|
self.sisi("retro", *GAP, "--improve", "Warm up beta retro rule", cwd=pb)
|
||||||
for rnd in range(30):
|
for rnd in range(30):
|
||||||
old_c = self.legacy(pc, "- Legacy rule round %d [owner 2026-11-20]\n" % rnd)
|
old_c = self.legacy(pc, "- Legacy rule round %d [owner 2026-11-20]\n" % rnd)
|
||||||
ans = self.write(os.path.join(self.tmp, "org-%d.md" % rnd),
|
ans = self.write(os.path.join(self.tmp, "org-%d.md" % rnd),
|
||||||
"## user\n- Organized user rule %d [owner 2026-11-24]\n## project\n- Organized beta rule %d [owner 2026-11-24]\n" % (rnd, rnd))
|
"## user\n- Organized user rule %d [retro 2026-11-24]\n## project\n- Organized beta rule %d [retro 2026-11-24]\n" % (rnd, rnd))
|
||||||
cmds = [
|
cmds = [
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "learn", "--project", "Alpha one rule round %d" % rnd], pa, ""),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "learn", "--project", "Alpha one rule round %d" % rnd], pa, ""),
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "learn", "--project", "Alpha two rule round %d" % rnd], pa, ""),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "learn", "--project", "Alpha two rule round %d" % rnd], pa, ""),
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "learn", "--project", "Beta one rule round %d" % rnd], pb, ""),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "retro", *GAP, "--improve", "Beta one rule round %d" % rnd], pb, ""),
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "learn", "User rule round %d" % rnd], pa, ""),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "retro", *GAP, "--scope", "user", "--improve", "User rule round %d" % rnd], pa, ""),
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "retro", *GAP, "--improve", "Retro rule round %d" % rnd], pa, ""),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "retro", *GAP, "--improve", "Retro rule round %d" % rnd], pa, ""),
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "tick"], pc, json.dumps({"session_id": "c1-%d" % rnd, "cwd": pc})),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "tick"], pc, json.dumps({"session_id": "c1-%d" % rnd, "cwd": pc})),
|
||||||
([sys.executable, os.path.join(REPO, "bin", "sisi"), "tick"], pc, json.dumps({"session_id": "c2-%d" % rnd, "cwd": pc})),
|
([sys.executable, os.path.join(REPO, "bin", "sisi"), "tick"], pc, json.dumps({"session_id": "c2-%d" % rnd, "cwd": pc})),
|
||||||
@@ -1285,7 +1288,9 @@ cli.main(["tick"])
|
|||||||
self.assertEqual(self.sisi("learn", "--project", "Rule in beta before organize fails commit", cwd=beta)[0], 0)
|
self.assertEqual(self.sisi("learn", "--project", "Rule in beta before organize fails commit", cwd=beta)[0], 0)
|
||||||
gamma = self.project("org-older")
|
gamma = self.project("org-older")
|
||||||
self.assertEqual(self.sisi("learn", "--project", "Rule in gamma that revert must not undo", cwd=gamma)[0], 0)
|
self.assertEqual(self.sisi("learn", "--project", "Rule in gamma that revert must not undo", cwd=gamma)[0], 0)
|
||||||
ans = self.write(os.path.join(self.tmp, "org-lock.md"), "## user\n- Kept user rule [owner 2026-11-24]\n## project\n- Organized beta rule [owner 2026-11-24]\n")
|
ans = self.write(os.path.join(self.tmp, "org-lock.md"),
|
||||||
|
"## user\n" + self.read(self.data, "rules", "user.md") + "- Kept user rule [owner 2026-11-24]\n"
|
||||||
|
"## project\n" + self.read(beta, ".sisi", "rules.md") + "- Organized beta rule [retro 2026-11-24]\n")
|
||||||
lock = self.write(os.path.join(self.data, ".git", "index.lock"), "")
|
lock = self.write(os.path.join(self.data, ".git", "index.lock"), "")
|
||||||
rc_org, out_org, err_org = self.sisi("rules", "organize", "--record", ans, cwd=beta)
|
rc_org, out_org, err_org = self.sisi("rules", "organize", "--record", ans, cwd=beta)
|
||||||
self.assertEqual((rc_org, err_org), (0, ""), out_org)
|
self.assertEqual((rc_org, err_org), (0, ""), out_org)
|
||||||
@@ -3172,6 +3177,71 @@ cli.main(["tick"])
|
|||||||
res = faults.check(self.project("enospc-proj"))
|
res = faults.check(self.project("enospc-proj"))
|
||||||
self.assertIn("90% full (100 GB free)", res)
|
self.assertIn("90% full (100 GB free)", res)
|
||||||
|
|
||||||
|
def test_organize_protects_owner_rules_dedupes_notices_and_catches_concurrent_adds(self):
|
||||||
|
# 1. Owner rules are immutable: shortening a quote or dropping an owner rule is refused and names the line;
|
||||||
|
# reordering owner rules while merging two retro rules is accepted.
|
||||||
|
own1 = "- Disk space is mine: 'CLEAN IT UP! BE MORE PROACTIVE!!!' [owner 2026-11-25]"
|
||||||
|
own2 = "- Never write a to-do file for the owner [owner 2026-11-25]"
|
||||||
|
ret1 = "- Check row counts before exporting tables [retro 2026-11-24]"
|
||||||
|
ret2 = "- Verify row counts after exporting tables [retro 2026-11-24]"
|
||||||
|
self.write(os.path.join(self.mkdir(self.data, "rules"), "user.md"), "\n".join([own1, own2, ret1, ret2]) + "\n")
|
||||||
|
self.write(os.path.join(self.mkdir(self.proj, ".sisi"), "rules.md"), "- Project retro rule one [retro 2026-11-24]\n")
|
||||||
|
before_u = self.read(self.data, "rules", "user.md")
|
||||||
|
brief = self.sisi("rules", "organize")[1]
|
||||||
|
self.assertIn("verbatim", brief.lower())
|
||||||
|
ans = os.path.join(self.tmp, "org-ans.md")
|
||||||
|
# shortened owner quote -> refused, names the line, changes nothing
|
||||||
|
shortened = "## user\n- Disk space is mine: the owner asked four times. [owner 2026-11-25]\n%s\n%s\n## project\n- Project retro rule one [retro 2026-11-24]\n" % (own2, ret1)
|
||||||
|
rc, out, _ = self.sisi("rules", "organize", "--record", self.write(ans, shortened))
|
||||||
|
self.assertEqual(rc, 1, out)
|
||||||
|
self.assertIn(own1, out)
|
||||||
|
self.assertEqual(self.read(self.data, "rules", "user.md"), before_u)
|
||||||
|
# dropped owner rule -> refused, names the line, changes nothing
|
||||||
|
dropped = "## user\n%s\n%s\n## project\n- Project retro rule one [retro 2026-11-24]\n" % (own1, ret1)
|
||||||
|
rc, out, _ = self.sisi("rules", "organize", "--record", self.write(ans, dropped))
|
||||||
|
self.assertEqual(rc, 1, out)
|
||||||
|
self.assertIn(own2, out)
|
||||||
|
self.assertEqual(self.read(self.data, "rules", "user.md"), before_u)
|
||||||
|
# reordered owner rules + merged two retro rules -> accepted
|
||||||
|
merged = "## user\n%s\n%s\n- Check row counts before and after exporting tables [retro 2026-11-25]\n## project\n- Project retro rule one [retro 2026-11-24]\n" % (own2, own1)
|
||||||
|
rc, out, _ = self.sisi("rules", "organize", "--record", self.write(ans, merged))
|
||||||
|
self.assertEqual(rc, 0, out)
|
||||||
|
self.assertIn(own1, self.read(self.data, "rules", "user.md"))
|
||||||
|
self.assertIn(own2, self.read(self.data, "rules", "user.md"))
|
||||||
|
self.assertIn("before and after exporting tables", self.read(self.data, "rules", "user.md"))
|
||||||
|
|
||||||
|
# 2. A list over budget ONLY through [owner] rules gives no organize notice and never trims owner rules out
|
||||||
|
p_own = self.project("own-only")
|
||||||
|
big_owners = ["- Owner rule %02d: " % i + ("word " * 35) + "[owner 2026-11-25]" for i in range(25)]
|
||||||
|
self.write(os.path.join(self.mkdir(p_own, ".sisi"), "rules.md"), "\n".join(big_owners + [ret1]) + "\n")
|
||||||
|
tick_own = self.hook("tick", sid="own1", cwd=p_own)[1]
|
||||||
|
self.assertNotIn("need organizing", tick_own)
|
||||||
|
self.assertIn("Owner rule 00:", tick_own) # oldest owner rule never trimmed
|
||||||
|
|
||||||
|
# 3. Over budget through [retro] rules gives notice once across two sessions' starts and again after 30 minutes
|
||||||
|
p_ret = self.project("ret-over")
|
||||||
|
big_retros = ["- Retro rule %02d: " % i + ("word " * 35) + "[retro 2026-11-25]" for i in range(25)]
|
||||||
|
self.write(os.path.join(self.mkdir(p_ret, ".sisi"), "rules.md"), "\n".join(big_retros) + "\n")
|
||||||
|
self.assertIn("need organizing", self.hook("tick", sid="r1", cwd=p_ret)[1])
|
||||||
|
self.assertNotIn("need organizing", self.hook("tick", sid="r2", cwd=p_ret)[1]) # second session suppressed
|
||||||
|
# fake clock past 30 minutes -> notice fires again
|
||||||
|
sys.path.insert(0, REPO)
|
||||||
|
from sisi import rules, store
|
||||||
|
with mock.patch.object(store, "DATA", self.data), mock.patch.object(store, "STATE", os.path.join(self.tmp, "state")), \
|
||||||
|
mock.patch("time.time", return_value=time.time() + 1801):
|
||||||
|
self.assertIn("need organizing", rules.due(p_ret, claim=True))
|
||||||
|
|
||||||
|
# 4. A rule learned between `organize` and `--record` must not be lost (record is refused)
|
||||||
|
p_race = self.project("org-race")
|
||||||
|
self.write(os.path.join(self.mkdir(p_race, ".sisi"), "rules.md"), "- Initial retro rule one [retro 2026-11-24]\n")
|
||||||
|
self.assertEqual(self.sisi("rules", "organize", cwd=p_race)[0], 0)
|
||||||
|
self.assertEqual(self.sisi("retro", *GAP, "--improve", "New rule added while organizer was working", cwd=p_race)[0], 0)
|
||||||
|
stale_ans = "## user\n%s\n%s\n- Check row counts before and after exporting tables [retro 2026-11-25]\n## project\n- Initial retro rule one [retro 2026-11-24]\n" % (own2, own1)
|
||||||
|
rc, out, _ = self.sisi("rules", "organize", "--record", self.write(ans, stale_ans), cwd=p_race)
|
||||||
|
self.assertEqual(rc, 1, out)
|
||||||
|
self.assertIn("organize", out.lower())
|
||||||
|
self.assertIn("New rule added while organizer was working", self.read(p_race, ".sisi", "rules.md"))
|
||||||
|
|
||||||
def test_the_spec_is_unchanged(self):
|
def test_the_spec_is_unchanged(self):
|
||||||
for line in self.read(REPO, "docs", "spec", "SHA256SUMS").splitlines():
|
for line in self.read(REPO, "docs", "spec", "SHA256SUMS").splitlines():
|
||||||
digest, rel = line.split()
|
digest, rel = line.split()
|
||||||
@@ -3180,7 +3250,7 @@ cli.main(["tick"])
|
|||||||
|
|
||||||
def test_the_product_stays_small(self):
|
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")))
|
code = sum(len(self.read(p).splitlines()) for p in glob.glob(os.path.join(REPO, "sisi", "*.py")))
|
||||||
self.assertLessEqual(code, 1923, "sisi is two loops. If it needs more than this, find the simpler shape.")
|
self.assertLessEqual(code, 1948, "sisi is two loops. If it needs more than this, find the simpler shape.")
|
||||||
rulebook = self.read(REPO, "canon", "core.md")
|
rulebook = self.read(REPO, "canon", "core.md")
|
||||||
self.assertLess(len(rulebook.splitlines()), 135)
|
self.assertLess(len(rulebook.splitlines()), 135)
|
||||||
self.assertLessEqual(len(rulebook), 9872)
|
self.assertLessEqual(len(rulebook), 9872)
|
||||||
|
|||||||
Reference in new issue
Block a user