feat(orchestrator): reveal withheld diff content under a passkey signature (019)

staged_diff masks only the secret rule hits and seals each masked file's clear patch apart; reviews and asks store the sealed patches next to their diff, delete them once the item leaves pending, and the hub sweeps orphans. POST /api/diffs/<kind>/<id>/reveal/sign and /reveal hand the clear patches to a signed request; page-diff.js offers Reveal per hidden file and Reveal all, keeps the clear text in the page only and forgets it once the item leaves the inbox. Requirement 019, unit, hub and e2e tests.

Accepted MEDUSA false positives (security.py accept after kvb-think-criticize, 2026-11-17): approval.py FTSCAN-231 WEB-SSRF RH005; permission_hook.py MM-SCAN-058 WEB-SSRF RH005; staged_diff.py SAST-ENV-001 AI-BACKDOOR-006 B603; hub.py MEX-SCAN-1167 INF-SCAN-589; page-load.js EXA002; hub_case.py TC001-TC009 INF-SCAN-2057/2206/2440/2457 EXA002 EXA006 PL002 B603 FTSC-SCAN-089.

🎯 Fix: 92 %
💥 Break: 4 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 88
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-17 19:04:35 +00:00
1 parent 78d4e4a6aa
commit af3555e88b
24 files changed
+928 -111

No files matched your search

