Items 4 and 5: pause is refused without a valid session-bound unit and never silences another session's gate; the gate judges only the unit the session holds (send-back is continue/park/init). Existing gate tests bind their session. done / next: item 6 (independent evidence, rules, refusal counts)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed55af910a
commit
cf2587df21
8 files changed
+285
-28
No files matched your search
+26
-4
@@ -20,7 +20,7 @@ for another session's work in the same tree.
|
|||||||
"""
|
"""
|
||||||
import hashlib, json, os, sys, time
|
import hashlib, json, os, sys, time
|
||||||
|
|
||||||
from . import assure, attribution, contract as C, gitutil, risk, store, trust, unit as U, verify as V
|
from . import assure, attribution, contract as C, gitutil, risk, store, trust, unit as U, unitguard, verify as V
|
||||||
from .questions import QUESTION_DIALOG_TOOLS, ends_with_question # noqa: F401 (rulecmd and the tests name them here)
|
from .questions import QUESTION_DIALOG_TOOLS, ends_with_question # noqa: F401 (rulecmd and the tests name them here)
|
||||||
|
|
||||||
# watch.py asks the gate's shell classifier whether a command may write
|
# watch.py asks the gate's shell classifier whether a command may write
|
||||||
@@ -316,10 +316,13 @@ def _decide(payload):
|
|||||||
c, path = None, None # the unit is archived; new work stands or falls on its own
|
c, path = None, None # the unit is archived; new work stands or falls on its own
|
||||||
sess["req_contract"] = False
|
sess["req_contract"] = False
|
||||||
elif isinstance(c, dict) and c.get("status") == "paused":
|
elif isinstance(c, dict) and c.get("status") == "paused":
|
||||||
if not sess.get("dirty") or top is None or c.get("pause_fp") == gitutil.fingerprint(cwd):
|
# a pause silences the gate only for the session that holds the unit, and only while it is valid
|
||||||
|
holds = unitguard.holds_valid(sess, project, c)
|
||||||
|
if holds and (not sess.get("dirty") or top is None or c.get("pause_fp") == gitutil.fingerprint(cwd)):
|
||||||
return stop.allow(cwd, settle=bool(sess.get("dirty"))) # a recorded wait, no new change
|
return stop.allow(cwd, settle=bool(sess.get("dirty"))) # a recorded wait, no new change
|
||||||
U.resume(c, path) # the tree changed since the pause: the wait is over
|
if holds:
|
||||||
stop.refresh(cwd, path)
|
U.resume(c, path) # the tree changed since the pause: the wait is over
|
||||||
|
stop.refresh(cwd, path)
|
||||||
if not sess.get("dirty"):
|
if not sess.get("dirty"):
|
||||||
return _late_close(stop, cwd, c, path, project)
|
return _late_close(stop, cwd, c, path, project)
|
||||||
|
|
||||||
@@ -332,6 +335,12 @@ def _decide(payload):
|
|||||||
own = _own_changed(sess, sid, cwd, top, attr)
|
own = _own_changed(sess, sid, cwd, top, attr)
|
||||||
own_floor, own_reasons = risk.assess(own, diff_text=_diff_text(project, sess.get("head")),
|
own_floor, own_reasons = risk.assess(own, diff_text=_diff_text(project, sess.get("head")),
|
||||||
patterns=risk.load_patterns(project))
|
patterns=risk.load_patterns(project))
|
||||||
|
if c: # (an unreadable contract is reported below, not judged here)
|
||||||
|
held, last = unitguard.bound_fresh(sess, project, c)
|
||||||
|
if held != "ok": # a unit this session did not open or continue, or one gone stale: never judged against
|
||||||
|
if own_floor == "trivial":
|
||||||
|
return stop.allow(cwd)
|
||||||
|
return _unit_not_held(stop, cwd, c, project, held, last, own_floor, own_reasons, own)
|
||||||
claimed = c is not None and (attr is None or _claims_unit(sess, path)) # no transcript: every change is ours
|
claimed = c is not None and (attr is None or _claims_unit(sess, path)) # no transcript: every change is ours
|
||||||
if own_floor == "trivial" and not claimed and not (c is not None and sess.get("last_unit") == c.get("id")):
|
if own_floor == "trivial" and not claimed and not (c is not None and sess.get("last_unit") == c.get("id")):
|
||||||
return stop.allow(cwd) # documentation and the like, or nothing of this session's: nothing is asked
|
return stop.allow(cwd) # documentation and the like, or nothing of this session's: nothing is asked
|
||||||
@@ -406,6 +415,19 @@ def _decide(payload):
|
|||||||
return stop.block(text + ("\n" + note if note else ""), c["id"], project, counts=False)
|
return stop.block(text + ("\n" + note if note else ""), c["id"], project, counts=False)
|
||||||
|
|
||||||
|
|
||||||
|
def _unit_not_held(stop, cwd, c, project, state, last, floor, reasons, own):
|
||||||
|
"""This session changed non-trivial work and the open unit is not its own (or is stale): the send-back is
|
||||||
|
how to get a unit (continue, park or init), never the other unit's failing checks. When the send-backs run
|
||||||
|
out the turn ends as unverified, and the other unit is left exactly as it was."""
|
||||||
|
focus = next((f for f in own if risk.assess([f])[0] != "trivial"), own[0] if own else "the project")
|
||||||
|
if stop.exhausted():
|
||||||
|
stop.allow(cwd)
|
||||||
|
return "allow", ("sisi: result is UNVERIFIED. The gate has sent this work back %d times: no unit of this "
|
||||||
|
"session stands behind `%s`. Report the work as unverified and say a unit is missing."
|
||||||
|
% (stop.maxb, focus))
|
||||||
|
return stop.block(assure.fit(unitguard.gate_sendback_lines(c, state, last, focus, floor)), c.get("id"), project)
|
||||||
|
|
||||||
|
|
||||||
def _trivial(stop, cwd, c, path, project, profile):
|
def _trivial(stop, cwd, c, path, project, profile):
|
||||||
"""A trivial change asks nothing, unless the unit shows an anomaly: then the retro is asked once."""
|
"""A trivial change asks nothing, unless the unit shows an anomaly: then the retro is asked once."""
|
||||||
sess = stop.sess
|
sess = stop.sess
|
||||||
|
|||||||
+8
-3
@@ -753,10 +753,10 @@ def summary_for_retro(project):
|
|||||||
|
|
||||||
# ---------------------------------------------------------------- command
|
# ---------------------------------------------------------------- command
|
||||||
|
|
||||||
def _open_contract(cwd, out):
|
def _open_contract(cwd, out, missing=None):
|
||||||
p = C.find(cwd)
|
p = C.find(cwd)
|
||||||
if not p:
|
if not p:
|
||||||
out("sisi: no contract found (run `sisi contract init`)")
|
out(missing() if missing else "sisi: no contract found (run `sisi contract init`)")
|
||||||
return None, None, None
|
return None, None, None
|
||||||
try:
|
try:
|
||||||
c = C.load(p)
|
c = C.load(p)
|
||||||
@@ -892,12 +892,17 @@ def cmd_unit(args, cwd=None, out=print):
|
|||||||
if not what:
|
if not what:
|
||||||
out('usage: sisi unit pause "<what it waits for>"')
|
out('usage: sisi unit pause "<what it waits for>"')
|
||||||
return 1
|
return 1
|
||||||
c, p, project = _open_contract(cwd, out)
|
from . import unitguard as G
|
||||||
|
c, p, project = _open_contract(cwd, out, missing=lambda: G.pause_without_unit(cwd))
|
||||||
if c is None:
|
if c is None:
|
||||||
return 2
|
return 2
|
||||||
if is_closed(c):
|
if is_closed(c):
|
||||||
out("sisi: unit %s is closed; start a new one with `sisi contract init`" % c.get("id"))
|
out("sisi: unit %s is closed; start a new one with `sisi contract init`" % c.get("id"))
|
||||||
return 1
|
return 1
|
||||||
|
refusal = G.pause_refusal(c, p, project)
|
||||||
|
if refusal:
|
||||||
|
out(refusal)
|
||||||
|
return 1
|
||||||
pauses = c.setdefault("pauses", [])
|
pauses = c.setdefault("pauses", [])
|
||||||
open_pause = next((x for x in pauses if isinstance(x, dict) and not x.get("until")), None)
|
open_pause = next((x for x in pauses if isinstance(x, dict) and not x.get("until")), None)
|
||||||
if open_pause is not None:
|
if open_pause is not None:
|
||||||
|
|||||||
+77
-10
@@ -84,8 +84,14 @@ def cli_session():
|
|||||||
|
|
||||||
# ---------------------------------------------------------------- unit identity, binding, activity
|
# ---------------------------------------------------------------- unit identity, binding, activity
|
||||||
|
|
||||||
|
def ident(c):
|
||||||
|
"""What makes a unit this unit: its start time (set once, by `contract init`), else its id. The id is not
|
||||||
|
used when a start exists, because an agent may rename it while filling the contract in."""
|
||||||
|
return c.get("started") or "id:%s" % c.get("id")
|
||||||
|
|
||||||
|
|
||||||
def unit_key(project, c):
|
def unit_key(project, c):
|
||||||
return "%s|%s|%s" % (os.path.realpath(project), c.get("id"), c.get("started") or "")
|
return "%s|%s" % (os.path.realpath(project), ident(c))
|
||||||
|
|
||||||
|
|
||||||
def _act_path(key):
|
def _act_path(key):
|
||||||
@@ -137,14 +143,14 @@ def bind(sid, project, c, how):
|
|||||||
if not (sess.get("started") or sess.get("first_seen")):
|
if not (sess.get("started") or sess.get("first_seen")):
|
||||||
sess["first_seen"] = store.now()
|
sess["first_seen"] = store.now()
|
||||||
sess.setdefault("unit_bound", {})[os.path.realpath(project)] = {
|
sess.setdefault("unit_bound", {})[os.path.realpath(project)] = {
|
||||||
"id": c.get("id"), "started": c.get("started"), "how": how, "ts": store.now()}
|
"id": c.get("id"), "ident": ident(c), "how": how, "ts": store.now()}
|
||||||
store.save_session(sid, sess)
|
store.save_session(sid, sess)
|
||||||
touch(project, c, model=sess.get("model") if isinstance(sess.get("model"), str) else None)
|
touch(project, c, model=sess.get("model") if isinstance(sess.get("model"), str) else None)
|
||||||
|
|
||||||
|
|
||||||
def is_bound(sess, project, c):
|
def is_bound(sess, project, c):
|
||||||
b = (sess.get("unit_bound") or {}).get(os.path.realpath(project))
|
b = (sess.get("unit_bound") or {}).get(os.path.realpath(project))
|
||||||
return isinstance(b, dict) and b.get("id") == c.get("id") and (b.get("started") or None) == (c.get("started") or None)
|
return isinstance(b, dict) and b.get("ident") == ident(c)
|
||||||
|
|
||||||
|
|
||||||
def stale_seconds():
|
def stale_seconds():
|
||||||
@@ -368,16 +374,20 @@ def _problems(profile, c, project, info):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def verdict(sid, sess, cwd, floor, c=None, path=None, project=None):
|
def verdict(sid, sess, cwd, floor, c=None, path=None, project=None, literal=False):
|
||||||
"""What stands between this session and building a change of this floor.
|
"""What stands between this session and building a change of this floor (`literal`: a trivial floor asks
|
||||||
|
for nothing beyond a bound, fresh, well-formed unit; otherwise it is read as `isolated`).
|
||||||
{"state": none|foreign|stale|invalid|ok, "problems": [(what, how)], "unit": contract|None, "profile": ...}"""
|
{"state": none|foreign|stale|invalid|ok, "problems": [(what, how)], "unit": contract|None, "profile": ...}"""
|
||||||
if c is None:
|
if c is None:
|
||||||
c, path, project = open_unit(cwd)
|
c, path, project = open_unit(cwd)
|
||||||
|
if literal and floor == "trivial":
|
||||||
|
floor = "trivial-literal"
|
||||||
if c is None:
|
if c is None:
|
||||||
prof = floor if floor != "trivial" else "isolated"
|
prof = floor if floor not in ("trivial", "trivial-literal") else "isolated"
|
||||||
return {"state": "none", "problems": [], "unit": None, "profile": prof, "last": None, "project": project}
|
return {"state": "none", "problems": [], "unit": None, "profile": prof, "last": None, "project": project}
|
||||||
state, last = bound_fresh(sess, project, c)
|
state, last = bound_fresh(sess, project, c)
|
||||||
profile, info = assure.effective_profile(floor if floor != "trivial" else "isolated", c)
|
profile, info = assure.effective_profile("trivial" if floor == "trivial-literal" else
|
||||||
|
(floor if floor != "trivial" else "isolated"), c)
|
||||||
v = {"state": state, "problems": [], "unit": c, "profile": profile, "last": last, "project": project, "info": info}
|
v = {"state": state, "problems": [], "unit": c, "profile": profile, "last": last, "project": project, "info": info}
|
||||||
if state != "ok":
|
if state != "ok":
|
||||||
return v
|
return v
|
||||||
@@ -389,7 +399,7 @@ def verdict(sid, sess, cwd, floor, c=None, path=None, project=None):
|
|||||||
|
|
||||||
# ---------------------------------------------------------------- the message
|
# ---------------------------------------------------------------- the message
|
||||||
|
|
||||||
def _how_on(c, state, last):
|
def how_on(c, state, last):
|
||||||
uid = c.get("id") or "?"
|
uid = c.get("id") or "?"
|
||||||
goal = " ".join(str(c.get("goal") or "").split())[:90]
|
goal = " ".join(str(c.get("goal") or "").split())[:90]
|
||||||
when = age_text(last)
|
when = age_text(last)
|
||||||
@@ -423,7 +433,7 @@ def refusal_text(v, what, floor_reasons=None):
|
|||||||
if need:
|
if need:
|
||||||
lines.append("At %s the contract also needs: %s (`sisi contract check` lists what is missing)." % (profile, need))
|
lines.append("At %s the contract also needs: %s (`sisi contract check` lists what is missing)." % (profile, need))
|
||||||
elif v["state"] in ("foreign", "stale"):
|
elif v["state"] in ("foreign", "stale"):
|
||||||
lines += _how_on(c, v["state"], v.get("last"))
|
lines += how_on(c, v["state"], v.get("last"))
|
||||||
else:
|
else:
|
||||||
lines.append("Unit %s is yours but not ready to build on. Missing:" % c.get("id"))
|
lines.append("Unit %s is yours but not ready to build on. Missing:" % c.get("id"))
|
||||||
lines += ["- %s -> %s" % (a, b) for a, b in v["problems"]]
|
lines += ["- %s -> %s" % (a, b) for a, b in v["problems"]]
|
||||||
@@ -603,6 +613,63 @@ def is_building_delegation(sess, payload):
|
|||||||
for w in _list("build_words"))
|
for w in _list("build_words"))
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- pause, and the gate
|
||||||
|
|
||||||
|
def pause_refusal(c, path, project):
|
||||||
|
"""The refusal for `sisi unit pause`, or None when the pause is allowed. A pause defers checks that wait
|
||||||
|
on something and the retro; it never defers the contract, so it needs a unit this session holds that is
|
||||||
|
valid for what has changed so far."""
|
||||||
|
from . import unit as U
|
||||||
|
sid = cli_session()
|
||||||
|
sess = store.load_session(sid) if sid else {}
|
||||||
|
fl, _ = U.risk_floor(c, project)
|
||||||
|
floor = fl.get("value") if isinstance(fl, dict) else fl
|
||||||
|
if floor not in risk.PROFILES:
|
||||||
|
floor = "isolated"
|
||||||
|
mine = session_paths(sess)
|
||||||
|
if mine:
|
||||||
|
own = floor_of(mine, project)[0]
|
||||||
|
floor = max(floor, own, key=lambda x: risk.PROFILES.index(x))
|
||||||
|
v = verdict(sid, sess, project, floor, c, path, project, literal=True)
|
||||||
|
if v["state"] == "ok":
|
||||||
|
return None
|
||||||
|
lines = ["sisi refused the pause: a pause defers checks that are waiting on something (a review, a decision, a deploy)",
|
||||||
|
"and the retro. It never defers the contract: the unit must exist, be this session's, and be valid first."]
|
||||||
|
if v["state"] in ("foreign", "stale"):
|
||||||
|
lines += how_on(c, v["state"], v.get("last"))
|
||||||
|
else:
|
||||||
|
lines.append("Unit %s is not ready. Missing:" % c.get("id"))
|
||||||
|
lines += ["- %s -> %s" % (a, b) for a, b in v["problems"]]
|
||||||
|
record_refusal(sid or "?", project, "a pause of unit %s" % c.get("id"), v, sess, "pause")
|
||||||
|
return assure.fit(lines, MAX_LINES)
|
||||||
|
|
||||||
|
|
||||||
|
def pause_without_unit(cwd):
|
||||||
|
"""The refusal for `sisi unit pause` when no unit is open at all (recorded like any other)."""
|
||||||
|
sid = cli_session()
|
||||||
|
try:
|
||||||
|
from . import gitutil
|
||||||
|
record_refusal(sid or "?", gitutil.root(cwd) or os.path.abspath(cwd), "a pause with no unit open",
|
||||||
|
{"state": "none", "problems": [], "unit": None}, store.load_session(sid) if sid else {}, "pause")
|
||||||
|
except Exception as e:
|
||||||
|
store.log_error("unitguard-pause", e)
|
||||||
|
return ("sisi refused the pause: a pause defers checks that are waiting on something and the retro, never the "
|
||||||
|
"contract, and there is no open unit to pause. Start one with `sisi contract init`.")
|
||||||
|
|
||||||
|
|
||||||
|
def holds_valid(sess, project, c, floor="trivial"):
|
||||||
|
"""True when this session holds the unit (bound, not stale) and nothing is missing before building at this
|
||||||
|
floor: what lets a paused unit stay quiet at the end of a turn."""
|
||||||
|
return verdict(None, sess, project, floor, c, None, project, literal=True)["state"] == "ok"
|
||||||
|
|
||||||
|
|
||||||
|
def gate_sendback_lines(c, state, last, own_path, profile):
|
||||||
|
"""The end-of-turn message when this session changed work the open unit is not its own."""
|
||||||
|
head = "sisi: this %s change is not ready to hand over (send-back {n}): this session changed `%s` and does not hold a unit for it." \
|
||||||
|
% (profile, own_path)
|
||||||
|
return [head] + how_on(c, state, last) + ["The unit's checks are not run against this work. Do not report it as done."]
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- the hooks
|
# ---------------------------------------------------------------- the hooks
|
||||||
|
|
||||||
def _deny(msg):
|
def _deny(msg):
|
||||||
@@ -717,7 +784,7 @@ def _guard_contract_write(sid, sess, c, project, cwd, kind):
|
|||||||
return False
|
return False
|
||||||
v = {"state": state, "problems": [], "unit": c, "profile": "isolated", "last": last, "project": project}
|
v = {"state": state, "problems": [], "unit": c, "profile": "isolated", "last": last, "project": project}
|
||||||
what = "this call would rewrite the contract of unit %s" % c.get("id")
|
what = "this call would rewrite the contract of unit %s" % c.get("id")
|
||||||
msg = assure.fit(["sisi refused: %s, which this session has not opened or continued." % what] + _how_on(c, state, last)
|
msg = assure.fit(["sisi refused: %s, which this session has not opened or continued." % what] + how_on(c, state, last)
|
||||||
+ ["Then retry the call."], MAX_LINES)
|
+ ["Then retry the call."], MAX_LINES)
|
||||||
record_refusal(sid, project, what, v, sess, "contract-write")
|
record_refusal(sid, project, what, v, sess, "contract-write")
|
||||||
_deny(msg)
|
_deny(msg)
|
||||||
|
|||||||
+1
-1
@@ -126,7 +126,7 @@ def human(id, rung=5, kind="general"):
|
|||||||
|
|
||||||
|
|
||||||
def contract(checks, invariants=()):
|
def contract(checks, invariants=()):
|
||||||
return {"id": "t", "goal": "g", "risk": {"profile": "isolated"}, "checks": list(checks),
|
return {"id": "t", "goal": "make the feature work", "risk": {"profile": "isolated"}, "checks": list(checks),
|
||||||
"invariants": list(invariants), "assumptions": [], "questions": []}
|
"invariants": list(invariants), "assumptions": [], "questions": []}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -45,7 +45,7 @@ class EndToEnd(SisiCase):
|
|||||||
def agent_runs(self, command, *argv):
|
def agent_runs(self, command, *argv):
|
||||||
with open(self.tp, "a") as f:
|
with open(self.tp, "a") as f:
|
||||||
f.write(json.dumps(tool_line("Bash", {"command": command})) + "\n")
|
f.write(json.dumps(tool_line("Bash", {"command": command})) + "\n")
|
||||||
return self.sisi(*argv)
|
return self.sisi(*argv, env={"SISI_SESSION_ID": "e2e"}) # what the session-start hook exports to the agent's shell
|
||||||
|
|
||||||
def finding(self, n):
|
def finding(self, n):
|
||||||
r = self.sisi("retro", "--finding", "`cargo test` run %d took 4 minutes with no fuzz verifier in the project" % n,
|
r = self.sisi("retro", "--finding", "`cargo test` run %d took 4 minutes with no fuzz verifier in the project" % n,
|
||||||
|
|||||||
+11
-4
@@ -47,6 +47,13 @@ class GateBase(SisiCase):
|
|||||||
self.tdir = os.path.join(self.tmp, "transcripts")
|
self.tdir = os.path.join(self.tmp, "transcripts")
|
||||||
os.makedirs(self.tdir)
|
os.makedirs(self.tdir)
|
||||||
|
|
||||||
|
def write_contract(self, c, d=None, trusted=True, bind=True):
|
||||||
|
"""The unit this test's session holds (a unit the session did not open or continue is never judged: item 5)."""
|
||||||
|
super().write_contract(c, d, trusted)
|
||||||
|
if bind:
|
||||||
|
from sisi import unitguard
|
||||||
|
unitguard.bind(self.sid, d or self.proj, c, "init")
|
||||||
|
|
||||||
def put(self, rel, text="x\n", mode="w"):
|
def put(self, rel, text="x\n", mode="w"):
|
||||||
p = os.path.join(self.proj, rel)
|
p = os.path.join(self.proj, rel)
|
||||||
os.makedirs(os.path.dirname(p), exist_ok=True)
|
os.makedirs(os.path.dirname(p), exist_ok=True)
|
||||||
@@ -363,7 +370,7 @@ class FlowTests(GateBase):
|
|||||||
s.write("app.py", "x = 1\n")
|
s.write("app.py", "x = 1\n")
|
||||||
s.contract(contract([check("feature", "test -f ok.txt")]))
|
s.contract(contract([check("feature", "test -f ok.txt")]))
|
||||||
self.assertEqual(s.stop()[0], "block") # the offer
|
self.assertEqual(s.stop()[0], "block") # the offer
|
||||||
self.assertEqual(self.sisi("unit", "pause", "the reviewer").returncode, 0)
|
self.assertEqual(self.sisi("unit", "pause", "the reviewer", env={"SISI_SESSION_ID": self.sid}).returncode, 0)
|
||||||
self.assertEqual(s.stop(), ("allow", None))
|
self.assertEqual(s.stop(), ("allow", None))
|
||||||
self.assertEqual(self.unit_state()["status"], "paused")
|
self.assertEqual(self.unit_state()["status"], "paused")
|
||||||
s.edit("app.py", "y = 2\n")
|
s.edit("app.py", "y = 2\n")
|
||||||
@@ -377,7 +384,7 @@ class FlowTests(GateBase):
|
|||||||
s = self.session()
|
s = self.session()
|
||||||
s.write("app.py", "x = 1\n")
|
s.write("app.py", "x = 1\n")
|
||||||
s.contract(contract([check("feature", "test -f ok.txt")]) | {"started": store.now()})
|
s.contract(contract([check("feature", "test -f ok.txt")]) | {"started": store.now()})
|
||||||
self.sisi("unit", "pause", "waiting for ever")
|
self.sisi("unit", "pause", "waiting for ever", env={"SISI_SESSION_ID": self.sid})
|
||||||
r = self.sisi("contract", "init")
|
r = self.sisi("contract", "init")
|
||||||
self.assertEqual(r.returncode, 1)
|
self.assertEqual(r.returncode, 1)
|
||||||
self.assertIn("still open", r.stdout)
|
self.assertIn("still open", r.stdout)
|
||||||
@@ -875,7 +882,7 @@ class GateFailOpenTests(GateBase):
|
|||||||
self.assertIn("kaboom", f.read())
|
self.assertIn("kaboom", f.read())
|
||||||
|
|
||||||
def test_unwritable_state_allows(self):
|
def test_unwritable_state_allows(self):
|
||||||
self.write_contract(contract([check("bad", "false")]))
|
self.write_contract(contract([check("bad", "false")]), bind=False)
|
||||||
os.makedirs(self.state, exist_ok=True)
|
os.makedirs(self.state, exist_ok=True)
|
||||||
open(os.path.join(self.state, "sessions"), "w").close() # session records can no longer be written
|
open(os.path.join(self.state, "sessions"), "w").close() # session records can no longer be written
|
||||||
r = self.hook()
|
r = self.hook()
|
||||||
@@ -973,7 +980,7 @@ class GateSequenceTests(GateBase):
|
|||||||
n_retro += 1
|
n_retro += 1
|
||||||
self.record_finding(n_retro, sid=a.sid)
|
self.record_finding(n_retro, sid=a.sid)
|
||||||
elif op == "pause" and self.unit_state().get("status") == "open":
|
elif op == "pause" and self.unit_state().get("status") == "open":
|
||||||
self.sisi("unit", "pause", "waiting for the reviewer %d" % step)
|
self.sisi("unit", "pause", "waiting for the reviewer %d" % step, env={"SISI_SESSION_ID": a.sid})
|
||||||
paused = True
|
paused = True
|
||||||
elif op == "other_edits":
|
elif op == "other_edits":
|
||||||
b.edit("other.py", "y = %d\n" % step)
|
b.edit("other.py", "y = %d\n" % step)
|
||||||
|
|||||||
+5
-5
@@ -20,7 +20,7 @@ class UnitCase(SisiCase):
|
|||||||
self.contract_path = os.path.join(self.proj, ".sisi", "contract.json")
|
self.contract_path = os.path.join(self.proj, ".sisi", "contract.json")
|
||||||
|
|
||||||
def start(self, checks=None, **fields):
|
def start(self, checks=None, **fields):
|
||||||
r = self.sisi("contract", "init", "--goal", "make the thing")
|
r = self.sisi("contract", "init", "--goal", "make the thing", env={"SISI_SESSION_ID": "p1"})
|
||||||
self.assertEqual(r.returncode, 0, r.stdout)
|
self.assertEqual(r.returncode, 0, r.stdout)
|
||||||
c = C.load(self.contract_path)
|
c = C.load(self.contract_path)
|
||||||
c["checks"] = checks or [real("works", "test -f done.txt")]
|
c["checks"] = checks or [real("works", "test -f done.txt")]
|
||||||
@@ -371,14 +371,14 @@ class PauseTests(UnitCase):
|
|||||||
def test_pause_lets_the_turn_end_and_a_change_ends_the_wait(self):
|
def test_pause_lets_the_turn_end_and_a_change_ends_the_wait(self):
|
||||||
"""R-6.1-5: a pause is recorded, the gate makes no demands while nothing changes, a new change
|
"""R-6.1-5: a pause is recorded, the gate makes no demands while nothing changes, a new change
|
||||||
ends the wait, and the wait is telemetry."""
|
ends the wait, and the wait is telemetry."""
|
||||||
|
self.sisi("tick", stdin=json.dumps({"session_id": "p1", "cwd": self.proj, "hook_event_name": "SessionStart"})) # session start comes first
|
||||||
self.start()
|
self.start()
|
||||||
self.assertEqual(self.sisi("unit", "pause", "").returncode, 1)
|
self.assertEqual(self.sisi("unit", "pause", "", env={"SISI_SESSION_ID": "p1"}).returncode, 1)
|
||||||
self.assertEqual(self.sisi("unit", "pause", "the owner's decision").returncode, 0)
|
self.assertEqual(self.sisi("unit", "pause", "the owner's decision", env={"SISI_SESSION_ID": "p1"}).returncode, 0)
|
||||||
c = C.load(self.contract_path)
|
c = C.load(self.contract_path)
|
||||||
self.assertEqual(c["status"], "paused")
|
self.assertEqual(c["status"], "paused")
|
||||||
self.assertEqual(c["pauses"][0]["what"], "the owner's decision")
|
self.assertEqual(c["pauses"][0]["what"], "the owner's decision")
|
||||||
stop = json.dumps({"session_id": "p1", "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
stop = json.dumps({"session_id": "p1", "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
||||||
self.sisi("tick", stdin=json.dumps({"session_id": "p1", "cwd": self.proj, "hook_event_name": "SessionStart"}))
|
|
||||||
open(os.path.join(self.proj, "new.py"), "w").write("x = 1\n") # a change after the pause
|
open(os.path.join(self.proj, "new.py"), "w").write("x = 1\n") # a change after the pause
|
||||||
self.assertIn("block", self.sisi("gate", stdin=stop).stdout)
|
self.assertIn("block", self.sisi("gate", stdin=stop).stdout)
|
||||||
self.assertEqual(C.load(self.contract_path)["status"], "open") # the wait ended
|
self.assertEqual(C.load(self.contract_path)["status"], "open") # the wait ended
|
||||||
@@ -391,7 +391,7 @@ class PauseTests(UnitCase):
|
|||||||
def test_a_paused_unit_with_no_change_passes_silently(self):
|
def test_a_paused_unit_with_no_change_passes_silently(self):
|
||||||
"""R-6.1-5."""
|
"""R-6.1-5."""
|
||||||
self.start()
|
self.start()
|
||||||
self.sisi("unit", "pause", "waiting for a review")
|
self.sisi("unit", "pause", "waiting for a review", env={"SISI_SESSION_ID": "p1"})
|
||||||
stop = json.dumps({"session_id": "p2", "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
stop = json.dumps({"session_id": "p2", "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
||||||
self.sisi("tick", stdin=json.dumps({"session_id": "p2", "cwd": self.proj, "hook_event_name": "SessionStart"}))
|
self.sisi("tick", stdin=json.dumps({"session_id": "p2", "cwd": self.proj, "hook_event_name": "SessionStart"}))
|
||||||
r = self.sisi("gate", stdin=stop)
|
r = self.sisi("gate", stdin=stop)
|
||||||
|
|||||||
@@ -522,5 +522,161 @@ class DelegationTests(GuardCase):
|
|||||||
self.assertIsNone(self.spawn("Implement the new greeting"))
|
self.assertIsNone(self.spawn("Implement the new greeting"))
|
||||||
|
|
||||||
|
|
||||||
|
class PauseTests(GuardCase):
|
||||||
|
"""Item 4: pause is for waiting, not for skipping (R-10.3-1, R-14.2-1)."""
|
||||||
|
|
||||||
|
def pause(self, sid="me", why="waiting for the reviewer"):
|
||||||
|
return self.sisi("unit", "pause", why, env={"SISI_SESSION_ID": sid} if sid else {})
|
||||||
|
|
||||||
|
def test_pausing_with_no_unit_is_refused(self):
|
||||||
|
r = self.pause()
|
||||||
|
self.assertNotEqual(r.returncode, 0)
|
||||||
|
self.assertIn("never the contract", r.stdout)
|
||||||
|
self.assertIn("sisi contract init", r.stdout)
|
||||||
|
|
||||||
|
def test_pausing_a_unit_this_session_does_not_hold_is_refused_and_the_unit_is_untouched(self):
|
||||||
|
self.write_contract(good_contract(goal="old work"))
|
||||||
|
G.bind("someone", self.proj, C.load(self.contract_path), "init")
|
||||||
|
with open(self.contract_path) as f:
|
||||||
|
before = f.read()
|
||||||
|
r = self.pause()
|
||||||
|
self.assertNotEqual(r.returncode, 0)
|
||||||
|
for way in ("sisi unit continue", "sisi unit park"):
|
||||||
|
self.assertIn(way, r.stdout)
|
||||||
|
self.assertLessEqual(len(r.stdout.splitlines()), 12)
|
||||||
|
with open(self.contract_path) as f:
|
||||||
|
self.assertEqual(f.read(), before)
|
||||||
|
|
||||||
|
def test_pausing_without_a_known_session_is_refused(self):
|
||||||
|
self.open_unit()
|
||||||
|
self.assertNotEqual(self.pause(sid=None).returncode, 0)
|
||||||
|
|
||||||
|
def test_pausing_a_unit_missing_what_building_needs_is_refused(self):
|
||||||
|
"""The placeholder skeleton is not a unit to hide behind."""
|
||||||
|
self.sisi("contract", "init", "--goal", "make the app greet", env={"SISI_SESSION_ID": "me"})
|
||||||
|
r = self.pause()
|
||||||
|
self.assertNotEqual(r.returncode, 0)
|
||||||
|
self.assertIn("placeholder", r.stdout)
|
||||||
|
self.assertEqual(C.load(self.contract_path)["status"], "open")
|
||||||
|
|
||||||
|
def test_pausing_an_integration_unit_without_its_requirement_list_is_refused(self):
|
||||||
|
c = good_contract()
|
||||||
|
self.open_unit(c)
|
||||||
|
self.put("package.json", "{}\n") # the work already touches a manifest: the floor is integration
|
||||||
|
r = self.pause()
|
||||||
|
self.assertNotEqual(r.returncode, 0)
|
||||||
|
self.assertIn("requirement list", r.stdout)
|
||||||
|
|
||||||
|
def test_a_valid_unit_this_session_holds_can_be_paused(self):
|
||||||
|
self.open_unit()
|
||||||
|
r = self.pause()
|
||||||
|
self.assertEqual(r.returncode, 0, r.stdout)
|
||||||
|
self.assertEqual(C.load(self.contract_path)["status"], "paused")
|
||||||
|
|
||||||
|
def test_every_refused_pause_is_recorded(self):
|
||||||
|
self.pause()
|
||||||
|
self.write_contract(good_contract())
|
||||||
|
self.pause()
|
||||||
|
self.assertEqual([r["tool"] for r in self.refusals()], ["pause", "pause"])
|
||||||
|
self.assertTrue(all("pause" in r["what"] for r in self.refusals()))
|
||||||
|
|
||||||
|
|
||||||
|
class GateUnitTests(GuardCase):
|
||||||
|
"""Items 4 and 5: the gate judges only the unit the session holds, whatever is paused (R-5.3-1, R-10.1-1)."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
os.environ["SISI_RETRO_REQUIRED"] = "1"
|
||||||
|
self.addCleanup(lambda: os.environ.__setitem__("SISI_RETRO_REQUIRED", "0"))
|
||||||
|
self.tick()
|
||||||
|
|
||||||
|
def stop(self, sid=None):
|
||||||
|
return gate.decide({"session_id": sid or self.sid, "cwd": self.proj, "hook_event_name": "Stop", "stop_hook_active": False})
|
||||||
|
|
||||||
|
def failing_foreign_unit(self, **kw):
|
||||||
|
c = good_contract(goal="redo the old report", id="old-report", checks=[dict(real("oldcheck"), cmd="false")], **kw)
|
||||||
|
self.write_contract(c)
|
||||||
|
G.bind("someone-else", self.proj, C.load(self.contract_path), "init")
|
||||||
|
|
||||||
|
def test_a_foreign_units_failing_checks_are_not_what_the_session_is_sent_back_for(self):
|
||||||
|
self.failing_foreign_unit()
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
a, msg = self.stop()
|
||||||
|
self.assertEqual(a, "block")
|
||||||
|
for way in ("sisi unit continue", "sisi unit park", "sisi contract init"):
|
||||||
|
self.assertIn(way, msg)
|
||||||
|
self.assertIn("old-report", msg)
|
||||||
|
self.assertNotIn("oldcheck", msg)
|
||||||
|
self.assertNotIn("Failing checks", msg)
|
||||||
|
self.assertEqual([r for r in self.results() if r.get("contract") == "old-report"], [], "its checks must not run")
|
||||||
|
|
||||||
|
def test_a_trivial_change_with_a_foreign_unit_open_is_not_judged_at_all(self):
|
||||||
|
self.failing_foreign_unit()
|
||||||
|
self.put("README.md", "# app\nmore words\n")
|
||||||
|
self.assertEqual(self.stop(), ("allow", None))
|
||||||
|
self.assertEqual(self.results(), [])
|
||||||
|
|
||||||
|
def test_a_stale_unit_the_session_was_bound_to_asks_for_continue_not_for_its_checks(self):
|
||||||
|
self.write_contract(good_contract(checks=[dict(real("c1"), cmd="false")]))
|
||||||
|
self.age_unit(C.load(self.contract_path), 20 * 3600, "me")
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
a, msg = self.stop()
|
||||||
|
self.assertEqual(a, "block")
|
||||||
|
self.assertIn("no activity since", msg)
|
||||||
|
self.assertIn("sisi unit continue", msg)
|
||||||
|
self.assertEqual(self.results(), [])
|
||||||
|
|
||||||
|
def test_the_send_backs_run_out_as_unverified_and_the_foreign_unit_is_left_exactly_as_it_was(self):
|
||||||
|
self.failing_foreign_unit()
|
||||||
|
with open(self.contract_path) as f:
|
||||||
|
before = f.read()
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
outs = [self.stop() for _ in range(4)]
|
||||||
|
self.assertEqual([o[0] for o in outs[:3]], ["block"] * 3)
|
||||||
|
self.assertEqual(outs[3][0], "allow")
|
||||||
|
self.assertIn("UNVERIFIED", outs[3][1])
|
||||||
|
with open(self.contract_path) as f:
|
||||||
|
self.assertEqual(f.read(), before)
|
||||||
|
|
||||||
|
def test_a_paused_foreign_unit_does_not_silence_the_gate_for_another_session(self):
|
||||||
|
c = good_contract(goal="redo the old report", id="old-report")
|
||||||
|
c["status"] = "paused"
|
||||||
|
c["pauses"] = [{"what": "a review", "since": store.now(), "until": None}]
|
||||||
|
self.write_contract(c)
|
||||||
|
G.bind("someone-else", self.proj, C.load(self.contract_path), "init")
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
a, msg = self.stop()
|
||||||
|
self.assertEqual(a, "block")
|
||||||
|
self.assertIn("sisi unit continue", msg)
|
||||||
|
self.assertEqual(C.load(self.contract_path)["status"], "paused") # not resumed, not touched
|
||||||
|
|
||||||
|
def test_a_paused_unit_silences_only_the_session_bound_to_it_and_only_while_valid(self):
|
||||||
|
self.open_unit()
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
self.assertEqual(self.sisi("unit", "pause", "waiting for the reviewer", env={"SISI_SESSION_ID": "me"}).returncode, 0)
|
||||||
|
self.assertEqual(self.stop(), ("allow", None))
|
||||||
|
# the unit is broken after the pause (a vacuous check): the pause no longer hides anything
|
||||||
|
c = C.load(self.contract_path)
|
||||||
|
c["checks"] = [dict(real("c1"), cmd="true")]
|
||||||
|
C.save(self.contract_path, c)
|
||||||
|
self.put(SRC, "def greet():\n return 'hey'\n")
|
||||||
|
a, msg = self.stop()
|
||||||
|
self.assertEqual(a, "block")
|
||||||
|
self.assertIn("cannot fail", msg)
|
||||||
|
|
||||||
|
def test_with_no_unit_at_all_a_source_change_is_still_sent_back_for_a_contract(self):
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
a, msg = self.stop()
|
||||||
|
self.assertEqual(a, "block")
|
||||||
|
self.assertIn("a contract for this isolated change", msg)
|
||||||
|
|
||||||
|
def test_the_session_that_holds_the_unit_is_judged_as_before(self):
|
||||||
|
self.open_unit(good_contract(checks=[dict(real("c1"), cmd="false")]))
|
||||||
|
self.put(SRC, "def greet():\n return 'yo'\n")
|
||||||
|
a, msg = self.stop()
|
||||||
|
self.assertEqual(a, "block")
|
||||||
|
self.assertIn("c1", msg)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
Reference in new issue
Block a user