Files
scientific-agent-skills/tests/_contract/structure.py
T
Timothy Kassis 4fb7e0bc29 Test every script-bearing skill, and enforce that it stays that way
AGENTS.md has always said that a skill shipping scripts/ puts its tests in
tests/<name>/, but nothing checked it: 54 of the 100 such skills had no suite
at all, including docx, pptx, xlsx, pdf and scanpy. All 100 now do.

tests/_meta is the guard. It runs the shared structural contract across every
skill in a single process -- safe because it parses scripts with ast and never
imports them -- and fails when a skill ships scripts/ without a suite or
without a [skills.<name>] entry in skill-requirements.toml. It needs no
scientific packages and finishes in seconds, so skill-tests.yml blocks every
pull request on it, plus the packages=[] suites.

tests/_contract holds what the per-skill suites were each reimplementing:
frontmatter conformance, the 500-line limit, no tests or bytecode under
skills/, local links resolving, scripts parsing, no eval/exec/os.system, no
standard-library shadowing, no hardcoded local paths, valid shell scripts. Also
the --help contract, which skips when a skill's packages are absent and runs
for real under --isolated, and shared behaviour for the files docx/pptx/xlsx
and five schematic-shipping skills carry byte-identical copies of, with drift
detection so they cannot diverge silently.

67 new suites, 3325 test functions. The existing suites were retrofitted: 18
no longer pin an exact skill version, so a version bump no longer breaks a
test; 11 duplicated structural methods removed; 19 wired to the --help
contract; and 5 that failed collection without their packages now skip
cleanly. run_all.py in the bare project environment goes from 6 failures to 0.

Writing the tests surfaced 19 defects in the skills, fixed here with version
bumps. The ones that changed scientific output:

  - openpiv reported vorticity 0 for a rotating flow, from a sign error in
    openpiv's y-up coordinate relabelling; solid-body rotation now gives 2w
    exactly, on grids of either orientation
  - deepchem returned solubility predictions in z-scored space while labelling
    them log(mol/L), because it transformed a y-less dataset instead of
    untransforming the output
  - neuropixels-analysis had the Allen and IBL ISI thresholds swapped,
    contradicting its own references/QUALITY_METRICS.md and inverting the two
    standards' relative strictness
  - scanpy's summarize() raised TypeError on every AnnData under anndata 0.13,
    which reports an unnamed None key on .layers; scanpy convert was broken
  - experimental-design's Latin hypercube was never reproducible: pyDOE3 draws
    from its own default_rng and ignores numpy's global seed
  - primekg shipped a hardcoded path naming a person, which is why
    no_personal_paths is now a contract rule

The remainder is upstream API drift, each verified against the installed
package: retired symbols in bioservices 1.16, gget helpers that returned lists
where a string was written, ArviZ 1.x kwargs in pymc, a positional-only
factory in pymoo, ReduceLROnPlateau(verbose=) in torch 2.13, a removed scvelo
parameter, and PyPDF2 in scientific-slides.

Two manifest environments could not build and are pinned: gget, where an
unpinned scanpy walked back to 1.9.8 and pulled llvmlite 0.36 which does not
compile on 3.13, and pymatgen, pinned to the snapshot its own _common.py
enforces rather than loosening that check. deepchem gains torch, without which
no model class exists.

python tests/run_all.py --isolated: 101 passed, 0 failed.
2026-07-28 10:20:12 -07:00

433 lines
16 KiB
Python

