Faults: add per-project open-faults list, hooks, retro listing, and rulebook text
ci / test (push) Canceled after 0s
ci / test (push) Canceled after 0s
This commit is contained in:
1 parent
f011df5a46
commit
4ab516c803
7 files changed
+229
-12
No files matched your search
@@ -8,6 +8,7 @@ Before and after the work
|
||||
record what done means and what the person will need next; nothing starts before
|
||||
sisi done --pass N EVIDENCE | --fail N WHY
|
||||
give a check its verdict; `sisi done` alone shows it
|
||||
sisi fault [TEXT | --started|--blocked|--fixed N NOTE] open faults in this project; alone lists them
|
||||
|
||||
The learning loop
|
||||
sisi retro what happened in this piece of work, to set against the objective
|
||||
@@ -61,6 +62,9 @@ def main(argv):
|
||||
if cmd in ("retro", "done"):
|
||||
from . import retro
|
||||
return (retro.run if cmd == "retro" else retro.done)(args)
|
||||
if cmd == "fault":
|
||||
from . import faults
|
||||
return faults.run(args)
|
||||
one = {"rules": rules.run, "pick": models.pick, "learn": rules.learn, "models": models.run, "judge": models.judge,
|
||||
"direct": hooks.direct}
|
||||
if cmd in one:
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Open faults per project: seen problems that must get a started fix, a named blocker, or evidence of a fix."""
|
||||
import json, os, time
|
||||
from . import store
|
||||
|
||||
USAGE = 'usage: sisi fault "<what is wrong, 4+ words>" | --started N "<fix>" | --blocked N "<blocker and unblock>" | --fixed N "<evidence>"'
|
||||
HEAD = 'sisi: open faults. Fix each now or say what blocks it (`sisi fault --started N "<fix>"` | `--blocked N "<blocker>"` | `--fixed N "<evidence>"`):'
|
||||
BLOCK = 'sisi: open fault(s) with no fix started and no blocker named: %s. Start a fix (`sisi fault --started N "<fix>"`), name what blocks it (`sisi fault --blocked N "<blocker and unblock>"`), or close it (`sisi fault --fixed N "<evidence>"`).'
|
||||
_path = lambda cwd=None: os.path.join(store.STATE, "core-faults", store.project_key(cwd) + ".json")
|
||||
|
||||
|
||||
def _load(p, strict=False):
|
||||
d = os.path.dirname(p)
|
||||
why, raw = (("a symlink", None) if os.path.islink(d) else ("not a directory", None) if os.path.lexists(d) and not os.path.isdir(d) else store.regular(p, True))
|
||||
if why and strict:
|
||||
raise OSError("%s is %s" % (p, why))
|
||||
try:
|
||||
doc = json.loads(raw.decode("utf-8")) if raw is not None and not why else {"next": 1, "items": []}
|
||||
if not (isinstance(doc, dict) and isinstance(doc.get("items"), list)):
|
||||
raise ValueError
|
||||
items = [x for x in doc["items"] if isinstance(x, dict) and type(x.get("id")) is int and x.get("state") in ("open", "started", "blocked") and isinstance(x.get("text"), str)]
|
||||
return {"next": doc["next"] if type(doc.get("next")) is int and doc["next"] > 0 else max((x["id"] for x in items), default=0) + 1, "items": items}
|
||||
except (ValueError, KeyError, TypeError, UnicodeDecodeError):
|
||||
if strict:
|
||||
raise OSError("%s is not valid JSON" % p)
|
||||
return {"next": 1, "items": []}
|
||||
|
||||
|
||||
def _fmt(f):
|
||||
s = max(0, int(time.time() - f["at"])) if type(f.get("at")) in (int, float) and f["at"] == f["at"] and abs(f["at"]) < 1e12 else 0
|
||||
age = "%ds" % s if s < 60 else "%dm" % (s // 60) if s < 3600 else "%dh" % (s // 3600) if s < 86400 else "%dd" % (s // 86400)
|
||||
return "#%d [%s, %s] %s%s" % (f["id"], f["state"], age, f["text"], " — " + str(f["note"]) if f.get("note") else "")
|
||||
|
||||
|
||||
def lines(cwd=None):
|
||||
return [_fmt(f) for f in _load(_path(cwd))["items"]]
|
||||
|
||||
def show(cwd=None, gate=False):
|
||||
opened = [f for f in _load(_path(cwd))["items"] if not gate or f["state"] == "open"]
|
||||
return (BLOCK % "; ".join("#%d %s" % (f["id"], f["text"]) for f in opened) if gate else "\n".join([HEAD] + [_fmt(f) for f in opened])) if opened else ""
|
||||
|
||||
|
||||
def run(args, cwd=None):
|
||||
if not args:
|
||||
return print("\n".join(lines(cwd)) or "no open faults.") or 0
|
||||
flag = args[0] if args[0] in ("--started", "--blocked", "--fixed") else ""
|
||||
body = " ".join(" ".join(args[2:] if flag else args).split())[:300]
|
||||
if (flag and (len(args) < 3 or not args[1].isdecimal() or len(body.split()) < 2)) or (not flag and (args[0].startswith("-") or len(body.split()) < 4)):
|
||||
return print(USAGE) or 1
|
||||
p = _path(cwd)
|
||||
try:
|
||||
with store.lock(p):
|
||||
doc = _load(p, strict=True)
|
||||
if os.path.exists(p) and not (os.stat(p).st_mode & 0o200):
|
||||
raise OSError("%s is read-only" % p)
|
||||
if flag:
|
||||
hit = next((f for f in doc["items"] if f["id"] == int(args[1])), None)
|
||||
if not hit:
|
||||
return print("sisi fault: #%s not found; %s" % (args[1], USAGE)) or 1
|
||||
doc["items"] = [f for f in doc["items"] if f["id"] != hit["id"]] if flag == "--fixed" else doc["items"]
|
||||
hit.update(state=flag[2:], note=body)
|
||||
else:
|
||||
hit = {"id": doc["next"], "state": "open", "at": time.time(), "text": body, "note": ""}
|
||||
doc["next"], doc["items"] = doc["next"] + 1, doc["items"] + [hit]
|
||||
out = json.dumps(doc)
|
||||
if len(out.encode("utf-8")) > 64000:
|
||||
raise OSError("%s would exceed 64,000 bytes" % p)
|
||||
store.write(p, out)
|
||||
except OSError as e:
|
||||
return print("sisi fault: %s" % e) or 1
|
||||
return print(_fmt(hit)) or 0
|
||||
+19
-7
@@ -99,18 +99,26 @@ def _begin(state, cwd):
|
||||
state.update(started=time.time(), project=store.project_key(cwd), snapshot=store.snapshot(cwd))
|
||||
|
||||
|
||||
def _faults(cwd, gate=False):
|
||||
if not os.path.lexists(os.path.join(store.STATE, "core-faults", store.project_key(cwd) + ".json")):
|
||||
return ""
|
||||
from . import faults
|
||||
return faults.show(cwd, gate)
|
||||
|
||||
|
||||
def tick(p):
|
||||
"""Session start: remember where the work began; print this project's learned rules and what is due."""
|
||||
sid, cwd = p["session_id"], p["cwd"] or os.getcwd()
|
||||
sid, cwd, worker = p["session_id"], p["cwd"] or os.getcwd(), p["subagent"]
|
||||
if sid:
|
||||
with store.session(sid) as s:
|
||||
_begin(s, cwd)
|
||||
worker = worker or bool(s.get("worker"))
|
||||
env = os.environ.get("CLAUDE_ENV_FILE")
|
||||
if env and re.fullmatch(r"[A-Za-z0-9_-]+", str(sid)):
|
||||
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")):
|
||||
rules.build_rulebook()
|
||||
text = "\n".join(x for x in (rules.project_text(cwd), rules.due(cwd), models.stale()) if x)
|
||||
text = "\n".join(x for x in (rules.project_text(cwd), rules.due(cwd), models.stale(), "" if worker else _faults(cwd)) if x)
|
||||
if text:
|
||||
print(text)
|
||||
return 0
|
||||
@@ -118,16 +126,18 @@ def tick(p):
|
||||
|
||||
def prompt(p):
|
||||
"""Each request: a new turn begins. The first request tells a delegated worker from a lead."""
|
||||
sid = p["session_id"]
|
||||
sid, cwd, worker = p["session_id"], p["cwd"] or os.getcwd(), p["subagent"]
|
||||
if sid:
|
||||
with store.session(sid) as s:
|
||||
_begin(s, p["cwd"] or os.getcwd())
|
||||
_begin(s, cwd)
|
||||
if not s.get("prompts") and WORKER.search(p["prompt"][:2000]):
|
||||
s["worker"] = True
|
||||
worker = worker or bool(s.get("worker"))
|
||||
s["prompts"] = s.get("prompts", 0) + 1
|
||||
s.get("prompts_since") or s.update(began=time.time()) # the first request of a piece of work
|
||||
s["prompts_since"] = s.get("prompts_since", 0) + 1
|
||||
s["direct"], s["extra"], s["blocks"] = 0, 0, 0
|
||||
not worker and (text := _faults(cwd)) and print(text)
|
||||
return 0
|
||||
|
||||
|
||||
@@ -199,6 +209,7 @@ def gate(p, for_subagent=False):
|
||||
_begin(s, cwd)
|
||||
if s.get("worker"):
|
||||
return 0
|
||||
open_f = _faults(cwd, True)
|
||||
reason, demand = " ".join("sisi: " + LATE % (w, time.strftime("%H:%M", time.localtime(t))) for w, t in models.overdue(s)), False
|
||||
if not reason and not s.get("paused"):
|
||||
checks = (s.get("done") or {}).get("checks") or []
|
||||
@@ -209,9 +220,10 @@ def gate(p, for_subagent=False):
|
||||
if checks and not unsaid and not failed and (not worked or quick):
|
||||
from . import retro
|
||||
retro.record(s, cwd, {}, sid) # every check passed and nothing changed, or it was quick and alone: the work closes itself
|
||||
return 0
|
||||
reason = "" if unsaid and models.waiting(s) else OPEN % "; ".join(unsaid) if unsaid else GATE if worked else FAILED % ", ".join(failed) if failed else ""
|
||||
demand = bool(reason and not unsaid)
|
||||
else:
|
||||
reason = "" if unsaid and models.waiting(s) else OPEN % "; ".join(unsaid) if unsaid else GATE if worked else FAILED % ", ".join(failed) if failed else ""
|
||||
demand = bool(reason and not unsaid)
|
||||
reason = " ".join(x for x in (open_f, reason) if x)
|
||||
if not reason or s.get("blocks", 0) >= 2:
|
||||
return 0
|
||||
s["blocks"] = s.get("blocks", 0) + 1
|
||||
|
||||
+3
-1
@@ -48,7 +48,9 @@ def facts(state, cwd):
|
||||
"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))]
|
||||
return out + d[:1] + d[1:][-1:] + ["learning (measured): last 10 pieces of work named: %s" % (named or "none yet")]
|
||||
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):
|
||||
|
||||
Reference in new issue
Block a user