diff --git a/README.md b/README.md index 0815789..32c121e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ person will need next: delegating or editing is refused until then. Every check (`sisi done --pass N` or `--fail N`) before the turn ends; a turn that changed things, or failed a check, is sent to the retro, and any other closes itself, as does work that took under `retro_minutes`, passed every check and delegated nothing. While a delegated job is open and not late, a stop is not held up for checks without a verdict. Each record in `retro.jsonl` carries `session_id`, `retro_seconds` (when demanded), the numbers (minutes, messages, tool calls, delegations, checks), the `term` it fell short on (`value`, `evidence`, `time`, `cost`, -`attention` or `learning`: the terms of the objective), and (for gap retros) the `problem` name. `sisi retro` lays the facts out under those terms, lists known problems (most recently cited first), and shows their trend. Citing the same problem a third time (2nd miss) and after opens a fault requiring a hook or automatic check rather than another rule. +`attention` or `learning`: the terms of the objective), and (for gap retros) the `problem` name. `sisi retro` lays the facts out under those terms, lists known problems (most recently cited first), and shows their trend. Citing the same problem a third time (2nd miss) and after opens a fault requiring a hook or automatic check rather than another rule. Problems are shared across all projects in your store while the fault opens in the project where the problem was cited, and a reworded name opens a new problem (matching is by the same words, ignoring case and spacing), so cite by number. **Faults.** Record any fault you see with `sisi fault ""` (kept per project across sessions). Where the tool has hooks, session start and every request show the faults still not fixed, and with the gate @@ -103,7 +103,7 @@ Rules live at three levels: using sisi, the same for all users. `SPEC.md` is what it aims at. - **Your store** (`$SISI_DATA`, else `$XDG_DATA_HOME/sisi`, else `~/.local/share/sisi`; a git repository): `rules/user.md` (how you like to work, across all your projects), `models.md` (your workers), `retro.jsonl` (each piece of work: session, - `retro_seconds`, counts, goal, term, problem) and `outcomes.jsonl` (each judged delegation: session, `project_key`, `model`, + `retro_seconds`, counts, goal, term, problem; problem tracking reads the last 64,000,000 bytes) and `outcomes.jsonl` (each judged delegation: session, `project_key`, `model`, `started`/`ended`, minutes, verdict, why). All plain text you can read and edit. - **Each project** (`.sisi/rules.md` in the project's folder: in a git repository, the root of its main checkout, shared by all its worktrees; outside git, the folder sisi is run in): rules specific to one project. In a bare repository with diff --git a/canon/core.md b/canon/core.md index 2b0855d..816fa8f 100644 --- a/canon/core.md +++ b/canon/core.md @@ -110,8 +110,8 @@ tests, evidence and completion. When work ends, the lead runs `sisi retro` to see what happened and known problems. Set it against the objective by term: where did work fall short, why, and what instruction prevents it? Record -`sisi retro --gap … --cause … --improve … --term T --problem N|""` (cite listed problem `N` or name a new one; on a 2nd miss, a rule did not hold and a fault opens). The improvement becomes a rule at -once: product (every user, in the product), user (all your projects, in your store), or project (`.sisi/rules.md` at root, in git main checkout's, which may be checked in). Name the root cause, not general +`sisi retro --gap … --cause … --improve … --term T --problem N|""` (`--problem` is required with `--gap` or refused: cite `N` or name a new one; each citation after opening is a miss; at the 2nd miss a fault opens). The improvement becomes a rule at +once: product (every user, in the product), user (how this person works across projects, in their store), or project (`.sisi/rules.md` at root, in git its main checkout's, which may be checked in). Name the root cause, not general advice. Prefer a rule that makes next work do more by itself over a caution; a caution says what stays allowed without asking. Work can always be done better: when nothing fell short, name the one thing that would have scored better (`--no-gap … --term T --better …`). When the person states a lasting preference, record their words at once with `sisi learn`. When session start says diff --git a/sisi/retro.py b/sisi/retro.py index 9c322ed..7ae8171 100644 --- a/sisi/retro.py +++ b/sisi/retro.py @@ -81,7 +81,7 @@ def trend(): def _problems(recs=None): """Known problems derived from retro.jsonl in order of first appearance (1-based number).""" by_key, out = {}, [] - for idx, r in enumerate(store.records("retro.jsonl") if recs is None else recs): + for idx, r in enumerate(store.records("retro.jsonl", 64000000) if recs is None else recs): if not (isinstance(r.get("problem"), str) and (name := clean(r["problem"]))): continue date, key = str(r.get("ts") or "")[:10] or "?", name.lower() @@ -98,12 +98,16 @@ def _problem_lines(probs): def _resolve_problem(problem, probs): - if not problem or len(problem) > 400: - return None, "sisi retro: every --gap retro names a problem (--problem N to cite, or --problem \"\" to open one)." - if re.fullmatch(r"[+-]?\d+", problem): - n = int(problem) - return (probs[n - 1], "") if 1 <= n <= len(probs) else (None, "sisi retro: problem #%s does not exist." % problem) - return next((p for p in probs if p["name"].lower() == problem.lower()), {"num": len(probs) + 1, "name": problem, "attempts": []}), "" + if not problem or not (norm := " ".join(problem.split())): + return None, 'sisi retro: every --gap retro names a problem (--problem N to cite, or --problem "" to open one).' + if clean(problem) != norm: + return None, "sisi retro: name the problem in plain words (no secrets, tokens, or long hex/digit strings)." + if len(norm) > 400: + return None, "sisi retro: problem name is at most 400 characters." + if re.fullmatch(r"[+-]?\d+", norm): + n = int(norm) + return (probs[n - 1], "") if 1 <= n <= len(probs) else (None, "sisi retro: problem #%s does not exist." % norm) + return next((p for p in probs if p["name"].lower() == norm.lower()), {"num": len(probs) + 1, "name": norm, "attempts": []}), "" def report(sid, cwd): @@ -182,7 +186,7 @@ def _options(args): opt, i = {}, 0 while i < len(args): if args[i] in FLAGS and i + 1 < len(args) and args[i][2:] not in opt: - opt[args[i][2:]] = clean(args[i + 1]) + opt[args[i][2:]] = args[i + 1] if args[i] == "--problem" else clean(args[i + 1]) i += 2 else: return None @@ -206,10 +210,13 @@ def run(args, cwd=None): else "no-gap" if set(opt) == {"no-gap"} and _words(opt, "no-gap", 5) else "gap" if set(opt) == {"gap", "cause", "improve"} and all(_words(opt, k, 4) for k in opt) else None) if (kind is None or len(opt.get("improve", "")) > 400 or len(better or "") > 400 or (kind == "later" and (term or better)) - or (kind != "no-gap" and better) or (kind != "gap" and problem is not None)): + or (kind != "no-gap" and better)): print("sisi retro: that is not a retro yet (each part needs a real sentence; an improvement is at most " "400 characters).\n" + USAGE) return 1 + if kind != "gap" and problem is not None: + print("sisi retro: --problem goes only with --gap.") + return 1 if kind != "later" and term not in TERMS: print("sisi retro: name the term of the objective where the work fell short, or could most have scored better. " + TERM_HELP) return 1 @@ -217,16 +224,15 @@ def run(args, cwd=None): print("sisi retro: a piece of work can always be done better. Add --better \"\"; it is recorded, and becomes a rule only if it recurs." % term) return 1 - prob, prior = None, [] + prob = None if kind == "gap": probs = _problems() prob, why = _resolve_problem(problem, probs) if not prob: plines = _problem_lines(probs) - print(why + "\n" + ("known problems (most recent first):\n" + "\n".join(plines) if plines else "known problems: none yet.") + - "\nRe-run with --problem N (one of the numbers above) or --problem \"\".") + print(why + ("\nknown problems (most recent first):\n" + "\n".join(plines) + "\nRe-run with --problem N (one of the numbers above) or --problem \"\"." + if plines else "\nknown problems: none yet.\nRe-run with --problem \"\".")) return 1 - prior = list(prob["attempts"]) opt.update({"term": term, "problem": prob["name"]} if kind == "gap" else {"term": term, "better": clean(better)} if kind == "no-gap" else {}) if sid: with store.session(sid) as s: @@ -242,9 +248,11 @@ def run(args, cwd=None): elif kind != "later": record({}, cwd, dict(opt, scope=scope) if kind == "gap" else opt) if kind == "gap": - misses = len(prior) + found = next((p for p in _problems() if p["name"].lower() == opt["problem"].lower()), None) + prior = found["attempts"][:-1] if found else prob["attempts"] + prob, misses = found or prob, len(prior) if misses: - print("problem %d '%s' has come back: %d misses" % (prob["num"], prob["name"], misses)) + print("problem %d '%s' has come back: %d miss%s" % (prob["num"], prob["name"], misses, "" if misses == 1 else "es")) for a in prior: print(" - [%s] %s" % (a["date"], a["improve"] or a["cause"] or "(no improvement recorded)")) else: diff --git a/tests/test_core.py b/tests/test_core.py index fe6473c..61d129e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -3657,7 +3657,7 @@ cli.main(["tick"]) 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, 2161, "sisi is two loops. If it needs more than this, find the simpler shape.") + self.assertLessEqual(code, 2169, "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()), 137) self.assertLessEqual(len(rulebook), 10530) diff --git a/tests/test_problems.py b/tests/test_problems.py index b7045bd..1a9a068 100644 --- a/tests/test_problems.py +++ b/tests/test_problems.py @@ -1,6 +1,8 @@ """Tests for retro problem tracking and repeat-miss fault escalation.""" +import contextlib import glob import hashlib +import io import json import os import shutil @@ -8,8 +10,11 @@ import subprocess import sys import tempfile import unittest +from unittest import mock REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, REPO) +from sisi import retro, store GAP = ("--gap", "the export dropped rows with empty names until the person noticed", "--cause", "no check compared the row counts before and after", "--term", "evidence") @@ -113,7 +118,8 @@ class Problems(unittest.TestCase): "--problem", "1") self.assertEqual((rc, err), (0, ""), out) self.assertEqual(self.records()[-1]["problem"], "export drops empty names") - self.assertIn("problem 1 'export drops empty names' has come back: 1 misses", out) + self.assertIn("problem 1 'export drops empty names' has come back: 1 miss", out) + self.assertNotIn("1 misses", out) self.assertIn("Compare row counts before and after every export", out) # Add more non-problem records and cite #2; numbers stay stable @@ -124,7 +130,8 @@ class Problems(unittest.TestCase): "--improve", "Hash inputs before reusing build artifacts", "--term", "value", "--problem", "2") self.assertEqual(rc, 0, out) - self.assertIn("problem 2 'stale build artifacts' has come back: 1 misses", out) + self.assertIn("problem 2 'stale build artifacts' has come back: 1 miss", out) + self.assertNotIn("1 misses", out) self.assertIn("Always clean output directory before packaging release", out) def test_misses_and_fault_escalation_lifecycle(self): @@ -138,7 +145,8 @@ class Problems(unittest.TestCase): rc, out, _ = self.sisi("retro", *GAP, "--improve", "Second rule attempt for empty names", "--problem", "1") self.assertEqual(rc, 0, out) - self.assertIn("problem 1 'empty name export bug' has come back: 1 misses", out) + self.assertIn("problem 1 'empty name export bug' has come back: 1 miss", out) + self.assertNotIn("1 misses", out) self.assertIn("First rule attempt for empty names", out) self.assertEqual(self.sisi("fault")[1].strip(), "no open faults.") @@ -200,6 +208,152 @@ class Problems(unittest.TestCase): self.assertNotIn("problem item 02", rep) self.assertNotIn("problem item 03", rep) + def test_race_derives_number_and_misses_after_append(self): + real_append = store.append + log_path = os.path.join(self.data, "retro.jsonl") + + def racing_append(extra_records): + injected = False + + def wrapper(path, line, once=False): + nonlocal injected + if not injected and path == log_path: + injected = True + for rec in extra_records: + real_append(path, json.dumps(rec)) + return real_append(path, line, once=once) + + return wrapper + + base_args = list(GAP) + ["--improve", "Always verify export row count in CI checks", "--problem"] + 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")): + # Between pre-check (empty log) and append, another session opens a different problem and the same problem + extra = [ + {"ts": "2026-11-26T10:00:00", "gap": "other gap", "cause": "other cause", + "improve": "Other rule first", "term": "time", "problem": "other race problem"}, + {"ts": "2026-11-26T10:00:01", "gap": "same gap", "cause": "same cause", + "improve": "Earlier racing rule for shared problem", "term": "evidence", "problem": "shared race problem"}, + ] + out1 = io.StringIO() + with mock.patch.object(store, "append", side_effect=racing_append(extra)), contextlib.redirect_stdout(out1): + rc1 = retro.run(base_args + ["shared race problem"], cwd=self.proj) + self.assertEqual(rc1, 0, out1.getvalue()) + self.assertIn("problem 2 'shared race problem' has come back: 1 miss", out1.getvalue()) + self.assertNotIn("opened problem 1", out1.getvalue()) + rep1 = self.sisi("retro")[1] + self.assertIn("#1: other race problem (1 attempt", rep1) + self.assertIn("#2: shared race problem (2 attempts", rep1) + + # Second race: pre-check sees #2 with 2 attempts (1 miss); racing session adds 3rd attempt before append + extra2 = [ + {"ts": "2026-11-26T10:05:00", "gap": "third gap", "cause": "third cause", + "improve": "Third racing rule for shared problem", "term": "evidence", "problem": "shared race problem"}, + ] + out2 = io.StringIO() + with mock.patch.object(store, "append", side_effect=racing_append(extra2)), contextlib.redirect_stdout(out2): + rc2 = retro.run(base_args + ["2"], cwd=self.proj) + self.assertEqual(rc2, 0, out2.getvalue()) + self.assertIn("problem 2 'shared race problem' has come back: 3 misses", out2.getvalue()) + self.assertIn("opened fault #1", out2.getvalue()) + rep2 = self.sisi("retro")[1] + self.assertIn("#2: shared race problem (4 attempts", rep2) + + def test_large_log_past_2mb_keeps_problem_numbers_and_attempts(self): + os.makedirs(self.data, exist_ok=True) + log = os.path.join(self.data, "retro.jsonl") + first = json.dumps({"ts": "2026-01-01T10:00:00", "gap": "early gap", "cause": "early cause", + "improve": "Early rule for problem one", "term": "evidence", "problem": "early problem"}) + "\n" + pad_line = json.dumps({"ts": "2026-06-01T10:00:00", "no-gap": "all checks passed cleanly today " + ("x" * 120), + "better": "start the build earlier next time around", "term": "time"}) + "\n" + last = json.dumps({"ts": "2026-11-26T10:00:00", "gap": "late gap", "cause": "late cause", + "improve": "Late rule for problem two", "term": "value", "problem": "late problem"}) + "\n" + repeat_count = (2_100_000 // len(pad_line)) + 1 + with open(log, "w", encoding="utf-8") as f: + f.write(first + (pad_line * repeat_count) + last) + self.assertGreater(os.path.getsize(log), 2_000_000) + + rep = self.sisi("retro")[1] + self.assertIn("#1: early problem (1 attempt", rep) + self.assertIn("#2: late problem (1 attempt", rep) + + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Second rule for early problem", "--problem", "1") + self.assertEqual(rc, 0, out) + self.assertIn("problem 1 'early problem' has come back: 1 miss", out) + rep2 = self.sisi("retro")[1] + self.assertIn("#1: early problem (2 attempts", rep2) + self.assertIn("#2: late problem (1 attempt", rep2) + + def test_wording_refusals_and_miss_singular(self): + # With no problems yet, refusals must not say "numbers above" + for p_args in ((), ("--problem", "1")): + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Should not be written anywhere", *p_args) + self.assertNotEqual(rc, 0) + self.assertIn("none yet", out) + self.assertNotIn("numbers above", out) + self.assertEqual(self.records(), []) + + # Problem name over 400 characters is refused with a message mentioning the 400-character limit + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Should not be written anywhere", "--problem", "p" * 401) + self.assertNotEqual(rc, 0) + self.assertIn("400", out) + self.assertEqual(self.records(), []) + + # --no-gap with --problem says --problem goes only with --gap + rc, out, _ = self.sisi("retro", "--no-gap", "the export kept every row cleanly", + "--term", "time", "--better", "start the first build sooner than before", + "--problem", "1") + self.assertNotEqual(rc, 0) + self.assertIn("--problem", out) + self.assertIn("--gap", out) + self.assertNotIn("that is not a retro yet", out) + self.assertEqual(self.records(), []) + + def test_redacted_problem_names_refused(self): + bad_names = ( + "sk-abcdefghijklmnopqrstuvwx", + "0123456789abcdef0123456789abcdef01234567", + "1234567890" * 4, + "api_key=supersecret999 broken", + ) + for bad in bad_names: + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Should not record secret problem name", "--problem", bad) + self.assertNotEqual(rc, 0, bad) + self.assertIn("plain words", out) + self.assertNotIn(bad, out) + self.assertEqual(self.records(), []) + self.assertFalse(os.path.exists(os.path.join(self.proj, ".sisi", "rules.md"))) + + def test_two_problems_each_open_own_fault_and_case_spacing_joins(self): + self.assertEqual(self.sisi("retro", *GAP, "--improve", "Alpha rule one for export", "--problem", "alpha issue")[0], 0) + self.assertEqual(self.sisi("retro", *GAP, "--improve", "Bravo rule one for export", "--problem", "bravo issue")[0], 0) + + # Citing by name with different case and spacing joins the same problem + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Alpha rule two by name", "--problem", " ALPHA issue ") + self.assertEqual(rc, 0, out) + self.assertIn("problem 1 'alpha issue' has come back: 1 miss", out) + self.assertNotIn("1 misses", out) + self.assertEqual(self.sisi("fault")[1].strip(), "no open faults.") + + # 2nd miss on problem 1 opens its fault + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Alpha rule three opens fault", "--problem", "1") + self.assertEqual(rc, 0, out) + self.assertIn("problem 1 'alpha issue' has come back: 2 misses", out) + + # Same-length second problem joins via case/spacing then reaches 2 misses and opens its own fault + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Bravo rule two by name", "--problem", " bravo ISSUE ") + self.assertEqual(rc, 0, out) + self.assertIn("problem 2 'bravo issue' has come back: 1 miss", out) + rc, out, _ = self.sisi("retro", *GAP, "--improve", "Bravo rule three opens fault", "--problem", "2") + self.assertEqual(rc, 0, out) + self.assertIn("problem 2 'bravo issue' has come back: 2 misses", out) + + faults_lines = self.sisi("fault")[1].strip().splitlines() + self.assertEqual(len(faults_lines), 2, faults_lines) + self.assertTrue(any("alpha issue" in l for l in faults_lines), faults_lines) + self.assertTrue(any("bravo issue" in l for l in faults_lines), faults_lines) + if __name__ == "__main__": unittest.main()