"""The structural contract every skill in this repository must satisfy.
Nothing here imports skill code. Scripts are read and parsed with `ast`, never
executed, so the whole contract is safe to run against every skill inside a
single interpreter -- which is what `tests/_meta` does. Per-skill suites that
*do* import their scripts still need one process each; see `tests/conftest.py`.
Every check takes a skill directory and returns a list of human-readable
problems, empty when the skill conforms. Returning problems rather than
asserting keeps the checks reusable: `tests/_meta` reports them per rule and
per skill, so one broken link names the file and the line rather than failing
an opaque assertion.
"""
from __future__ import annotations
import ast
import re
import subprocess
import sys
from collections.abc import Callable, Iterable
from functools import lru_cache
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
SKILLS_DIR = REPO_ROOT / "skills"
# The Agent Skills specification defines a closed set of top-level keys. Any
# other key is a validation error, and because strictyaml rejects the whole
# document, it takes `name` and `description` down with it.
ALLOWED_FRONTMATTER_FIELDS = frozenset(
{"name", "description", "license", "compatibility", "allowed-tools", "metadata"}
)
MAX_SKILL_MD_LINES = 500
# `__import__` is deliberately absent: several skills use it for a legitimate
# availability probe (`try: __import__("torch")`) or to reach pathlib before
# the sys.path insert that makes `_common` importable.
BANNED_BUILTIN_CALLS = frozenset({"eval", "exec"})
BANNED_OS_CALLS = frozenset({"system", "popen"})
# Inline-code and markdown-link references to files the skill ships.
_INLINE_PATH = re.compile(r"`((?:assets|references|scripts)/[A-Za-z0-9_./-]+)`")
_MARKDOWN_LINK = re.compile(r"\]\(((?:assets|references|scripts)/[A-Za-z0-9_./-]+)\)")
# An absolute path under someone's home or drive mount is a leaked local
# environment: it names a person, and it cannot work on any other machine.
_PERSONAL_PATH = re.compile(
r"/(?:mnt/[a-z]/Users|home|Users)/(?!<|\$|\{)([A-Za-z0-9._-]+)/"
)
# Service and platform accounts that legitimately appear in documentation --
# `/home/dnanexus/` is where a DNAnexus worker runs, not somebody's laptop --
# plus the placeholder spellings skills use for "your username".
_IMPERSONAL_ACCOUNTS = frozenset(
{
"dnanexus", "ubuntu", "root", "runner", "ec2-user", "jovyan", "vscode",
"airflow", "nextflow", "opt", "shared", "linuxbrew",
"user", "username", "you", "me", "youruser", "your-user", "name",
}
)
def script_bearing_skills(skills_dir: Path = SKILLS_DIR) -> list[Path]:
"""Skill directories that ship at least one file under `scripts/`.
An empty `scripts/` directory does not count. Git cannot track empty
directories, so one only ever exists as local cruft, and requiring a test
suite for a skill that ships no scripts would be nonsense.
"""
return [
skill
for skill in sorted(skills_dir.iterdir())
if skill.is_dir()
and (skill / "SKILL.md").is_file()
and any((skill / "scripts").rglob("*"))
]
def all_skill_names(skills_dir: Path = SKILLS_DIR) -> set[str]:
return {
skill.name
for skill in skills_dir.iterdir()
if skill.is_dir() and (skill / "SKILL.md").is_file()
}
def _frontmatter(skill: Path) -> str | None:
"""The raw YAML between the opening and closing `---`, or None."""
text = (skill / "SKILL.md").read_text(encoding="utf-8")
if not text.startswith("---\n"):
return None
end = text.find("\n---", 3)
if end == -1:
return None
return text[4:end]
def _top_level_entries(frontmatter: str) -> list[tuple[str, str]]:
"""(key, value) for every unindented `key: value` line."""
entries = []
for line in frontmatter.splitlines():
match = re.match(r"^([A-Za-z][A-Za-z0-9_-]*):(.*)$", line)
if match:
entries.append((match.group(1), match.group(2).strip()))
return entries
def _metadata_scalars(frontmatter: str) -> list[tuple[str, str]]:
"""(key, value) for scalars nested one level under `metadata:`.
Host manifest blocks (`openclaw`, `hermes`) nest further; their contents
are deliberately skipped -- the spec exempts them from the string-only
rule, and they carry booleans on purpose.
"""
lines = frontmatter.splitlines()
try:
start = next(i for i, line in enumerate(lines) if line.startswith("metadata:"))
except StopIteration:
return []
scalars = []
for line in lines[start + 1 :]:
if line.strip() and not line.startswith((" ", "\t")):
break
match = re.match(r"^ ([A-Za-z][A-Za-z0-9_-]*):(.*)$", line)
if match and match.group(2).strip():
scalars.append((match.group(1), match.group(2).strip()))
return scalars
def frontmatter_problems(skill: Path) -> list[str]:
"""Frontmatter parses, uses only spec fields, and is versioned."""
problems: list[str] = []
if not (skill / "SKILL.md").is_file():
return [f"{skill.name}: no SKILL.md"]
frontmatter = _frontmatter(skill)
if frontmatter is None:
return [f"{skill.name}: SKILL.md has no `---` delimited frontmatter"]
entries = _top_level_entries(frontmatter)
keys = [key for key, _ in entries]
for key in keys:
if key not in ALLOWED_FRONTMATTER_FIELDS:
problems.append(
f"{skill.name}: top-level `{key}` is not one of the six spec fields "
"-- move it under `metadata`"
)
for required in ("name", "description", "metadata"):
if required not in keys:
problems.append(f"{skill.name}: frontmatter is missing `{required}`")
values = dict(entries)
name = values.get("name", "").strip("\"'")
if name and name != skill.name:
problems.append(
f"{skill.name}: frontmatter name is `{name}`, must equal the directory name"
)
# strictyaml rejects JSON flow style outright, taking the whole document
# with it -- so `name` and `description` become unreadable too.
for key, value in entries:
if value.startswith(("{", "[")):
problems.append(
f"{skill.name}: `{key}` uses JSON flow style; strictyaml rejects it"
)
tools = values.get("allowed-tools")
if tools is not None and ("," in tools or tools.startswith("[")):
problems.append(
f"{skill.name}: `allowed-tools` must be a space-separated string"
)
scalars = dict(_metadata_scalars(frontmatter))
if "version" not in scalars:
problems.append(f"{skill.name}: `metadata.version` is required")
for key, value in scalars.items():
# Only values YAML would actually coerce. `1.0.0` has two dots, so it
# is already a string; `1.0` is a float and must be quoted.
ambiguous = re.fullmatch(
r"\d+|\d+\.\d+|true|false|yes|no|on|off|\d{4}-\d{2}-\d{2}", value, re.I
)
if ambiguous:
problems.append(
f"{skill.name}: `metadata.{key}: {value}` must be quoted to stay a string"
)
return problems
def length_problems(skill: Path) -> list[str]:
"""SKILL.md stays under 500 lines; longer material belongs in references/."""
lines = len((skill / "SKILL.md").read_text(encoding="utf-8").splitlines())
if lines > MAX_SKILL_MD_LINES:
return [
f"{skill.name}: SKILL.md is {lines} lines, over the {MAX_SKILL_MD_LINES}-line "
"limit -- move reference material into references/"
]
return []
def stray_test_problems(skill: Path) -> list[str]:
"""Tests never live under skills/ -- a skill ships only what an agent loads."""
strays = [
str(path.relative_to(skill))
for path in sorted(skill.rglob("*"))
if (path.is_dir() and path.name == "tests")
or (path.is_file() and path.name.startswith("test_") and path.suffix == ".py")
]
return [
f"{skill.name}: ships {stray}; tests belong in tests/{skill.name}/"
for stray in strays
]
@lru_cache(maxsize=1)
def _tracked_files(skills_dir: Path = SKILLS_DIR) -> frozenset[str] | None:
"""Repo-relative paths git tracks under `skills/`, or None outside a checkout."""
try:
listed = subprocess.run(
["git", "ls-files", "--", str(skills_dir)],
capture_output=True,
text=True,
timeout=60,
cwd=REPO_ROOT,
)
except (OSError, subprocess.SubprocessError):
return None
if listed.returncode != 0:
return None
return frozenset(listed.stdout.split())
def bytecode_problems(skill: Path) -> list[str]:
"""No compiled bytecode ships with a skill.
"Ships" means committed. Importing a skill's scripts is what creates
`__pycache__`, and running one by hand outside pytest -- which sets
`PYTHONDONTWRITEBYTECODE` -- leaves some behind, so scanning the working
tree would fail on local cruft that is already gitignored. Fall back to a
filesystem scan only when git cannot answer.
"""
tracked = _tracked_files()
candidates = [
path
for path in sorted(skill.rglob("*"))
if path.suffix in {".pyc", ".pyo"} or path.name == "__pycache__"
]
if tracked is not None:
candidates = [
path
for path in candidates
if str(path.relative_to(REPO_ROOT)) in tracked
or any(entry.startswith(f"{path.relative_to(REPO_ROOT)}/") for entry in tracked)
]
return [
f"{skill.name}: ships bytecode artifact {path.relative_to(skill)}"
for path in candidates
]
def link_problems(skill: Path, known_skills: Iterable[str] | None = None) -> list[str]:
"""Every `assets/`, `references/`, or `scripts/` path in the docs resolves.
Skills routinely point at each other -- bulk-rnaseq documents
`pathway-enrichment`'s `scripts/run_enrichment.py`, citation-management
points at `pyzotero` -> `references/exports.md`. A path that does not
resolve inside this skill is therefore accepted when another skill is
named on the same line and owns it.
"""
names = set(known_skills) if known_skills is not None else all_skill_names()
documents = [skill / "SKILL.md", *sorted((skill / "references").glob("*.md"))]
problems = []
for document in documents:
if not document.is_file():
continue
for number, line in enumerate(document.read_text(encoding="utf-8").splitlines(), 1):
for relative in set(_INLINE_PATH.findall(line)) | set(
_MARKDOWN_LINK.findall(line)
):
if (skill / relative).exists():
continue
owners = [
other
for other in names
if other != skill.name
and other in line
and (SKILLS_DIR / other / relative).exists()
]
if owners:
continue
problems.append(
f"{skill.name}: {document.name}:{number} references "
f"`{relative}`, which does not exist"
)
return problems
def _script_paths(skill: Path, suffix: str = ".py") -> list[Path]:
return sorted((skill / "scripts").rglob(f"*{suffix}"))
def compile_problems(skill: Path) -> list[str]:
"""Every bundled Python script parses -- a syntax error can never ship."""
problems = []
for path in _script_paths(skill):
try:
ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
except SyntaxError as error:
problems.append(
f"{skill.name}: {path.relative_to(skill)} does not parse: "
f"line {error.lineno}: {error.msg}"
)
return problems
def dynamic_execution_problems(skill: Path) -> list[str]:
"""No `eval`, `exec`, `os.system`, or `os.popen` in bundled scripts.
A narrower rule than the Cisco scanner's, and a complementary one: this is
deterministic and runs on every skill every time, where the scanner is
LLM-backed and runs only on changed skills.
"""
problems = []
for path in _script_paths(skill):
try:
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
except SyntaxError:
continue # compile_problems already reports it
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
function = node.func
if isinstance(function, ast.Name) and function.id in BANNED_BUILTIN_CALLS:
problems.append(
f"{skill.name}: {path.relative_to(skill)}:{node.lineno} "
f"calls {function.id}()"
)
if (
isinstance(function, ast.Attribute)
and function.attr in BANNED_OS_CALLS
and isinstance(function.value, ast.Name)
and function.value.id == "os"
):
problems.append(
f"{skill.name}: {path.relative_to(skill)}:{node.lineno} "
f"calls os.{function.attr}()"
)
return problems
def shadow_module_problems(skill: Path) -> list[str]:
"""No script shadows a standard-library module.
Tests and the scripts themselves put `scripts/` on `sys.path`, so a file
named `json.py` or `csv.py` there wins over the real module for the rest
of the process.
"""
return [
f"{skill.name}: scripts/{path.name} shadows the standard-library module "
f"`{path.stem}`"
for path in sorted((skill / "scripts").glob("*.py"))
if path.stem in sys.stdlib_module_names
]
def personal_path_problems(skill: Path) -> list[str]:
"""No script or document hardcodes somebody's home directory.
A path like `/mnt/c/Users/<name>/Data/kg.csv` names a real person and works
on exactly one machine. Placeholders are fine -- `/home/$USER/`,
`/Users/<you>/` -- so the pattern skips anything that looks templated.
"""
documents = [
skill / "SKILL.md",
*sorted((skill / "references").glob("*.md")),
*_script_paths(skill),
*_script_paths(skill, ".sh"),
]
problems = []
for document in documents:
if not document.is_file():
continue
for number, line in enumerate(
document.read_text(encoding="utf-8", errors="replace").splitlines(), 1
):
for match in _PERSONAL_PATH.finditer(line):
if match.group(1).lower() in _IMPERSONAL_ACCOUNTS:
continue
problems.append(
f"{skill.name}: {document.relative_to(skill)}:{number} hardcodes "
f"the local path `{match.group(0)}`"
)
return problems
def shell_script_problems(skill: Path) -> list[str]:
"""Bundled shell scripts have a shebang, the executable bit, and parse."""
problems = []
for path in _script_paths(skill, ".sh"):
relative = path.relative_to(skill)
first_line = path.read_text(encoding="utf-8").splitlines()[:1]
if not first_line or not first_line[0].startswith("#!"):
problems.append(f"{skill.name}: {relative} has no shebang")
if not path.stat().st_mode & 0o111:
problems.append(f"{skill.name}: {relative} is not executable")
syntax = subprocess.run(
["bash", "-n", str(path)], capture_output=True, text=True, timeout=30
)
if syntax.returncode != 0:
problems.append(
f"{skill.name}: {relative} fails `bash -n`: {syntax.stderr.strip()}"
)
return problems
#: Rule name -> check. `tests/_meta` iterates this so a new rule is picked up
#: repo-wide by adding one entry.
CHECKS: dict[str, Callable[[Path], list[str]]] = {
"frontmatter": frontmatter_problems,
"skill_md_length": length_problems,
"no_tests_under_skills": stray_test_problems,
"no_bytecode": bytecode_problems,
"local_links_resolve": link_problems,
"scripts_compile": compile_problems,
"no_dynamic_execution": dynamic_execution_problems,
"no_stdlib_shadowing": shadow_module_problems,
"no_personal_paths": personal_path_problems,
"shell_scripts": shell_script_problems,
}