Files
cthulhu-tentacles/tools/check.py
T

225 lines
12 KiB
Python

# Copyright (c) 2026 Storm Dragon
# SPDX-License-Identifier: GPL-3.0-or-later
"""Check the repository and the built Tentacle without running a reader."""
import json
import sys
import hashlib
import os
from pathlib import Path
import select
import subprocess
import tempfile
import tomllib
from urllib.parse import urlparse
def require(condition, message):
if not condition:
raise RuntimeError(message)
def read_toml(path):
with path.open("rb") as source:
return tomllib.load(source)
def check_metadata(repoRoot):
repository = read_toml(repoRoot / "repository.toml")
catalog = read_toml(repoRoot / "catalog.toml")
require(repository["format-version"] == catalog["format-version"] == 1,
"Unsupported metadata format")
require(repository["id"] == "storm.cthulhu-tentacles" and repository["name"],
"Repository identity is missing")
require([item["id"] for item in catalog["tentacles"]] == ["hello-world", "wormhole", "battery-status", "remote"],
"Catalog must list Hello World, Wormhole, Battery Status, and Remote")
for item in catalog["tentacles"]:
name = item["id"]
require(item["manifest"] == f"{name}/tentacle.toml", "Manifest path must match ID")
manifest = read_toml(repoRoot / item["manifest"])
require(manifest["format-version"] == 1 and manifest["id"] == name
and manifest["name"] and manifest["version"] == ({"wormhole":"0.1.2", "remote":"0.1.1"}.get(name, "0.1.0")), "Invalid Tentacle metadata")
expectedGrants = ["presentation"] + (["interaction"] if name in ("wormhole", "battery-status") else [])
if name == "remote":
expectedGrants = ["presentation", "interaction", "output-observe", "remote-present", "keyboard-capture",
"keyboard-inject", "braille-stream", "clipboard-exchange"]
expectedApis = (["commands", "session-events", "presentation", "interaction", "remote-access"]
if name == "remote" else ["commands"] + expectedGrants)
expectedProgram = "wormhole-tentacle" if name == "wormhole" else name
require(manifest["entry-point"] == [f"bin/{expectedProgram}"]
and manifest["capabilities"] == expectedGrants
and manifest["apis"] == expectedApis,
"Unexpected launch or grants")
require((repoRoot / "LICENSE").is_file() and (repoRoot / name / "LICENSE").is_file()
and (repoRoot / name / "README.md").is_file(), "License and help are required")
seen = set()
for release in item.get("releases", []):
key = (release["platform"], release["architecture"])
require(key not in seen and key[0] == "linux" and key[1] in ("amd64", "arm64"),
"Invalid or duplicate release target")
seen.add(key)
parsed = urlparse(release["url"])
require(parsed.scheme == "https" and parsed.hostname and not parsed.username
and not parsed.fragment, "Release URL must be HTTPS without credentials")
digest = release["sha256"]
require(len(digest) == 64 and all(character in "0123456789abcdef" for character in digest),
"Invalid SHA-256 digest")
artifactPath = repoRoot / "dist" / parsed.path.rsplit("/", 1)[-1]
if artifactPath.exists():
require(hashlib.sha256(artifactPath.read_bytes()).hexdigest() == digest,
"Prepared package does not match its catalog digest")
def check_registration(programPath, name):
apis = ["commands", "presentation"] + (["interaction"] if name in ("wormhole", "battery-status") else [])
with subprocess.Popen([str(programPath)], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
try:
initialization = {
"jsonrpc": "2.0", "id": "1", "method": "initialize",
"params": {"protocol": {"major": 1, "minor": 0}, "session": "check",
"apis": apis,
"capabilities": apis},
}
process.stdin.write((json.dumps(initialization) + "\n").encode())
process.stdin.flush()
require(select.select([process.stdout], [], [], 5)[0], "Registration timed out")
registration = json.loads(process.stdout.readline())
require(registration["id"] == "1" and registration["result"]["session"] == "check",
"Invalid registration response")
commands = registration["result"]["commands"]
require(len(commands) == (9 if name == "wormhole" else 2 if name == "battery-status" else 1) and commands[0]["id"] == name
and commands[0]["presentation"] is True, "Unexpected commands or grant")
require(commands[0]["bindings"] == [
{"layout": layout, "scope": "global", "key": ("Cthulhu+Ctrl+Shift+W" if name == "wormhole" else "Cthulhu+Shift+B" if name == "battery-status" else "Cthulhu+Alt+Shift+H")}
for layout in ("desktop", "laptop")], "Wrong suggested shortcut")
if name == "battery-status":
require(commands[0].get("interaction") is True
and commands[1]["id"] == "close-menu"
and commands[1].get("interaction") is True
and not commands[1].get("bindings")
and not any(command.get("settings") for command in commands),
"Battery Status must have one bound entry point and an unbound continuation")
if name == "wormhole":
require([command["id"] for command in commands if command.get("settings")]
== ["settings"], "Wrong registered settings entry point")
process.stdin.close()
require(process.wait(timeout=5) == 0, "Tentacle did not stop cleanly on EOF")
finally:
if process.poll() is None:
process.kill()
process.wait(timeout=5)
def check_host(repoRoot, programPath, name):
readerRoot = repoRoot.parent / "cthulhu"
environment = os.environ.copy()
environment.setdefault("GOCACHE", "/tmp/cthulhu-cache")
flags = ["-interaction"] if name in ("wormhole", "battery-status") else []
result = subprocess.run([
"go", "run", "./cmd/tentacle-host", "-presentation",
*flags, "-command", name, str(programPath),
], cwd=readerRoot, env=environment, capture_output=True, text=True, timeout=60, check=True)
if name == "battery-status":
value = json.loads(result.stdout)
form = value.get("form")
if form:
require(form.get("title") == "Battery Menu" and value.get("next") == "close-menu"
and len(form["elements"][0]["items"]) > 1,
"Host returned an unexpected Battery Menu")
else:
require(value.get("done") is True and value.get("speech")
and value.get("speech") == value.get("braille"),
"Host returned an unexpected battery announcement")
result = subprocess.run([
"go", "run", "./cmd/tentacle-host", "-presentation", "-interaction",
"-command", "close-menu", str(programPath),
], cwd=readerRoot, env=environment, capture_output=True, text=True,
timeout=60, check=True)
require(json.loads(result.stdout) == {"done": True},
"Battery menu completion must be silent")
return
if name == "wormhole":
value = json.loads(result.stdout)
require(value.get("form", {}).get("title") == "Wormhole menu"
or value == {"speech": "Magic wormhole is not installed",
"braille": "Magic wormhole is not installed", "uninterruptible": True},
"Host returned an unexpected Wormhole menu")
result = subprocess.run([
"go", "run", "./cmd/tentacle-host", "-presentation", "-interaction",
"-command", "settings", str(programPath),
], cwd=readerRoot, env=environment, capture_output=True, text=True,
timeout=60, check=True)
value = json.loads(result.stdout)
require(value.get("form", {}).get("title") == "Wormhole preferences"
and value.get("next") == "save-settings",
"Host returned an unexpected Wormhole settings form")
return
require(json.loads(result.stdout) == {
"speech": "Hello world!", "braille": "Hello world!", "uninterruptible": True,
}, "The actual host returned an unexpected greeting")
def check_remote_manifest(repoRoot, readerRoot):
"""Check Remote's protocol and grants against the matching reader SDK."""
manifest = read_toml(repoRoot / "remote/tentacle.toml")
require(manifest["format-version"] == 1 and manifest["id"] == "remote" and manifest["name"] == "Remote"
and manifest["runtime"] == "native" and manifest["platforms"] == ["linux"]
and manifest["entry-point"] == ["bin/remote"]
and manifest["protocol"] == {"major": 1, "minor": 1}, "Invalid Remote manifest")
require((repoRoot / "remote/LICENSE").is_file() and (repoRoot / "remote/README.md").is_file(),
"Remote license and help are required")
script = ("import json,sys; sys.path.insert(0, sys.argv[1]); "
"from cthulhu_tentacle.session import valid_negotiation; "
"print(json.dumps(valid_negotiation(json.loads(sys.argv[2]), json.loads(sys.argv[3]))))")
result = subprocess.run([sys.executable, "-I", "-c", script, str(readerRoot / "sdk/python"),
json.dumps(manifest["apis"]), json.dumps(manifest["capabilities"])],
capture_output=True, text=True, timeout=30, check=True)
require(json.loads(result.stdout) is True, "Remote manifest APIs and grants fail reader negotiation")
def check_remote(repoRoot):
programPath = repoRoot / "remote/bin/remote"
readerRoot = repoRoot.parent / "cthulhu"
check_remote_manifest(repoRoot, readerRoot)
environment = os.environ.copy()
environment["CTHULHU_REMOTE_BINARY"] = str(programPath)
with tempfile.TemporaryDirectory(prefix="cthulhu-remote-check-") as directory:
result = subprocess.run([
"go", "run", "./cmd/tentacle-host", "-presentation", "-interaction",
"-session-events", "-remote-grants", "output-observe,remote-present",
"-command", "remote", str(programPath), "--data-dir", str(Path(directory) / "state"),
], cwd=readerRoot, env=environment, capture_output=True, text=True,
timeout=60, check=True)
value = json.loads(result.stdout)
require(value.get("next") == "remote-select"
and value["form"]["title"] == "Remote"
and any(item.get("id") == "channel-key" and item.get("secret") for item in value["form"]["elements"]),
"Remote did not present its compact connection form with a masked key")
subprocess.run([
"go", "test", "./internal/app", "-run", "^(TestRemoteConnectionCommand|TestRemoteSpeech|TestRemoteDocument|TestRemoteReadAll|TestRemoteCurrentLine|TestRemoteNative|TestRemoteReview|TestRemoteFlatReview|TestRemoteWhereAmI|TestRemoteKeyboard)",
"-count=1", "-timeout=90s",
], cwd=readerRoot, env=environment, timeout=120, check=True)
subprocess.run([
"go", "test", "./internal/input", "./internal/remoteinput", "./internal/tentacle", "./internal/tentaclewire",
"./cmd/cthulhu-input-helper",
"-run", "^TestRemote(Speech|Background|Key)|^TestSessionSDKLifecycleConformance$|^TestHelper|^TestAdoptPipe|^TestSupervisor", "-count=1", "-timeout=60s",
], cwd=readerRoot, env=environment, timeout=90, check=True)
def main():
repoRoot = Path(__file__).resolve().parents[1]
programPath = repoRoot / "hello-world/bin/hello-world"
check_metadata(repoRoot)
for name, binary in (("hello-world", "hello-world"), ("wormhole", "wormhole-tentacle"),
("battery-status", "battery-status")):
programPath = repoRoot / name / "bin" / binary
check_registration(programPath, name)
check_host(repoRoot, programPath, name)
check_remote(repoRoot)
print("Repository metadata, existing Tentacle workflows and actual-host Remote consent/relay/speech/keyboard IPC checks passed.")
if __name__ == "__main__":
main()