Learning loop: replay validated only when it ran, mechanisms carry earned scope, owner approval shown and gated, ledger of rows sisi wrote, subagent quarantine from session state, migration merges and reports, dead paths rewritten
This commit is contained in:
1 parent
29d239ca3b
commit
4821a1b6a4
19 files changed
+3017
-1100
No files matched your search
+79
-47
@@ -8,7 +8,7 @@ existing learning is attached to it as a recurrence candidate, never refused in
|
||||
Gate compatibility (gate.py calls these; its control flow is unchanged): TEMPLATE,
|
||||
satisfied(), repo_of(), lessons_block().
|
||||
"""
|
||||
import json, os, subprocess, sys
|
||||
import hashlib, json, os, subprocess, sys
|
||||
|
||||
from . import learning as L, paths
|
||||
|
||||
@@ -21,7 +21,9 @@ TEMPLATE = (
|
||||
"infrastructure|verification|knowledge|benchmark --expect \"<observable effect>\" --validate \"<how it will be tested>\"\n"
|
||||
" record also needs: --limits \"<where it applies and where it does not>\"\n"
|
||||
" drop also needs: --why-not \"<why it is not worth promoting>\"\n"
|
||||
"Generic findings are refused. A recurrence of a known learning may add --same-as ID."
|
||||
"Generic findings are refused. A finding that resembles a learning in scope here is attached to it as\n"
|
||||
"a recurrence (--new records it as its own); --same-as ID names the learning it repeats.\n"
|
||||
"A parent adopts a subagent's candidate by id: sisi retro --adopt ID."
|
||||
)
|
||||
|
||||
|
||||
@@ -29,11 +31,6 @@ def repo_of(cwd):
|
||||
return L.project_key(cwd)
|
||||
|
||||
|
||||
def notes_for(entry_id):
|
||||
"""Field notes per library entry were part of the retired trace regime; none remain."""
|
||||
return None
|
||||
|
||||
|
||||
def _git_head(cwd):
|
||||
try:
|
||||
p = subprocess.run(["git", "rev-parse", "HEAD"], cwd=cwd, capture_output=True,
|
||||
@@ -111,7 +108,7 @@ def unit_summary(project):
|
||||
|
||||
|
||||
OPTS = ("finding", "cause", "impact", "disposition", "change", "mechanism", "scope",
|
||||
"expect", "validate", "limits", "why-not", "same-as", "candidate")
|
||||
"expect", "validate", "limits", "why-not", "same-as", "adopt", "target")
|
||||
|
||||
|
||||
def _parse(argv):
|
||||
@@ -131,70 +128,103 @@ def _parse(argv):
|
||||
return out
|
||||
|
||||
|
||||
def _refuse(msg):
|
||||
print("sisi retro: %s" % msg, file=sys.stderr)
|
||||
return 2
|
||||
|
||||
|
||||
def _adopt(lid, project):
|
||||
"""A parent adopts a subagent's candidate by id. Never by resemblance, never from a subagent."""
|
||||
if L.is_subagent_session():
|
||||
return _refuse("a subagent cannot adopt a candidate; the parent session adopts it by id")
|
||||
try:
|
||||
rec = L.adopt(lid, L.unit_id(project), project)
|
||||
except L.LearningError as e:
|
||||
return _refuse(str(e))
|
||||
print("learning %s adopted: no longer a candidate, delivered like any learning in its project" % rec["id"])
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_retro(argv, stdin=None):
|
||||
project = os.getcwd()
|
||||
if not argv:
|
||||
sys.stdout.write(unit_summary(project))
|
||||
return 0
|
||||
o = _parse(argv)
|
||||
if o.get("adopt"):
|
||||
return _adopt(o["adopt"], project)
|
||||
finding = (o.get("finding") or "").strip()
|
||||
cause = (o.get("cause") or "").strip()
|
||||
impact = (o.get("impact") or "").strip()
|
||||
disp = (o.get("disposition") or "").strip()
|
||||
def refuse(msg):
|
||||
print("sisi retro: %s" % msg, file=sys.stderr)
|
||||
return 2
|
||||
change = (o.get("change") or "").strip()
|
||||
if not finding or not cause:
|
||||
return refuse("--finding and --cause are required (run `sisi retro` alone for the unit record)")
|
||||
if L.GENERIC.match(finding) or len(finding) < 20 or len(cause) < 12:
|
||||
return refuse("generic findings are refused: say what happened, with evidence, and the root cause")
|
||||
return _refuse("--finding and --cause are required (run `sisi retro` alone for the unit record)")
|
||||
if len(finding) < 12 or len(cause) < 8 or L.is_generic(finding) or L.is_generic(cause) \
|
||||
or (change and L.is_generic(change)):
|
||||
return _refuse("generic findings are refused: say what happened, with evidence, and the root cause")
|
||||
if impact not in L.IMPACTS:
|
||||
return refuse("--impact must be one of %s" % "|".join(L.IMPACTS))
|
||||
return _refuse("--impact must be one of %s" % "|".join(L.IMPACTS))
|
||||
if disp not in ("promote", "record", "drop"):
|
||||
return refuse("--disposition must be promote, record or drop")
|
||||
return _refuse("--disposition must be promote, record or drop")
|
||||
if disp == "promote":
|
||||
if not (o.get("change") or "").strip():
|
||||
return refuse("promote needs --change \"<the exact improvement>\"")
|
||||
if not change:
|
||||
return _refuse("promote needs --change \"<the exact improvement>\"")
|
||||
if o.get("mechanism") not in L.MECHANISMS:
|
||||
return refuse("--mechanism must be one of %s" % "|".join(L.MECHANISMS))
|
||||
return _refuse("--mechanism must be one of %s" % "|".join(L.MECHANISMS))
|
||||
if o.get("scope") and o["scope"] not in L.SCOPES:
|
||||
return refuse("--scope must be one of %s" % "|".join(L.SCOPES))
|
||||
return _refuse("--scope must be one of %s" % "|".join(L.SCOPES))
|
||||
if not (o.get("expect") or "").strip() or not (o.get("validate") or "").strip():
|
||||
return refuse("promote needs --expect \"<observable effect>\" and --validate \"<how it will be tested>\"")
|
||||
return _refuse("promote needs --expect \"<observable effect>\" and --validate \"<how it will be tested>\"")
|
||||
if disp == "record" and not (o.get("limits") or "").strip():
|
||||
return refuse("record needs --limits \"<where it applies and where it does not>\"")
|
||||
return _refuse("record needs --limits \"<where it applies and where it does not>\"")
|
||||
if disp == "drop" and not (o.get("why-not") or "").strip():
|
||||
return refuse("drop needs --why-not \"<why it is not worth promoting>\"")
|
||||
return _refuse("drop needs --why-not \"<why it is not worth promoting>\"")
|
||||
if o.get("same-as") and o.get("new"):
|
||||
return _refuse("--same-as and --new contradict each other")
|
||||
|
||||
uid = L.unit_id(project)
|
||||
pk = L.project_key(project)
|
||||
sub = L.is_subagent_session()
|
||||
rows = L.load_learnings()
|
||||
# recurrence candidate: --same-as, an adopted candidate id, or resemblance
|
||||
target = None
|
||||
for key in ("same-as", "candidate"):
|
||||
if o.get(key):
|
||||
target = L.get(rows, o[key])
|
||||
if target is None:
|
||||
return refuse("no learning %r" % o[key])
|
||||
if target is None:
|
||||
sim = L.similar_learnings(rows, finding + " " + (o.get("change") or ""), limit=1)
|
||||
target = sim[0] if sim else None
|
||||
visible = L.learnings_in_scope(project, rows) # only learnings in force here can be matched
|
||||
target, how = None, None
|
||||
if o.get("same-as"):
|
||||
if sub:
|
||||
return _refuse("a subagent's findings are held as candidates; it cannot attach to a learning")
|
||||
target = L.get(visible, o["same-as"])
|
||||
how = "named with --same-as"
|
||||
if target is None:
|
||||
# A learning from another project: this finding, a unit here, is its own evidence for
|
||||
# it, but only if it plainly is the same thing.
|
||||
other = L.get(rows, o["same-as"])
|
||||
if other is None or not L.is_registered(other) or other.get("candidate") or other.get("status") in L.ENDED:
|
||||
return _refuse("no learning %r" % o["same-as"])
|
||||
if len(L.stems(finding + " " + cause + " " + change) & set(other.get("terms") or [])) < 2:
|
||||
return _refuse("this finding does not share enough with %s to be a recurrence of it; "
|
||||
"record it as its own (--new)" % other["id"])
|
||||
target, how = other, "named with --same-as; this project's own finding is the evidence"
|
||||
elif not sub and not o.get("new"):
|
||||
sim = L.similar_learnings(visible, finding + " " + change, limit=1)
|
||||
if sim:
|
||||
target, how = sim[0], "resembles it (--new records this finding as its own learning instead)"
|
||||
|
||||
replay = None
|
||||
head = _git_head(project)
|
||||
if head:
|
||||
replay = {"repo": pk, "after": head, "before": None}
|
||||
f = {"unit": uid, "project": pk, "ts": L.now(), "finding": finding, "cause": cause,
|
||||
"impact": impact, "disposition": disp,
|
||||
"learning": target["id"] if target else None, "recurrence_of": target["id"] if target else None}
|
||||
ts = L.now()
|
||||
fid = "F-" + hashlib.sha1(("%s|%s|%s|%s" % (uid, pk, ts, finding)).encode()).hexdigest()[:8]
|
||||
f = {"id": fid, "unit": uid, "project": pk, "ts": ts, "finding": L.scrub_secrets(finding),
|
||||
"cause": L.scrub_secrets(cause), "impact": impact, "disposition": disp,
|
||||
"learning": target["id"] if target else None, "recurrence_of": target["id"] if target else None,
|
||||
"by_subagent": sub}
|
||||
L.append_finding(f)
|
||||
|
||||
if target is not None:
|
||||
if target.get("candidate") and not L.is_subagent_session():
|
||||
target["candidate"] = False # a parent retro adopting a subagent candidate
|
||||
L.add_recurrence(target, uid, project, rows)
|
||||
print("finding recorded for unit %s; attached to learning %s as a recurrence candidate%s"
|
||||
% (uid, target["id"],
|
||||
added = L.add_recurrence(target, uid, project, rows, finding=fid)
|
||||
print("finding recorded for unit %s; attached to learning %s (%s) as a recurrence%s%s"
|
||||
% (uid, target["id"], how, "" if added else " (this unit was already counted for it)",
|
||||
" (flagged not working: the next cycle must revise or revert it)"
|
||||
if target.get("flagged_not_working") else ""))
|
||||
return 0
|
||||
@@ -202,16 +232,16 @@ def cmd_retro(argv, stdin=None):
|
||||
print("finding recorded for unit %s; dropped: %s" % (uid, o.get("why-not", "")))
|
||||
return 0
|
||||
rec = L.new_record(finding, cause, impact, disp, project, uid,
|
||||
change=(o.get("change") or "").strip() or None,
|
||||
mechanism=o.get("mechanism"), scope=o.get("scope"),
|
||||
change=change or None, mechanism=o.get("mechanism"), scope=o.get("scope"),
|
||||
expect=(o.get("expect") or "").strip() or None,
|
||||
validate=(o.get("validate") or "").strip() or None,
|
||||
limits=(o.get("limits") or "").strip() or None,
|
||||
why_not=(o.get("why-not") or "").strip() or None,
|
||||
subagent=L.is_subagent_session(), replay=replay)
|
||||
subagent=sub, replay=replay, target=(o.get("target") or "").strip() or None)
|
||||
print("finding recorded for unit %s; learning %s (%s)%s"
|
||||
% (uid, rec["id"], rec["status"],
|
||||
", held as a candidate until a parent retro adopts it" if rec.get("candidate") else ""))
|
||||
", held as a candidate until the parent adopts it (`sisi retro --adopt %s`)" % rec["id"]
|
||||
if rec.get("candidate") else ""))
|
||||
return 0
|
||||
|
||||
|
||||
@@ -235,8 +265,10 @@ def cmd_lessons(argv):
|
||||
if not rows:
|
||||
print("no learnings recorded")
|
||||
return 0
|
||||
ledger = L.load_ledger()
|
||||
for r in rows:
|
||||
val = (r.get("validation") or {}).get("result") or "unvalidated"
|
||||
print("%s %-11s %-11s %s" % (r["id"], r.get("status"), val if val == "validated" else "unvalidated",
|
||||
L.derive_title(r.get("title", ""), 100)))
|
||||
print("%s %-11s %-11s %s%s" % (r["id"], r.get("status"), val if val == "validated" else "unvalidated",
|
||||
L.derive_title(r.get("title", ""), 100),
|
||||
"" if L.is_registered(r, ledger) else " [not delivered: not written by sisi's commands]"))
|
||||
return 0
|
||||
Reference in new issue
Block a user