Files
sisi/sisi/retro.py
T
swaits 5f488476e8
ci / test (push) Canceled after 0s
Faults: review fixes (redaction, crash-proof reads, bounds, validation, wording)
- Redact secrets and strip lone surrogates in fault text and notes via shared store.clean.
- Make fault reads never raise on non-UTF-8 (0xFF), deep JSON (60,000 '['), or unreadable dirs (mode 000): command prints one line and returns 1; hooks warn with path, keep printing project rules, and return 0.
- Cap request-hook fault output to 10 oldest + 'and N more: sisi fault'; refuse past 50 open faults; bound notes to 300 chars; require positive <=9-digit id and >=4-word notes.
- Make concurrency test deterministic (fails 6/6 with no-op lock) and fix worker tick-before-prompt test order.
- Tighten rule 7, rule 9, and README wording (no over-promising on gate; clear only once no running job uses it).

Size: sisi/*.py stays within the 1,923-line ceiling (1,923 lines, unchanged).
canon/core.md grew from 8,374 chars (before commit f011df5) to 9,872 chars (+1,498 chars across this piece of work; +98 chars in this commit, raising the rulebook char ceiling in test_the_product_stays_small from 9,774 to 9,872).
2026-11-25 11:08:45 -07:00

215 lines
12 KiB
Python

"""The retro: what happened, set against the objective; the gap, its cause, and the instruction that would have
prevented it. That instruction becomes a learned rule at once."""
import json, math, os, re, time
from . import rules, store
DONE_USAGE = """usage: sisi done --goal "<the goal in their words, 5+ words>" --check "<a check that can fail, 4+ words>" [--check ...] \\
--next "<what they will need next and what you do about it: now, room, follow-up or drop; 4+ words>"
sisi done --pass N "<evidence>" | --fail N "<why>" sisi done: show it"""
FLAGS = ("--gap", "--cause", "--improve", "--no-gap", "--later", "--scope", "--term", "--better")
TERMS = {"value": "what the person wanted", "evidence": "verified by checks that fit the risk", "time": "elapsed time",
"cost": "compute and tool cost", "attention": "the person's attention", "learning": "getting better at the next piece of work"}
TERM_HELP = "--term is one of: %s." % ", ".join(TERMS)
clean = store.clean
USAGE = """Record one of these:
sisi retro --gap "<where the work fell short of the objective, with the evidence>" \\
--cause "<the root cause>" \\
--improve "<the instruction that would have prevented it, worded as a rule>" --term TERM [--scope user]
The improvement becomes a rule at once: in this project, or with --scope user in all your work.
sisi retro --no-gap "<what shows the work met the objective>" --term TERM \\
--better "<one concrete thing that would have scored better on that term, 5+ words>"
TERM is where the work fell short, or could most have scored better: %s.
A piece of work can always be done better; the better is recorded with the retro, and becomes a rule only if it recurs, when the rules are organized.
sisi retro --later "<what the unfinished work is waiting for>\"""" % "; ".join("%s = %s" % kv for kv in TERMS.items())
def counts(state, cwd):
since = state.get("retro_at") or state.get("started") or time.time()
checks = (state.get("done") or {}).get("checks") or []
return {"minutes": int((time.time() - since) / 60), "messages": state.get("prompts_since", 0),
"direct": state.get("direct_since", 0), "delegations": state.get("delegations_since", 0),
"commits": len(store.git(["log", "--oneline", "--since=@%d" % since], cwd)[1].splitlines()),
"checks_pass": sum(c["verdict"] == "pass" for c in checks), "checks_fail": state.get("fails_since", 0)}
def facts(state, cwd):
n, dirty = counts(state, cwd), store.git(["status", "--porcelain"], cwd)[1].splitlines()
d = show(state["done"]) if state.get("done") else ["(no done recorded)"]
named = fell(store.records("retro.jsonl")[-10:])
out = ["time (measured): %d minutes since %s" % (n["minutes"], "the last retro" if state.get("retro_at") else "the session began"),
"cost (measured): you made %d tool call(s) yourself and %d delegation(s); workers used: %s" % (
n["direct"], n["delegations"], ", ".join(state.get("workers_since") or []) or "none")]
out += [' you extended your own direct work: "%s"' % r for r in state.get("reasons_since") or []]
out += ["attention (measured): the person wrote %d message(s) in that time: read them again. Was any a correction, "
"or something they had to say twice?" % n["messages"],
"evidence (measured): %d check(s) passed, %d failed" % (n["checks_pass"], n["checks_fail"])] + d[1:-1]
out += ["value (judged): did the person get what they wanted? changed: %d commit(s), %d file(s) uncommitted" % (n["commits"], len(dirty))]
from . import faults
fs = faults.lines(cwd)
return out + d[:1] + d[1:][-1:] + (["open faults (%d):" % len(fs)] + [" " + l for l in fs] if fs else []) + ["learning (measured): last 10 pieces of work named: %s" % (named or "none yet")]
def show(d):
mark = lambda c: "[%s: %s]" % (c["verdict"], c["evidence"]) if c["verdict"] else "[no verdict yet]"
return (["done: " + d["goal"]] + [" %d. %s %s" % (i, c["text"], mark(c)) for i, c in enumerate(d["checks"], 1)]
+ ["next: " + d["next"]])
def fell(group):
"""How many of these records named each term, e.g. "time 4, attention 2" (old records carry none)."""
return ", ".join("%s %d" % (k, n) for k in TERMS for n in [sum(r.get("term") == k for r in group)] if n)
def trend():
"""The measured trend, by term, over the records that carry numbers: the last 10 pieces of work and the 10 before."""
records = store.records("retro.jsonl")
units = [r for r in records if isinstance(r.get("unit"), dict)]
num = lambda x, k: x[k] if type(x.get(k)) in (int, float) else 0
median = lambda v: (lambda s: (s[(len(s) - 1) // 2] + s[len(s) // 2]) / 2.0)(sorted(v))
out = []
for label, group in (("last", units[-10:]), ("before", units[-20:-10])):
if group:
u = [r["unit"] for r in group]
out.append(" %s %d: time median %g min; cost median %g calls; attention median %g messages; evidence %d checks failed" % (
label, len(group), median([num(x, "minutes") for x in u]),
median([num(x, "direct") + num(x, "delegations") for x in u]), median([num(x, "messages") for x in u]),
sum(num(x, "checks_fail") for x in u)))
out.append(" fell short or could do better on: %s" % (fell(records[-10:]) or "nothing named yet"))
return ["measured (pieces of work with numbers):"] + out if len(units) >= 2 else []
def report(sid, cwd):
state = {}
if sid:
with store.session(sid) as s:
state = dict(s)
print("sisi retro: what happened in this piece of work, by term of the objective.")
print("\n".join(facts(state, cwd)))
print("\nThe objective: %s" % rules.objective())
print("\nSet what happened against the objective, term by term. Where did the work fall short, why, and what "
"instruction would have prevented it? Name the root cause; general advice is not an improvement.\n")
print(USAGE)
if trend():
print("\n" + "\n".join(trend()))
def record(s, cwd, opt, sid=None):
"""Append one record for the piece of work in this session state, and begin the next one."""
d, now, dem = s.get("done") or {}, time.time(), s.get("retro_demanded")
entry = dict(opt, ts=time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(now)),
project=os.path.basename(store.project_root(cwd)), unit=counts(s, cwd),
goal=d.get("goal", ""), workers=s.get("workers_since", []))
if d:
entry["done"] = d
if sid:
entry["session_id"] = sid
if type(dem) in (int, float) and math.isfinite(dem) and dem <= now:
entry["retro_seconds"] = max(0, int(round(now - dem)))
log = store.data("retro.jsonl")
store.append(log, json.dumps(entry, sort_keys=True))
if not store.commit("retro recorded", log):
print("sisi: retro saved, but store git commit failed.")
s["snapshot"], s["changed"], s["retro_at"] = store.snapshot(cwd), False, now
for key in ("prompts_since", "direct_since", "delegations_since", "reasons_since", "workers_since", "done", "paused",
"fails_since", "picked", "starts", "retro_demanded", "began"):
s.pop(key, None)
def done(args, cwd=None):
"""`sisi done`: define done before the work (goal, checks, what comes next), then give every check a verdict."""
sid, opt, verdict, i = store.current_session(cwd or os.getcwd()), {}, None, 0
while i < len(args):
if args[i] in ("--goal", "--next", "--check") and i + 1 < len(args):
opt.setdefault(args[i][2:], []).append(clean(args[i + 1]))
i += 2
elif args[i] in ("--pass", "--fail") and i + 2 < len(args) and not verdict:
verdict, i = (args[i][2:], args[i + 1], clean(args[i + 2])), i + 3
else:
i = len(args) + 1
words = lambda key, least: all(len(x.split()) >= least for x in opt[key])
new = bool(opt) and not verdict and set(opt) == {"goal", "next", "check"} and len(opt["goal"]) == len(opt["next"]) == 1 \
and words("goal", 5) and words("next", 4) and words("check", 4)
if i != len(args) or not sid or not (new or (verdict and not opt) or not (opt or verdict)):
print(DONE_USAGE)
return 1
with store.session(sid) as s:
if new:
s["done"] = {"goal": opt["goal"][0], "next": opt["next"][0],
"checks": [{"text": c, "verdict": None, "evidence": ""} for c in opt["check"]]}
elif verdict:
kind, n, why = verdict
if not (s.get("done") and n.isdecimal() and 1 <= int(n) <= len(s["done"]["checks"]) and len(why.split()) >= 3):
print(DONE_USAGE)
return 1
s["done"]["checks"][int(n) - 1].update(verdict=kind, evidence=why)
s["fails_since"] = s.get("fails_since", 0) + (kind == "fail")
s.pop("paused", None) if new or verdict else None
print("\n".join(show(s["done"])) if s.get("done") else "no done recorded yet.\n" + DONE_USAGE)
return 0
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])
i += 2
else:
return None
return opt
def _words(opt, key, least):
return len((opt.get(key) or "").split()) >= least
def run(args, cwd=None):
cwd = cwd or os.getcwd()
sid = store.current_session(cwd)
if not args:
return report(sid, cwd) or 0
opt = _options(args)
scope, term, better = [(opt or {}).pop(k, None) for k in ("scope", "term", "better")]
scope = scope or rules.PROJECT
kind = (None if not opt or scope not in (rules.USER, rules.PROJECT)
else "later" if set(opt) == {"later"} and _words(opt, "later", 3)
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):
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 != "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
if kind == "no-gap" and len((better or "").split()) < 5:
print("sisi retro: a piece of work can always be done better. Add --better \"<one concrete thing that would have "
"scored better on %s, 5+ words>\"; it is recorded, and becomes a rule only if it recurs." % term)
return 1
opt.update({"term": term} if kind == "gap" else {"term": term, "better": clean(better)} if kind == "no-gap" else {})
if sid:
with store.session(sid) as s:
if kind == "no-gap" and any(c["verdict"] == "fail" for c in (s.get("done") or {}).get("checks", [])):
print("sisi retro: a failed check is a gap; record it with --gap.")
return 1
if kind == "gap":
opt["scope"] = scope
if kind == "later":
s["paused"] = True
else:
record(s, cwd, opt, sid)
elif kind != "later":
record({}, cwd, dict(opt, scope=scope) if kind == "gap" else opt)
if kind == "gap":
try:
added, warn = rules.add(opt["improve"], scope, "retro", cwd)
print(warn or "recorded. In force from the next session%s: %s" % (
" in every project" if scope == rules.USER else " in this project", added))
except OSError as e:
print("recorded, but could not write %s (%s)." % (rules.path(scope, cwd) or cwd, e))
if rules.due(cwd):
print(rules.due(cwd))
else:
print("recorded." if kind == "no-gap" else "noted: the retro waits until this work ends.")
return 0