Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions board/copy_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Shared regression assertion for rendered dashboard copy buttons.

Use from a renderer's HTML tests, after rendering a representative page. This
checks the actual clipboard attributes, independently of skill-name registries
or prompt conversion helpers. It performs no network or filesystem access.
"""

import re
from html.parser import HTMLParser


class _CopyButtons(HTMLParser):
def __init__(self):
super().__init__(convert_charrefs=True)
self.count = 0
self.violations = []

def handle_starttag(self, tag, attrs):
if tag != "button":
return
attrs = dict(attrs)
terminal = "term" in (attrs.get("class") or "").split() or (attrs.get("aria-label") or "").lower().startswith("copy command:")
for key in ("data-cmd", "data-copy"):
if key not in attrs:
continue
self.count += 1
payload = attrs[key] or ""
# Absolute executable paths (/usr/bin/tool) are not slash skills.
# A bare /tool is ambiguous and must be marked as a terminal chip.
if not terminal and re.match(r"^\s*/[a-zA-Z][\w-]*(?=\s|$)", payload):
self.violations.append(f"{key}: {payload.strip()[:160]}")


def assert_portable_copy_payloads(page):
"""Fail on slash-skill clipboard payloads, or a vacuous page with no buttons.

Both data-cmd (board family) and data-copy (Eyes) are checked. HTML entities
are decoded by the parser; shell commands and explicit terminal chips are
preserved. This is an assertion only: never rewrite the renderer's output.
"""
parser = _CopyButtons()
parser.feed(page)
parser.close()
assert parser.count, "No copy payloads found; render an actionable dashboard fixture"
assert not parser.violations, "Assistant-specific copy payloads:\n" + "\n".join(
parser.violations)
4 changes: 4 additions & 0 deletions tests/test_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ def test_html_is_self_contained_with_one_tap_payloads(tmp_path):
"runs.json": _run_json("failure")}))
r = _run(["--html"], tmp_path, stub)
page = r.stdout
import sys
sys.path.insert(0, str(BRAIN_HOME / "board"))
from copy_contract import assert_portable_copy_payloads
assert_portable_copy_payloads(page)
# Self-containment: inline script and href anchors are allowed; external
# ASSETS are not (the invariant the Heart board's tests settled on).
# data-cmd payloads legitimately carry URLs, so strip them first.
Expand Down
46 changes: 46 additions & 0 deletions tests/test_copy_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""The output guard catches regressions without a hard-coded skill registry."""
import sys
from html import escape
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "board"))
from copy_contract import assert_portable_copy_payloads # noqa: E402


@pytest.mark.parametrize("attribute", ["data-cmd", "data-copy"])
@pytest.mark.parametrize("payload", ["/health", " /new_skill task\nKeep edits.",
"/future-skill --auto"])
def test_rejects_client_commands_in_either_clipboard_attribute(attribute, payload):
page = f'<button {attribute}="{escape(payload, quote=True)}">Copy</button>'
with pytest.raises(AssertionError, match="Assistant-specific copy payloads"):
assert_portable_copy_payloads(page)


def test_checks_entities_and_every_button_not_just_the_first():
page = '<button data-cmd="Use the health skill.">Copy</button>'
page += '<button data-copy="&#10;&#47;future_skill task">Copy</button>'
with pytest.raises(AssertionError, match="future_skill"):
assert_portable_copy_payloads(page)


@pytest.mark.parametrize("payload", ["Use the health skill. Inspect CI.",
"bash bin/morning.sh", "pyauto-heart tick && pyauto-heart readiness",
"/usr/bin/tool --flag", "Read /tmp/input and use the health skill."])
def test_prose_shell_commands_and_absolute_paths_are_allowed(payload):
assert_portable_copy_payloads(
f'<button data-cmd="{escape(payload, quote=True)}">Copy</button>')


@pytest.mark.parametrize("marker", ['class="copy term"',
'aria-label="copy command: run the check"'])
def test_explicit_terminal_buttons_can_copy_a_root_executable(marker):
assert_portable_copy_payloads(f'<button {marker} data-cmd="/tool --flag">Run</button>')


@pytest.mark.parametrize("page", ["", '<a href="/health">Health</a>',
'<script>const example = "/health";</script>'])
def test_missing_clipboard_buttons_cannot_pass_vacuously(page):
with pytest.raises(AssertionError, match="No copy payloads"):
assert_portable_copy_payloads(page)
2 changes: 2 additions & 0 deletions tests/test_cortex_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ def test_active_cards_lead_and_a_broken_tree_is_said_out_loud(tmp_skeleton):
def test_the_html_twin_wears_the_theme_with_real_copy_buttons(skeleton):
c = _cortex.census(skeleton)
html = _cortex.render_dashboard_html(c)
from copy_contract import assert_portable_copy_payloads
assert_portable_copy_payloads(html)
assert html.startswith("<!doctype html>")
assert "<!-- generated by `pyauto-brain cortex dashboard --apply` on " in html
assert _cortex._theme_css("cortex") in html
Expand Down
5 changes: 4 additions & 1 deletion tests/test_intake_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ def test_copy_details_never_swallow_the_next_row(tmp_path):


def _html(mind: Path) -> str:
return _intake.render_dashboard_html(_intake.census(mind))
from copy_contract import assert_portable_copy_payloads
page = _intake.render_dashboard_html(_intake.census(mind))
assert_portable_copy_payloads(page)
return page


def test_html_task_has_a_copy_button_holding_the_command(tmp_path):
Expand Down
Loading