diff --git a/orchestrator/kvb_orchestrator/approving.py b/orchestrator/kvb_orchestrator/approving.py index a910267..3a38aa5 100644 --- a/orchestrator/kvb_orchestrator/approving.py +++ b/orchestrator/kvb_orchestrator/approving.py @@ -213,18 +213,20 @@ class Approving: def requester(self, raw: dict, who: str) -> str: """Returns: - The requester a denial addresses, empty for every other item. + The agent a denial addresses: the requester the inbox showed, or + for a direct channel the claimed agent the operator picked + instead; empty for every other item. Raises: - Conflict when it is no longer the one the inbox showed the - operator, sent back as raw["to"]. + Conflict when raw["to"] is neither, the requester sent back as + raw["to"]. """ if raw["verb"] != "reject" or "channel" not in raw: return "" name = self.addressee(who)[0] - if raw["to"] != name: + if raw["to"] != name and not self.pickable(raw["channel"], raw["to"]): raise Conflict({"id": raw["id"], "status": "recipient changed", "to": name}) - return name + return raw["to"] def operator_entry(self, raw: dict, tasks: dict[str, dict]) -> dict: """Args: diff --git a/orchestrator/kvb_orchestrator/denying.py b/orchestrator/kvb_orchestrator/denying.py index 8e9f96c..8d4ec3b 100644 --- a/orchestrator/kvb_orchestrator/denying.py +++ b/orchestrator/kvb_orchestrator/denying.py @@ -132,6 +132,15 @@ class Denying: def addressee(self, who: str) -> tuple[str, str]: return requester_of(read_claims(self.council), who) + def pickable(self, channel: str, name: object) -> bool: + """Returns: + Whether the operator may address a denial over channel to name: + a direct channel and the agent of a claim with a radio address + that no other agent's claim shares. + """ + claims = read_claims(self.council) + return channel in DIRECT and isinstance(name, str) and any(claim["agent"] == name and claim["address"] for claim in claims) and requester_of(claims, name)[0] == name + def tell(self, item: dict, proof: dict | None, who: str, what: str) -> str: """Args: item: the denied inbox item: its note holds the chosen channel, diff --git a/orchestrator/kvb_orchestrator/mcp.py b/orchestrator/kvb_orchestrator/mcp.py index af5af5b..7f6161b 100644 --- a/orchestrator/kvb_orchestrator/mcp.py +++ b/orchestrator/kvb_orchestrator/mcp.py @@ -56,6 +56,7 @@ class Mcp: "notes": text_property("Text shown with the review, such as the commit table; may be empty."), "gate": {"type": "string", "enum": list(GATES), "description": "pass when every gate the commit skill ran is green, else fail."}, "score": text_property("The kvb-check-score line of the commit, one line."), + "agent": text_property("Your agent name as the radio knows it; the operator's denial and its reason go to it."), }, ), tool("review_status", "Status of a review (pending, approved, rejected, superseded: a newer review of the same worktree and branch replaced it, named by superseded_by), its tree, decision time and the passkey proof that approval.py verify --record checks.", {"id": review_id}), diff --git a/orchestrator/templates/page-deny.js b/orchestrator/templates/page-deny.js index c4f2a9d..336df51 100644 --- a/orchestrator/templates/page-deny.js +++ b/orchestrator/templates/page-deny.js @@ -1,9 +1,16 @@ const DENY_CHANNELS = [["post", true, "when its turn ends"], ["call", true, "at once, wakes it"], ["newsletter", false, "when their turn ends"], ["broadcast", false, "at once"], ["pan-pan", false, "at once, urgent"], ["security", false, "at once, as a safety notice"]]; const DENY_REASON = 2000; - const requesterOf = (item) => item.to || "nobody (recipient unclear)"; + const DIRECT_DENY = new Set(DENY_CHANNELS.filter(([, direct]) => direct).map(([id]) => id)); + const denyRecipients = (item) => [...new Set([item.to, ...state.claims.map((claim) => claim.agent)].filter((agent) => agent && agent !== HUMAN))].sort(); - function denyNow(item, channel, reason) { - return submit([{...entryOf(item, "reject", ""), channel, reason, to: item.to}], `Denial (${CLASSES[channel]}): ${short(item.title)}`); + /** + * @param {Object} item the denied inbox item. + * @param {string} channel the radio class. + * @param {string} reason the operator's reason, possibly empty. + * @param {string} to the agent a direct channel tells; a class to all keeps the item's requester. + */ + function denyNow(item, channel, reason, to) { + return submit([{...entryOf(item, "reject", ""), channel, reason, to: DIRECT_DENY.has(channel) ? to : item.to}], `Denial (${CLASSES[channel]}): ${short(item.title)}`); } /** @@ -31,20 +38,37 @@ function denySheet(item) { const name = `deny-${keyOf(item)}`; - const choices = DENY_CHANNELS.map(([id, direct, hint]) => h("label", {class: "deny-channel"}, - h("input", {type: "radio", name, value: id, checked: id === "post"}), - h("span", {}, h("strong", {}, CLASSES[id]), h("small", {}, `to ${direct ? requesterOf(item) : "all"}, ${hint}`)))); - const reason = h("textarea", {class: "field", maxlength: DENY_REASON, rows: 3, placeholder: "e.g. make the gate green first"}); + const recipient = h("select", {class: "field"}, h("option", {value: ""}, "Pick the agent to tell"), + denyRecipients(item).map((agent) => h("option", {value: agent, selected: agent === item.to}, revealed(agent)))); const confirm = h("button", {type: "button", class: "btn danger"}, icon("bi-x-lg"), "Deny"); - confirm.addEventListener("click", () => { - const picked = choices.map((label) => label.querySelector("input")).find((input) => input.checked); - closeSheet(); - denyNow(item, picked.value, reason.value); + const hints = []; + const picked = () => choices.map((label) => label.querySelector("input")).find((input) => input.checked); + const paintDeny = () => { + hints.forEach(([small, direct, hint]) => { + small.textContent = `to ${direct ? revealed(recipient.value) || "nobody yet: pick a recipient" : "all"}, ${hint}`; + }); + confirm.disabled = DIRECT_DENY.has(picked().value) && !recipient.value; + }; + const choices = DENY_CHANNELS.map(([id, direct, hint]) => { + const small = h("small", {}); + hints.push([small, direct, hint]); + return h("label", {class: "deny-channel"}, + h("input", {type: "radio", name, value: id, checked: id === "call", on: {change: paintDeny}}), + h("span", {}, h("strong", {}, CLASSES[id]), small)); }); + recipient.addEventListener("change", paintDeny); + const reason = h("textarea", {class: "field", maxlength: DENY_REASON, rows: 3, placeholder: "e.g. make the gate green first"}); + confirm.addEventListener("click", () => { + const channel = picked().value; + closeSheet(); + denyNow(item, channel, reason.value, recipient.value); + }); + paintDeny(); openSheet("Deny", [ h("div", {class: "who"}, pill(ITEM_LABELS[item.kind], KIND_TONES[item.kind]), item.agent ? person(item.agent) : null), h("p", {class: "text"}, item.title), h("fieldset", {class: "deny-channels"}, h("legend", {}, "Channel"), choices), + h("label", {class: "deny-recipient"}, "Recipient of a post or call", recipient), reasonChips(reason), h("label", {class: "deny-reason"}, "Reason (optional, the agent reads it too)", reason), h("p", {class: "muted"}, "The passkey signs the denial with channel, recipient and reason; then 3 s remain to undo it.", @@ -54,7 +78,8 @@ } function denySplit(item) { - const main = iconBtn("bi-x-lg", "Deny", "btn small danger", () => denyNow(item, "post", ""), `Deny: by post to ${requesterOf(item)}, 3 s to undo`); + const main = iconBtn("bi-x-lg", "Deny", "btn small danger", () => denyNow(item, "post", "", item.to), item.to ? `Deny: by post to ${revealed(item.to)}, 3 s to undo` : "Deny: the card names no agent; pick one under Deny via …"); + main.disabled = !item.to; const more = h("button", {type: "button", class: "btn small danger icon-only", "aria-haspopup": "dialog", ...named("Deny via …", "Deny via another channel or with a reason")}, caret()); more.addEventListener("click", (event) => { event.stopPropagation(); diff --git a/tests/e2e/hub_e2e_inbox.py b/tests/e2e/hub_e2e_inbox.py index ecdafd7..eee7b2f 100644 --- a/tests/e2e/hub_e2e_inbox.py +++ b/tests/e2e/hub_e2e_inbox.py @@ -94,10 +94,26 @@ DENY_SHEET = """(async () => {@HELPERS@@LABELS@ const row = await until(() => rowOf("git push"), 8000); press(row, "Deny via …"); const box = await until(() => document.querySelector(".sheet-box"), 4000); + const channels = all(".deny-channels input[type=radio]", box).map((input) => [input.value, input.checked]); + const hints = all(".deny-channel small", box).map(text); + const recipient = box.querySelector(".deny-recipient select"); + const deny = all("button", box).find((button) => text(button) === "Deny"); + const picked = recipient.value; + const sendable = !deny.disabled; + recipient.value = ""; + recipient.dispatchEvent(new Event("change")); + const blocked = deny.disabled; + box.querySelector(".deny-channels input[value=broadcast]").click(); + const toAll = !deny.disabled; + box.querySelector(".deny-channels input[value=call]").click(); + recipient.value = picked; + recipient.dispatchEvent(new Event("change")); return { title: text(box.querySelector(".panel-title")), - channels: all(".deny-channels input[type=radio]", box).map((input) => [input.value, input.checked]), - hints: all(".deny-channel small", box).map(text), + channels, + hints, + recipient: picked, + gate: [sendable, blocked, toAll, !deny.disabled], reason: Boolean(box.querySelector("textarea")), unlabelled: unlabelled(box), wide: wide(), diff --git a/tests/e2e/test_hub_inbox_e2e.py b/tests/e2e/test_hub_inbox_e2e.py index 17a6836..bd4cbcd 100644 --- a/tests/e2e/test_hub_inbox_e2e.py +++ b/tests/e2e/test_hub_inbox_e2e.py @@ -102,8 +102,9 @@ class TestHubInbox(base.OrchestratorE2ECase): self.layout(probe) self.assertTrue(probe["console"].startswith("doneExit code 0"), probe) self.assertEqual(sheet["title"], "Deny") - self.assertEqual(sheet["channels"], [["post", True], ["call", False], ["newsletter", False], ["broadcast", False], ["pan-pan", False], ["security", False]]) + self.assertEqual(sheet["channels"], [["post", False], ["call", True], ["newsletter", False], ["broadcast", False], ["pan-pan", False], ["security", False]]) self.assertEqual(sheet["hints"][:3], ["to Skills, when its turn ends", "to Skills, at once, wakes it", "to all, when their turn ends"]) + self.assertEqual((sheet["recipient"], sheet["gate"]), ("Skills", [True, True, True, True])) self.assertEqual((sheet["reason"], sheet["unlabelled"], sheet["wide"]), (True, [], False)) denied, gone, accepted = rest[6:] self.assertNotIn("error", denied, denied) diff --git a/tests/test_hub_deny.py b/tests/test_hub_deny.py index eb10d85..911f872 100644 --- a/tests/test_hub_deny.py +++ b/tests/test_hub_deny.py @@ -10,7 +10,7 @@ import unittest from unittest import mock from hub_case import HubCase -from hub_support import ASK, CLIENT, PAGE, Key, key, signing, verifier +from hub_support import ASK, CLIENT, PAGE, TREE, Key, key, signing, verifier from kvb_orchestrator import batches, denying, skill_scripts PUSH = "git push" @@ -196,6 +196,30 @@ class TestHubDeny(HubCase): self.assertIn("recipient changed", answer.decode()) self.assertEqual((self.ask_status(ask_id, "0"), self.radio()), ("pending", [])) + def test_the_operator_picks_the_recipient_of_a_review_that_names_no_agent(self): + self.claim("s1", "Skills session") + self.claim("s2", "Docs session") + review_id = self.tool("review_request", self.arguments("feat: nameless", "", self.page("bundle.html", PAGE)))["structuredContent"]["id"] + entry = self.denial("review", review_id, "", "call", "rebase first") + self.assertEqual(entry["to"], "") + for to, channel in (("Ghost", "call"), ("Docs", "broadcast"), ("", "post")): + with self.subTest(to=to, channel=channel): + status, _, answer = self.post_json("/api/sign", {"items": [{**entry, "to": to, "channel": channel}]}, self.guard(self.host)) + if to: + self.assertEqual(status, 409, answer) + self.assertIn("recipient changed", answer.decode()) + else: + self.assertEqual(status, 200, answer) + picked = {**entry, "to": "Docs"} + subject = signing.review_item(review_id, "/repo/.git", "/repo/.worktrees/feature/x", "feature/x", TREE) + self.assertEqual(self.prepare([picked])["payload"]["items"], [signing.item_hash(signing.denial_item(subject, "call", "rebase first", "Docs"))]) + with mock.patch.object(batches, "UNDO", 0.05): + self.assertEqual(self.approve([picked], self.signed([picked], self.pin()))[0], 202) + self.settle(lambda: self.radio() != []) + sent = self.radio()[-1] + self.assertEqual(sent[:4], ["send", "call", "--to", "Docs session"]) + self.assertTrue(sent[-1].endswith("Denied by the operator: commit feat: nameless on feature/x (from an agent). Reason: rebase first"), sent[-1]) + def test_a_claim_planted_before_signing_leaves_a_direct_denial_without_recipient(self): self.claim("s1", "Skills session") self.write_council(PLANTED) diff --git a/tests/test_hub_mcp.py b/tests/test_hub_mcp.py index b36ab99..fcde0dd 100644 --- a/tests/test_hub_mcp.py +++ b/tests/test_hub_mcp.py @@ -112,7 +112,7 @@ class TestHubMcp(HubCase): def test_mcp_requires_gate_score_and_recommended(self): tools = {entry["name"]: entry["inputSchema"] for entry in self.rpc("tools/list", {})["result"]["tools"]} self.assertEqual(tools["review_request"]["properties"]["gate"]["enum"], ["pass", "fail"]) - self.assertTrue({"gate", "score"} <= set(tools["review_request"]["required"])) + self.assertTrue({"gate", "score", "agent"} <= set(tools["review_request"]["required"])) self.assertIn("recommended", tools["decision_request"]["required"]) review_id = self.request("feat: gated", "") decision_id = self.decision("Wait")