Files
Seth Hobson 98862b56d8 feat: AGENTS.md canonical context + OpenAI harness-engineering layout (#542)
* feat: AGENTS.md canonical context + OpenAI harness-engineering layout

Promote AGENTS.md to the committed cross-harness context file (per the
agents.md convention and OpenAI's harness-engineering blog). Harness-
specific files become thin redirects:

- AGENTS.md          — canonical, committed (~74 lines, table-of-contents)
- CLAUDE.md          — `@AGENTS.md` import + Claude-specific addenda
- GEMINI.md          — Gemini-specific setup only
- .gemini/settings.json — redirects Gemini CLI's context to read AGENTS.md
- ARCHITECTURE.md    — new at root, top-level architectural map
- gemini-extension.json — bumps version to 1.7.0, sets contextFileName: AGENTS.md
- .gitignore         — drops the AGENTS.md entry (file is now committed)

Harness support verified:
- Codex CLI reads AGENTS.md natively (root → cwd walk, 32 KiB cap)
- Cursor 2.5+ reads AGENTS.md natively
- OpenCode reads AGENTS.md natively (wins over CLAUDE.md if both exist)
- Claude Code: `CLAUDE.md` first line is `@AGENTS.md` (Anthropic's
  documented interop pattern)
- Gemini CLI: `.gemini/settings.json` context.fileName redirect
  (Gemini doesn't support @-imports)

Codex adapter no longer generates AGENTS.md — `emit_global` instead
validates the committed file fits Codex's 32 KiB cap and the 150-line
table-of-contents convention. Tests updated. Clean-output target no
longer touches AGENTS.md.

## Auxiliary files updated for multi-harness reality

- `.github/ISSUE_TEMPLATE/bug_report.yml` — dropdown for harness +
  component path; renames "subagent" → "plugin/agent/skill/command"
- `.github/ISSUE_TEMPLATE/feature_request.yml` — scope dropdown covers
  framework / harness / tooling / docs / CI in addition to components
- `.github/ISSUE_TEMPLATE/new_subagent.yml` — relabeled "New Component
  Proposal" with component-type dropdown (plugin/agent/skill/command/
  harness adapter) and cross-harness portability field
- `.github/ISSUE_TEMPLATE/config.yml` — links to AGENTS.md, authoring
  guide, per-harness docs; updated Contributing link to root
- `.github/CONTRIBUTING.md` — thin pointer to canonical root CONTRIBUTING.md
- `.github/PULL_REQUEST_TEMPLATE.md` — new; scope + affected-harness
  checklists, test-plan checklist, portability-notes section
- CONTRIBUTING.md (root) — updated to reference AGENTS.md / ARCHITECTURE.md
- gemini-extension.json — version 1.6.0 → 1.7.0, count fixes, redirects
  to AGENTS.md as contextFileName

## Code-quality CI

New `.github/workflows/code-quality.yml` with three jobs:
- `python-lint` — `ruff check`, `ruff format --check`, `ty check` on
  the adapter framework + plugin-eval. yt-design-extractor.py legacy
  code excluded.
- `markdown-lint` — markdownlint-cli2 against README, AGENTS, ARCHITECTURE,
  CLAUDE, top-level guides, and docs/. Config in `.markdownlint.json`.
- `json-lint` — validates every JSON / TOML / YAML in the repo (excluding
  generated trees).

Required ty environment config added to plugin-eval/pyproject.toml so
`tools.adapters.*` resolves from outside the package.

Fixed one ty error in `tools/adapters/base.py:HarnessAdapter.capabilities`
(return-type annotation didn't match the `Capability` dataclass returned).
Fixed two ruff SIM108 ternary suggestions in codex.py and doc_gardener.py.
ruff format applied across all in-scope files (formatting-only diffs).

## Tests + verification

- 387 pytest tests pass (1 new test for the AGENTS.md validate-don't-overwrite behavior)
- `make validate STRICT=1` clean
- `make garden` 0 errors (10 warnings — remaining oversize source skills)
- `make smoke-test` clean against locally installed OpenCode/Gemini/Codex/Claude Code
- Real-CLI round-trip: `opencode agent list` discovers 193 subagents,
  `gemini extensions validate .` succeeds, all 191 Codex agent TOMLs parse

## Tag recommendations (separate task — for repo About panel)

Top 20 by reach + relevance (from `gh api search/repositories?q=topic:<tag>`):
automation mcp ai-agents developer-tools claude-code anthropic agentic-ai
agents prompt-engineering cursor multi-agent agent-skills orchestration
opencode workflows gemini-cli codex-cli claude-code-skills cursor-rules
claude-code-plugins

* fix(ci): YAML multi-doc + markdownlint scope/rules

Two CI failures on PR #542, both fixed:

## JSON/TOML/YAML syntax job (3 false-positive YAML errors)

The job's YAML validation used `yaml.safe_load` which only reads the first
document in a multi-document YAML stream. Three Kubernetes manifest
templates use the standard `---` document separator (valid YAML) and were
mis-flagged:

  plugins/kubernetes-operations/skills/k8s-manifest-generator/assets/configmap-template.yaml
  plugins/kubernetes-operations/skills/k8s-manifest-generator/assets/service-template.yaml
  plugins/kubernetes-operations/skills/k8s-security-policies/assets/network-policy-template.yaml

Switched to `list(yaml.safe_load_all(...))` so multi-doc YAML is accepted.

## Markdown lint job (lots of pre-existing plugin-README violations)

Two changes:

1. **Narrow the lint glob** — markdownlint now runs against top-level
   guides (README, AGENTS, ARCHITECTURE, CLAUDE, per-harness setup,
   CONTRIBUTING) and our authored `docs/` only. Per-plugin READMEs
   (`plugins/*/README.md`) are owned by their plugin authors and not
   lint-gated as part of this framework PR. Lint enforcement for those
   belongs at the plugin-author layer, not the framework PR layer.

2. **Tighten `.markdownlint.json`** — disable two rules that produce
   noise without catching real defects:
   - MD040 (fenced-code-language) — terminal output / shell command
     blocks frequently omit a language by convention
   - MD060 (table-column-style) — cosmetic table-pipe spacing; doesn't
     affect rendering

   Genuine formatting rules kept: MD029 (ol-prefix), MD031 (blanks-
   around-fences), MD032 (blanks-around-lists), MD056 (table-column-
   count), MD058 (blanks-around-tables).

## Real defects caught and fixed

The narrower scope still caught 5 real issues:

- `docs/agent-skills.md:397` — code fence inside an ordered list item
  needed a blank line before the fence
- `docs/authoring.md:90` — bulleted list needed a blank line above
- `docs/plugin-eval.md:87` — table needed a blank line above
- `OPENCODE.md:39` — table-column-count error caused by literal `|`
  inside backticks: ``mode: primary|subagent|all`` (3 cells reads as 5)
  Rewrote as ``mode:` one of `primary` / `subagent` / `all``

## Verification

- `npx markdownlint-cli2 "*.md" "docs/*.md"` → 0 errors
- `yaml.safe_load_all` accepts all multi-doc YAMLs (0 errors)
- All other CI jobs already passing (Python ruff/ty, multi-harness
  generate, CLI smoke test, plugin-eval pytest, tools pytest)

* fix: address PR #542 bot feedback

- Codex P2 (chatgpt-codex-connector): emit_global now reads AGENTS.md from
  the repo root (WORKTREE), not output_root. Previously `--output-root <scratch>`
  produced a false "missing" warning even when AGENTS.md was committed at the
  real root, breaking --strict generation outside the repo. Added a constructor
  arg `repo_root` so tests can stage a fake AGENTS.md without touching the
  committed file, plus a regression test that proves the two paths are decoupled.

- CodeRabbit nitpick (code-quality.yml): added workflow-level `permissions:
  contents: read` and `persist-credentials: false` on every checkout. Skipped
  the SHA-pinning recommendation — it's a heavier blanket-policy decision and
  the workflow has no write scope to abuse.

- CodeRabbit nitpick (pyproject.toml): consolidated the duplicate `dev` groups
  by moving `ty` into `[project.optional-dependencies].dev` and removing the
  now-empty `[dependency-groups]` block. Dropped `--group dev` from
  `code-quality.yml`'s `uv sync` since `--all-extras` now covers it.

- Verified gemini-extension.json counts (82/191/155/102) against the actual
  source-of-truth: 81 local plugins + 1 external = 82 in marketplace.json,
  191 agent .md files, 155 SKILL.md files, 102 command .md files. Counts are
  correct as-is — CodeRabbit's quick-win was a regex miscount.
2026-05-22 11:16:39 -04:00

477 lines
16 KiB
Python

"""Shared parsing, source models, and adapter base class for all harness adapters."""
from __future__ import annotations
import json
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from pathlib import Path
WORKTREE = Path(__file__).resolve().parent.parent.parent
PLUGINS_DIR = WORKTREE / "plugins"
# ── Parsing helpers (lifted from the original generate_gemini_commands.py) ────
def read_file(path: Path) -> str:
"""Read file content as UTF-8 string, returning '' on missing/unreadable file."""
try:
return path.read_text(encoding="utf-8")
except OSError:
return ""
def read_plugin_json(plugin_dir: Path) -> dict:
"""Read and parse plugin.json from plugin directory."""
path = plugin_dir / ".claude-plugin" / "plugin.json"
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except (OSError, json.JSONDecodeError):
return {}
def parse_frontmatter(content: str) -> tuple[dict, str]:
"""Return (fields, body). Tolerant YAML-ish parser; no external dep.
Supports:
- scalar fields (`name: foo`)
- inline lists (`tools: [a, b]`) and block lists (key: \\n - a\\n - b)
- YAML block scalar indicators `>` and `|` (folded/literal multi-line strings)
- 2-space continuation of scalar values
"""
fields: dict = {}
if not content.startswith("---"):
return fields, content
end = content.find("\n---", 3)
if end == -1:
return fields, content
block = content[3:end].strip()
body = content[end + 4 :].lstrip("\n")
current_key = None
in_list = False
in_block_scalar = False # True while inside `key: >` or `key: |` continuation
for line in block.splitlines():
m = re.match(r"^(\w[\w-]*):\s*(.*)", line)
if m:
current_key = m.group(1)
val = m.group(2).strip()
# Handle YAML block scalar indicators (>, >-, |, |-) — value continues on
# following indented lines; we collapse to a single space-joined string.
if val in (">", ">-", "|", "|-"):
fields[current_key] = ""
in_block_scalar = True
in_list = False
continue
in_block_scalar = False
# Inline list syntax: `tools: [a, b, c]` (single-line, balanced brackets).
if val.startswith("[") and val.endswith("]"):
inner = val[1:-1].strip()
if not inner:
fields[current_key] = []
else:
fields[current_key] = _split_inline_list(inner)
in_list = False
continue
val = val.strip('"')
if val == "[":
fields[current_key] = []
in_list = True
elif val == "":
# Empty scalar OR start of a block list (`tools:\n - item`). Initialize
# as empty STRING so downstream string consumers don't crash; if a `- item`
# continuation appears next, the list branch will replace the value.
fields[current_key] = ""
in_list = True
else:
fields[current_key] = val
in_list = False
elif (
in_block_scalar
and current_key
and (line.startswith(" ") or line.startswith("\t") or line == "")
):
text = line.strip()
if text:
existing = fields.get(current_key) or ""
fields[current_key] = (existing + " " + text).strip() if existing else text
elif in_list and (
isinstance(fields.get(current_key), list)
or (isinstance(fields.get(current_key), str) and fields[current_key] == "")
):
# YAML block-list items MUST be indented relative to the key (` - item`).
# A bare `- ...` at column 0 is not a list continuation — it could be a
# markdown bullet that the author accidentally placed after an empty key.
# Require indentation before treating as list-mode.
is_indented = line.startswith((" ", "\t"))
stripped = line.strip()
if is_indented and stripped.startswith("-"):
# Block-list item: `- item` — promote the empty-string sentinel to a list
# on first `- ` continuation so downstream consumers see a list.
if not isinstance(fields.get(current_key), list):
fields[current_key] = []
item = stripped[1:].strip().strip('"').strip("'")
if item:
fields[current_key].append(item)
elif is_indented and isinstance(fields.get(current_key), list):
# Inline-bracket continuation (rare); tolerate brackets/quotes
item = stripped.strip('",[] ')
if item and item != "]":
fields[current_key].append(item)
elif current_key and isinstance(fields.get(current_key), str) and line.startswith(" "):
fields[current_key] += " " + line.strip().strip('"')
return fields, body
def h1_from_body(body: str) -> str:
"""Extract the first H1 heading from Markdown body."""
for line in body.splitlines():
line = line.strip()
if line.startswith("# "):
return line[2:].strip()
return ""
def context_paragraph(body: str, max_chars: int = 300) -> str:
"""Extract the first substantive paragraph after the H1 heading."""
lines = body.splitlines()
past_h1 = False
collecting = False
paras: list[str] = []
for line in lines:
stripped = line.strip()
if stripped.startswith("# ") and not past_h1:
past_h1 = True
continue
if not past_h1:
continue
if stripped.startswith("#"):
if collecting:
break
continue
if stripped.startswith("You MUST") or stripped.startswith("## CRITICAL"):
break
if stripped:
collecting = True
paras.append(stripped)
elif collecting and paras:
break
text = " ".join(paras).strip()
if len(text) > max_chars:
text = text[: max_chars - 3] + "..."
return text
def token_estimate(text: str) -> int:
"""Rough heuristic: 1 token ≈ 4 characters. Good enough for context budget audits."""
return max(1, len(text) // 4)
def _split_inline_list(inner: str) -> list[str]:
"""Split a comma-separated YAML inline list, respecting single/double-quoted items.
Handles `tools: [Read, Grep]` → ['Read', 'Grep']
AND `tools: ["foo, bar", baz]` → ['foo, bar', 'baz']
"""
items: list[str] = []
buf: list[str] = []
quote: str | None = None
for ch in inner:
if quote:
if ch == quote:
quote = None
else:
buf.append(ch)
elif ch in ('"', "'"):
quote = ch
elif ch == ",":
item = "".join(buf).strip()
if item:
items.append(item)
buf = []
else:
buf.append(ch)
tail = "".join(buf).strip()
if tail:
items.append(tail)
return items
def split_tools_list(raw) -> list[str]:
"""Normalize a `tools:` frontmatter value (string or list) into a list of tool names."""
if isinstance(raw, list):
return [t.strip() for t in raw if t.strip()]
if isinstance(raw, str):
return [t.strip() for t in raw.split(",") if t.strip()]
return []
# ── Source-of-truth dataclasses ───────────────────────────────────────────────
@dataclass
class AgentSource:
"""One agent: plugins/<plugin>/agents/<name>.md."""
plugin: str
name: str
path: Path
frontmatter: dict
body: str
@property
def description(self) -> str:
return (self.frontmatter.get("description") or "").strip()
@property
def model(self) -> str:
return (self.frontmatter.get("model") or "inherit").strip()
@property
def tools(self) -> list[str]:
return split_tools_list(self.frontmatter.get("tools"))
@property
def color(self) -> str:
return (self.frontmatter.get("color") or "").strip()
@dataclass
class SkillSource:
"""One skill: plugins/<plugin>/skills/<name>/SKILL.md."""
plugin: str
name: str
dir: Path
frontmatter: dict
body: str
@property
def description(self) -> str:
return (self.frontmatter.get("description") or "").strip()
@property
def references_dir(self) -> Path | None:
d = self.dir / "references"
return d if d.is_dir() else None
@property
def body_bytes(self) -> int:
return len(self.body.encode("utf-8"))
@dataclass
class CommandSource:
"""One slash command: plugins/<plugin>/commands/<name>.md."""
plugin: str
name: str
path: Path
frontmatter: dict
body: str
@property
def description(self) -> str:
return (self.frontmatter.get("description") or "").strip()
@property
def argument_hint(self) -> str:
return (self.frontmatter.get("argument-hint") or "").strip()
@dataclass
class PluginSource:
"""Materialized view of one plugin's source tree."""
name: str
dir: Path
plugin_json: dict
agents: list[AgentSource] = field(default_factory=list)
skills: list[SkillSource] = field(default_factory=list)
commands: list[CommandSource] = field(default_factory=list)
@property
def description(self) -> str:
return (self.plugin_json.get("description") or "").strip()
@property
def version(self) -> str:
return (self.plugin_json.get("version") or "0.0.0").strip()
@property
def author(self) -> dict:
return self.plugin_json.get("author") or {}
def load_plugin(plugin_name: str) -> PluginSource | None:
"""Load a plugin's source tree from `plugins/<name>/`.
Plugin names with `__` are rejected: the adapter framework uses
`<plugin>__<leaf>` as a namespace separator across Codex/OpenCode/Gemini.
A plugin name containing `__` would break stale-detection and produce
ambiguous reverse-mappings in doc_gardener.
"""
if "__" in plugin_name:
# We use stderr-style print here only at the load site (no logger in this module).
import sys
print(
f"warning: skipping plugin `{plugin_name}` — plugin names must not contain "
"`__` (the adapter namespace separator).",
file=sys.stderr,
)
return None
plugin_dir = PLUGINS_DIR / plugin_name
if not plugin_dir.is_dir():
return None
plugin_json = read_plugin_json(plugin_dir)
plugin = PluginSource(name=plugin_name, dir=plugin_dir, plugin_json=plugin_json)
agents_dir = plugin_dir / "agents"
if agents_dir.is_dir():
for md in sorted(agents_dir.glob("*.md")):
fm, body = parse_frontmatter(read_file(md))
plugin.agents.append(
AgentSource(plugin=plugin_name, name=md.stem, path=md, frontmatter=fm, body=body)
)
skills_dir = plugin_dir / "skills"
if skills_dir.is_dir():
for sd in sorted(skills_dir.iterdir()):
skill_file = sd / "SKILL.md"
if not (sd.is_dir() and skill_file.is_file()):
continue
fm, body = parse_frontmatter(read_file(skill_file))
plugin.skills.append(
SkillSource(plugin=plugin_name, name=sd.name, dir=sd, frontmatter=fm, body=body)
)
commands_dir = plugin_dir / "commands"
if commands_dir.is_dir():
for md in sorted(commands_dir.glob("*.md")):
fm, body = parse_frontmatter(read_file(md))
plugin.commands.append(
CommandSource(plugin=plugin_name, name=md.stem, path=md, frontmatter=fm, body=body)
)
return plugin
def list_plugins() -> list[str]:
"""All plugin directory names under plugins/."""
if not PLUGINS_DIR.is_dir():
return []
return sorted(p.name for p in PLUGINS_DIR.iterdir() if p.is_dir())
# ── HarnessAdapter base class ─────────────────────────────────────────────────
@dataclass
class EmitResult:
"""What an adapter produced for one plugin (or globally)."""
written: list[Path] = field(default_factory=list)
skipped: list[str] = field(default_factory=list)
warnings: list[str] = field(default_factory=list)
class HarnessAdapter(ABC):
"""Base class for all per-harness adapters.
Subclasses declare a `harness_id` (matches `capabilities.CAPABILITIES` key)
and implement `emit_plugin` plus, optionally, `emit_global`.
"""
harness_id: str = ""
output_root: Path = WORKTREE
def __init__(self, output_root: Path | None = None) -> None:
if output_root is not None:
self.output_root = output_root
@property
def capabilities(self):
"""Return this harness's Capability dataclass (see capabilities.py)."""
from tools.adapters.capabilities import CAPABILITIES
return CAPABILITIES[self.harness_id]
@abstractmethod
def emit_plugin(self, plugin: PluginSource) -> EmitResult:
"""Emit all artifacts for one plugin into `self.output_root`."""
def emit_global(self, plugins: list[PluginSource]) -> EmitResult:
"""Emit cross-cutting artifacts (manifests, context files, marketplaces).
Default no-op. Adapters that need a marketplace.json, AGENTS.md, etc.
override this.
"""
return EmitResult()
# ── Shared utilities ──────────────────────────────────────────────────
def write(self, rel_path: str | Path, content: str) -> Path:
"""Write `content` to `self.output_root / rel_path`, creating parent dirs.
Refuses to write outside `self.output_root` (catches `..` segments and absolute paths).
"""
target = (self.output_root / rel_path).resolve()
root = self.output_root.resolve()
if not target.is_relative_to(root):
raise ValueError(f"refusing to write outside output_root: {target} (root={root})")
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(content, encoding="utf-8")
return target
def write_bytes(self, rel_path: str | Path, content: bytes) -> Path:
"""Binary counterpart of `write` — for mirroring non-UTF-8 reference assets."""
target = (self.output_root / rel_path).resolve()
root = self.output_root.resolve()
if not target.is_relative_to(root):
raise ValueError(f"refusing to write outside output_root: {target} (root={root})")
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(content)
return target
def mirror_file(self, src: Path, rel_path: str | Path) -> Path:
"""Copy `src` to `self.output_root / rel_path`, preserving binary content.
Use this for `references/` assets (PDFs, images, etc.) that may not be UTF-8 text.
"""
return self.write_bytes(rel_path, src.read_bytes())
def strip_claude_tool_refs(self, body: str, tool_case: str = "lower") -> str:
"""Rewrite Claude Code tool names embedded in prose into harness-neutral verbs.
Conservative — only matches "the <Tool> tool" and bare backticked tool names,
not arbitrary occurrences of words like 'Read' or 'Bash' which may be valid prose.
"""
replacements = {
"Read": "open" if tool_case == "lower" else "read",
"Edit": "edit",
"Write": "write",
"Bash": "shell",
"Grep": "rg",
"Glob": "glob",
"WebFetch": "fetch",
"WebSearch": "search",
"TodoWrite": "todo",
}
out = body
for camel, replacement in replacements.items():
out = re.sub(rf"\bthe `{camel}` tool\b", f"`{replacement}`", out)
out = re.sub(rf"\bthe {camel} tool\b", f"`{replacement}`", out)
if tool_case == "lower":
out = re.sub(rf"`{camel}`", f"`{camel.lower()}`", out)
return out