sisi is two loops and nothing else: a forced retro whose improvement becomes a rule at once, with the rules organized by a worker; and delegation by a researched models table, with the lead stopped when it does the work itself. Everything else is removed (it is kept under the tag before-core-rewrite).
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b0baf2473
commit
084bfc1d6d
326 files changed
+1182
-69591
No files matched your search
+58
-336
@@ -1,347 +1,69 @@
|
||||
"""Command dispatch. Heavy modules are imported lazily so hooks (tick, gate) stay fast."""
|
||||
import json, os, sys
|
||||
"""sisi: hooks that force a retro and turn what it finds into rules, and a models table to delegate by."""
|
||||
import os, sys, time
|
||||
|
||||
HELP = """sisi: a self-improvement loop for AI coding agents.
|
||||
|
||||
def _pop_opt(args, name):
|
||||
"""Remove `name VALUE` from args, return value or None."""
|
||||
if name in args:
|
||||
i = args.index(name)
|
||||
v = args[i + 1] if i + 1 < len(args) else None
|
||||
del args[i:i + 2]
|
||||
return v
|
||||
return None
|
||||
The learning loop
|
||||
sisi retro what happened in this piece of work, to set against the objective
|
||||
sisi retro --gap G --cause C --improve I [--scope user]
|
||||
record the gap; the improvement becomes a rule at once
|
||||
sisi retro --no-gap TEXT | --later TEXT
|
||||
nothing fell short; or the work is not finished yet
|
||||
sisi learn [--project] TEXT the person's own words become a rule at once
|
||||
sisi rules the learned rules in force here
|
||||
sisi rules organize [--record FILE]
|
||||
the brief for a cheap worker to audit and organize them; record its answer
|
||||
sisi rules revert undo the last change to the learned rules
|
||||
|
||||
Delegation
|
||||
sisi models the workers you have: what each is good at and what it costs
|
||||
sisi models research [--record FILE]
|
||||
the brief for a research worker to rebuild that table; record its answer
|
||||
sisi judge WORKER good|bad WHY how a delegation went
|
||||
sisi direct REASON allow yourself more direct tool calls this turn, with the reason
|
||||
|
||||
def cmd_contract(args):
|
||||
from . import contract as C, gitutil, store, trust
|
||||
sub = args[0] if args else ""
|
||||
if sub == "init":
|
||||
goal = _pop_opt(args, "--goal")
|
||||
p = os.path.join(os.getcwd(), C.REL)
|
||||
project = os.getcwd()
|
||||
if os.path.exists(p):
|
||||
try:
|
||||
old = C.load(p)
|
||||
except (OSError, ValueError):
|
||||
old = None
|
||||
from . import unit as _U
|
||||
if not _U.is_closed(old):
|
||||
print("sisi: %s already exists and the unit %s (%s) is still open. Three ways on: "
|
||||
"`sisi unit continue` if it is this work; `sisi unit park \"<why>\"` to set it aside, "
|
||||
"kept, and start new work; or `sisi unit close --abandon \"<reason>\"` if it was dropped. "
|
||||
"Then `sisi contract init` again for new work."
|
||||
% (p, (old or {}).get("id", "?"), ((old or {}).get("goal") or "no goal")[:80]))
|
||||
return 1
|
||||
# A closed unit is replaced. Its archive should already exist; if a hand-edited
|
||||
# contract claims closed with none, archive it now so nothing is ever lost.
|
||||
if old is not None and old.get("id") and not os.path.exists(_U.archive_path(project, old["id"])):
|
||||
try:
|
||||
import time as _t
|
||||
rec = _U.build_record(old, project, old.get("delivery_status") or "abandoned", _t.time())
|
||||
_U.archive_record(rec, project)
|
||||
if not _U._summary_exists(project, rec["id"]):
|
||||
store.append("units", _U.summary_line(rec))
|
||||
except Exception as e:
|
||||
store.log_error("contract-init-archive", e)
|
||||
os.makedirs(os.path.dirname(p), exist_ok=True)
|
||||
skel = C.skeleton(goal, project)
|
||||
from . import unit as _U
|
||||
skel["id"] = _U.fresh_id(skel["id"], project)
|
||||
skel["base_head"] = gitutil.head(project)
|
||||
with open(p, "w", encoding="utf-8") as f:
|
||||
json.dump(skel, f, indent=2)
|
||||
f.write("\n")
|
||||
trust.add(os.getcwd())
|
||||
try:
|
||||
from . import unitguard as _G
|
||||
_sid = _G.cli_session()
|
||||
if _sid:
|
||||
_G.bind(_sid, project, skel, "init")
|
||||
except Exception as e:
|
||||
store.log_error("contract-init-bind", e)
|
||||
print("created %s as a SKELETON, not a contract yet: replace the placeholder check with a real command "
|
||||
"(rung 1-4) and add every check that shows the work is done. Until then `sisi verify` fails and "
|
||||
"the end-of-turn gate ignores it." % p)
|
||||
try:
|
||||
from . import learning as _L
|
||||
block = _L.delivery_text(os.getcwd(), goal or "")
|
||||
if block:
|
||||
print(block)
|
||||
except Exception:
|
||||
pass
|
||||
return 0
|
||||
if sub == "check":
|
||||
p = C.find(os.getcwd())
|
||||
if not p:
|
||||
print("sisi: no contract found")
|
||||
return 2
|
||||
try:
|
||||
c = C.load(p)
|
||||
except (OSError, ValueError) as e:
|
||||
print("sisi: cannot read %s: %s" % (p, e))
|
||||
return 1
|
||||
errs = C.validate(c)
|
||||
for e in errs:
|
||||
print(" - " + e)
|
||||
if not errs:
|
||||
for w in C.warnings(c):
|
||||
print("warning: " + w)
|
||||
print("contract invalid" if errs else "contract ok: %s" % p)
|
||||
return 1 if errs else 0
|
||||
if sub == "waive":
|
||||
rest = list(args[1:])
|
||||
waived_check = _pop_opt(rest, "--check")
|
||||
reason = " ".join(rest).strip()
|
||||
if not reason:
|
||||
print('usage: sisi contract waive [--check ID] "<reason>"')
|
||||
return 1
|
||||
import time
|
||||
project = gitutil.root(os.getcwd()) or os.getcwd()
|
||||
extra = {}
|
||||
if gitutil.root(os.getcwd()):
|
||||
m = gitutil.measure(os.getcwd())
|
||||
extra = {"head": gitutil.head(os.getcwd()), "lines": m["lines"], "files": m["files"]}
|
||||
store.append("waivers", store.stamp(dict({"project": project, "reason": reason, "check": waived_check,
|
||||
"ns": time.time_ns()}, **extra)))
|
||||
cp = C.find(os.getcwd())
|
||||
if cp:
|
||||
try:
|
||||
c = C.load(cp)
|
||||
if isinstance(c, dict):
|
||||
c.setdefault("deviations", []).append(
|
||||
{"reason": reason, "check": waived_check, "ts": store.now()})
|
||||
C.save(cp, c)
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
print("deviation recorded for %s%s: the unit does not pass; it closes as a deviation "
|
||||
"and the completion report must say so" % (project, (" (check %s)" % waived_check) if waived_check else ""))
|
||||
return 0
|
||||
if sub in ("trust", "untrust"):
|
||||
p = C.find(os.getcwd())
|
||||
if not p:
|
||||
print("sisi: no contract found here")
|
||||
return 2
|
||||
(trust.add if sub == "trust" else trust.remove)(C.project_dir(p))
|
||||
_commit_personal(trust._file(), "trust: %s %s" % (sub, C.project_dir(p)))
|
||||
print("%sed %s" % (sub, C.project_dir(p)))
|
||||
return 0
|
||||
print("usage: sisi contract init|check|waive|trust|untrust")
|
||||
return 1
|
||||
Setting up
|
||||
sisi install | doctor wire the hooks and the rulebook into the agent tool; check them
|
||||
sisi release | rollback make this copy the one the hooks run; go back to the one before
|
||||
|
||||
Hooks (the agent tool calls these): sisi tick | prompt | watch | gate
|
||||
|
||||
def _commit_personal(path, msg):
|
||||
"""Commit a personal file that just changed (no-op for files outside the personal folder)."""
|
||||
from . import paths, pdata
|
||||
if os.path.dirname(path) == paths.data():
|
||||
pdata.commit(msg, [os.path.basename(path)])
|
||||
|
||||
|
||||
def cmd_oracle():
|
||||
from . import principles
|
||||
sys.stdout.write(principles.oracle_text())
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_wiring(name, args=()):
|
||||
from . import wiring
|
||||
if name == "install" and "--dry-run" in args:
|
||||
rc, notes = wiring.install_dry_run()
|
||||
for n in notes:
|
||||
print(n)
|
||||
return rc
|
||||
if name == "doctor":
|
||||
rows = wiring.doctor()
|
||||
w = max(len(r["tool"]) for r in rows)
|
||||
for r in rows:
|
||||
line = "%-*s %-9s blocking hook: %-8s %s" % (w, r["tool"], r["status"], r["blocking_hook"][:60], r["detail"])
|
||||
if r.get("meter"):
|
||||
line += " | orchestration meter: " + r["meter"]
|
||||
if r.get("action"):
|
||||
line += " | you need to: " + r["action"]
|
||||
print(line)
|
||||
try:
|
||||
from . import overlay
|
||||
print("always loaded : " + overlay.cost_line())
|
||||
except Exception:
|
||||
pass
|
||||
return 0
|
||||
rc, notes = getattr(wiring, name)()
|
||||
for n in notes:
|
||||
print(n)
|
||||
return rc
|
||||
|
||||
|
||||
def cmd_personal(cmd, args):
|
||||
from . import lifecycle, overlay, pdata
|
||||
try:
|
||||
if cmd == "overlay":
|
||||
return overlay.main(args)
|
||||
if cmd == "update":
|
||||
return lifecycle.cmd_update(args)
|
||||
if cmd == "export":
|
||||
print("exported to %s" % lifecycle.export(args[0] if args else None))
|
||||
return 0
|
||||
if cmd == "import":
|
||||
if len(args) != 1:
|
||||
print("usage: sisi import PATH")
|
||||
return 1
|
||||
for n in lifecycle.import_archive(args[0]):
|
||||
print(n)
|
||||
print("imported")
|
||||
return 0
|
||||
except (pdata.DataError, overlay.OverlayError) as e:
|
||||
print("sisi %s: %s" % (cmd, e), file=sys.stderr)
|
||||
return 1
|
||||
return 64
|
||||
|
||||
|
||||
def cmd_help(args):
|
||||
"""`sisi help` (four groups, one line per command) or `sisi help COMMAND` (that command in full)."""
|
||||
from . import helptext
|
||||
if not args:
|
||||
print(helptext.short(), end="")
|
||||
return 0
|
||||
text = helptext.long(args[0])
|
||||
if text is None:
|
||||
print("sisi: no command %r (try `sisi help`)" % args[0], file=sys.stderr)
|
||||
return 64
|
||||
print(text, end="" if text.endswith("\n") else "\n")
|
||||
return 0
|
||||
Your rules, models table and retro log live in ~/.local/share/sisi, a git repository. Settings: "core" in
|
||||
its config.json (gate, meter, direct_calls, direct_extension, rules_lines, rules_chars, organize_after,
|
||||
research_days)."""
|
||||
|
||||
|
||||
def main(argv):
|
||||
args = list(argv)
|
||||
t = _pop_opt(args, "--tool")
|
||||
if t:
|
||||
os.environ["SISI_TOOL"] = t
|
||||
cmd = args.pop(0) if args else "help"
|
||||
if args[:1] in (["--help"], ["-h"]) and cmd not in ("help", "-h", "--help"):
|
||||
args = [cmd]
|
||||
cmd = "help"
|
||||
if cmd == "gate":
|
||||
if t and t != "claude-code":
|
||||
from . import hookio
|
||||
if hookio.known(t):
|
||||
return hookio.gate_main(t, args)
|
||||
from . import gate
|
||||
return gate.main(args)
|
||||
if cmd == "watch":
|
||||
if t and t != "claude-code":
|
||||
from . import hookio
|
||||
if hookio.known(t):
|
||||
return hookio.watch_main(t, args)
|
||||
from . import watch
|
||||
return watch.main(args)
|
||||
if cmd == "tick":
|
||||
if t and t != "claude-code":
|
||||
from . import hookio
|
||||
if hookio.known(t):
|
||||
return hookio.tick_main(t, args)
|
||||
from . import tick
|
||||
return tick.main(args)
|
||||
if cmd == "prompt":
|
||||
if t and t != "claude-code":
|
||||
from . import hookio
|
||||
if hookio.known(t):
|
||||
return hookio.prompt_main(t, args)
|
||||
from . import prompt
|
||||
return prompt.main(args)
|
||||
if cmd == "rule":
|
||||
from . import rulecmd
|
||||
return rulecmd.main(args)
|
||||
if cmd in ("version", "--version", "-V"):
|
||||
from . import paths
|
||||
from . import hooks, install, models, retro, rules, store
|
||||
cmd, args = (argv[0] if argv else "help"), argv[1:]
|
||||
if cmd in ("tick", "prompt", "watch", "gate"):
|
||||
try:
|
||||
with open(os.path.join(paths.REPO, "VERSION"), encoding="utf-8") as f:
|
||||
print(f.read().strip())
|
||||
except OSError:
|
||||
print("sisi: no VERSION file in %s" % paths.REPO, file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
if cmd in ("help", "-h", "--help"):
|
||||
return cmd_help(args)
|
||||
if cmd == "contract":
|
||||
return cmd_contract(args)
|
||||
if cmd == "verify":
|
||||
from . import verify
|
||||
return verify.cmd_verify(args)
|
||||
if cmd in ("install", "uninstall", "doctor"):
|
||||
return cmd_wiring(cmd, args)
|
||||
if cmd in ("update", "export", "import", "overlay"):
|
||||
return cmd_personal(cmd, args)
|
||||
if cmd in ("release", "rollback", "promote"):
|
||||
from . import release
|
||||
try:
|
||||
return {"release": release.cmd_release, "rollback": release.cmd_rollback,
|
||||
"promote": release.cmd_promote}[cmd](args)
|
||||
except release.ReleaseError as e:
|
||||
print("sisi %s: %s" % (cmd, e), file=sys.stderr)
|
||||
return 1
|
||||
if cmd == "learn":
|
||||
from . import scope
|
||||
return scope.cmd_learn(args)
|
||||
if cmd == "handover":
|
||||
from . import handover
|
||||
return handover.main(args)
|
||||
p = hooks.payload()
|
||||
if cmd == "gate":
|
||||
return hooks.gate(p, "--subagent" in args)
|
||||
return {"tick": hooks.tick, "prompt": hooks.prompt, "watch": hooks.watch}[cmd](p)
|
||||
except Exception as e: # a hook must never break the agent's session
|
||||
store.append(os.path.join(store.STATE, "core-errors.log"), "%s %s: %r" % (time.strftime("%Y-%m-%dT%H:%M:%S"), cmd, e))
|
||||
return 0
|
||||
if cmd == "retro":
|
||||
from . import retro
|
||||
return retro.cmd_retro(args)
|
||||
if cmd == "lessons":
|
||||
from . import retro
|
||||
return retro.cmd_lessons(args)
|
||||
if cmd == "cycle":
|
||||
from . import cycle
|
||||
return cycle.cmd_cycle(args)
|
||||
if cmd == "approve":
|
||||
from . import cycle
|
||||
return cycle.cmd_approve(args) # learning changes (P-N), learned drivers (drv-NAME), allowance probes (probe-NAME)
|
||||
if cmd == "oracle":
|
||||
return cmd_oracle()
|
||||
if cmd == "risk":
|
||||
from . import risk
|
||||
return risk.main(args)
|
||||
if cmd == "status":
|
||||
from . import scorecard
|
||||
return scorecard.cmd_status(args)
|
||||
if cmd == "unit":
|
||||
from . import unit
|
||||
return unit.cmd_unit(args)
|
||||
if cmd == "verifiers":
|
||||
from . import verifiers
|
||||
return verifiers.main(args)
|
||||
if cmd == "done":
|
||||
from . import verifiers
|
||||
return verifiers.done_main(args)
|
||||
if cmd == "workers":
|
||||
from . import workers
|
||||
return workers.with_commit(workers.cmd_workers, args, "workers")
|
||||
if cmd == "route":
|
||||
from . import workers
|
||||
return workers.cmd_route(args)
|
||||
if cmd == "delegate":
|
||||
from . import workers
|
||||
return workers.with_commit(workers.cmd_delegate, args, "delegate")
|
||||
if cmd == "eval":
|
||||
from . import evaluate
|
||||
return evaluate.cmd_eval(args)
|
||||
if cmd in ("tasks", "checks"):
|
||||
from . import evaluate
|
||||
return evaluate.cmd_folded(cmd, args)
|
||||
rc = _learn_cli(cmd, args)
|
||||
if rc is not None:
|
||||
return rc
|
||||
print("sisi: unknown command %r (try `sisi help`)" % cmd, file=sys.stderr)
|
||||
return 64
|
||||
|
||||
|
||||
def _learn_cli(cmd, args):
|
||||
"""Data-layer commands (harvest, tasks, ...) live in learn_cli; None when `cmd` is not one of them."""
|
||||
import argparse
|
||||
from . import learn_cli
|
||||
ap = argparse.ArgumentParser(prog="sisi")
|
||||
sub = ap.add_subparsers(dest="cmd")
|
||||
learn_cli.register(sub)
|
||||
if cmd not in sub.choices:
|
||||
return None
|
||||
ns = ap.parse_args([cmd] + args)
|
||||
return ns.func(ns)
|
||||
return retro.run(args)
|
||||
if cmd == "rules":
|
||||
return rules.run(args)
|
||||
if cmd == "learn":
|
||||
return rules.learn(args)
|
||||
if cmd == "models":
|
||||
return models.run(args)
|
||||
if cmd == "judge":
|
||||
return models.judge(args)
|
||||
if cmd == "direct":
|
||||
return hooks.direct(args)
|
||||
if cmd in ("install", "release", "rollback", "doctor"):
|
||||
return getattr(install, cmd)()
|
||||
if cmd == "rulebook":
|
||||
print(rules.build_rulebook())
|
||||
return 0
|
||||
if cmd == "version":
|
||||
print(store.read(os.path.join(store.ROOT, "VERSION")).strip())
|
||||
return 0
|
||||
print(HELP)
|
||||
return 0 if cmd in ("help", "--help", "-h") else 1
|
||||
Reference in new issue
Block a user