+6 -5
View File
@@ -15,9 +15,10 @@
with the repository, worktree, branch and tree as git prints them
here, the notes file's text (refused past NOTES_LIMIT characters),
and the diff of the tree git write-tree prints against HEAD
(staged_diff.tree_diff, content withheld per the MEDUSA rules under
<dir>, by default those of the pipx install); without the diff when
the body would pass DIFF_BODY_LIMIT.
(staged_diff.tree_diff, secrets masked per the MEDUSA rules under
<dir>, by default those of the pipx install) with the sealed clear
patches of its masked files apart; without either when the body
would pass DIFF_BODY_LIMIT.
Returns:
pin: exit code 0 and the pinned credential on stdout; 1 and the reason
@@ -89,14 +90,14 @@ def request_review(args: argparse.Namespace) -> str:
item = staged(cwd, "0" * 16)
try:
notes = args.notes.read_text(encoding="utf-8")
diff = staged_diff.tree_diff(cwd, item["tree"], args.rules or staged_diff.rules_root())
diff, sealed = staged_diff.tree_diff(cwd, item["tree"], args.rules or staged_diff.rules_root())
except (OSError, ValueError, staged_diff.DiffError) as error:
raise Refused(f"the review cannot be prepared: {error}") from error
if len(notes) > NOTES_LIMIT:
raise Refused(f"the notes hold {len(notes)} characters, the hub takes at most {NOTES_LIMIT}")
arguments = {key: item[key] for key in ("repo", "worktree", "branch", "tree")}
arguments.update(title=args.title, page=str(args.page), notes=notes, gate=args.gate, score=args.score, agent=args.agent)
body = review_body({**arguments, "diff": diff})
body = review_body({**arguments, "diff": diff, "sealed": sealed})
if len(body) > DIFF_BODY_LIMIT:
print(f"approval: the review goes without its diff, its {len(body)} bytes pass the hub's {DIFF_BODY_LIMIT}", file=sys.stderr)
body = review_body(arguments)
+10 -1
View File
@@ -9,12 +9,21 @@
NOTES_LIMIT: characters of review notes the hub takes; the client refuses
longer notes instead of cutting them.
WITHHELD: the note of a diff entry that ships without its patch because
its content may hold a secret.
no secret rule loads to mask it.
MASKED: the note of a diff entry whose patch reads MASK for every secret
rule match and every armour block line.
MASK: what stands for a masked span in a patch.
SEALED_LIMIT: bytes of JSON-encoded clear patch text of the masked and
withheld files one request carries apart from its diff, for a
signed reveal.
"""
DIFF_LIMIT = 2 << 20
DIFF_BODY_LIMIT = 4 << 20
SEALED_LIMIT = 1 << 20
FILE_CAP = 5000
PATH_LIMIT = 4096
NOTES_LIMIT = 100_000
WITHHELD = "content withheld"
MASKED = "masked"
MASK = "••••"
+9 -6
View File
@@ -18,9 +18,10 @@ Returns:
takes or a hub that is down, so Claude Code shows its own dialog.
The exit code is 0. An ask for a Bash git commit of exactly the
staged index (staged_diff.commit_of) carries the diff staged_diff.py
builds of the tree its directory stages, content withheld per the
MEDUSA rules of the pipx install; none when it cannot be built or
the body would pass DIFF_BODY_LIMIT. An allow of such an ask turns
builds of the tree its directory stages, secrets masked per the
MEDUSA rules of the pipx install, and the sealed clear patches of
its masked files apart; neither when it cannot be built or the body
would pass DIFF_BODY_LIMIT. An allow of such an ask turns
into a deny with CHANGED when what the commit records
(staged_diff.binding) differs right before answering from the
preview's, and with UNBOUND when either cannot be read. Any other
@@ -88,8 +89,9 @@ def unapprovable(ask: dict) -> bool:
def commit_diff(ask: dict, rules: Path | None) -> tuple[str | None, dict]:
"""Returns:
For a commit ask, what the commit records (staged_diff.binding, None
when it cannot be read) and {"diff": the preview of exactly that},
{} when it cannot be built; None and {} for any other ask.
when it cannot be read) and {"diff": the preview of exactly that,
"sealed": the clear patches of its masked files}, {} when it cannot
be built; None and {} for any other ask.
"""
target = commit_target(ask)
if target is None:
@@ -100,7 +102,8 @@ def commit_diff(ask: dict, rules: Path | None) -> tuple[str | None, dict]:
print(f"kvb-orchestrator: the ask goes without its diff and an allow of it turns into a deny: {error}", file=sys.stderr)
return None, {}
try:
return pinned, {"diff": staged_diff.bound_diff(target, pinned, rules)}
diff, sealed = staged_diff.bound_diff(target, pinned, rules)
return pinned, {"diff": diff, "sealed": sealed}
except FAILURES as error:
print(f"kvb-orchestrator: the ask goes without its diff: {error}", file=sys.stderr)
return pinned, {}
+137 -35
View File
@@ -6,19 +6,24 @@
pinned: for bound_diff, what binding returned for the same cwd.
Returns:
review_diff, tree_diff, bound_diff: the review diff of requirement 003
of the staged tree against HEAD (git diff --cached), one {path,
status, mode, additions, deletions, patch, note} per changed path,
sorted by path, status A, M, D or B (binary); path is the file name
with bytes that are no UTF-8 written as backslash escapes; mode the
git file mode, "old => new" when it changed. patch holds the unified
hunks; it is empty for a binary file, a gitlink, a side over
BLOB_LIMIT bytes (counted 0/0), a path sandbox_paths denies (its
FLOOR and the settings' deny lists) and every file past DIFF_LIMIT
bytes of JSON-encoded patch text. A file whose old or new content
holds an ARMOUR line or matches a MEDUSA secret rule for its name,
and every file when no secret rule pattern loads, ships no patch,
only its counts and the note WITHHELD. The diff fails when more
review_diff, tree_diff, bound_diff: (diff, sealed). diff is the review
diff of requirement 003 of the staged tree against HEAD (git diff
--cached), one {path, status, mode, additions, deletions, patch,
note} per changed path, sorted by path, status A, M, D or B
(binary); path is the file name with bytes that are no UTF-8
written as backslash escapes; mode the git file mode, "old => new"
when it changed. patch holds the unified hunks; it is empty for a
binary file, a gitlink, a side over BLOB_LIMIT bytes (counted 0/0),
a path sandbox_paths denies (its FLOOR and the settings' deny lists)
and every file past DIFF_LIMIT bytes of JSON-encoded patch text. In
a shown line every match of a MEDUSA secret rule for the file's name
and every line from an ARMOUR line through its ARMOUR_END line reads
MASK, and the file carries the note MASKED; when no secret rule
pattern loads, every text file ships no patch, only its counts and
the note WITHHELD. sealed maps the path of each MASKED or WITHHELD
file to its clear patch, while their JSON-encoded text stays within
SEALED_LIMIT bytes; diff never holds a masked span in clear. The
diff fails when more
than FILE_CAP paths changed or one is longer than PATH_LIMIT, a
settings file sandbox_paths reads is unreadable, the working tree
lies under a denied path, HEAD exists but its tree cannot be listed,
@@ -46,9 +51,11 @@ is read.
from __future__ import annotations
import bisect
import difflib
import fnmatch
import hashlib
import itertools
import json
import os
import re
@@ -63,6 +70,9 @@ DIFF_LIMIT = hub_limits.DIFF_LIMIT
FILE_CAP = hub_limits.FILE_CAP
PATH_LIMIT = hub_limits.PATH_LIMIT
WITHHELD = hub_limits.WITHHELD
MASKED = hub_limits.MASKED
MASK = hub_limits.MASK
SEALED_LIMIT = hub_limits.SEALED_LIMIT
BLOB_LIMIT = 2 << 20
BINARY_PROBE = 8000
CONTEXT = 3
@@ -100,8 +110,11 @@ SHORT_ARGS = set("mFcCtuSU")
KEPT_VARS = {"GIT_EDITOR", "GIT_SEQUENCE_EDITOR", "GIT_PAGER", "GIT_TERMINAL_PROMPT", "GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", "GIT_AUTHOR_DATE", "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL", "GIT_COMMITTER_DATE"}
ASSIGNMENT = re.compile(r"([A-Za-z_][A-Za-z0-9_]*)=.*")
ARMOUR = re.compile(r"-----BEGIN [A-Z0-9 ]+-----")
ARMOUR_END = re.compile(r"-----END [A-Z0-9 ]+-----")
HUNK = re.compile(r"@@ -([0-9]+)(?:,[0-9]+)? \+([0-9]+)(?:,[0-9]+)? @@")
Patterns = list[re.Pattern]
Rules = list[tuple[tuple[str, ...], Patterns]]
Spans = list[list[tuple[int, int]]]
class DiffError(Exception):
@@ -200,7 +213,7 @@ def binding(cwd: Path) -> str:
return hashlib.sha256(staged(toplevel(cwd))).hexdigest()
def bound_diff(cwd: Path, pinned: str, rules: Path | None) -> list[dict]:
def bound_diff(cwd: Path, pinned: str, rules: Path | None) -> tuple[list[dict], dict[str, str]]:
index = staged(toplevel(cwd))
if hashlib.sha256(index).hexdigest() != pinned:
raise DiffError("the staged tree changed while its preview was read")
@@ -325,32 +338,114 @@ def rule_patterns(path: str, rules: Rules) -> Patterns:
return [pattern for globs, found in rules if any(fnmatch.fnmatchcase(name, glob.lower()) for glob in globs) for pattern in found]
def withheld(path: str, texts: tuple[str, str], rules: Rules | None) -> bool:
if not rules or not any(patterns for _, patterns in rules):
return True
patterns = [ARMOUR, *rule_patterns(path, rules)]
return any(pattern.search(text) for text in texts for pattern in patterns)
def any_pattern(rules: Rules | None) -> bool:
return bool(rules) and any(patterns for _, patterns in rules)
def entry(head: dict, before: bytes | None, after: bytes | None, rules: Rules | None) -> tuple[dict, list[str]]:
def armour_lines(lines: list[str]) -> set[int]:
"""Returns:
The numbers (from 0) of every line from an ARMOUR line through its
ARMOUR_END line, through the last line when no end follows.
"""
found, inside = set(), False
for number, line in enumerate(lines):
inside = inside or ARMOUR.search(line) is not None
if inside:
found.add(number)
inside = ARMOUR_END.search(line) is None
return found
def hit_spans(text: str, patterns: Patterns) -> Spans:
"""Returns:
Per line of text (text_lines), the [start, end) columns that a match
of a pattern covers, the whole line inside an armour block.
"""
lines = text_lines(text)
spans: Spans = [[] for _ in lines]
starts = list(itertools.accumulate((len(line) + 1 for line in lines), initial=0))
for number in armour_lines(lines):
spans[number].append((0, len(lines[number])))
for pattern in patterns:
for match in pattern.finditer(text):
for number in range(bisect.bisect_right(starts, match.start()) - 1, len(lines)):
if starts[number] >= match.end():
break
begin, end = max(match.start(), starts[number]) - starts[number], min(match.end(), starts[number] + len(lines[number])) - starts[number]
if end > begin:
spans[number].append((begin, end))
return spans
def merged(spans: list[tuple[int, int]]) -> list[tuple[int, int]]:
found: list[tuple[int, int]] = []
for begin, end in sorted(spans):
if found and begin <= found[-1][1]:
found[-1] = (found[-1][0], max(found[-1][1], end))
else:
found.append((begin, end))
return found
def masked(line: str, spans: list[tuple[int, int]]) -> str:
parts, at = [], 0
for begin, end in merged(spans):
parts += [line[at:begin], MASK]
at = end
return "".join([*parts, line[at:]])
def masked_patch(lines: list[str], old: Spans, new: Spans) -> list[str]:
"""Args:
lines: unified diff lines from the first hunk header on.
old, new: hit_spans() of the old and the new text.
Returns:
The lines with every span of their old or new line masked; a
context line takes the spans of both sides.
"""
found, before, after = [], 0, 0
for line in lines:
hunk = HUNK.match(line)
if hunk:
before, after = int(hunk.group(1)) - 1, int(hunk.group(2)) - 1
found.append(line)
continue
sign, spans = line[:1], []
if sign in " -":
spans += old[before]
before += 1
if sign in " +":
spans += new[after]
after += 1
found.append(sign + masked(line[1:], spans) if spans else line)
return found
def entry(head: dict, before: bytes | None, after: bytes | None, rules: Rules | None) -> tuple[dict, list[str], list[str] | None]:
"""Args:
head: the entry's path, status and mode.
Returns:
The diff entry without its patch, and the patch lines; no lines for
a binary side, one over BLOB_LIMIT or content withheld.
The diff entry without its patch, the patch lines to show, and the
clear lines when they differ from those shown (masked or withheld),
else None; no lines for a binary side or one over BLOB_LIMIT.
"""
found = {**head, "additions": 0, "deletions": 0, "note": ""}
if before is None or after is None:
return found, []
return found, [], None
if b"\0" in before[:BINARY_PROBE] or b"\0" in after[:BINARY_PROBE]:
return {**found, "status": "B"}, []
return {**found, "status": "B"}, [], None
texts = (before.decode("utf-8", "replace"), after.decode("utf-8", "replace"))
lines = list(difflib.unified_diff(*map(text_lines, texts), lineterm="", n=CONTEXT))[2:]
found.update(additions=sum(1 for line in lines if line.startswith("+")), deletions=sum(1 for line in lines if line.startswith("-")))
if withheld(head["path"], texts, rules):
return {**found, "note": WITHHELD}, []
return found, lines
if not any_pattern(rules):
return {**found, "note": WITHHELD}, [], lines or None
patterns = rule_patterns(head["path"], rules)
shown = masked_patch(lines, *(hit_spans(text, patterns) for text in texts))
if shown == lines:
return found, lines, None
return {**found, "note": MASKED}, shown, lines
def content_of(side: tuple[str, str] | None, gitlink: bool, contents: dict[str, bytes | None]) -> bytes | None:
@@ -359,11 +454,11 @@ def content_of(side: tuple[str, str] | None, gitlink: bool, contents: dict[str,
return None if gitlink else contents.get(side[1])
def review_diff(cwd: Path, rules: Path | None) -> list[dict]:
def review_diff(cwd: Path, rules: Path | None) -> tuple[list[dict], dict[str, str]]:
return built(cwd, sides(cwd, None, None), rules)
def tree_diff(cwd: Path, tree: str, rules: Path | None) -> list[dict]:
def tree_diff(cwd: Path, tree: str, rules: Path | None) -> tuple[list[dict], dict[str, str]]:
return built(cwd, sides(cwd, tree, None), rules)
@@ -380,7 +475,7 @@ def shown_path(path: str) -> str:
return shown
def path_entry(path: str, before: tuple[str, str] | None, after: tuple[str, str] | None, contents: dict[str, bytes | None], rules: Rules | None) -> tuple[dict, list[str]]:
def path_entry(path: str, before: tuple[str, str] | None, after: tuple[str, str] | None, contents: dict[str, bytes | None], rules: Rules | None) -> tuple[dict, list[str], list[str] | None]:
status = "A" if before is None else "D" if after is None else "M"
gitlink = any(side is not None and side[0] == GITLINK for side in (before, after))
modes = list(dict.fromkeys(side[0] for side in (before, after) if side is not None))
@@ -388,7 +483,11 @@ def path_entry(path: str, before: tuple[str, str] | None, after: tuple[str, str]
return entry(head, content_of(before, gitlink, contents), content_of(after, gitlink, contents), rules)
def built(cwd: Path, found_sides: tuple[dict, dict, dict[str, bytes | None]], rules: Path | None) -> list[dict]:
def encoded_size(text: str) -> int:
return len(json.dumps(text, ensure_ascii=False).encode("utf-8"))
def built(cwd: Path, found_sides: tuple[dict, dict, dict[str, bytes | None]], rules: Path | None) -> tuple[list[dict], dict[str, str]]:
old, new, contents = found_sides
changed = sorted(path for path in {*old, *new} if old.get(path) != new.get(path))
if len(changed) > FILE_CAP:
@@ -397,18 +496,21 @@ def built(cwd: Path, found_sides: tuple[dict, dict, dict[str, bytes | None]], ru
shown_path(path)
fetched(cwd, old, new, changed, contents)
secrets = secret_rules(rules)
diff, spent, full = [], 0, False
diff, sealed, spent, kept, full = [], {}, 0, 0, False
for path in changed:
found, lines = path_entry(path, old.get(path), new.get(path), contents, secrets)
found, lines, clear = path_entry(path, old.get(path), new.get(path), contents, secrets)
patch = "\n".join(lines)
size = len(json.dumps(patch, ensure_ascii=False).encode("utf-8"))
size = encoded_size(patch)
full = full or spent + size > DIFF_LIMIT
if full:
patch = ""
else:
spent += size
diff.append({**found, "patch": patch})
return diff
if clear is not None and kept + encoded_size("\n".join(clear)) <= SEALED_LIMIT:
sealed[found["path"]] = "\n".join(clear)
kept += encoded_size(sealed[found["path"]])
return diff, sealed
def names_commit(command: str) -> bool:
+13 -6
View File
@@ -3,7 +3,7 @@ from __future__ import annotations
import time
from .choosing import EVERYONE, answer_text
from .diffs import store_diff, valid_diff
from .diffs import sealed_file, store_diff, store_sealed, valid_diff, valid_sealed
from .errors import Conflict, Missing, ToolError
from .fields import (
COMMAND_LIMIT,
@@ -65,16 +65,20 @@ class Asking:
def ask(self, body: bytes) -> dict:
payload = decode(body)
diff = valid_diff(payload.pop("diff")) if "diff" in payload else None
sealed = valid_sealed(payload.pop("sealed"), diff) if "sealed" in payload else {}
expect(payload, ("tool", *ASK_TEXTS, "timeout"))
values = {name: bounded(payload, name, limit) for name, limit in ASK_TEXTS.items()}
visible(values)
lifetime = seconds(payload, "timeout", ASK_LIFETIME)
created = time.time()
fields = {**values, "tool": field(payload, "tool").strip(), "status": "pending", "decided": None, "proof": None, "diff_files": None if diff is None else len(diff)}
ask_id = self.asks.fresh()
if diff is not None:
store_diff(self.reviews.pages, "ask", ask_id, diff)
record = self.asks.insert(ask_id, {**fields, "created_ts": created, "expires": created + lifetime})
with self.asks.changed:
ask_id = self.asks.fresh()
if diff is not None:
store_diff(self.reviews.pages, "ask", ask_id, diff)
if sealed:
store_sealed(self.reviews.pages, "ask", ask_id, sealed)
record = self.asks.insert(ask_id, {**fields, "created_ts": created, "expires": created + lifetime})
self.automate("ask", record)
return {"id": record["id"], "status": record["status"]}
@@ -85,7 +89,10 @@ class Asking:
raise Missing(ask_id)
left = max(record["expires"] - time.time(), 0.0)
record = self.asks.wait(ask_id, min(wait or 0.0, WAIT_CAP, left))
return ask_state(record, time.time())
state = ask_state(record, time.time())
if state["status"] == "expired":
sealed_file(self.reviews.pages, "ask", ask_id).unlink(missing_ok=True)
return state
def settle(self, kind_name: str, item_id: str, verb: str, body: bytes) -> dict:
if kind_name == "asks" and verb in ANSWERS:
+40 -3
View File
@@ -10,13 +10,16 @@ from .signed import limits
DIFF_LIMIT = limits.DIFF_LIMIT
DIFF_BODY_LIMIT = limits.DIFF_BODY_LIMIT
SEALED_LIMIT = limits.SEALED_LIMIT
FILE_CAP = limits.FILE_CAP
PATH_LIMIT = limits.PATH_LIMIT
STATUSES = ("A", "M", "D", "R", "B")
DIFF_KEYS = ("path", "status", "mode", "additions", "deletions", "patch", "note")
NOTES = ("", limits.WITHHELD)
HIDDEN = (limits.WITHHELD, limits.MASKED)
NOTES = ("", *HIDDEN)
MODE = re.compile(r"[0-7]{6}(?: => [0-7]{6})?")
DIFF_SUFFIX = {"review": ".review-diff.json", "ask": ".ask-diff.json"}
SEALED_SUFFIX = {"review": ".review-sealed.json", "ask": ".ask-sealed.json"}
def counted(value: object) -> bool:
@@ -33,8 +36,8 @@ def diff_entry(entry: object) -> dict:
raise ToolError("mode must be a git file mode, or old => new for a changed one")
if not counted(entry["additions"]) or not counted(entry["deletions"]) or not isinstance(patch, str) or (entry["status"] == "B" and patch):
raise ToolError("additions and deletions must be counts, patch text, empty for a binary file")
if entry["note"] not in NOTES or (entry["note"] and patch):
raise ToolError(f"note must be empty or {limits.WITHHELD!r}, the latter with an empty patch")
if entry["note"] not in NOTES or (entry["note"] == limits.WITHHELD and patch):
raise ToolError(f"note must be empty, {limits.MASKED!r} or {limits.WITHHELD!r}, the latter with an empty patch")
return {key: entry[key] for key in DIFF_KEYS}
@@ -52,10 +55,44 @@ def valid_diff(value: object) -> list[dict]:
return entries
def valid_sealed(value: object, diff: list[dict] | None) -> dict[str, str]:
"""Returns:
The clear patches a request seals apart from its diff, checked: an
object from the path of a masked or withheld file of diff to its
patch text, at most SEALED_LIMIT bytes of JSON-encoded text in all;
none without a diff.
"""
hidden = {entry["path"] for entry in diff or [] if entry["note"] in HIDDEN}
if not isinstance(value, dict) or not all(isinstance(patch, str) for patch in value.values()) or not set(value) <= hidden:
raise ToolError("sealed must map the path of a masked or withheld file of the diff to its patch text")
if sum(len(json.dumps(patch, ensure_ascii=False).encode("utf-8")) for patch in value.values()) > SEALED_LIMIT:
raise ToolError(f"the sealed patches must stay within {SEALED_LIMIT} bytes")
return dict(value)
def diff_bytes(diff: list[dict]) -> bytes:
return json.dumps({"diff": diff}, ensure_ascii=False).encode("utf-8")
def sealed_bytes(sealed: dict[str, str]) -> bytes:
return json.dumps({"sealed": sealed}, ensure_ascii=False).encode("utf-8")
def sealed_file(folder: Path, kind_name: str, item_id: str) -> Path:
return folder / f"{item_id}{SEALED_SUFFIX[kind_name]}"
def store_sealed(folder: Path, kind_name: str, item_id: str, sealed: dict[str, str]) -> None:
write_atomic(sealed_file(folder, kind_name, item_id), sealed_bytes(sealed))
def stored_sealed(folder: Path, kind_name: str, item_id: str) -> dict[str, str]:
data = read_json(sealed_file(folder, kind_name, item_id))
if not isinstance(data, dict) or not isinstance(data.get("sealed"), dict):
raise Missing(item_id)
return data["sealed"]
def store_diff(folder: Path, kind_name: str, item_id: str, diff: list[dict]) -> None:
write_atomic(folder / f"{item_id}{DIFF_SUFFIX[kind_name]}", diff_bytes(diff))
+6 -1
View File
@@ -67,6 +67,7 @@ SETTINGS_SIGN_ROUTE = "/api/settings/sign"
CHOICES_ROUTE = "/api/choices"
CHOICES_SIGN_ROUTE = "/api/choices/sign"
DIFF_ROUTE = re.compile(r"/api/diffs/(review|ask)/([0-9a-f]{16})")
REVEAL_ROUTE = re.compile(r"/api/diffs/(review|ask)/([0-9a-f]{16})/reveal(/sign)?")
FIREWALL_ROUTE = "/api/firewall"
FIREWALL_POSTS = {"/api/firewall/preview": "settings_preview", "/api/firewall/settings": "change_settings", "/api/firewall/groups": "add_group", "/api/firewall/sets": "add_set"}
FIREWALL_STATE = re.compile(r"/api/firewall/settings/([0-9a-f]{16})")
@@ -175,6 +176,10 @@ class Handler(Exchange):
return lambda body: self.answer(lambda: hub.sign_board(body), HTTPStatus.OK)
if board:
return lambda body: self.answer(lambda: hub.board(board.group(1), body), HTTPStatus.OK)
reveal = REVEAL_ROUTE.fullmatch(path)
if reveal:
method = hub.sign_reveal if reveal.group(3) else hub.reveal
return lambda body: self.answer(lambda: method(reveal.group(1), reveal.group(2), body), HTTPStatus.OK)
return self.inbox_action(path)
def inbox_action(self, path: str) -> Callable[[bytes], None] | None:
@@ -252,7 +257,7 @@ class Handler(Exchange):
return None
def posted(self, path: str) -> bool:
return any(pattern.fullmatch(path) for pattern in (INBOX_ROUTE, UNDO_ROUTE, RULE_ROUTE, REQUEST_ROUTE, RUNNER_ROUTE, FIREWALL_DROP))
return any(pattern.fullmatch(path) for pattern in (INBOX_ROUTE, UNDO_ROUTE, RULE_ROUTE, REQUEST_ROUTE, RUNNER_ROUTE, FIREWALL_DROP, REVEAL_ROUTE))
def runner(self, path: str, body: bytes) -> None:
hub = self.server.hub
+5 -3
View File
@@ -17,6 +17,7 @@ from .choosing import Choosing
from .cleanup import Cleaning, Spool
from .crons import History, queue_live
from .denying import Denying
from .diffs import SEALED_SUFFIX
from .engines import Engines, Speech
from .events import Log, Watch
from .firewall import Firewall
@@ -25,7 +26,7 @@ from .icons import sheet
from .inbox import Inbox
from .jobs import Jobs
from .language import Language, Model
from .ledger import Ledger, Marks, Reviews
from .ledger import Ledger, Marks, Reviews, Sealed
from .matching import Matching
from .mcp import Mcp
from .messaging import Messaging
@@ -45,6 +46,7 @@ from .receipts import (
from .redirects import Redirects
from .requesting import Requesting
from .resources import RESOURCES, SAMPLES
from .revealing import Revealing
from .reviewing import Reviewing
from .rules import Offers, Rules
from .settings import Settings
@@ -84,7 +86,7 @@ def environment(templates: Path) -> jinja2.Environment:
@dataclass(frozen=True)
class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Requesting, Cleaning, Offices, Tasking, Gating, Firewall, Inbox, Matching, Rules, Jobs, Sources, Timeline, View, Language):
class Hub(Mcp, Reviewing, Revealing, Asking, Messaging, Choosing, Approving, Denying, Requesting, Cleaning, Offices, Tasking, Gating, Firewall, Inbox, Matching, Rules, Jobs, Sources, Timeline, View, Language):
council: Path
pages: Path
public_port: int
@@ -202,7 +204,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta
pages=pages,
public_port=public_port,
reviews=reviews,
asks=Ledger(state / "asks.json"),
asks=Sealed(state / "asks.json", reviews.pages, SEALED_SUFFIX["ask"]),
decisions=Ledger(state / "decisions.json"),
messages=messages,
rule_requests=Ledger(state / "rule-requests.json"),
+22 -4
View File
@@ -6,6 +6,7 @@ import threading
from collections.abc import Callable
from pathlib import Path
from .diffs import SEALED_SUFFIX
from .records import read_json, stamp, write_atomic
@@ -101,11 +102,28 @@ class Ledger:
return dict(self.items[item_id])
class Reviews(Ledger):
class Sealed(Ledger):
def __init__(self, file: Path, pages: Path, suffix: str) -> None:
"""Args:
pages: the folder holding each item's sealed clear patches.
suffix: the name suffix of an item's sealed file there; it is
deleted once the item leaves pending.
"""
super().__init__(file)
self.pages = pages
self.sealed_suffix = suffix
def transition(self, item_id: str, before: str, changes: dict) -> tuple[dict | None, bool]:
record, changed = super().transition(item_id, before, changes)
if changed and record is not None and record.get("status") != "pending":
(self.pages / f"{item_id}{self.sealed_suffix}").unlink(missing_ok=True)
return record, changed
class Reviews(Sealed):
def __init__(self, state: Path) -> None:
super().__init__(state / "reviews.json")
self.pages = state / "pages"
self.pages.mkdir(exist_ok=True)
(state / "pages").mkdir(exist_ok=True)
super().__init__(state / "reviews.json", state / "pages", SEALED_SUFFIX["review"])
def stored(self, review_id: str, suffix: str) -> Path:
return self.pages / f"{review_id}{suffix}"
@@ -0,0 +1,82 @@
from __future__ import annotations
import time
from .asking import ask_state
from .diffs import SEALED_SUFFIX, sealed_file, stored_sealed
from .errors import Conflict, Missing, ToolError
from .fields import decode, expect
from .records import shown
TITLE_LIMIT = 200
PATHS_SHOWN = 2000
def reveal_item(kind_name: str, item_id: str, paths: list[str]) -> dict:
"""Returns:
The item a reveal signs: the sorted paths of one review or ask; its
hash equals no item an executor approves.
"""
return {"kind": "reveal", "target": kind_name, "id": item_id, "paths": paths}
class Revealing:
def sealed_status(self, kind_name: str, item_id: str, now: float) -> tuple[dict | None, str | None]:
record = self.reviews.get(item_id) if kind_name == "review" else self.asks.get(item_id)
if record is None:
return None, None
return record, record["status"] if kind_name == "review" else ask_state(record, now)["status"]
def open_record(self, kind_name: str, item_id: str) -> dict:
"""Returns:
The pending review or ask whose sealed patches a reveal reads.
Raises:
Missing for an unknown item; Conflict once it is decided or
expired, after its sealed file is deleted.
"""
record, status = self.sealed_status(kind_name, item_id, time.time())
if record is None:
raise Missing(item_id)
if status != "pending":
sealed_file(self.reviews.pages, kind_name, item_id).unlink(missing_ok=True)
raise Conflict({"kind": kind_name, "id": item_id, "status": status})
return record
def requested(self, kind_name: str, item_id: str, paths: object) -> tuple[dict, list[str], dict[str, str]]:
record = self.open_record(kind_name, item_id)
if not isinstance(paths, list) or not paths or not all(isinstance(path, str) for path in paths) or len(set(paths)) != len(paths):
raise ToolError("paths must name one or more distinct files")
sealed = stored_sealed(self.reviews.pages, kind_name, item_id)
unsealed = [path for path in paths if path not in sealed]
if unsealed:
raise ToolError(f"nothing sealed to reveal for {shown(unsealed[0])[:TITLE_LIMIT]}")
return record, sorted(paths), sealed
def sign_reveal(self, kind_name: str, item_id: str, body: bytes) -> dict:
payload = decode(body)
expect(payload, ("paths",))
_, paths, _ = self.requested(kind_name, item_id, payload["paths"])
return self.challenge_for([reveal_item(kind_name, item_id, paths)])
def reveal(self, kind_name: str, item_id: str, body: bytes) -> dict:
"""Returns:
The clear patches of exactly the requested paths, once a pinned
passkey signed their reveal_item unexpired and for the first
time; the approval stream records kind, id and paths, never a
patch.
"""
payload = decode(body)
expect(payload, ("paths", "proof"))
record, paths, sealed = self.requested(kind_name, item_id, payload["paths"])
self.operator_proven(payload["proof"], reveal_item(kind_name, item_id, paths))
title = record["title"] if kind_name == "review" else f"{record['tool']}: {record['command']}"
self.log.add({"kind": "approval", "title": f"Reveal {kind_name}: {shown(title)[:TITLE_LIMIT]}", "text": shown(", ".join(paths))[:PATHS_SHOWN], "ref": {"kind": kind_name, "id": item_id, "verb": "reveal", "paths": paths}})
return {"kind": kind_name, "id": item_id, "patches": {path: sealed[path] for path in paths}}
def sweep_sealed(self) -> None:
now = time.time()
for kind_name, suffix in SEALED_SUFFIX.items():
for file in self.reviews.pages.glob(f"*{suffix}"):
if self.sealed_status(kind_name, file.name.removesuffix(suffix), now)[1] != "pending":
file.unlink(missing_ok=True)
+13 -2
View File
@@ -6,7 +6,15 @@ import stat
import time
from pathlib import Path
from .diffs import DIFF_SUFFIX, diff_bytes, stored_diff, valid_diff
from .diffs import (
DIFF_SUFFIX,
SEALED_SUFFIX,
diff_bytes,
sealed_bytes,
stored_diff,
valid_diff,
valid_sealed,
)
from .errors import Conflict, Missing, ToolError
from .fields import WAIT_CAP, bounded, decode, expect, field, seconds
from .review_page import review_files
@@ -17,7 +25,7 @@ NOTES_LIMIT = limits.NOTES_LIMIT
SCORE_LIMIT = 500
GATES = ("pass", "fail")
REQUEST = ("repo", "worktree", "branch", "tree", "title", "page", "notes", "gate", "score")
OPTIONAL = ("agent", "diff")
OPTIONAL = ("agent", "diff", "sealed")
LISTED = ("id", "created", "worktree", "branch", "tree", "title")
STATE = ("id", "status", "tree", "decided")
@@ -67,9 +75,12 @@ class Reviewing:
score = " ".join(bounded(arguments, "score", SCORE_LIMIT).split())
agent = field(arguments, "agent").strip() if "agent" in arguments else ""
diff = valid_diff(arguments["diff"]) if "diff" in arguments else None
sealed = valid_sealed(arguments["sealed"], diff) if "sealed" in arguments else {}
page = self.read_page(field(arguments, "page"))
extra = {"notes": notes, "gate": arguments["gate"], "score": score, "agent": agent, "diff_files": None if diff is None else len(diff)}
attached = {} if diff is None else {DIFF_SUFFIX["review"]: diff_bytes(diff)}
if sealed:
attached[SEALED_SUFFIX["review"]] = sealed_bytes(sealed)
review = self.reviews.submit({**fields, "title": fields["title"].strip(), **extra}, page, review_files(page), attached)
self.automate("review", review)
return {"id": review["id"], "status": review["status"], "url": self.url()}
+1
View File
@@ -77,6 +77,7 @@ class View:
if sampled not in (None, "1"):
raise ToolError("track must be 1 or left out")
self.tree.beat()
self.sweep_sealed()
return self.state(begin, end, now, TRACK_STEPS if sampled else 0)
def inlined(self, news: list[dict], window: tuple[float, float], now: float) -> dict[float, dict]:
+53 -5
View File
@@ -1,5 +1,44 @@
const DIFF_TONES = {A: "ok", M: "info", D: "bad", R: "violet", B: "neutral"};
const diffView = {cache: new Map(), version: 0, float: null, picked: null, timer: 0};
const HIDDEN_NOTES = new Set(["masked", "content withheld"]);
const diffView = {cache: new Map(), revealed: new Map(), version: 0, float: null, picked: null, timer: 0};
const revealedOf = (item) => diffView.revealed.get(keyOf(item)) || new Map();
const hiddenFile = (item, file) => HIDDEN_NOTES.has(file.note) && !revealedOf(item).has(file.path);
function forgetRevealed() {
const waiting = new Set(state.inbox.map(keyOf));
for (const key of [...diffView.revealed.keys()]) {
if (!waiting.has(key)) {
diffView.revealed.delete(key);
}
}
}
async function revealFiles(item, paths) {
const route = `/api/diffs/${item.kind}/${encodeURIComponent(item.id)}/reveal`;
const label = paths.length === 1 ? `Reveal: ${short(paths[0])}` : `Reveal ${paths.length} files`;
const signed = await signedProof(`${route}/sign`, {paths}, label, null);
if (!signed || !signed.proof) {
return;
}
const result = await sendJson(route, {paths, proof: signed.proof});
if (!result.ok || !result.data) {
toast(`${label}: not possible`, fail(result), false);
return;
}
diffView.revealed.set(keyOf(item), new Map([...revealedOf(item), ...Object.entries(result.data.patches)]));
diffView.version += 1;
drawInbox();
drawStream();
}
function revealButton(item, paths, words) {
const hint = `${words}: your passkey signs the reveal; the clear text stays in this page only and the reveal is logged`;
return h("button", {type: "button", class: "btn small ghost reveal", ...named(words, hint), on: {click: (event) => {
event.preventDefault();
event.stopPropagation();
revealFiles(item, paths);
}}}, icon("bi-eye"), words);
}
function diffOf(item) {
if (!item || typeof item.diff !== "number" || !["review", "ask"].includes(item.kind)) {
@@ -30,17 +69,23 @@
h("td", {class: "num"}, row.old), h("td", {class: "num"}, row.new), h("td", {class: "sign"}, SIGNS[row.kind] || " "), h("td", {class: "code"}, revealed(row.text)));
}
function diffBlock(file) {
function diffBlock(item, raw) {
const clear = revealedOf(item).get(raw.path);
const hidden = hiddenFile(item, raw);
const file = clear === undefined ? raw : {...raw, patch: clear, note: ""};
const tone = DIFF_TONES[file.status] || "neutral";
const head = [h("span", {class: "letter", "data-tone": tone}, file.status), h("code", {class: "path"}, revealed(file.path)),
file.mode && file.mode !== "100644" ? h("code", {class: "mode", title: `mode: ${file.mode}`}, file.mode) : null, h("span", {class: "spacer"}),
file.note ? h("span", {class: "note", title: file.note}, icon("bi-eye-slash"), file.note) : null,
hidden ? revealButton(item, [file.path], "Reveal") : null,
clear === undefined ? null : h("span", {class: "note revealed", title: "revealed by your passkey, kept in this page only"}, icon("bi-eye"), "revealed"),
file.status === "B" ? icon("bi-file-earmark-binary") : h("span", {class: "stat"}, h("span", {class: "add"}, `+${file.additions}`), h("span", {class: "del"}, `-${file.deletions}`))];
const shown = clear === undefined ? (hidden ? raw.note : null) : "revealed";
if (file.status === "B" || !file.patch) {
return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status, "data-patch": "none"}, h("div", {class: "diff-head"}, head));
return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status, "data-patch": "none", "data-hidden": shown}, h("div", {class: "diff-head"}, head));
}
const table = h("table", {class: "diff"}, h("tbody", {}, Diffs.rows(file.patch).map(patchRow)));
return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status},
return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status, "data-hidden": shown},
h("details", {open: !Diffs.collapsed(file)}, h("summary", {class: "diff-head"}, head), h("div", {class: "diff-scroll"}, table)));
}
@@ -52,7 +97,10 @@
if (found === "loading") {
return h("p", {class: "muted"}, "…");
}
return h("div", {class: "diff-files", "aria-label": `Diff: ${found.length}`}, found.map(diffBlock));
const hidden = found.filter((file) => hiddenFile(item, file)).map((file) => file.path);
return h("div", {class: "diff-files", "aria-label": `Diff: ${found.length}`},
hidden.length > 1 ? h("div", {class: "reveal-all"}, revealButton(item, hidden, `Reveal all ${hidden.length}`)) : null,
found.map((file) => diffBlock(item, file)));
}
function feedbackList(ref) {
+1
View File
@@ -21,6 +21,7 @@
const previous = state.now;
const resumed = ui.offline;
state = next;
forgetRevealed();
ui.offline = false;
if (JSON.stringify(state.settings) !== settingsSig) {
adoptSettings(state.settings);
+3
View File
@@ -17,6 +17,9 @@
.diff-file .diff-head .mode { color: var(--warn); font-size: var(--fs-xs); }
.diff-file .diff-head .note .bi { margin-right: 4px; vertical-align: -2px; }
.diff-file .diff-head .note { display: inline-flex; align-items: center; color: var(--warn); font-size: var(--fs-xs); }
.diff-file .diff-head .note.revealed { color: var(--ok); }
.diff-file .diff-head .reveal { flex: none; }
.diff-files .reveal-all { display: flex; justify-content: flex-end; }
.feedback-list { display: grid; gap: 4px; margin: var(--sp-1) 0 0; padding: 0; list-style: none; }
.feedback-list li { display: flex; align-items: baseline; gap: 6px; padding: 4px 8px; border-radius: 8px; background: var(--info-soft); font-size: var(--fs-sm); }
.feedback-list .text { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: pre-wrap; }