The approve hook refuses decisions, not prose that mentions the command. done / next: repeat runs and the final rehearsal
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
08d6921fcb
commit
a11491c985
3 files changed
+23
-7
No files matched your search
+3
-2
@@ -41,9 +41,10 @@ limited, and the rest waits for you:
|
||||
`sisi approve` lists every waiting change in full (the exact command, the pattern and profile, the rule text as a
|
||||
diff). Deciding one takes two layers:
|
||||
|
||||
- The before-tool hook refuses any shell command an agent runs whose text invokes `sisi approve` with an argument,
|
||||
- The before-tool hook refuses any shell command an agent runs whose text invokes `sisi approve` with a decision
|
||||
(`--yes` or `--no` for a change it names by id, or by something a shell fills in),
|
||||
however it is wrapped (`script`, `sh -c`, `env`, `expect`, a path to the launcher, `python -m sisi`, quotes and
|
||||
backslashes inside the words), for main threads and workers alike, and tells the agent that approval is yours
|
||||
backslashes inside the words); prose that only mentions the command (a search for it, a doc line) is allowed, for main threads and workers alike, and tells the agent that approval is yours
|
||||
alone and to show you the command to run yourself. The plain listing (`sisi approve` with no arguments) is
|
||||
allowed. The hook fails open on an error, so it is a guard, not a wall: a command built from pieces, from a
|
||||
variable or from an encoded string is not recognised by its text.
|
||||
|
||||
+10
-3
@@ -122,6 +122,7 @@ def note_shell(sess, cmd, payload):
|
||||
|
||||
_APPROVE = re.compile(r"(?:^|[\s/;&|(=`$])(?:sisi|sisi\.py|__main__\.py)(?=\s)(?:\s+-{1,2}[\w=-]+)*\s+approve\b([^;&|\n)`]*)")
|
||||
_DECISION = re.compile(r"\bP-\d+\b[\s\S]*--(?:yes|no)\b|--(?:yes|no)\b[\s\S]*\bP-\d+\b")
|
||||
_FLAG = re.compile(r"--(?:yes|no)\b")
|
||||
APPROVE_REFUSED = ("sisi: refused. Approving or refusing a learning change is the owner's alone; an agent does not "
|
||||
"run `sisi approve ID --yes|--no`, however it is wrapped. Show the owner what is waiting (run "
|
||||
"`sisi approve` with no arguments to list it, and show him the listing) and give him the command "
|
||||
@@ -130,7 +131,8 @@ APPROVE_REFUSED = ("sisi: refused. Approving or refusing a learning change is th
|
||||
|
||||
def refuse_self_approval(cmd, sid):
|
||||
"""Before-tool guard, for main threads and workers alike: a shell command whose text invokes
|
||||
sisi's approve command with an argument is refused (exit 2, the reason on stderr), however it
|
||||
sisi's approve command with a decision (`--yes` or `--no` for a change it names) is refused (exit 2,
|
||||
the reason on stderr), however it
|
||||
is wrapped (`script`, `sh -c`, `env`, `expect`, a path to the launcher, `python -m sisi`). The
|
||||
plain listing, `sisi approve`, is allowed. Quotes, backslashes and IFS tricks are stripped
|
||||
before the text is read. The attempt is recorded in the store with the session id. Fails open:
|
||||
@@ -139,8 +141,13 @@ def refuse_self_approval(cmd, sid):
|
||||
itself requires at the terminal is the second layer."""
|
||||
try:
|
||||
text = re.sub(r"\$\{?IFS\}?", " ", re.sub(r"[\"'\\]", "", cmd or ""))
|
||||
m = _APPROVE.search(text)
|
||||
hit = bool(m and m.group(1).strip()) or bool(re.search(r"\bsisi\b", text) and _DECISION.search(text))
|
||||
# a decision has --yes or --no and names the change: an id, or something a shell would fill in
|
||||
# (a variable, a command, a brace); prose that only mentions the command (`sisi approve ID --yes`
|
||||
# in a doc, a search pattern) is not an invocation
|
||||
named = bool(re.search(r"\bP-\d+\b", text))
|
||||
hit = any(_FLAG.search(m.group(1)) and (named or re.search(r"[${`]", m.group(1)))
|
||||
for m in _APPROVE.finditer(text)) \
|
||||
or bool(re.search(r"\bsisi\b", text) and _DECISION.search(text))
|
||||
if not hit:
|
||||
return
|
||||
pid = re.search(r"\bP-\d+\b", text)
|
||||
|
||||
@@ -32,7 +32,7 @@ class Replay(Base):
|
||||
before, after = self.fixed_repo()
|
||||
self.recorded(before, after)
|
||||
cmd = "sh -c 'echo ran > %s; grep -q fixed f.txt'" % self.marker
|
||||
p = self.cycle_file([self.cmd_action(cmd)])
|
||||
p = self.cycle_file([self.cmd_action(cmd, before=before, after=after)]) # the file's own commits: ignored
|
||||
r = self.sisi("cycle", "check", p)
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
self.assertFalse(os.path.exists(self.marker), "check executed a command from the file")
|
||||
@@ -175,6 +175,11 @@ class Approval(Base):
|
||||
"sisi${IFS}approve${IFS}P-0001${IFS}--yes",
|
||||
"cd x; sisi approve P-0001 --yes; echo done",
|
||||
"a=approve; sisi $a P-0001 --yes",
|
||||
"ID=P-0003; sisi approve $ID --yes",
|
||||
'sisi approve "$1" --no',
|
||||
"sisi approve $(echo P-0001) --yes",
|
||||
"sisi approve P-000{1,2} --yes",
|
||||
"sisi approve docs; sisi approve $X --yes",
|
||||
]
|
||||
for cmd in wrapped:
|
||||
with self.subTest(cmd=cmd):
|
||||
@@ -189,7 +194,10 @@ class Approval(Base):
|
||||
"""R-14.3-2 (item 3a): `sisi approve` with no arguments is the listing; nothing else is touched."""
|
||||
for cmd in ("sisi approve", "/path/to/bin/sisi approve", "sisi approve | head -20", "python3 -m sisi approve;",
|
||||
"sisi cycle apply f.json", "sisi cycle check c.json", "echo approve the plan",
|
||||
"git commit -m 'approve the design'", "ls"):
|
||||
"git commit -m 'approve the design'", "ls",
|
||||
# prose that only mentions the command is not an invocation
|
||||
'grep -rn "sisi approve" docs', "grep -n 'sisi approve ID --yes|--no' SECURITY.md",
|
||||
"python3 - <<'E'\nprint('give him: sisi approve ID --yes')\nE"):
|
||||
with self.subTest(cmd=cmd):
|
||||
self.assertEqual(self.hook(cmd), 0, cmd)
|
||||
self.assertEqual(L.load_approvals(), [])
|
||||
|
||||
Reference in new issue
Block a user