Gate judges a session by its own edits, measured, not guessed
ci / tests (macos-latest, python 3.10) (push) Canceled after 0s
ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.10) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.10) (push) Canceled after 0s
ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.10) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
The end-of-turn gate attributes changed files to the session that changed them: edit tool calls by path, and shell commands by a snapshot of the tree before and after. A session answers for failing checks, and is asked for a retrospective, only over its own files. Another agent's work in progress is allowed once, marked unverified, with a note that the work is not done. Single-session behaviour is unchanged, shown by a property test. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
e5b603486b
commit
5744f3149c
15 files changed
+1387
-41
No files matched your search
+501
-7
@@ -1,8 +1,8 @@
|
||||
"""End-of-turn gate. Claude Code Stop hook: JSON on stdin, block = JSON {"decision":"block","reason":...} on stdout, exit 0.
|
||||
Anything that goes wrong inside sisi allows (fail open) and is logged to the state dir."""
|
||||
import hashlib, json, os, sys, time
|
||||
import hashlib, json, os, re, shlex, sys, time
|
||||
|
||||
from . import contract as C, deep as D, gitutil, store, trust, verify as V
|
||||
from . import contract as C, deep as D, gitutil, paths, store, trust, verify as V
|
||||
|
||||
|
||||
def _read_payload():
|
||||
@@ -71,6 +71,369 @@ class _Ctx:
|
||||
return "block", template.replace("{n}", "%d of %d" % (blocks + 1, maxb))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- attribution
|
||||
# Attribution reads this session's own transcript (Claude-Code-shaped JSONL: assistant records whose
|
||||
# message.content holds tool_use blocks). Only bytes after the per-session offset stored in the session
|
||||
# record are read, so a long transcript costs only its new tail; subagent transcripts found under
|
||||
# <transcript-without-.jsonl>/subagents/** are read the same way and count as this session's edits.
|
||||
# Edit/Write/MultiEdit/NotebookEdit (and any tool whose name says it writes) count, with the file paths
|
||||
# in their input. Shell (Bash) commands count when they plausibly write: a redirect, tee/dd-of, an
|
||||
# in-place editor (sed -i, perl -i, awk -i inplace), git apply/checkout/restore/reset/clean/mv/rm,
|
||||
# patch, cp/mv/rm/touch/mkdir, or any program that is not on the provably read-only list (ls, cat,
|
||||
# grep, git status/diff/log/..., test runners and interpreters therefore count: they can write).
|
||||
# Known blind spots: a read-only-looking command that writes through indirection sisi cannot see
|
||||
# (a script reached by a read-only interpreter flag combination that slips the lists, a daemon, or a
|
||||
# tool whose transcript shape is not Claude-Code JSONL), and writes made between the last complete
|
||||
# transcript line and a crash. A missing/unreadable transcript, a corrupt line, or a shell command
|
||||
# that cannot be tokenised is treated as an edit by this session (fail strict, never fail open here);
|
||||
# so is an unread tail over 64 MB, which is skipped (offset moved to its end) rather than scanned
|
||||
# inside the gate's time budget.
|
||||
|
||||
EDIT_TOOLS = {"Edit", "Write", "MultiEdit", "NotebookEdit"}
|
||||
_READONLY_CMDS = {"ls", "cat", "head", "tail", "grep", "egrep", "fgrep", "rg", "ag", "wc", "file",
|
||||
"stat", "du", "df", "which", "type", "pwd", "date", "env", "printenv", "sort",
|
||||
"uniq", "cut", "tr", "jq", "yq", "diff", "cmp", "md5sum", "sha1sum", "sha256sum",
|
||||
"basename", "dirname", "realpath", "readlink", "ps", "top", "free", "uname",
|
||||
"hostname", "id", "whoami", "less", "more", "man", "help", "history"}
|
||||
_READONLY_GIT = {"status", "diff", "log", "show", "rev-parse", "ls-files", "ls-tree", "describe",
|
||||
"blame", "grep", "cat-file", "branch", "tag", "remote", "config", "add", "commit"}
|
||||
_GH_READ = {("run", "list"), ("run", "view"), ("run", "watch"), ("pr", "list"), ("pr", "view"),
|
||||
("pr", "status"), ("pr", "diff"), ("pr", "checks"), ("issue", "list"), ("issue", "view"),
|
||||
("issue", "status"), ("repo", "view"), ("repo", "list"), ("release", "list"),
|
||||
("release", "view"), ("workflow", "list"), ("workflow", "view"), ("auth", "status")}
|
||||
_WRITE_WORDS = ("edit", "write", "create", "delete", "move", "rename", "patch", "apply", "insert")
|
||||
_SHELL_WRITE_RE = re.compile(
|
||||
r">>?[^=]|\btee\b|\bdd\b[^;&|]*\bof=|\bsed\b[^;&|]*\s-[a-z]*i|\bperl\b[^;&|]*\s-[a-z]*i"
|
||||
r"|\bawk\b[^;&|]*inplace|\bgit\s+(apply|checkout|restore|reset|clean|mv|rm)\b"
|
||||
r"|\bpatch\b|\b(cp|mv|rm|touch|mkdir|rmdir|install|ln)\b", re.I)
|
||||
|
||||
|
||||
def _shell_may_write(command):
|
||||
"""True: plausibly writes files. False: provably read-only. None: cannot be tokenised."""
|
||||
if not isinstance(command, str) or not command.strip():
|
||||
return False
|
||||
if _SHELL_WRITE_RE.search(command):
|
||||
return True
|
||||
try:
|
||||
lex = shlex.shlex(command, posix=True, punctuation_chars=True)
|
||||
lex.whitespace_split = True
|
||||
tokens = list(lex)
|
||||
except ValueError:
|
||||
return None
|
||||
segments, cur = [], []
|
||||
for tok in tokens:
|
||||
if tok in (";", "&", "|", "&&", "||"):
|
||||
segments.append(cur)
|
||||
cur = []
|
||||
else:
|
||||
cur.append(tok)
|
||||
segments.append(cur)
|
||||
for seg in segments:
|
||||
i = 0
|
||||
while i < len(seg) and (re.match(r"^[A-Za-z_][A-Za-z0-9_]*=", seg[i]) or seg[i] in ("sudo", "command", "exec", "time", "nice")):
|
||||
i += 1
|
||||
if i >= len(seg):
|
||||
continue
|
||||
prog = os.path.basename(seg[i])
|
||||
rest = seg[i + 1:]
|
||||
if prog in ("cd", "export", "unset", "set", "alias", "umask", "wait", "jobs", "fg", "bg",
|
||||
"exit", "return", "break", "continue", "shift", "readonly", "local", "declare",
|
||||
"typeset", "echo", "printf", "true", "false", ":", "test", "[", "[[", "eval"):
|
||||
if prog == "eval":
|
||||
return True
|
||||
continue
|
||||
if prog == "find":
|
||||
if any(a in ("-delete", "-exec", "-execdir", "-ok", "-okdir", "-fls", "-fprint") for a in rest):
|
||||
return True
|
||||
continue
|
||||
if prog == "git":
|
||||
sub = next((a for a in rest if not a.startswith("-")), "")
|
||||
if sub == "stash": # only listing a stash leaves the tree alone; push/pop/apply change it
|
||||
tail = rest[rest.index(sub) + 1:]
|
||||
if not tail or tail[0] in ("list", "show"):
|
||||
continue
|
||||
return True
|
||||
if sub in _READONLY_GIT and not (sub == "config" and any(not a.startswith("-") for a in rest[rest.index(sub) + 1:])):
|
||||
continue # add/commit touch .git only, never a working-tree file
|
||||
return True
|
||||
if prog == "gh":
|
||||
pair = tuple(a for a in rest if not a.startswith("-"))[:2]
|
||||
if pair in _GH_READ:
|
||||
continue
|
||||
if pair and pair[0] == "api" and not any(a in ("-X", "--method", "-f", "-F", "--field", "--raw-field")
|
||||
or a.startswith(("-f", "-F")) for a in rest):
|
||||
continue
|
||||
return True
|
||||
if prog == "sisi":
|
||||
# sisi writes only personal data and state, never the project — except contract
|
||||
# init/waive/trust, which do record something about (or in) this project.
|
||||
if rest and rest[0] == "contract":
|
||||
if len(rest) > 1 and rest[1] in ("init", "waive", "trust", "untrust"):
|
||||
return True
|
||||
continue
|
||||
continue
|
||||
if prog in _READONLY_CMDS:
|
||||
if prog in ("sed", "awk", "jq", "yq") and any("inplace" in a or a.startswith("-i") for a in rest):
|
||||
return True
|
||||
continue
|
||||
return True # interpreters, build/test runners, scripts, anything unlisted: can write
|
||||
return False
|
||||
|
||||
|
||||
def _input_files(inp):
|
||||
out = []
|
||||
if isinstance(inp, dict):
|
||||
for k in ("file_path", "notebook_path", "path", "filename", "filePath"):
|
||||
if isinstance(inp.get(k), str) and inp[k]:
|
||||
out.append(inp[k])
|
||||
return out
|
||||
|
||||
|
||||
def _parse_transcript_chunk(data, measured=False):
|
||||
"""Parse complete JSONL bytes. Returns (edits, files, writing, corrupt, consumed_bytes, shell_edits).
|
||||
In measured mode Bash calls are not guessed from their text at all: the before/after tree
|
||||
measurement (sisi/watch.py) has already attributed whatever they actually changed — except
|
||||
`sisi contract init/waive/trust`, which record project state outside the measured files."""
|
||||
if data.endswith(b"\n") or not data:
|
||||
consumed, body = len(data), data
|
||||
else:
|
||||
cut = data.rfind(b"\n")
|
||||
tail = data[cut + 1:]
|
||||
try:
|
||||
json.loads(tail.decode("utf-8", "replace"))
|
||||
consumed, body = len(data), data # a complete final line without its newline yet
|
||||
except ValueError:
|
||||
consumed, body = cut + 1, data[:cut + 1] # partial line: leave it for the next read
|
||||
edits, files, writing, corrupt, shell_edits = 0, [], False, 0, 0
|
||||
for raw in body.split(b"\n"):
|
||||
if not raw.strip():
|
||||
continue
|
||||
if len(raw) > 131072 and b"tool_use" not in raw and b"Bash" not in raw:
|
||||
continue # a huge tool-result line: no tool call hides in it worth parsing
|
||||
try:
|
||||
d = json.loads(raw.decode("utf-8", "replace"))
|
||||
except ValueError:
|
||||
corrupt += 1
|
||||
edits += 1 # an unreadable line may be the edit we cannot see: count it
|
||||
writing = True
|
||||
continue
|
||||
if not isinstance(d, dict):
|
||||
continue
|
||||
content = (d.get("message") or {}).get("content") if isinstance(d.get("message"), dict) else None
|
||||
for x in content if isinstance(content, list) else []:
|
||||
if not isinstance(x, dict) or x.get("type") != "tool_use":
|
||||
continue
|
||||
name = x.get("name") or ""
|
||||
inp = x.get("input") if isinstance(x.get("input"), dict) else {}
|
||||
if name in EDIT_TOOLS or any(w in name.lower() for w in _WRITE_WORDS):
|
||||
edits += 1
|
||||
writing = True
|
||||
files += _input_files(inp)
|
||||
elif name in ("Bash", "Shell", "bash", "shell"):
|
||||
cmd = inp.get("command") if isinstance(inp.get("command"), str) else inp.get("cmd")
|
||||
if measured:
|
||||
if isinstance(cmd, str) and re.search(r"\bsisi\s+contract\s+(init|waive|trust|untrust)\b", cmd):
|
||||
edits += 1
|
||||
shell_edits += 1
|
||||
writing = True
|
||||
files.append(".sisi/contract.json")
|
||||
continue # everything else was measured, not guessed
|
||||
may = _shell_may_write(cmd)
|
||||
if may is None:
|
||||
corrupt += 1
|
||||
edits += 1
|
||||
shell_edits += 1
|
||||
writing = True
|
||||
elif may:
|
||||
edits += 1
|
||||
shell_edits += 1
|
||||
writing = True
|
||||
return edits, files, writing, corrupt, consumed, shell_edits
|
||||
|
||||
|
||||
def _scan_one(sess, store_key, path):
|
||||
"""Advance one transcript's offset in the session record. Returns (edits, files, writing, unknown)."""
|
||||
offsets = sess.setdefault("attr_sub_offsets", {}) if store_key == "sub" else None
|
||||
try:
|
||||
size = os.path.getsize(os.path.expanduser(path))
|
||||
except OSError:
|
||||
return 0, [], False, True, 0 # missing or unreadable: unknown, treated as edited by the caller
|
||||
if store_key == "sub":
|
||||
offset = int(offsets.get(path, 0) or 0)
|
||||
else:
|
||||
offset = int(sess.get("attr_offset", 0) or 0)
|
||||
unknown = False
|
||||
if offset > size:
|
||||
offset, unknown = 0, True # truncated or replaced: rescan, and do not trust the gap
|
||||
if size - offset > 64 * 1024 * 1024:
|
||||
# Too much unread tail to scan inside the gate's budget: skip it and count it as a
|
||||
# possible edit by this session (strict), rather than guessing it was someone else's.
|
||||
if store_key == "sub":
|
||||
offsets[path] = size
|
||||
else:
|
||||
sess["attr_offset"] = size
|
||||
return 1, [], True, True, 0
|
||||
edits, files, writing, sh = 0, [], False, 0
|
||||
try:
|
||||
with open(os.path.expanduser(path), "rb") as f:
|
||||
f.seek(offset)
|
||||
data = f.read()
|
||||
except OSError:
|
||||
return 0, [], False, True, 0
|
||||
if data:
|
||||
e, fl, w, corrupt, consumed, sh = _parse_transcript_chunk(data, measured=bool(sess.get("attr_measured")))
|
||||
edits, files, writing = e, fl, w
|
||||
unknown = unknown or corrupt > 0
|
||||
offset += consumed
|
||||
if store_key == "sub":
|
||||
offsets[path] = offset
|
||||
else:
|
||||
sess["attr_offset"] = offset
|
||||
return edits, files, writing, unknown, sh
|
||||
|
||||
|
||||
def _subagent_transcripts(tp):
|
||||
"""Native subagent transcripts under this session's transcript, Claude Code layout."""
|
||||
if not isinstance(tp, str) or not tp.endswith(".jsonl"):
|
||||
return []
|
||||
root = os.path.join(os.path.expanduser(tp)[:-len(".jsonl")], "subagents")
|
||||
out = []
|
||||
for dirpath, dirs, names in os.walk(root):
|
||||
dirs.sort()
|
||||
for n in sorted(names):
|
||||
if n.startswith("agent-") and n.endswith(".jsonl"):
|
||||
out.append(os.path.join(dirpath, n))
|
||||
return out
|
||||
|
||||
|
||||
def _update_attribution(sess, tp):
|
||||
"""Refresh this session's edit count from its own (and its native subagents') transcripts.
|
||||
|
||||
Returns {"gen", "writing", "unknown"} or None when no transcript path is known at all
|
||||
(the caller then falls back to the pre-attribution behaviour). Fails towards edited."""
|
||||
if not isinstance(tp, str) or not tp:
|
||||
return None
|
||||
if sess.get("attr_tp") != tp:
|
||||
sess.update(attr_tp=tp, attr_offset=0, attr_gen=0, attr_pass_gen=0, attr_retro_gen=0,
|
||||
attr_baseline_gen=0, attr_writing=False, attr_files=[], attr_sub_offsets={})
|
||||
sess.setdefault("attr_gen", 0)
|
||||
sess.setdefault("attr_pass_gen", 0)
|
||||
sess.setdefault("attr_retro_gen", 0)
|
||||
sess.setdefault("attr_baseline_gen", 0)
|
||||
unknown = False
|
||||
total, all_files, shell_total = 0, [], 0
|
||||
e, fl, w, unk, sh = _scan_one(sess, "main", tp)
|
||||
total += e
|
||||
shell_total += sh
|
||||
all_files += fl
|
||||
unknown = unknown or unk
|
||||
if w:
|
||||
sess["attr_writing"] = True
|
||||
for sub in _subagent_transcripts(tp):
|
||||
e, fl, w, unk, sh = _scan_one(sess, "sub", sub)
|
||||
total += e
|
||||
shell_total += sh
|
||||
all_files += fl
|
||||
unknown = unknown or unk
|
||||
if w:
|
||||
sess["attr_writing"] = True
|
||||
sess["attr_gen"] = int(sess.get("attr_gen", 0)) + total
|
||||
if all_files:
|
||||
have = sess.setdefault("attr_files", [])
|
||||
for f in all_files:
|
||||
if f not in have:
|
||||
have.append(f)
|
||||
del have[:-500]
|
||||
if unknown:
|
||||
sess["attr_writing"] = True
|
||||
return {"gen": int(sess["attr_gen"]), "writing": bool(sess.get("attr_writing")), "unknown": bool(unknown),
|
||||
"edits": total, "files": all_files, "shell_edits": shell_total}
|
||||
|
||||
|
||||
def _changed_paths(top, cwd, own_files):
|
||||
"""Up to five working-tree paths changed against HEAD that this session did not edit."""
|
||||
try:
|
||||
rels = gitutil.measure(cwd, gitutil.head(cwd))["files"]
|
||||
except Exception:
|
||||
return []
|
||||
own = set()
|
||||
for f in own_files or []:
|
||||
own.add(os.path.normpath(f))
|
||||
if top:
|
||||
try:
|
||||
own.add(os.path.normpath(os.path.relpath(f, top)))
|
||||
except ValueError:
|
||||
pass
|
||||
out = []
|
||||
for r in rels:
|
||||
if os.path.normpath(r) not in own:
|
||||
out.append(r)
|
||||
if len(out) >= 5:
|
||||
break
|
||||
return out
|
||||
|
||||
|
||||
def _other_sessions(top, cwd, changed, sid, tp):
|
||||
"""Session ids that plausibly made `changed` edits, from session records and sibling transcripts."""
|
||||
found = []
|
||||
|
||||
def note(x):
|
||||
if x and x != sid and x not in found:
|
||||
found.append(x)
|
||||
|
||||
def mentions(path, limit=2 * 1024 * 1024):
|
||||
try:
|
||||
with open(os.path.expanduser(path), "rb") as f:
|
||||
f.seek(0, 2)
|
||||
size = f.tell()
|
||||
f.seek(max(0, size - limit))
|
||||
data = f.read()
|
||||
except OSError:
|
||||
return False
|
||||
return any(os.path.basename(c).encode() in data or c.encode() in data for c in changed)
|
||||
|
||||
try:
|
||||
names = sorted(os.listdir(os.path.join(paths.state(), "sessions")))
|
||||
except OSError:
|
||||
names = []
|
||||
for n in names:
|
||||
if not n.endswith(".json"):
|
||||
continue
|
||||
try:
|
||||
with open(os.path.join(paths.state(), "sessions", n), encoding="utf-8") as f:
|
||||
r = json.load(f)
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
if not isinstance(r, dict):
|
||||
continue
|
||||
osid = r.get("session_id") or n[:-5]
|
||||
if osid == sid:
|
||||
continue
|
||||
proj = r.get("req_project") or r.get("cwd")
|
||||
if top and proj and gitutil.root(proj) not in (top, proj) and os.path.realpath(proj) != os.path.realpath(top):
|
||||
# a record for another folder says nothing about this tree
|
||||
if os.path.realpath(proj) != os.path.realpath(cwd):
|
||||
continue
|
||||
files = r.get("attr_files") or []
|
||||
hit = any(os.path.basename(f) in [os.path.basename(c) for c in changed] or
|
||||
(top and os.path.normpath(os.path.join(top, c)) == os.path.normpath(f)) for f in files for c in changed)
|
||||
if hit:
|
||||
note(osid)
|
||||
elif isinstance(r.get("transcript_path"), str) and changed and mentions(r["transcript_path"]):
|
||||
note(osid)
|
||||
if isinstance(tp, str) and changed:
|
||||
try:
|
||||
sibs = sorted(os.listdir(os.path.dirname(os.path.expanduser(tp)) or "."))
|
||||
except OSError:
|
||||
sibs = []
|
||||
for n in sibs:
|
||||
if n.endswith(".jsonl") and os.path.join(os.path.dirname(os.path.expanduser(tp)), n) != os.path.expanduser(tp):
|
||||
p = os.path.join(os.path.dirname(os.path.expanduser(tp)), n)
|
||||
if mentions(p):
|
||||
note(n[:-len(".jsonl")])
|
||||
return found[:3]
|
||||
|
||||
|
||||
def decide(payload):
|
||||
"""Return ('allow', message_or_None) or ('block', reason)."""
|
||||
cfg = store.config()
|
||||
@@ -89,6 +452,44 @@ def decide(payload):
|
||||
sess["req_contract"], sess["req_project"] = bool(path), cwd
|
||||
if isinstance(payload.get("transcript_path"), str):
|
||||
sess["transcript_path"] = payload["transcript_path"] # `sisi retro` reads the request's start from it
|
||||
tp = sess.get("transcript_path") if isinstance(sess.get("transcript_path"), str) else None
|
||||
try:
|
||||
attr = _update_attribution(sess, tp) if tp else None
|
||||
except Exception as e: # attribution must never break the gate: fail towards strict, not open
|
||||
store.log_error("gate-attribution", e)
|
||||
attr = {"gen": int(sess.get("attr_gen", 0) or 0), "writing": True, "unknown": True}
|
||||
if attr is not None:
|
||||
try:
|
||||
from . import watch
|
||||
if watch.consume_pending(sess, sid, cwd):
|
||||
attr["unknown"] = True # a shell command with a before but no after: strict
|
||||
except Exception as e:
|
||||
store.log_error("gate-watch-pending", e)
|
||||
since_pass = sess.setdefault("attr_since_pass", [])
|
||||
top0 = gitutil.root(cwd)
|
||||
for f in attr.get("files") or []:
|
||||
rel = f
|
||||
if os.path.isabs(f) and top0:
|
||||
try:
|
||||
rel = os.path.relpath(f, top0)
|
||||
except ValueError:
|
||||
rel = f
|
||||
if rel not in since_pass:
|
||||
since_pass.append(rel)
|
||||
if attr.get("shell_edits") and not sess.get("attr_measured") and "*shell*" not in since_pass:
|
||||
since_pass.append("*shell*") # a text-classified shell edit: its paths are unknown
|
||||
try:
|
||||
store.save_session(sid, sess) # persist the offset even on an early return below
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _own_since(key):
|
||||
if attr is None:
|
||||
return None # no transcript path known: pre-attribution behaviour
|
||||
return bool(attr["unknown"] or int(sess.get("attr_gen", 0) or 0) > int(sess.get(key, 0) or 0))
|
||||
|
||||
own_since_pass = _own_since("attr_pass_gen")
|
||||
own_since_retro = _own_since("attr_retro_gen")
|
||||
c, errs = None, []
|
||||
if path:
|
||||
project = C.project_dir(path)
|
||||
@@ -127,9 +528,81 @@ def decide(payload):
|
||||
from . import retro
|
||||
if retro.satisfied(repo_key, since, fp):
|
||||
sess["retro_done"] = True
|
||||
if attr is not None:
|
||||
sess["attr_retro_gen"] = int(sess.get("attr_gen", 0) or 0)
|
||||
return False
|
||||
return True
|
||||
|
||||
_csp = {}
|
||||
|
||||
def changed_paths_since_pass():
|
||||
if "v" not in _csp:
|
||||
v = None
|
||||
if attr is not None:
|
||||
try:
|
||||
from . import watch
|
||||
v = watch.changed_since_pass(sid, cwd)
|
||||
except Exception as e:
|
||||
store.log_error("gate-watch-diff", e)
|
||||
_csp["v"] = v if v is not None else _changed_paths(top, cwd, [])
|
||||
return _csp["v"]
|
||||
|
||||
def own_responsible():
|
||||
"""This session answers for the failing tree when a path that changed since the last
|
||||
passing state is attributed to it (edit tool path, or a measured command window), or
|
||||
when attribution is uncertain. Gen-only fallback when no path differs (e.g. the
|
||||
contract itself changed, which the stat snapshot does not track)."""
|
||||
if attr is None:
|
||||
return True
|
||||
if attr["unknown"]:
|
||||
return True
|
||||
changed = changed_paths_since_pass()
|
||||
if not changed:
|
||||
return bool(own_since_pass)
|
||||
since = set(sess.get("attr_since_pass") or [])
|
||||
if "*shell*" in since or "*shell-unmeasured*" in since:
|
||||
return True
|
||||
return any(p in changed for p in since)
|
||||
|
||||
def other_allow(failing_text, contract_id=None):
|
||||
"""Someone else's failing tree: allow once per distinct tree state, recorded unverified."""
|
||||
if sess.get("other_notice_fp") == fp:
|
||||
return ctx.allow()
|
||||
sess["other_notice_fp"] = fp
|
||||
store.append("results", {"ts": store.now(), "contract": contract_id, "project": project if path else repo_key,
|
||||
"check": "gate", "item": "gate", "kind": None, "rung": None,
|
||||
"status": "unverified", "duration_s": 0.0, "tool": store.tool(), "session": sid})
|
||||
own_paths = set(sess.get("attr_since_pass") or [])
|
||||
changed = [p for p in changed_paths_since_pass() if p not in own_paths][:5] \
|
||||
or _changed_paths(top, cwd, sess.get("attr_files") or [])
|
||||
others = _other_sessions(top, cwd, changed, sid, tp)
|
||||
lines = ["sisi: the contract is NOT satisfied, but this session has not edited files since the "
|
||||
"checks last passed (or since it started), so this turn may end. Do not report this work "
|
||||
"as done; the result is recorded as UNVERIFIED. Relay this to the human.",
|
||||
"Failing checks:\n" + failing_text]
|
||||
if changed:
|
||||
lines.append("Files changed in the tree that this session did not edit (likely another "
|
||||
"session's work in progress): " + ", ".join(changed))
|
||||
if others:
|
||||
lines.append("The changed files appear in the transcript/session record of session(s): "
|
||||
+ ", ".join(others))
|
||||
else:
|
||||
lines.append("Files changed by another session are the likely cause; no other session "
|
||||
"could be identified from the records available.")
|
||||
return ctx.allow("\n".join(lines))
|
||||
|
||||
def other_tree_changed():
|
||||
"""The tree moved since this session's last pass or its start (dirty-vs-HEAD is the
|
||||
fallback proxy when no start fingerprint was ever recorded for the session)."""
|
||||
if fp is not None:
|
||||
if sess.get("attr_pass_fp"):
|
||||
return fp != sess["attr_pass_fp"]
|
||||
if sess.get("baseline_fp") is not None:
|
||||
return fp != sess["baseline_fp"]
|
||||
if sess.get("last_fp") is not None:
|
||||
return fp != sess["last_fp"]
|
||||
return bool(_changed_paths(top, cwd, sess.get("attr_files") or []))
|
||||
|
||||
w = _new_waiver(top, sess) if (top and not path) else None # a fresh waiver re-opens the decision
|
||||
# A turn that changed nothing is not punished and not re-checked.
|
||||
if fp is not None and not w:
|
||||
@@ -161,15 +634,29 @@ def decide(payload):
|
||||
sess["req_check_s"] = float(sess.get("req_check_s", 0.0)) + (time.time() - t_run)
|
||||
V.record(c, project, results, fp)
|
||||
if V.ok(results):
|
||||
own_before_pass = own_since_pass
|
||||
responsible_before_pass = own_responsible() if attr is not None else True
|
||||
other_before_pass = attr is not None and not responsible_before_pass and other_tree_changed()
|
||||
if attr is not None:
|
||||
sess["attr_pass_gen"] = int(sess.get("attr_gen", 0) or 0)
|
||||
sess["attr_pass_fp"] = fp
|
||||
sess["attr_since_pass"] = []
|
||||
try:
|
||||
from . import watch
|
||||
watch.save_pass_snapshot(sid, cwd)
|
||||
except Exception as e:
|
||||
store.log_error("gate-watch-pass", e)
|
||||
if dfail:
|
||||
tails = "\n".join("- deep check %s [%s]:\n%s" % (i["id"], i["status"], "\n".join(" " + l for l in i["tail"].splitlines()[-15:]))
|
||||
for i in ds["failed"])
|
||||
if other_before_pass:
|
||||
return other_allow(tails[:4000], c["id"] if c else None)
|
||||
return ctx.block("sisi: the fast checks pass, but the deep checks failed on the current tree "
|
||||
"(send-back {n}). Fix the work, then re-run them with `sisi verify --deep "
|
||||
"--background` (see `sisi verify --state`). Do not report this work as done.\n\n%s"
|
||||
% tails[:4000], c["id"], project, kind="deep")
|
||||
changed = fp is None or fp != sess.get("baseline_fp")
|
||||
if need_retro(changed):
|
||||
if need_retro(changed if attr is None else bool(own_since_retro)):
|
||||
from . import retro
|
||||
return ctx.block(retro.TEMPLATE, c["id"] if c else None, project, kind="retro")
|
||||
if ds and ds["state"] != "fresh-pass" and sess.get("deep_notice") != (fp or "nofp"):
|
||||
@@ -181,6 +668,8 @@ def decide(payload):
|
||||
return ctx.allow({"message": "sisi: deep checks %s; the work is not yet fully verified" % ds["state"],
|
||||
"context": text})
|
||||
return ctx.allow()
|
||||
if attr is not None and not own_responsible() and other_tree_changed():
|
||||
return other_allow(V.failure_report(results), c["id"] if c else None)
|
||||
msg = ("sisi: the contract at %s is not satisfied (send-back {n}). Fix the work, then run "
|
||||
"`sisi verify` until it passes. Do not report this work as done.\n\n%s"
|
||||
% (path, V.failure_report(results)))
|
||||
@@ -200,8 +689,12 @@ def decide(payload):
|
||||
if sess.get("baseline") is None:
|
||||
sess.update(baseline=gitutil.measure(cwd), head=gitutil.head(cwd), started=sess.get("started") or store.now())
|
||||
sess["baseline_fp"] = fp
|
||||
if attr is not None:
|
||||
sess["attr_baseline_gen"] = int(sess.get("attr_gen", 0) or 0)
|
||||
store.save_session(sid, sess)
|
||||
return "allow", None
|
||||
if attr is not None and not _own_since("attr_baseline_gen") and not attr["unknown"]:
|
||||
return ctx.allow() # this session edited nothing here: no retro, no growth send-back for others' work
|
||||
# Fix 4: growth is measured from the session start, or from the latest waiver.
|
||||
if w:
|
||||
sess["baseline"] = {"lines": w.get("lines", 0), "files": w.get("files", [])}
|
||||
@@ -213,7 +706,7 @@ def decide(payload):
|
||||
dl = max(0, now["lines"] - base["lines"])
|
||||
df = len(set(now["files"]) - set(base["files"]))
|
||||
if not (dl > int(g.get("growth_lines", 40)) or df > int(g.get("growth_files", 3))):
|
||||
if need_retro(bool(sess.get("waiver_ns"))): # a waiver in this session means real work happened
|
||||
if need_retro(bool(sess.get("waiver_ns")) if attr is None else bool(own_since_retro)):
|
||||
from . import retro
|
||||
return ctx.block(retro.TEMPLATE, kind="retro")
|
||||
return ctx.allow()
|
||||
@@ -223,9 +716,6 @@ def decide(payload):
|
||||
"the change needs no checks, run `sisi contract waive \"<reason>\"` instead." % (dl, df))
|
||||
|
||||
|
||||
EDIT_TOOLS = {"Edit", "Write", "MultiEdit", "NotebookEdit"}
|
||||
|
||||
|
||||
def subagent_edits(transcript):
|
||||
"""(edited_files, start_ts) from a subagent transcript (JSONL). start_ts is in sisi's timestamp format, or None."""
|
||||
edited, start = False, None
|
||||
@@ -241,6 +731,10 @@ def subagent_edits(transcript):
|
||||
for x in content if isinstance(content, list) else []:
|
||||
if isinstance(x, dict) and x.get("type") == "tool_use" and x.get("name") in EDIT_TOOLS:
|
||||
edited = True
|
||||
if isinstance(x, dict) and x.get("type") == "tool_use" and x.get("name") in ("Bash", "Shell"):
|
||||
inp = x.get("input") if isinstance(x.get("input"), dict) else {}
|
||||
if _shell_may_write(inp.get("command")) is not False:
|
||||
edited = True
|
||||
return edited, start
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user