M3.3: the trust dial (the agent merges approved PRs)
A project's trust.merge is operator (the default) or approved (L3, D5, #65). Set to approved, the harness, never the model, merges a PR in the PR watch once an approving review from a gitea.operators account is on its current commit, its checks pass (none at all counts; pending waits) and it's mergeable; the item then goes on as after any merge. Each reason to hold off (failed checks, conflicts, a refused merge) is said once per commit. Gitea enforces it: protection requires approvals from an operators-only whitelist and dismisses stale ones. At start, for each approved project, the agent checks by behaviour that its own unapproved merge is refused, with a throwaway branch and PR made through the API (never the workspace); if it isn't refused, the operator merges until a restart. - gitea: approvals, combined check status, a PR's head commit and mergeability, API-made branches and their deletion. - Test and bring-up repositories get the protection. - Tests: the decision (unit), the hard wall on a real Gitea (test_trust_live), an e2e test where the operator only approves, and a walk property, merges_only_approved, checked against a mutant that ignores which commit was approved. Closes #65 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPQ14L3LikyefCMFBqEbKE
This commit is contained in:
1 parent
21cbf1682c
commit
ab469b60bb
17 files changed
+733
-11
No files matched your search
@@ -138,13 +138,20 @@ def make_repo(
|
||||
"POST",
|
||||
f"/repos/{full}/branch_protections",
|
||||
# Pushes only by the operator (who sets up fixtures, as an owner
|
||||
# can); the agent's are refused, and merging needs an approval.
|
||||
# can); the agent's are refused. Merging needs an approval only
|
||||
# the operator can give, on the PR as it is now (D5, M3.3): the
|
||||
# agent may merge once it has one.
|
||||
json={
|
||||
"branch_name": "main",
|
||||
"enable_push": True,
|
||||
"enable_push_whitelist": True,
|
||||
"push_whitelist_usernames": [OWNER],
|
||||
"required_approvals": 1,
|
||||
"enable_approvals_whitelist": True,
|
||||
"approvals_whitelist_username": [OWNER],
|
||||
"dismiss_stale_approvals": True,
|
||||
"enable_merge_whitelist": True,
|
||||
"merge_whitelist_usernames": [OWNER, agent],
|
||||
},
|
||||
)
|
||||
# The labels the agent reads (D1), as a project would have them.
|
||||
|
||||
@@ -61,7 +61,9 @@ def running(
|
||||
ssh_host: SSHHost,
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
request: pytest.FixtureRequest,
|
||||
) -> Iterator[tuple[RunningLoop, InMemoryTransport, str]]:
|
||||
mode = request.node.get_closest_marker("merge")
|
||||
run = uuid.uuid4().hex[:8]
|
||||
stack = contextlib.ExitStack()
|
||||
# Kept outside pytest's temp directories, which pytest prunes, so a
|
||||
@@ -80,6 +82,7 @@ def running(
|
||||
push_key=fresh.push_key,
|
||||
workspace=Path(f"/tmp/nisse-e2e-{run}/sandbox"),
|
||||
build_host=build_host(ssh_host),
|
||||
merge=mode.args[0] if mode else "operator",
|
||||
)
|
||||
config = replace(
|
||||
live_config,
|
||||
@@ -229,6 +232,27 @@ class Operator:
|
||||
state = gitea._request("GET", f"/repos/{repo}/issues/{issue}")["state"]
|
||||
assert state == "closed"
|
||||
|
||||
def approve_and_wait(self, issue: int) -> None:
|
||||
"""Approve the item's PR, as the operator, and see the agent merge
|
||||
it and close the item (trust.merge: approved)."""
|
||||
gitea, repo = self.agent.gitea, self.agent.config.projects[0].repo
|
||||
as_operator = operator_gitea(self.agent.config.gitea_url)
|
||||
pr = loop_of(self.item(issue)).pr
|
||||
as_operator._request(
|
||||
"POST", f"/repos/{repo}/pulls/{pr}/reviews", json={"event": "APPROVED"}
|
||||
)
|
||||
deadline = time.monotonic() + DEADLINE_S
|
||||
nudges = 0
|
||||
while self.item(issue).status != "closed" and time.monotonic() < deadline:
|
||||
self.tick()
|
||||
if loop_of(self.item(issue)).wait == ANSWER:
|
||||
nudges += 1
|
||||
assert nudges <= MAX_ANSWERS, f"never closed: {self.last()}"
|
||||
self.say("It's merged; please close the item.", issue)
|
||||
self.settle()
|
||||
assert gitea._request("GET", f"/repos/{repo}/pulls/{pr}")["merged"]
|
||||
assert self.item(issue).outcome == "completed"
|
||||
|
||||
def tidy(self, issues: list[int], prs: list[int]) -> None:
|
||||
"""Leave the sandbox tidy: whatever is still open gets closed."""
|
||||
gitea, repo = self.agent.gitea, self.agent.config.projects[0].repo
|
||||
@@ -690,3 +714,28 @@ def test_a_question_leaves_the_restatement_up(
|
||||
assert loop_of(operator.item(issues[0])).started
|
||||
finally:
|
||||
operator.tidy(issues, [])
|
||||
|
||||
|
||||
@pytest.mark.merge("approved")
|
||||
def test_an_approved_pr_is_merged_by_the_agent(
|
||||
running: tuple[RunningLoop, InMemoryTransport, str],
|
||||
) -> None:
|
||||
"""M3.3: on a project set to merge approved PRs, the operator only
|
||||
approves; the agent merges and closes the item."""
|
||||
agent, transport, run = running
|
||||
operator = Operator(agent, transport)
|
||||
repo = agent.config.projects[0].repo
|
||||
as_operator = operator_gitea(agent.config.gitea_url)
|
||||
issue = None
|
||||
pr = 0
|
||||
try:
|
||||
issue = as_operator.create_issue(repo, f"vowels_{run}", task(run))
|
||||
as_operator.assign_issue(repo, issue, agent.account)
|
||||
operator.tick()
|
||||
item = operator.drive_until(MERGE, issue)
|
||||
pr = loop_of(item).pr
|
||||
operator.approve_and_wait(issue)
|
||||
said = " ".join(m.text for m in transport.outbound)
|
||||
assert f"Merged PR #{pr} after" in said
|
||||
finally:
|
||||
operator.tidy([issue] if issue is not None else [], [pr] if pr else [])
|
||||
@@ -0,0 +1,75 @@
|
||||
"""The trust dial's hard wall, on a real Gitea (D4, D5; M3.3): with the
|
||||
protection a `merge: approved` project has, the agent's own token can't
|
||||
merge its PR until an operator approves the PR as it is now; it can then;
|
||||
and a commit pushed after the approval needs approving again."""
|
||||
|
||||
import base64
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from e2e_support import fresh_repo, operator_gitea
|
||||
|
||||
from nisse_dev.config import AgentConfig
|
||||
from nisse_dev.gitea import GiteaClient, GiteaError
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
|
||||
def add_file(gitea: GiteaClient, repo: str, branch: str, path: str) -> None:
|
||||
gitea._request(
|
||||
"POST",
|
||||
f"/repos/{repo}/contents/{path}",
|
||||
json={
|
||||
"content": base64.b64encode(f"{path}\n".encode()).decode(),
|
||||
"message": f"add {path}",
|
||||
"branch": branch,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def approve(operator: GiteaClient, repo: str, pr: int) -> None:
|
||||
operator._request(
|
||||
"POST", f"/repos/{repo}/pulls/{pr}/reviews", json={"event": "APPROVED"}
|
||||
)
|
||||
|
||||
|
||||
def test_merging_needs_the_operators_approval_of_the_pr_as_it_is(
|
||||
tmp_path: Path, live_config: AgentConfig, gitea: GiteaClient
|
||||
) -> None:
|
||||
operator = operator_gitea(live_config.gitea_url)
|
||||
run = uuid.uuid4().hex[:8]
|
||||
with fresh_repo(operator, run, tmp_path / "keys", agent=gitea.whoami()) as repo:
|
||||
name = repo.name
|
||||
# The agent's PR, its branch made through the API.
|
||||
gitea.branch_with_file(
|
||||
name, base="main", branch="m-1", path="a.txt", text="a\n", message="a"
|
||||
)
|
||||
pr = gitea.open_pull_request(name, head="m-1", base="main", title="a", body="")
|
||||
with pytest.raises(GiteaError):
|
||||
gitea.merge_pull_request(name, pr.number) # no approval yet
|
||||
|
||||
approve(operator, name, pr.number)
|
||||
[approval] = gitea.approvals(name, pr.number)
|
||||
head = gitea.pull_request(name, pr.number).head_sha
|
||||
assert (approval.author, approval.commit) == (operator.whoami(), head)
|
||||
gitea.merge_pull_request(name, pr.number) # approved: the agent may
|
||||
assert gitea.pull_request(name, pr.number).merged
|
||||
|
||||
# Approved, then changed: the approval no longer stands.
|
||||
gitea.branch_with_file(
|
||||
name, base="main", branch="m-2", path="b.txt", text="b\n", message="b"
|
||||
)
|
||||
second = gitea.open_pull_request(
|
||||
name, head="m-2", base="main", title="b", body=""
|
||||
)
|
||||
approve(operator, name, second.number)
|
||||
add_file(gitea, name, "m-2", "c.txt") # a fix pushed after the approval
|
||||
after = gitea.pull_request(name, second.number).head_sha
|
||||
assert not [
|
||||
a
|
||||
for a in gitea.approvals(name, second.number)
|
||||
if a.commit == after and not a.stale
|
||||
]
|
||||
with pytest.raises(GiteaError):
|
||||
gitea.merge_pull_request(name, second.number)
|
||||
Reference in new issue
Block a user