From c3dc1b8c582a8d000dcae32648ba634cf4505f77 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 19 Nov 2026 10:14:39 +0000 Subject: [PATCH] feat(orchestrator): every entry that waits on the operator is a Match card MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match now builds asks, choices, decision rows and rule requests from the inbox items, beside commits and commands, so its card count follows the Tasks badge. A decision card shows its options with the recommended one marked; a pick is released with the same single passkey, and the hub posts the answer of a decision row to the row's agent itself and keeps the row off the stack until its question changes. A choice cannot be rejected; a rule card's accept opens its row, its reject carries the feedback. Every card shows the scores its text carries as labels, value included; outside a commit's score line only an emoji-led score counts. Answered decision rows leave the badge and show as answered in the inbox. Requirement 012 records the operator's amendment of 2026-11-19 and decision 11. ๐ŸŽฏ Fix: 92 % ๐Ÿ’ฅ Break: 4 % ๐Ÿ’ธ Tokens: โ€“ โฑ๏ธ Velocity: โ€“ ๐Ÿ›ก๏ธ Security: 100 % ๐Ÿ† Score: 88 ๐Ÿšฆ Gate: โœ… 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- docs/requirements/012-match-review.md | 24 ++- orchestrator/kvb_orchestrator/approving.py | 19 ++- orchestrator/kvb_orchestrator/choosing.py | 46 ++++++ orchestrator/kvb_orchestrator/denying.py | 3 +- orchestrator/kvb_orchestrator/hub.py | 2 + orchestrator/kvb_orchestrator/inbox.py | 6 +- orchestrator/kvb_orchestrator/matching.py | 61 +++++++- orchestrator/kvb_orchestrator/scores.py | 25 +-- orchestrator/templates/page-dom.js | 2 +- orchestrator/templates/page-inbox.js | 5 +- orchestrator/templates/page-match.js | 91 ++++++++--- tests/e2e/test_match_waiting_e2e.py | 168 +++++++++++++++++++++ tests/test_hub_match.py | 92 +++++++++-- 13 files changed, 489 insertions(+), 55 deletions(-) create mode 100644 tests/e2e/test_match_waiting_e2e.py diff --git a/docs/requirements/012-match-review.md b/docs/requirements/012-match-review.md index e33e239..6a2ceac 100644 --- a/docs/requirements/012-match-review.md +++ b/docs/requirements/012-match-review.md @@ -13,7 +13,14 @@ terminal prompts. their payload: the command, its directory, its reason and the content of every file the command reads (an issue body, a script, a list file). Rejected: staged commits only; decision rows as cards (they keep their own - pick view). + pick view). Amended by the operator 2026-11-19: every entry under Waits on + Operator is a card, so the card count equals the Tasks badge: asks, + choices, decision rows and rule requests join commits and commands. A + decision card shows its options with the recommended one marked; a pick is + that card's decision and goes out with the same single passkey, the hub + posting the answer to the row's agent. A choice cannot be rejected. A rule + card can be rejected with feedback; accepting opens its row, because the + passkey signs the rule itself. 2. **Place**: a Match view under the Tasks tab; the Tasks tab badge counts every waiting entry as 001 defines it, the cards included (operator 2026-11-17: asks and decisions stay visible in the badge). Rejected: a @@ -53,6 +60,10 @@ terminal prompts. 10. **Responsive**: the view works at 390 px and on desktop, with touch swipe and mouse drag. Reaching the hub from a phone is a separate requirement. +11. **Score labels** (operator 2026-11-19): every card shows the scores its + text carries as labels, ๐Ÿ’ฐ value included: a commit card from its score + line, every other card from its command, question or reason, where only + an emoji-led score counts, so "fix 2 tests" is no score. ## Approach @@ -136,6 +147,14 @@ flowchart TD contains the feedback. - [ ] The Tasks tab badge equals the number of waiting inbox entries, every waiting card among them. +- [ ] Every ask, choice, decision row and rule request under Waits on + Operator is a card of its kind; a decision row answered through Match + leaves the stack until its question changes, and its agent receives the + pick as a post. +- [ ] A decision card's option buttons record the pick; a choice's reject is + refused; a rule card's accept opens its row and its reject carries the + feedback. +- [ ] Every card shows the score labels its text carries, ๐Ÿ’ฐ value included. ### Tests @@ -150,3 +169,6 @@ flowchart TD feedback, open the list overview and release through a virtual WebAuthn authenticator; negative control: a decision changed after signing is refused. +- [ ] E2E: Chromium at 1920, 1366 and 390 px decides an ask, a choice, a + decision row and a rule request and releases them with one signature + (`tests/e2e/test_match_waiting_e2e.py`). diff --git a/orchestrator/kvb_orchestrator/approving.py b/orchestrator/kvb_orchestrator/approving.py index 0db39cb..5023237 100644 --- a/orchestrator/kvb_orchestrator/approving.py +++ b/orchestrator/kvb_orchestrator/approving.py @@ -4,7 +4,7 @@ import secrets import time from .asking import ask_state -from .choosing import answer_signed +from .choosing import answer_signed, answered, row_answer_signed from .cleanup import cleanup_request from .denying import ( DENIABLE, @@ -179,6 +179,8 @@ class Approving: if item["note"] is not None: return self.deny(item, proof) if item["kind"] == "operator": + if item["label"]: + return self.answer_row(item, proof) if not all(self.payload_holds(bound) for bound in item.get("bound", [])): return "refused: a payload file changed since the release" return self.queue_job(item, proof) @@ -225,6 +227,8 @@ class Approving: if row is None: raise Missing(str(raw["id"])) visible({name: row[name] for name in ("task", "command", "directory")}) + if row["check"] == "decision" and raw["verb"] == "approve": + return self.row_answer_entry(raw, row, tasks) if raw["verb"] == "approve" and not runnable(row): raise ToolError("an operator row is approved only when it holds a directory and a command") if digest(row["command"]) != raw["sha256"]: @@ -234,6 +238,19 @@ class Approving: bound = self.payload_bound(row, raw["payload"]) if "payload" in raw else [] return {**inbox_item(raw, "", row["command"], "", operator_signed(row), self.requester(raw, adder(tasks[row["task"]]))), "bound": bound} + def row_answer_entry(self, raw: dict, row: dict, tasks: dict[str, dict]) -> dict: + """Returns: + The signed item of a decision row answered with one of its option + labels: the post row_answer_signed() builds to the row's agent. + """ + if raw["label"] not in [option["label"] for option in row["options"]] or "payload" in raw: + raise ToolError("a decision row is approved with one of its option labels and no payload") + if digest(row["command"]) != raw["sha256"]: + raise Conflict({"id": row["task"], "status": "changed"}) + if answered(self.answers.get(row["task"]), row): + raise Conflict({"id": row["task"], "status": "answered"}) + return inbox_item(raw, raw["label"], row["command"], "", row_answer_signed(row, adder(tasks[row["task"]]), raw["label"]), "") + def item_of(self, raw: object, tasks: dict[str, dict]) -> dict: if not isinstance(raw, dict): raise ToolError("every item must be an object with kind, id, verb, label and sha256, and a denial also channel, reason and to") diff --git a/orchestrator/kvb_orchestrator/choosing.py b/orchestrator/kvb_orchestrator/choosing.py index a5b1038..04f3788 100644 --- a/orchestrator/kvb_orchestrator/choosing.py +++ b/orchestrator/kvb_orchestrator/choosing.py @@ -4,6 +4,8 @@ from .council import agent from .errors import ToolError from .fields import decode, expect, field from .messaging import DIRECT, EVERYONE, TO_ALL +from .records import digest +from .register import REGISTER, adder, operator_rows, read_register from .signed import signing CHOICE_REQUEST = "send me your open decisions as choices" @@ -29,6 +31,25 @@ def answer_signed(decision: dict, label: str) -> dict: return signing.message_item(agent(decision["agent"]), ANSWER_CLASS, answer_text(decision, label), "", "", "") +def row_answer_signed(row: dict, to: str, label: str) -> dict: + """Args: + row: an open decision row of the register. + to: the row's adding agent. + label: one of the row's option labels. + + Returns: + The post to that agent that a Match pick signs, in the words the + inbox's own answer sends. + """ + if not to or EVERYONE in to: + raise ToolError(f"a decision row is answered only when an agent other than {EVERYONE} holds role A on it") + return signing.message_item(agent(to), ANSWER_CLASS, f"Decision {row['task']}\nAnswer: {label}", "", "", "") + + +def answered(record: dict | None, row: dict) -> bool: + return record is not None and record["sha256"] == digest(row["command"]) + + def recipients(payload: dict) -> list[str]: """Args: payload: the page's class and to: 1 to RECIPIENT_LIMIT distinct @@ -98,3 +119,28 @@ class Choosing: run = self.radioed(message, CHANNELS[ANSWER_CLASS]) where = f"{ANSWER_CLASS} to {message['address']}" return f"decided ยท {where}" if run["code"] == 0 else f"decided ยท {where} failed ({run['code']})" + + def answer_row(self, item: dict, proof: dict) -> str: + """Args: + item: a checked operator item of a decision row whose label is + the picked option and whose signed is row_answer_signed(). + proof: the relayed signature, verified here once, as the hub + sends the answer itself. + + Returns: + The outcome words for the stream; the row stays open until its + agent closes it, while answers keeps it off the Match stack. + """ + tasks = read_register(self.council / REGISTER) + row = next((row for row in operator_rows(tasks) if row["task"] == item["id"]), None) + if row is None or item["label"] not in [option["label"] for option in row["options"]] or row_answer_signed(row, adder(tasks[item["id"]]), item["label"]) != item["signed"]: + return "refused: the row changed since the release" + self.operator_proven(proof, item["signed"]) + with self.answers.changed: + if answered(self.answers.get(item["id"]), row): + return "already answered" + message = self.stored_message(item["signed"], proof, None) + self.answers.insert(item["id"], {"sha256": digest(row["command"]), "label": item["label"], "message": message["id"]}) + run = self.radioed(message, CHANNELS[ANSWER_CLASS]) + where = f"{ANSWER_CLASS} to {message['address']}" + return f"answered ยท {where}" if run["code"] == 0 else f"answered ยท {where} failed ({run['code']})" diff --git a/orchestrator/kvb_orchestrator/denying.py b/orchestrator/kvb_orchestrator/denying.py index c993e8c..fdc629d 100644 --- a/orchestrator/kvb_orchestrator/denying.py +++ b/orchestrator/kvb_orchestrator/denying.py @@ -84,7 +84,8 @@ class Denying: if run["code"] != 0: return f"failed: {run['command']} โ†’ {run['code']}" command, directory = row["command"][:200], row["directory"] - return f"stopped ยท {self.tell(item, proof, adder(tasks[item['id']]), f'operator command {command} in {directory}')}" + what = f"decision {item['id']}: {command}" if row["check"] == "decision" else f"operator command {command} in {directory}" + return f"stopped ยท {self.tell(item, proof, adder(tasks[item['id']]), what)}" if item["kind"] == "review": record, changed = self.reviews.decide(item["id"], {"status": "rejected", "proof": proof, **note}) if record is None: diff --git a/orchestrator/kvb_orchestrator/hub.py b/orchestrator/kvb_orchestrator/hub.py index b9e7e0b..2398346 100644 --- a/orchestrator/kvb_orchestrator/hub.py +++ b/orchestrator/kvb_orchestrator/hub.py @@ -96,6 +96,7 @@ class Hub(Mcp, Reviewing, Revealing, Asking, Messaging, Choosing, Approving, Den reviews: Reviews asks: Ledger decisions: Ledger + answers: Ledger messages: Ledger rule_requests: Ledger uploads: Path @@ -235,6 +236,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta reviews=reviews, asks=Sealed(state / "asks.json", reviews.pages, SEALED_SUFFIX["ask"]), decisions=Ledger(state / "decisions.json"), + answers=Ledger(state / "answers.json"), messages=messages, rule_requests=Ledger(state / "rule-requests.json"), uploads=uploads, diff --git a/orchestrator/kvb_orchestrator/inbox.py b/orchestrator/kvb_orchestrator/inbox.py index c3c9b47..af50f8b 100644 --- a/orchestrator/kvb_orchestrator/inbox.py +++ b/orchestrator/kvb_orchestrator/inbox.py @@ -3,6 +3,7 @@ from __future__ import annotations import time from .approving import INBOX_KINDS +from .choosing import answered from .council import read_claims from .denying import requester_of from .errors import ToolError @@ -20,7 +21,7 @@ SNOOZE_LIMIT = 8 * 86400 class Inbox: def inbox(self, now: float, tasks: dict[str, dict], directory: Directory) -> list[dict]: claims = read_claims(self.council) - base = {"agent": "", "to": "", "detail": "", "green": False, "gate": "", "score": "", "warnings": [], "options": [], "recommended": "", "check": "", "command": "", "directory": "", "sha256": "", "runnable": False, "deniable": False, "job": None, "rule": None, "dry_run": None, "diff": None, "settled": False} + base = {"agent": "", "to": "", "detail": "", "green": False, "gate": "", "score": "", "warnings": [], "options": [], "recommended": "", "check": "", "command": "", "directory": "", "sha256": "", "runnable": False, "deniable": False, "job": None, "rule": None, "dry_run": None, "diff": None, "settled": False, "answered": ""} items = [] for review in self.reviews.pending(): created = iso_epoch(review["created"]) or now @@ -42,7 +43,8 @@ class Inbox: rows = [row for row in operator_rows(tasks) if pattern is not None and pattern.fullmatch(row["task"])] for row in rows: busy = row["task"] in held - fields = {"title": row["command"], "detail": row["why"], "agent": adder(tasks[row["task"]]), "to": requester_of(claims, adder(tasks[row["task"]]))[0], "command": row["command"], "directory": row["directory"], "sha256": digest(row["command"]), "check": row["check"], "options": row["options"], "recommended": row["recommended"]} + answer = self.answers.get(row["task"]) + fields = {"answered": answer["label"] if answered(answer, row) else "", "title": row["command"], "detail": row["why"], "agent": adder(tasks[row["task"]]), "to": requester_of(claims, adder(tasks[row["task"]]))[0], "command": row["command"], "directory": row["directory"], "sha256": digest(row["command"]), "check": row["check"], "options": row["options"], "recommended": row["recommended"]} state = {"runnable": runnable(row) and not busy, "deniable": not busy, "job": console_of(consoles.get(row["task"]))} items.append({**base, "kind": "operator", "id": row["task"], "ts": self.watch.first(row["task"], now), **fields, **state}) listed = {row["task"] for row in rows} diff --git a/orchestrator/kvb_orchestrator/matching.py b/orchestrator/kvb_orchestrator/matching.py index 573ac5f..6fb0063 100644 --- a/orchestrator/kvb_orchestrator/matching.py +++ b/orchestrator/kvb_orchestrator/matching.py @@ -1,21 +1,23 @@ from __future__ import annotations +import json import time -from .council import read_claims +from .council import NEWS, read_claims from .denying import requester_of from .diffs import stored_diff from .errors import Conflict, Missing, ToolError from .fields import decode, expect, field from .payloads import denied_roots, payload_files, payload_hash from .previewing import merge_hash -from .records import digest, iso_epoch +from .records import digest, iso_epoch, read_jsonl from .register import REGISTER, adder, operator_rows, read_register from .scores import metric_chips, score_labels from .signed import signing from .tree import inside OPERATOR = "operator/" +WAITING = ("ask", "decision", "rule") def repository_of(common_dir: str) -> str: @@ -57,7 +59,13 @@ class Matching: scan = self.tree.get() for row in rows: agent = adder(tasks[row["task"]]) - cards.append({**self.command_card(row, agent, born_of(agent, names, claims), roots, scan), "to": requester_of(claims, agent)[0]}) + cards.append({**self.command_card(row, agent, born_of(agent, names, claims), roots, scan, labels), "to": requester_of(claims, agent)[0]}) + items = self.inbox(now, tasks, self.directory(claims, read_jsonl(self.council / NEWS), now)) + cards += [self.waiting_card(item, born_of(item["agent"], names, claims), scan, labels) for item in items if self.waits(item)] + asked = {row["task"] for row in operator_rows(tasks) if row["check"] == "decision"} + for answer in self.answers.listed(): + if answer["id"] not in asked: + self.answers.remove(answer["id"]) listed = {card["id"] for card in cards} for store in (self.match_snapshots, self.merge_previews): for snapshot in store.listed(): @@ -76,7 +84,7 @@ class Matching: place = {"repo": repository_of(review.get("repo", "")), "branch": review["branch"], "worktree": review["worktree"]} return { "id": f"review:{review['id']}", "kind": "commit", "ref": review["id"], "agent": review.get("agent", ""), "born": born, "created_ts": iso_epoch(review["created"]), - **place, "title": review["title"], "score": review.get("score", ""), "metrics": metric_chips(review.get("score", ""), labels), "gate": review.get("gate", ""), + **place, "title": review["title"], "score": review.get("score", ""), "metrics": metric_chips(review.get("score", ""), labels, True), "gate": review.get("gate", ""), "hash": review["tree"], "live_hash": live or review["tree"], "stale": bool(reason), "stale_reason": reason, "files": files, "command": "", "directory": review["worktree"], "reason": "", } @@ -93,7 +101,48 @@ class Matching: files = payload_files(row["command"], row["directory"], roots) return files, payload_hash(row["command"], row["directory"], files), None - def command_card(self, row: dict, agent: str, born: float | None, roots: list[str] | None, scan: dict) -> dict: + def waits(self, item: dict) -> bool: + """Returns: + Whether an inbox item is a Match card of its own: an ask, a + choice, a rule request, or a decision row not yet answered with + its current question, none held by a release in its undo window; + commits and commands have their own cards. + """ + if item["held"]: + return False + if item["kind"] == "operator": + return item["check"] == "decision" and not item["settled"] and item["job"] is None and not item["answered"] + return item["kind"] in WAITING + + def waiting_card(self, item: dict, born: float | None, scan: dict, labels: dict | None) -> dict: + """Args: + item: an inbox item waits() keeps. + born: when its agent was first seen. + scan: the branch tree. + labels: the score labels. + + Returns: + Its card: kind ask, decision (a choice or a decision row) or rule; + entry names the inbox kind a release signs, metrics the scores + its question, command or reason carries. + """ + files = [] + if item["kind"] == "ask" and item["diff"]: + try: + files = stored_diff(self.reviews.pages, "ask", item["id"])["diff"] + except Missing: + files = None + repo, branch, worktree = place_of(item["directory"], scan) if item["directory"] else ("", "", "") + shown = digest(json.dumps([item["title"], item["command"], item["detail"], item["options"], item["rule"]], sort_keys=True)) + return { + "id": f"{item['kind']}:{item['id']}", "kind": "decision" if item["kind"] == "operator" else item["kind"], "entry": item["kind"], "ref": item["id"], "agent": item["agent"], "to": item["to"], + "born": born, "created_ts": item["ts"], "repo": repo, "branch": branch, "worktree": worktree, "title": item["title"], "score": "", + "metrics": metric_chips("\n".join((item["title"], item["command"], item["detail"])), labels, False), "gate": "", "hash": shown, "live_hash": shown, "stale": False, "stale_reason": "", + "files": files, "command": item["command"], "directory": item["directory"], "reason": item["detail"], "sha256": item["sha256"], "options": item["options"], + "recommended": item["recommended"], "warnings": item["warnings"], "rule": item["rule"], "dry_run": item["dry_run"], + } + + def command_card(self, row: dict, agent: str, born: float | None, roots: list[str] | None, scan: dict, labels: dict | None) -> dict: card_id = f"operator:{row['task']}" files, live, preview = self.card_payload(row, roots) snapshot = self.match_snapshots.get(card_id) @@ -106,7 +155,7 @@ class Matching: repo, branch, worktree = place_of(row["directory"], scan) return { "id": card_id, "kind": "command", "ref": row["task"], "agent": agent, "born": born, "created_ts": snapshot["taken"], "repo": repo, "branch": branch, "worktree": worktree, - "title": row["command"], "score": "", "metrics": [], "gate": "", "hash": snapshot["hash"], "live_hash": live, "stale": stale, + "title": row["command"], "score": "", "metrics": metric_chips(f"{row['command']}\n{row['why']}", labels, False), "gate": "", "hash": snapshot["hash"], "live_hash": live, "stale": stale, "stale_reason": moved if stale else "", "files": snapshot["files"], "command": row["command"], "directory": row["directory"], "reason": row["why"], "sha256": digest(row["command"]), "merge": merge, } diff --git a/orchestrator/kvb_orchestrator/scores.py b/orchestrator/kvb_orchestrator/scores.py index 25b7457..536030d 100644 --- a/orchestrator/kvb_orchestrator/scores.py +++ b/orchestrator/kvb_orchestrator/scores.py @@ -9,7 +9,8 @@ SCORES = "scores.jsonl" LABELS = "kvb-check-score/labels.json" FILE_LIMIT = 64 << 20 VARIATION = "๏ธ" -VALUE = r"([0-9]+(?:\.[0-9]+)?(?:\s*%)?|โ€“|-|new)(?![0-9.])" +VALUE = r"([0-9]+(?:\.[0-9]+)?(?:\s*%|:[0-9]+(?:\.[0-9]+)?)?|โ€“|-|new)(?![0-9.])" +CHIP_EXTRAS = ("value",) def checked(labels: object) -> dict | None: @@ -61,23 +62,29 @@ def score_labels(skills: Path) -> dict | None: return checked(read_json(skills / LABELS)) -def metric_chips(score: str, labels: dict | None) -> list[dict]: +def metric_chips(score: str, labels: dict | None, bare: bool) -> list[dict]: """Args: score: an agent's free score line, such as line 1 or the message - block of score.py. + block of score.py, or a free text that may carry one. labels: the label file as checked() returns it, or None. + bare: whether a label without its emoji counts; False for a free + text, where "fix 2 tests" is no score. Returns: - In label order, per metric whose emoji (with or without its - variation selector) or label or former label stands right before a - value in score, the first such value: {"id", "emoji", "label", - "value"}; none without labels. + In label order, then the CHIP_EXTRAS, per metric whose emoji (with or + without its variation selector) or label or former label stands + right before a value in score (the label alone only when bare), the + first such value: {"id", "emoji", "label", "value"}; none without + labels. """ found = [] - for key, metric in (labels or {}).get("metrics", {}).items(): + extras = (labels or {}).get("extras") + shown = [(key, extras[key]) for key in CHIP_EXTRAS if isinstance(extras, dict) and checked({"metrics": {key: extras.get(key)}})] + for key, metric in [*(labels or {}).get("metrics", {}).items(), *shown]: emoji = re.escape(metric["emoji"].removesuffix(VARIATION)) + f"{VARIATION}?" names = "|".join(re.escape(name) for name in (metric["label"], *metric.get("former", []))) - match = re.search(rf"(?:{emoji}\s*(?:(?:{names})\s*:?\s*)?|\b(?:{names})\s*:?\s*){VALUE}", score, re.IGNORECASE) + alone = rf"|\b(?:{names})\s*:?\s*" if bare else "" + match = re.search(rf"(?:{emoji}\s*(?:(?:{names})\s*:?\s*)?{alone}){VALUE}", score, re.IGNORECASE) if match: found.append({"id": key, "emoji": metric["emoji"], "label": metric["label"], "value": " ".join(match.group(1).split())}) return found diff --git a/orchestrator/templates/page-dom.js b/orchestrator/templates/page-dom.js index d150a89..dc18ce8 100644 --- a/orchestrator/templates/page-dom.js +++ b/orchestrator/templates/page-dom.js @@ -54,7 +54,7 @@ const alarmRows = () => state.radio.filter((row) => (row.class === "mayday" || row.class === "pan-pan") && row.ts >= state.alarm_since).reverse(); const keyOf = (item) => `${item.kind}:${item.id}`; const visibleInbox = () => state.inbox.filter((item) => ui.snoozed || !item.until); - const waitingCount = () => state.inbox.filter((item) => !item.until).length; + const waitingCount = () => state.inbox.filter((item) => !item.until && !item.answered).length; const isGreen = (item) => item.green && !item.held && !item.until && item.kind !== "operator"; const selectable = (item) => !item.held && item.kind !== "rule" && !(item.kind === "review" && item.gate !== "pass") && !(item.kind === "operator" && !item.runnable) && !(item.kind === "decision" && !item.recommended); const entryOf = (item, verb, label) => ({kind: item.kind, id: item.id, verb, label, sha256: item.sha256 || ""}); diff --git a/orchestrator/templates/page-inbox.js b/orchestrator/templates/page-inbox.js index 5cc64d6..54838f6 100644 --- a/orchestrator/templates/page-inbox.js +++ b/orchestrator/templates/page-inbox.js @@ -107,6 +107,7 @@ const chooseOne = (item, label) => submit([entryOf(item, "approve", label)], `Choice: ${label}`); const asked = (item) => item.kind === "operator" && item.check === "decision"; const answers = new Map(); + const answerOf = (item) => answers.get(keyOf(item)) || item.answered || ""; async function answerRow(item, answer) { if (!item.agent) { @@ -133,7 +134,7 @@ } function choicePanel(item, pick, other) { - const done = answers.has(keyOf(item)); + const done = Boolean(answerOf(item)); const pane = item.options.some((option) => option.preview) ? h("pre", {class: "choice-preview", "aria-label": "Preview"}) : null; const show = (option) => { if (pane) { @@ -280,7 +281,7 @@ h("div", {class: "msg-meta"}, when(item.ts), asked(item) ? pill("Decision", KIND_TONES.decision) : pill(ITEM_LABELS[item.kind], KIND_TONES[item.kind]), - answers.has(keyOf(item)) ? pill(revealed(`answered: ${answers.get(keyOf(item))}`), "ok") : null, + answerOf(item) ? pill(revealed(`answered: ${answerOf(item)}`), "ok") : null, item.kind === "review" ? pill(item.gate === "pass" ? "Gate green" : "Gate red", item.gate === "pass" ? "ok" : "bad") : null, item.warnings.map((warning) => pill(warning, "bad")), item.pinned ? pill("pinned", "violet") : null, diff --git a/orchestrator/templates/page-match.js b/orchestrator/templates/page-match.js index 19095e1..d72cbb9 100644 --- a/orchestrator/templates/page-match.js +++ b/orchestrator/templates/page-match.js @@ -2,7 +2,7 @@ const MATCH_PEEKS = 2; const LONG_PRESS = 550; const PRESS_SLOP = 10; - const MATCH_KINDS = {commit: ["Commit", "info", "bi-git"], command: ["Command", "warn", "bi-terminal"]}; + const MATCH_KINDS = {commit: ["Commit", "info", "bi-git"], command: ["Command", "warn", "bi-terminal"], ask: ["Ask", "warn", "bi-shield-lock"], decision: ["Decision", "info", "bi-signpost-split"], rule: ["Rule", "neutral", "bi-shield-check"]}; const MATCH_VERDICTS = {accept: ["Accept", "ok", "bi-check-lg"], reject: ["Reject", "bad", "bi-x-lg"]}; const SWIPE_SHARE = 0.25; const SWIPE_UP = 80; @@ -70,7 +70,9 @@ return h("tr", {class: "match-mark", "data-line": mark.line}, h("td", {colspan: columns}, icon("bi-chat-square-text"), h("span", {class: "match-mark-text"}, revealed(mark.comment)))); } - const patched = (card) => card.kind === "commit" || Boolean(card.merge); + const patched = (card) => card.kind === "commit" || card.kind === "ask" || Boolean(card.merge); + const pickOnly = (card) => card.kind === "decision" && card.entry === "decision"; + const flies = (card) => !card.stale && card.kind !== "rule" && (card.kind !== "decision" || Boolean(card.recommended)); function matchFileRows(card, file) { const diffed = patched(card); @@ -202,7 +204,25 @@ card.stale ? h("p", {class: "match-stale"}, icon("bi-exclamation-triangle"), revealed(card.stale_reason)) : null); } - function matchCommand(card) { + function matchOptions(card) { + const kept = matchView.decisions.get(cardKey(card)); + return h("div", {class: "options match-options", role: "group", "aria-label": "Options"}, card.options.map((option) => h("button", { + type: "button", class: `option${option.label === card.recommended ? " recommended" : ""}`, "aria-pressed": String(Boolean(kept && kept.label === option.label)), + title: revealed(`${option.label}: picked now, sent to ${card.agent || "its agent"} as a post at the release`), on: {click: () => decide(card, "accept", "", option.label)}, + }, h("strong", {}, revealed(option.label)), option.description ? h("span", {}, revealed(option.description)) : null))); + } + + function matchPayload(card) { + if (card.kind === "decision") { + return h("section", {class: "match-payload"}, card.reason && card.reason !== "-" ? h("p", {class: "match-reason"}, revealed(card.reason)) : null, matchOptions(card)); + } + if (card.kind === "rule") { + return h("section", {class: "match-payload"}, ruleRequestBody({...card, detail: card.reason})); + } + if (card.kind === "ask") { + return h("section", {class: "match-payload"}, h("pre", {class: "match-command"}, h("code", {}, revealed(card.command))), h("p", {class: "match-directory"}, revealed(card.directory)), + card.warnings.length ? h("p", {class: "match-warnings"}, card.warnings.map((warning) => pill(warning, "bad"))) : null); + } if (card.kind !== "command") { return null; } @@ -218,9 +238,9 @@ function matchCard(card) { const files = card.files || []; - const none = card.kind === "commit" ? "No diff shipped with this stage." : card.merge ? (card.merge.error ? "No diff to show." : "The merge brings no change.") : "Its command names no file."; - const list = files.length ? h("ol", {class: "match-files", "aria-label": `Files: ${files.length}`}, files.map((file, index) => fileBlock(card, file, index))) : h("p", {class: "muted"}, none); - const body = h("div", {class: "match-body"}, matchCommand(card), list); + const none = card.kind === "commit" ? "No diff shipped with this stage." : card.kind === "ask" ? "Its tool call changes no file." : card.merge ? (card.merge.error ? "No diff to show." : "The merge brings no change.") : "Its command names no file."; + const list = files.length ? h("ol", {class: "match-files", "aria-label": `Files: ${files.length}`}, files.map((file, index) => fileBlock(card, file, index))) : card.kind === "decision" || card.kind === "rule" ? null : h("p", {class: "muted"}, none); + const body = h("div", {class: "match-body"}, matchPayload(card), list); body.addEventListener("scroll", () => openOnScroll(body, ".match-file"), {passive: true}); const article = h("article", {class: "card match-card front", "data-card": card.id, "data-kind": card.kind, "data-stale": String(Boolean(card.stale)), "aria-label": `${card.kind} card of ${card.agent || "an agent"}`}, matchHead(card), body); @@ -275,9 +295,15 @@ window.setTimeout(land, FLY_MS); } - function decide(card, verdict, feedback) { + /** + * @param {object} card the card decided. + * @param {string} verdict accept or reject. + * @param {string} feedback the rejection text, else empty. + * @param {string} label the picked option of a decision card, else empty. + */ + function decide(card, verdict, feedback, label) { flyOff(verdict, () => { - matchView.decisions.set(cardKey(card), {verdict, feedback}); + matchView.decisions.set(cardKey(card), {verdict, feedback, label}); drawMatch(true); }); } @@ -298,17 +324,34 @@ toast("Accept: refused", [`The card is stale: ${card.stale_reason}. Add it again to decide it.`], false); return; } - decide(card, "accept", ""); + if (card.kind === "rule") { + const item = state.inbox.find((entry) => keyOf(entry) === card.id); + if (item) { + openItem(item); + } else { + toast("Accept: the rule request is gone", [], false); + } + return; + } + if (card.kind === "decision" && !card.recommended) { + toast("Accept: pick one of its options", ["The decision recommends none, so the card waits for your pick."], false); + return; + } + decide(card, "accept", "", card.kind === "decision" ? card.recommended : ""); } function rejectCard(card) { + if (pickOnly(card)) { + toast("Reject: not possible", ["A choice is answered with one of its options."], false); + return; + } const kept = matchView.decisions.get(cardKey(card)); const words = h("textarea", {class: "field match-feedback", rows: 6, maxlength: FEEDBACK_LIMIT, "aria-label": "Feedback"}); words.value = (kept && kept.verdict === "reject" ? kept.feedback : feedbackOf(card)).slice(0, FEEDBACK_LIMIT); const confirm = h("button", {type: "button", class: "btn danger match-feedback-reject"}, icon("bi-x-lg"), "Reject"); confirm.addEventListener("click", () => { closeSheet(); - decide(card, "reject", words.value.trim()); + decide(card, "reject", words.value.trim(), ""); }); const note = `${card.agent || "The agent"} gets this text by radio call at the release; nothing is sent before.`; openSheet(`Reject: ${card.title}`, [h("p", {class: "muted"}, note), words, h("div", {class: "actions"}, h("span", {class: "spacer"}), btn("Cancel", "btn ghost", closeSheet), confirm)], false); @@ -361,7 +404,7 @@ const far = SWIPE_SHARE * article.getBoundingClientRect().width; const swiped = event.type === "pointercancel" ? "" : Math.abs(dx) >= far && Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? "accept" : "reject") : -dy >= SWIPE_UP && -dy > Math.abs(dx) ? "later" : ""; article.style.removeProperty("--lean"); - if (!((swiped === "accept" && !card.stale) || swiped === "later")) { + if (!((swiped === "accept" && flies(card)) || swiped === "later")) { article.style.removeProperty("transform"); delete article.dataset.leaning; } @@ -389,17 +432,18 @@ /** * @param {object} card a decided match card. - * @param {object} decision its {verdict, feedback}. - * @returns {object} the inbox entry the release signs: an approval of the review, or of the operator row bound to - * the payload hash the card showed, or its rejection carrying the feedback as a radio call to the card's agent. + * @param {object} decision its {verdict, feedback, label}. + * @returns {object} the inbox entry the release signs: an approval of the review or the ask, the picked option of a + * decision, the operator row bound to the payload hash the card showed, or a rejection carrying the feedback as a + * radio call to the card's agent. */ function matchEntry(card, decision) { - const kind = card.kind === "commit" ? "review" : "operator"; - const entry = {kind, id: card.ref, verb: decision.verdict === "accept" ? "approve" : "reject", label: "", sha256: kind === "operator" ? card.sha256 || "" : ""}; + const kind = card.entry || (card.kind === "commit" ? "review" : "operator"); + const entry = {kind, id: card.ref, verb: decision.verdict === "accept" ? "approve" : "reject", label: decision.verdict === "accept" ? decision.label || "" : "", sha256: kind === "operator" ? card.sha256 || "" : ""}; if (decision.verdict === "reject") { return {...entry, channel: "call", reason: decision.feedback, to: card.to || ""}; } - return kind === "operator" ? {...entry, payload: card.hash} : entry; + return card.kind === "command" ? {...entry, payload: card.hash} : entry; } function decided(cards) { @@ -420,7 +464,7 @@ const [kindName, kindTone, kindIcon] = MATCH_KINDS[card.kind] || [card.kind, "neutral", "bi-collection"]; const [word, tone, mark] = MATCH_VERDICTS[decision.verdict]; const other = decision.verdict === "accept" ? "reject" : "accept"; - const flip = iconBtn(MATCH_VERDICTS[other][2], MATCH_VERDICTS[other][0], "btn small ghost match-flip", () => (other === "accept" ? acceptCard(card) : rejectCard(card)), `${MATCH_VERDICTS[other][0]} instead`); + const flip = (other === "reject" && pickOnly(card)) || (other === "accept" && !flies(card)) ? null : iconBtn(MATCH_VERDICTS[other][2], MATCH_VERDICTS[other][0], "btn small ghost match-flip", () => (other === "accept" ? acceptCard(card) : rejectCard(card)), `${MATCH_VERDICTS[other][0]} instead`); const back = iconBtn("bi-arrow-counterclockwise", "Back to the stack", "btn small ghost match-undecide", () => { matchView.decisions.delete(cardKey(card)); matchView.overview = false; @@ -428,7 +472,7 @@ }); return h("li", {class: "match-decision", "data-card": card.id, "data-verdict": decision.verdict}, h("div", {class: "row"}, face(card.agent || "-", false), h("span", {class: "pill", "data-tone": kindTone}, icon(kindIcon), kindName), h("strong", {class: "match-decision-title"}, revealed(card.title)), h("span", {class: "spacer"}), - h("span", {class: "pill match-verdict", "data-tone": tone}, icon(mark), word), decision.verdict === "reject" ? iconBtn("bi-pencil", "Edit feedback", "btn small ghost match-edit", () => rejectCard(card)) : null, flip, back), + h("span", {class: "pill match-verdict", "data-tone": tone}, icon(mark), decision.label ? revealed(`${word}: ${decision.label}`) : word), decision.verdict === "reject" ? iconBtn("bi-pencil", "Edit feedback", "btn small ghost match-edit", () => rejectCard(card)) : null, flip, back), decision.verdict === "reject" ? h("pre", {class: "match-decision-feedback"}, revealedLines(decision.feedback === "" ? "no feedback text" : decision.feedback)) : null); } @@ -458,7 +502,7 @@ }) : null; return h("section", {class: "card match-overview", "aria-label": "Decisions"}, h("h2", {class: "panel-title"}, "Decisions"), - h("p", {class: "muted"}, "Nothing has run yet. Accepted commits get the operator's signature for their exact tree, accepted commands run once as jobs under Waits on Operator, every rejection goes to its agent by radio call."), + h("p", {class: "muted"}, "Nothing has run yet. Accepted commits get the operator's signature for their exact tree, accepted commands run once as jobs under Waits on Operator, accepted asks are granted, every picked option goes to its agent as a post, every rejection goes to its agent by radio call."), picked.length ? h("ol", {class: "match-decisions"}, picked.map(([card, decision]) => overviewRow(card, decision))) : h("p", {class: "muted"}, "No decision yet."), block && picked.length ? h("p", {class: "match-release-block"}, icon("bi-exclamation-triangle"), block) : null, h("div", {class: "actions"}, back, h("span", {class: "spacer"}), release)); @@ -534,7 +578,7 @@ dom.decisions.hidden = !matchView.decisions.size || shownOverview; if (!cards.length) { matchView.front = null; - dom.stack.replaceChildren(emptyState("No card waits", "No staged commit and no operator command waits for a decision.")); + dom.stack.replaceChildren(emptyState("No card waits", "Nothing waits on the operator.")); return; } if (shownOverview) { @@ -550,8 +594,9 @@ matchView.front = front; const act = (cls, iconName, word, hint, disabled, run) => h("button", {type: "button", class: `match-act ${cls}`, disabled, ...named(word, hint), on: {click: run}}, h("span", {class: "match-act-face", "aria-hidden": "true"}, icon(iconName)), h("span", {class: "match-act-label"}, word)); - const accept = act("match-accept", "bi-check-lg", "Accept", front.stale ? `Accept: refused while the card is stale (${front.stale_reason})` : "Accept (right arrow, swipe right)", Boolean(front.stale), () => acceptCard(front)); - const reject = act("match-reject", "bi-x-lg", "Reject", "Reject with feedback (left arrow, swipe left)", false, () => rejectCard(front)); + const hint = front.stale ? `Accept: refused while the card is stale (${front.stale_reason})` : front.kind === "rule" ? "Accept: opens its row, where the passkey signs exactly this rule" : front.kind === "decision" ? (front.recommended ? `Accept: ${front.recommended} (right arrow, swipe right)` : "Accept: pick one of its options") : "Accept (right arrow, swipe right)"; + const accept = act("match-accept", "bi-check-lg", "Accept", revealed(hint), Boolean(front.stale) || (front.kind === "decision" && !front.recommended), () => acceptCard(front)); + const reject = act("match-reject", "bi-x-lg", "Reject", pickOnly(front) ? "Reject: a choice is answered with one of its options" : "Reject with feedback (left arrow, swipe left)", pickOnly(front), () => rejectCard(front)); const later = act("match-later", "bi-arrow-up", "Later", "Later: to the back of the stack (up arrow, swipe up)", !rest.length, () => laterCard(front)); const peeks = rest.length ? h("div", {class: "match-peeks", role: "group", "aria-label": `${rest.length} more cards`}, rest.map(peekTab)) : null; const behind = rest.slice(0, MATCH_PEEKS).map((card, index) => { diff --git a/tests/e2e/test_match_waiting_e2e.py b/tests/e2e/test_match_waiting_e2e.py new file mode 100644 index 0000000..49c7557 --- /dev/null +++ b/tests/e2e/test_match_waiting_e2e.py @@ -0,0 +1,168 @@ +from __future__ import annotations + +import json +import os +import shutil +import sys +import time +import unittest +from pathlib import Path +from unittest import mock + +HERE = Path(__file__).resolve().parent +sys.path[:0] = [str(HERE.parent), str(HERE)] + +import hub_e2e_kit as base +from hub_case import HubCase +from hub_support import ASK, OPTIONS, iso +from kvb_orchestrator import batches, skill_scripts + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-match-waiting-shots")) +RELEASE_AT = 1366 +SCORED = "git commit -m 'feat: x' -m '๐ŸŽฏ Fix: 95 % ยท ๐Ÿ’ฅ Break: 2 % ยท ๐Ÿ’ฐ Value: 1:4 ยท ๐Ÿ† Score: 93'" +PICK = "operator:operator/pick" +FRONT = """(async () => {@HELPERS@ + const card = () => document.querySelector("#main .match-card.front"); + if (!(await until(card, 8000))) { + return {error: text(document.getElementById("main"))}; + } + const peek = document.querySelector('#main .match-peek[data-card="@PICK@"]'); + if (peek) { + peek.click(); + } + const node = await until(() => (card() && card().dataset.card === "@PICK@" ? card() : null), 3000); + return {front: node ? node.dataset.card : null, options: node ? all(".match-options .option", node).map((option) => [text(option.querySelector("strong")), option.classList.contains("recommended")]) : [], + wide: document.documentElement.scrollWidth > window.innerWidth + 1}; +})()""" +FLOW = """(async () => {@HELPERS@ + const posted = []; + const send = window.fetch.bind(window); + window.fetch = async (path, init) => { + if (String(path) === "/api/inbox/approve") { + posted.push(JSON.parse(init.body).items.map((item) => [item.kind, item.id, item.verb, item.label, item.channel || null, item.reason || null])); + } + return send(path, init); + }; + const card = () => document.querySelector("#main .match-card.front"); + if (!(await until(card, 8000))) { + return {error: text(document.getElementById("main"))}; + } + const key = (name) => document.body.dispatchEvent(new KeyboardEvent("keydown", {key: name, bubbles: true, cancelable: true})); + const seen = {}; + for (let step = 0; step < 8 && card(); step += 1) { + const node = card(); + const id = node.dataset.card; + const acts = Object.fromEntries(all("#main .match-actions .match-act").map((button) => [text(button.querySelector(".match-act-label")), button.disabled])); + seen[id] = {kind: node.dataset.kind, chips: all(".match-metric", node).map((chip) => `${chip.dataset.metric}=${text(chip.querySelector("strong"))}`), acts, + options: all(".match-options .option", node).map((option) => text(option.querySelector("strong"))), rule: Boolean(node.querySelector(".rule-request")), command: text(node.querySelector(".match-command"))}; + if (id.startsWith("operator:")) { + node.querySelector(".match-options .option:not(.recommended)").click(); + } else if (id.startsWith("decision:")) { + key("ArrowLeft"); + seen[id].refused = Boolean(await until(() => all("#toasts .toast").map(text).find((entry) => entry.startsWith("Reject: not possible")), 3000)); + node.querySelector(".match-options .option").click(); + } else if (id.startsWith("ask:")) { + key("ArrowRight"); + } else { + key("ArrowRight"); + seen[id].sheet = Boolean(await until(() => document.querySelector(".sheet-box .rule-request"), 3000)); + document.querySelector(".sheet-box .sheet-head button").click(); + await sleep(200); + seen[id].stayed = card() && card().dataset.card === id; + key("ArrowLeft"); + const feedback = await until(() => document.querySelector(".sheet-box .match-feedback"), 3000); + feedback.value = "too broad"; + document.querySelector(".sheet-box .match-feedback-reject").click(); + } + await until(() => !card() || card().dataset.card !== id, 3000); + } + const overview = await until(() => document.querySelector("#main .match-overview"), 3000); + const rows = overview ? all("#main .match-decision").map((item) => [item.dataset.card, item.dataset.verdict, text(item.querySelector(".match-verdict"))]) : []; + const found = {seen, rows, wide: document.documentElement.scrollWidth > window.innerWidth + 1}; + if (@RELEASE@) { + document.querySelector("#main .match-release").click(); + found.toast = await until(() => all("#toasts .toast").map(text).find((entry) => entry.startsWith("Release")), 15000); + found.emptied = Boolean(await until(() => document.querySelector("#main .match-stack .empty"), 15000)); + found.posted = posted; + } + return found; +})()""" + + +@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium") +class TestMatchWaiting(HubCase): + drive = base.OrchestratorE2ECase.drive + authenticator = base.OrchestratorE2ECase.authenticator + + def setUp(self): + super().setUp() + now = time.time() + self.write_council({ + "work/s1/claim.json": {"office": "work/s1", "session": "s1", "runtime": "claude", "name": "Skills session", "since": iso(now - 86400), "beat": now, "work": "match"}, + "rasci.json": {"operator/pick": {"title": "Merge the hub now?", "workdir": "/repo", "description": "the gate is green", "check": "decision", "options": OPTIONS, "recommended": "Merge now", "roles": {"Skills": "A", "Operator": "R"}}}, + }) + self.ask = self.create_ask({**ASK, "command": SCORED, "timeout": 600}) + decided = self.tool("decision_request", {"question": "Name the release?", "options": OPTIONS, "agent": "Skills", "recommended": ""}) + self.assertFalse(decided["isError"], decided) + self.choice = decided["structuredContent"]["id"] + rule = {"kind": "ask", "tool": "Bash", "pattern": "make test*", "scope": "/repo*", "expires": int(now) + 86400, "agent": "Skills", "why": "the gate runs make test"} + requested = self.tool("rule_request", rule) + self.assertFalse(requested["isError"], requested) + self.rule = requested["structuredContent"]["id"] + + def radio(self) -> list[list[str]]: + return [args for script, args in self.scripts.calls if script == skill_scripts.RADIO] + + def steps(self) -> list[dict]: + OUT.mkdir(parents=True, exist_ok=True) + steps = [] + for width, height in sorted(base.WIDTHS, key=lambda size: size[0] == RELEASE_AT): + url = f"http://localhost:{self.port}/?{width}#tasks/match" + front = FRONT.replace("@HELPERS@", base.HELPERS).replace("@PICK@", PICK) + flow = FLOW.replace("@HELPERS@", base.HELPERS).replace("@RELEASE@", "true" if width == RELEASE_AT else "false") + steps.append({"width": width, "height": height, "url": url, "wait": 1500, "probe": front, "shot": str(OUT / f"chromium-{width}-decision.png")}) + steps.append({"width": width, "height": height, "url": url, "wait": 1500, "probe": flow, "shot": str(OUT / f"chromium-{width}-overview.png")}) + return steps + + def check_flow(self, found: dict) -> None: + self.assertNotIn("error", found, found) + seen = found["seen"] + ask, choice, rule = f"ask:{self.ask}", f"decision:{self.choice}", f"rule:{self.rule}" + self.assertEqual(sorted(seen), sorted([PICK, ask, choice, rule]), found) + self.assertEqual({card_id: entry["kind"] for card_id, entry in seen.items()}, {PICK: "decision", ask: "ask", choice: "decision", rule: "rule"}) + self.assertEqual(seen[ask]["chips"], ["fix=95 %", "break=2 %", "score=93", "value=1:4"], found) + self.assertEqual(seen[ask]["command"], SCORED) + self.assertEqual((seen[PICK]["options"], seen[PICK]["acts"]["Accept"], seen[PICK]["acts"]["Reject"]), (["Merge now", "Wait"], False, False), found) + self.assertEqual((seen[choice]["acts"]["Accept"], seen[choice]["acts"]["Reject"], seen[choice]["refused"]), (True, True, True), found) + self.assertEqual((seen[rule]["rule"], seen[rule]["sheet"], seen[rule]["stayed"]), (True, True, True), found) + expected = [[PICK, "accept", "Accept: Wait"], [ask, "accept", "Accept"], [choice, "accept", "Accept: Merge now"], [rule, "reject", "Reject"]] + self.assertEqual(sorted(found["rows"]), sorted(expected), found) + self.assertFalse(found["wide"], found) + + def test_chromium_decides_every_waiting_kind_and_releases_them_with_one_signature(self): + with mock.patch.object(batches, "UNDO", 0.3): + data = self.drive(self.steps(), self.authenticator(), "cdp.mjs", os.environ.get("CHROMIUM", "chromium")) + self.settle(lambda: self.ask_status(self.ask, "0") == "allowed") + (OUT / "chromium-waiting.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.assertEqual([error for error in data["errors"] if "(style-src-elem)" not in error and "/favicon.ico" not in error], []) + fronts, flows = data["results"][0::2], data["results"][1::2] + for front in fronts: + with self.subTest(front=front): + self.assertEqual((front["front"], front["options"], front["wide"]), (PICK, [["Merge now", True], ["Wait", False]], False), front) + for found in flows: + with self.subTest(release="toast" in found): + self.check_flow(found) + released = flows[-1] + self.assertTrue(released["toast"].startswith("Release: 4 decisions"), released) + self.assertTrue(released["emptied"], released) + items = sorted(released["posted"][0]) + self.assertEqual(items, sorted([["operator", "operator/pick", "approve", "Wait", None, None], ["ask", self.ask, "approve", "", None, None], ["decision", self.choice, "approve", "Merge now", None, None], ["rule", self.rule, "reject", "", "call", "too broad"]])) + self.settle(lambda: len(self.radio()) == 3) + told = sorted(sent[-1].split("] ", 1)[1] for sent in self.radio()) + self.assertEqual(told[1], "Decision operator/pick Answer: Wait") + self.assertTrue(told[0].startswith(f"Choice for decision {self.choice} ") and told[2].startswith("Denied by the operator: rule request"), told) + self.assertEqual(self.tool("decision_wait", {"id": self.choice, "timeout_seconds": 0})["structuredContent"]["choice"], "Merge now") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hub_match.py b/tests/test_hub_match.py index 5935ae0..7303744 100644 --- a/tests/test_hub_match.py +++ b/tests/test_hub_match.py @@ -12,12 +12,14 @@ from pathlib import Path from unittest import mock from hub_case import HubCase -from hub_support import APPROVAL, GIT_ENV, HUB, REPO_ROOT, key, signing +from hub_support import APPROVAL, ASK, GIT_ENV, HUB, OPTIONS, REPO_ROOT, key, signing from kvb_orchestrator import batches, payloads, scores, skill_scripts MATCH = REPO_ROOT / "skills" / "kvb-match" / "scripts" / "match.py" CLIENT = HUB / "client" FEEDBACK = "app.py:2: name it after what it holds\nnotes.md:3: say why" +PICK = {"title": "Merge the hub now?", "workdir": "/repo", "description": "the gate is green", "check": "decision", "options": OPTIONS, "recommended": "Merge now", "roles": {"Skills": "A", "Operator": "R"}} +SCORED = "git commit -m 'fix 2 tests' -m '๐ŸŽฏ Fix: 95 %\n๐Ÿ’ฅ Break: 2 %\n๐Ÿ’ฐ Value: 1:4\n๐Ÿ† Score: 93'" TOLD = "app.py:2: name it after what it holds notes.md:3: say why" @@ -344,9 +346,10 @@ class TestCommandCards(MatchCase): self.assertFalse(fresh["stale"], fresh) self.assertIn("swapped", next(entry["content"] for entry in fresh["files"] if entry["path"].endswith("body.md"))) - def test_decision_rows_and_rows_at_the_runner_are_no_cards(self): - self.write_council({"rasci.json": {"operator/pick": {"title": "-", "workdir": str(self.work), "check": "decision", "roles": {"Operator": "R"}}}}) - self.assertEqual(self.cards(), []) + def test_a_command_card_shows_the_scores_its_command_carries_with_their_emoji_only(self): + self.operator_row("operator/commit", SCORED, str(self.work)) + chips = [(chip["id"], chip["value"]) for chip in self.card("operator:operator/commit")["metrics"]] + self.assertEqual(chips, [("fix", "95 %"), ("break", "2 %"), ("score", "93"), ("value", "1:4")]) def test_show_prints_the_command_and_its_payload(self): command = self.payload_command() @@ -358,6 +361,75 @@ class TestCommandCards(MatchCase): self.assertIn(words, shown.stdout) +class TestWaitingCards(MatchCase): + def radio(self) -> list[list[str]]: + return [args for script, args in self.scripts.calls if script == skill_scripts.RADIO] + + def answered(self, task: str) -> str: + return next(item["answered"] for item in self.snapshot()["inbox"] if item["id"] == task) + + def waiting(self) -> dict[str, str]: + self.claim("s1", "Skills session") + self.add(staged_repo(self.work)) + self.write_council({"rasci.json": {"operator/pick": PICK, "operator/ship": {"title": "make ship", "workdir": str(self.work), "check": "done", "roles": {"Skills": "A"}}}}) + rule = {"kind": "ask", "tool": "Bash", "pattern": "make test*", "scope": "/repo*", "expires": int(time.time()) + 86400, "agent": "Skills", "why": "the gate runs make test"} + requested = self.tool("rule_request", rule) + self.assertFalse(requested["isError"], requested) + return {"ask": self.create_ask({**ASK, "command": SCORED}), "decision": self.decision("Merge now"), "rule": requested["structuredContent"]["id"]} + + def test_every_entry_that_waits_on_the_operator_is_one_card_of_its_kind(self): + ids = self.waiting() + cards = self.cards() + inbox = self.snapshot()["inbox"] + self.assertEqual(sorted(card["id"] for card in cards), sorted(f"{item['kind']}:{item['id']}" for item in inbox)) + kinds = {card["id"]: (card["kind"], card["entry"]) for card in cards if "entry" in card} + self.assertEqual(kinds, {f"ask:{ids['ask']}": ("ask", "ask"), f"decision:{ids['decision']}": ("decision", "decision"), "operator:operator/pick": ("decision", "operator"), f"rule:{ids['rule']}": ("rule", "rule")}) + pick = self.card("operator:operator/pick") + self.assertEqual((pick["options"], pick["recommended"], pick["reason"], pick["agent"], pick["to"]), (OPTIONS, "Merge now", "the gate is green", "Skills", "Skills")) + ask = self.card(f"ask:{ids['ask']}") + self.assertEqual((ask["command"], ask["directory"], ask["files"]), (SCORED, "/repo", [])) + self.assertEqual([(chip["id"], chip["value"]) for chip in ask["metrics"]], [("fix", "95 %"), ("break", "2 %"), ("score", "93"), ("value", "1:4")]) + rule = self.card(f"rule:{ids['rule']}") + self.assertEqual((rule["rule"]["pattern"], rule["reason"], rule["dry_run"]["days"]), ("make test*", "the gate runs make test", 7)) + + def test_a_picked_option_of_a_decision_row_is_signed_once_with_the_rest_posted_to_its_agent_and_leaves_the_stack(self): + ids = self.waiting() + pick = self.card("operator:operator/pick") + entries = [ + {"kind": "operator", "id": "operator/pick", "verb": "approve", "label": "Wait", "sha256": pick["sha256"]}, + {"kind": "decision", "id": ids["decision"], "verb": "approve", "label": "Merge now", "sha256": ""}, + {"kind": "ask", "id": ids["ask"], "verb": "approve", "label": "", "sha256": ""}, + ] + answer = signing.message_item("Skills", "post", "Decision operator/pick\nAnswer: Wait", "", "", "") + self.assertIn(signing.item_hash(answer), self.prepare(entries)["payload"]["items"]) + for bad, code in (({**entries[0], "label": "Later"}, 400), ({**entries[0], "payload": "0" * 64}, 400), ({**entries[0], "sha256": "0" * 64}, 409)): + with self.subTest(bad=bad): + self.assertEqual(self.post_json("/api/sign", {"items": [bad]}, self.guard(self.host))[0], code) + with mock.patch.object(batches, "UNDO", 0.4): + self.assertEqual(self.approve(entries, self.signed(entries, self.pin()))[0], 202) + self.assertNotIn("operator:operator/pick", [card["id"] for card in self.cards()]) + self.settle(lambda: self.choice(ids["decision"]) == "Merge now" and self.ask_status(ids["ask"], "0") == "allowed") + self.settle(lambda: any(sent[0] == "post" and sent[-1].endswith("Decision operator/pick Answer: Wait") for sent in self.radio())) + sent = next(sent for sent in self.radio() if sent[-1].endswith("Answer: Wait")) + self.assertEqual(sent[:5], ["post", "--to", "Skills session", "--session", "Operator"]) + self.assertNotIn((skill_scripts.RASCI, ["set", "operator/pick", "--status", "stopped"]), self.scripts.calls) + self.assertEqual({card["kind"] for card in self.cards()}, {"commit", "command", "rule"}) + self.assertEqual(self.post_json("/api/sign", {"items": [entries[0]]}, self.guard(self.host))[0], 409) + self.assertEqual(self.answered("operator/pick"), "Wait") + self.write_council({"rasci.json": {"operator/pick": {**PICK, "title": "Merge the hub tonight?"}}}) + self.assertEqual((self.card("operator:operator/pick")["title"], self.answered("operator/pick")), ("Merge the hub tonight?", "")) + + def test_a_rejected_decision_row_stops_and_its_agent_hears_the_question_and_the_feedback(self): + self.waiting() + pick = self.card("operator:operator/pick") + entry = {"kind": "operator", "id": "operator/pick", "verb": "reject", "label": "", "sha256": pick["sha256"], "channel": "call", "reason": FEEDBACK, "to": pick["to"]} + with mock.patch.object(batches, "UNDO", 0.05): + self.assertEqual(self.approve([entry], self.signed([entry], self.pin()))[0], 202) + self.settle(lambda: (skill_scripts.RASCI, ["set", "operator/pick", "--status", "stopped"]) in self.scripts.calls) + self.settle(lambda: self.radio() != []) + self.assertTrue(self.radio()[0][6].endswith(f"Denied by the operator: decision operator/pick: Merge the hub now? (from Skills). Reason: {TOLD}"), self.radio()) + + class TestMergeCards(MatchCase): def merge_row(self) -> tuple[Path, str, str]: repo, target = merge_repo(self.work) @@ -438,18 +510,20 @@ class TestPayloadHash(unittest.TestCase): class TestMetricChips(unittest.TestCase): def test_score_lines_of_every_shape_give_one_chip_per_metric_in_label_order(self): labels = scores.score_labels(REPO_ROOT / "skills") - line = scores.metric_chips("๐ŸŽฏ fix 94 % ยท ๐Ÿ’ฅ break 4 % ยท ๐Ÿ’ธ tokens 100 % ยท โฑ๏ธ velocity 100 % ยท ๐Ÿ›ก๏ธ security 100 % ยท ๐Ÿ† score 90", labels) + line = scores.metric_chips("๐ŸŽฏ fix 94 % ยท ๐Ÿ’ฅ break 4 % ยท ๐Ÿ’ธ tokens 100 % ยท โฑ๏ธ velocity 100 % ยท ๐Ÿ›ก๏ธ security 100 % ยท ๐Ÿ† score 90", labels, True) self.assertEqual([(chip["id"], chip["emoji"], chip["label"], chip["value"]) for chip in line], [ ("fix", "๐ŸŽฏ", "Fix", "94 %"), ("break", "๐Ÿ’ฅ", "Break", "4 %"), ("tokens", "๐Ÿ’ธ", "Tokens", "100 %"), ("velocity", "โฑ๏ธ", "Velocity", "100 %"), ("security", "๐Ÿ›ก๏ธ", "Security", "100 %"), ("score", "๐Ÿ†", "Score", "90"), ]) - block = scores.metric_chips("๐ŸŽฏ Fix: 85 %\n๐Ÿ’ฅ Break: 5 %\nโฑ Speed: new\n๐Ÿ† Score: 81\n๐Ÿšฆ Gate: โ›” fix 85 % < 90", labels) + block = scores.metric_chips("๐ŸŽฏ Fix: 85 %\n๐Ÿ’ฅ Break: 5 %\nโฑ Speed: new\n๐Ÿ† Score: 81\n๐Ÿšฆ Gate: โ›” fix 85 % < 90", labels, True) self.assertEqual([(chip["id"], chip["value"]) for chip in block], [("fix", "85 %"), ("break", "5 %"), ("velocity", "new"), ("score", "81")]) - self.assertEqual([(chip["id"], chip["value"]) for chip in scores.metric_chips("Fix 92% Break 2.5%", labels)], [("fix", "92%"), ("break", "2.5%")]) + self.assertEqual([(chip["id"], chip["value"]) for chip in scores.metric_chips("Fix 92% Break 2.5%", labels, True)], [("fix", "92%"), ("break", "2.5%")]) for text in ("", "looks good", "๐ŸŽฏ high", "prefix 90 %"): with self.subTest(text=text): - self.assertEqual(scores.metric_chips(text, labels), []) - self.assertEqual(scores.metric_chips("๐ŸŽฏ 95 ๐Ÿ’ฅ 3", None), []) + self.assertEqual(scores.metric_chips(text, labels, True), []) + self.assertEqual(scores.metric_chips("fix 2 tests, score 3 ยท ๐Ÿ† 7", labels, False), [{"id": "score", "emoji": "๐Ÿ†", "label": "Score", "value": "7"}]) + self.assertEqual([(chip["id"], chip["value"]) for chip in scores.metric_chips("๐Ÿ’ฐ Value: 1:4 ยท ๐Ÿ’ฐ 2", labels, True)], [("value", "1:4")]) + self.assertEqual(scores.metric_chips("๐ŸŽฏ 95 ๐Ÿ’ฅ 3", None, True), []) if __name__ == "__main__":