agent-grammar 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ """agent-grammar: Test-gated AI agent workflow documentation for HTTP APIs."""
2
+
3
+ from agent_grammar.testing import AgentTestClient, step_boundary, workflow
4
+
5
+ __version__ = "0.1.2"
6
+
7
+ __all__ = ["AgentTestClient", "step_boundary", "workflow", "__version__"]
@@ -0,0 +1,65 @@
1
+ """ContextVar-based recorder lifecycle for the workflow capture pipeline."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from contextvars import ContextVar
6
+ from typing import Any
7
+
8
+ from agent_grammar._models import (
9
+ Binding,
10
+ BoundaryStep,
11
+ HttpStep,
12
+ WorkflowRecord,
13
+ slugify,
14
+ )
15
+
16
+
17
+ class WorkflowRecorder:
18
+ """Mutable builder collecting steps as a decorated test runs."""
19
+
20
+ def __init__(
21
+ self,
22
+ name: str,
23
+ intent: str,
24
+ bindings: list[Binding] | None = None,
25
+ ) -> None:
26
+ self.name = name
27
+ self.intent = intent
28
+ self.bindings: list[Binding] = list(bindings) if bindings else []
29
+ self.steps: list[HttpStep | BoundaryStep] = []
30
+ self.complete: bool = False
31
+
32
+ def add_http_step(self, step: HttpStep) -> None:
33
+ self.steps.append(step)
34
+
35
+ def add_boundary_step(self, step: BoundaryStep) -> None:
36
+ self.steps.append(step)
37
+
38
+ def mark_complete(self) -> None:
39
+ self.complete = True
40
+
41
+ def build(self) -> WorkflowRecord:
42
+ return WorkflowRecord(
43
+ name=self.name,
44
+ slug=slugify(self.name),
45
+ intent=self.intent,
46
+ bindings=list(self.bindings),
47
+ steps=list(self.steps),
48
+ )
49
+
50
+
51
+ _current_recorder: ContextVar[WorkflowRecorder | None] = ContextVar(
52
+ "agent_grammar_recorder", default=None
53
+ )
54
+
55
+
56
+ def get_active_recorder() -> WorkflowRecorder | None:
57
+ return _current_recorder.get()
58
+
59
+
60
+ def set_active_recorder(recorder: WorkflowRecorder | None) -> Any:
61
+ return _current_recorder.set(recorder)
62
+
63
+
64
+ def reset_recorder(token: Any) -> None:
65
+ _current_recorder.reset(token)
@@ -0,0 +1,50 @@
1
+ """Core data models for workflow recording and rendering."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from dataclasses import dataclass, field
7
+ from typing import Any, Union
8
+
9
+
10
+ @dataclass
11
+ class HttpStep:
12
+ method: str
13
+ path: str
14
+ request_json: Any | None
15
+ status_code: int
16
+ response_json: Any | None
17
+ domain: str = "Core Service"
18
+
19
+
20
+ @dataclass
21
+ class BoundaryStep:
22
+ domain: str
23
+ name: str
24
+
25
+
26
+ Step = Union[HttpStep, BoundaryStep]
27
+
28
+
29
+ @dataclass
30
+ class Binding:
31
+ source: str
32
+ target: str
33
+
34
+
35
+ @dataclass
36
+ class WorkflowRecord:
37
+ name: str
38
+ slug: str
39
+ intent: str
40
+ bindings: list[Binding] = field(default_factory=list)
41
+ steps: list[Step] = field(default_factory=list)
42
+
43
+
44
+ _SLUG_NON_ALNUM = re.compile(r"[^a-z0-9]+")
45
+
46
+
47
+ def slugify(name: str) -> str:
48
+ lowered = name.strip().lower()
49
+ collapsed = _SLUG_NON_ALNUM.sub("_", lowered)
50
+ return collapsed.strip("_")
@@ -0,0 +1 @@
1
+ """CLI entry points for agent-grammar."""
@@ -0,0 +1,92 @@
1
+ """``agent-grammar export-agent-docs`` subcommand."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ import click
8
+ from jinja2 import Environment, PackageLoader, select_autoescape
9
+
10
+ PLATFORMS = ("cursor", "claude", "copilot", "gemini")
11
+
12
+
13
+ def _build_environment() -> Environment:
14
+ return Environment(
15
+ loader=PackageLoader("agent_grammar", "templates"),
16
+ autoescape=select_autoescape(default=False),
17
+ trim_blocks=False,
18
+ lstrip_blocks=False,
19
+ keep_trailing_newline=True,
20
+ )
21
+
22
+
23
+ @click.command("export-agent-docs")
24
+ @click.option(
25
+ "--base-url",
26
+ required=True,
27
+ help="Production base URL of the API (e.g. https://api.example.com).",
28
+ )
29
+ @click.option(
30
+ "--api-version",
31
+ default="v1",
32
+ show_default=True,
33
+ help="API version namespace (used in the fetch URL and local file path).",
34
+ )
35
+ @click.option(
36
+ "--output-dir",
37
+ default="./agent-docs",
38
+ show_default=True,
39
+ type=click.Path(file_okay=False, dir_okay=True),
40
+ help="Directory to write the platform-specific system prompts.",
41
+ )
42
+ @click.option(
43
+ "--workflows-path",
44
+ default=None,
45
+ help=(
46
+ "Local file path where the agent should cache workflows. "
47
+ "Default: .agent/workflows_{version}.md"
48
+ ),
49
+ )
50
+ @click.option(
51
+ "--platform",
52
+ "platforms",
53
+ multiple=True,
54
+ type=click.Choice(PLATFORMS),
55
+ default=PLATFORMS,
56
+ show_default=True,
57
+ help="Which platform rules to generate. Repeat to select multiple.",
58
+ )
59
+ def export_agent_docs(
60
+ base_url: str,
61
+ api_version: str,
62
+ output_dir: str,
63
+ workflows_path: str | None,
64
+ platforms: tuple[str, ...],
65
+ ) -> None:
66
+ """Generate platform-specific system prompts for API consumers."""
67
+ base = base_url.rstrip("/")
68
+ workflows_path = workflows_path or f".agent/workflows_{api_version}.md"
69
+ fetch_url = f"{base}/{api_version}/agent-workflows"
70
+
71
+ output = Path(output_dir)
72
+ output.mkdir(parents=True, exist_ok=True)
73
+
74
+ env = _build_environment()
75
+ context = {
76
+ "base_url": base,
77
+ "api_version": api_version,
78
+ "workflows_path": workflows_path,
79
+ "fetch_url": fetch_url,
80
+ }
81
+
82
+ written: list[Path] = []
83
+ for platform in platforms:
84
+ template = env.get_template(f"{platform}.md.j2")
85
+ rendered = template.render(**context)
86
+ target = output / f"{platform}-rules.md"
87
+ target.write_text(rendered, encoding="utf-8")
88
+ written.append(target)
89
+
90
+ click.echo(f"Wrote {len(written)} file(s) to {output}:")
91
+ for path in written:
92
+ click.echo(f" - {path}")
@@ -0,0 +1,21 @@
1
+ """Top-level Click group for the ``agent-grammar`` console script."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import click
6
+
7
+ from agent_grammar import __version__
8
+ from agent_grammar.cli.export import export_agent_docs
9
+
10
+
11
+ @click.group()
12
+ @click.version_option(version=__version__, prog_name="agent-grammar")
13
+ def cli() -> None:
14
+ """agent-grammar: test-gated AI agent workflow documentation."""
15
+
16
+
17
+ cli.add_command(export_agent_docs)
18
+
19
+
20
+ if __name__ == "__main__": # pragma: no cover
21
+ cli()
@@ -0,0 +1 @@
1
+ """Server-side helpers for serving the compiled workflows.md."""
@@ -0,0 +1,100 @@
1
+ """FastAPI/Starlette helpers: GrammarRouter and AgentTelemetryMiddleware."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from pathlib import Path
7
+ from typing import Callable
8
+
9
+ from starlette.middleware.base import BaseHTTPMiddleware
10
+ from starlette.requests import Request
11
+ from starlette.responses import PlainTextResponse, Response
12
+
13
+ logger = logging.getLogger("agent_grammar.serve")
14
+
15
+ TELEMETRY_HEADER = "X-Agent-Grammar-Workflow"
16
+ MARKDOWN_MEDIA_TYPE = "text/markdown; charset=utf-8"
17
+
18
+
19
+ def GrammarRouter(
20
+ filepath: str | Path,
21
+ *,
22
+ reload: bool = False,
23
+ ):
24
+ """Return a FastAPI/Starlette router that serves the compiled markdown.
25
+
26
+ Args:
27
+ filepath: Path to the compiled workflows.md asset.
28
+ reload: If True, re-read the file on every request (dev mode).
29
+ If False, cache contents in memory at construction time.
30
+
31
+ Raises:
32
+ FileNotFoundError: If ``filepath`` does not exist at construction time.
33
+ ImportError: If FastAPI is not installed.
34
+ """
35
+ try:
36
+ from fastapi import APIRouter
37
+ except ImportError as exc: # pragma: no cover
38
+ raise ImportError(
39
+ "GrammarRouter requires FastAPI. Install with "
40
+ "`pip install agent-grammar[fastapi]`."
41
+ ) from exc
42
+
43
+ path = Path(filepath)
44
+ if not path.exists():
45
+ raise FileNotFoundError(
46
+ f"GrammarRouter: workflows file not found at {path!s}. "
47
+ "Did you run pytest to compile it?"
48
+ )
49
+
50
+ cached_content: str | None = None
51
+ if not reload:
52
+ cached_content = path.read_text(encoding="utf-8")
53
+
54
+ router = APIRouter()
55
+
56
+ @router.get("", include_in_schema=False)
57
+ @router.get("/", include_in_schema=False)
58
+ def _serve_workflows() -> PlainTextResponse:
59
+ if reload:
60
+ content = path.read_text(encoding="utf-8")
61
+ else:
62
+ assert cached_content is not None
63
+ content = cached_content
64
+ return PlainTextResponse(
65
+ content=content, media_type=MARKDOWN_MEDIA_TYPE
66
+ )
67
+
68
+ return router
69
+
70
+
71
+ class AgentTelemetryMiddleware(BaseHTTPMiddleware):
72
+ """Detect requests bearing ``X-Agent-Grammar-Workflow`` and report them.
73
+
74
+ The middleware never raises from the telemetry callback — failures are
75
+ logged and swallowed so business traffic is unaffected.
76
+ """
77
+
78
+ def __init__(
79
+ self,
80
+ app,
81
+ on_detect: Callable[[str], None] | None = None,
82
+ ) -> None:
83
+ super().__init__(app)
84
+ self.on_detect = on_detect
85
+
86
+ async def dispatch(self, request: Request, call_next) -> Response:
87
+ workflow_id = request.headers.get(TELEMETRY_HEADER)
88
+ response = await call_next(request)
89
+ if (
90
+ workflow_id
91
+ and self.on_detect is not None
92
+ and 200 <= response.status_code < 300
93
+ ):
94
+ try:
95
+ self.on_detect(workflow_id)
96
+ except Exception:
97
+ logger.exception(
98
+ "agent-grammar telemetry callback raised; ignoring."
99
+ )
100
+ return response
@@ -0,0 +1,10 @@
1
+ """Placeholder for Flask integration (planned for v0.2)."""
2
+
3
+ from __future__ import annotations
4
+
5
+
6
+ def GrammarBlueprint(*args, **kwargs): # pragma: no cover
7
+ raise NotImplementedError(
8
+ "Flask support is planned for v0.2. Use the FastAPI integration "
9
+ "in `agent_grammar.serve.fastapi` for now."
10
+ )
@@ -0,0 +1,22 @@
1
+ # CLAUDE.md — API Integration Directives
2
+
3
+ You are Claude, acting as an expert integration engineer. When the user asks you to write code that calls our services, you MUST follow the verified workflow grammar for the target API version below.
4
+
5
+ ## Target API Version: {{ api_version }}
6
+ *(If the user explicitly requests a different version, update the URL and local file path accordingly.)*
7
+
8
+ ## Mandatory Pre-flight Synchronization
9
+ Before generating any integration code:
10
+
11
+ 1. Check whether `{{ workflows_path }}` exists in the workspace.
12
+ 2. If it does not exist (or is stale), run:
13
+ ```bash
14
+ mkdir -p $(dirname {{ workflows_path }}) && curl -s -o {{ workflows_path }} {{ fetch_url }}
15
+ ```
16
+ 3. Read `{{ workflows_path }}` end-to-end before writing any code.
17
+
18
+ ## Generation Rules
19
+ - **Strict adherence:** Only use the sequences, HTTP methods, and payload structures defined in the versioned grammar. Do not invent endpoints.
20
+ - **Scope awareness:** `[Core Service]` rows are real endpoints — call them as documented. `[External/Mocked]` rows are conceptual placeholders — write custom local logic for them.
21
+ - **Parameter bindings:** Follow the "Precise Parameter Bindings & Payloads" table exactly. Pass response values forward as the bindings dictate.
22
+ - **Telemetry:** Every request to our service must include the header `X-Agent-Grammar-Workflow: [Workflow-ID]`, where `[Workflow-ID]` is the slug from the workflow you're implementing.
@@ -0,0 +1,20 @@
1
+ # GitHub Copilot — Custom Instructions for API Integration
2
+
3
+ You are GitHub Copilot, assisting an engineer who is writing integration code against our API. Follow the verified workflow grammar precisely.
4
+
5
+ ## Target API Version: {{ api_version }}
6
+ *(If the engineer is migrating to a new version, swap the version in the URL and file path below.)*
7
+
8
+ ## Mandatory Pre-flight Synchronization
9
+ 1. Check whether `{{ workflows_path }}` exists locally.
10
+ 2. If absent or stale, instruct the user to run:
11
+ ```
12
+ mkdir -p $(dirname {{ workflows_path }}) && curl -s -o {{ workflows_path }} {{ fetch_url }}
13
+ ```
14
+ 3. Read `{{ workflows_path }}` before suggesting integration code.
15
+
16
+ ## Generation Rules
17
+ - **Strict adherence:** Use only the documented sequences, HTTP methods, and payload structures.
18
+ - **Scope awareness:** `[Core Service]` endpoints are real and must be called as documented; `[External/Mocked]` steps require local glue code.
19
+ - **Bindings:** Honor the "Precise Parameter Bindings & Payloads" table.
20
+ - **Telemetry:** Every generated request must include `X-Agent-Grammar-Workflow: [Workflow-ID]` so the API owner can measure agent-driven adoption.
@@ -0,0 +1,17 @@
1
+ # Agent Directives for API Integration
2
+
3
+ You are an expert integration engineer. When asked to write code that interacts with our services, you MUST adhere to our verified workflow grammar for the target API version.
4
+
5
+ ## Target API Version: {{ api_version }}
6
+ *(Note: If the user requests upgrading to a newer API version, update the version in the URL and local file paths below accordingly).*
7
+
8
+ ## Mandatory Pre-flight Synchronization
9
+ 1. Check if the file `{{ workflows_path }}` exists in the local workspace.
10
+ 2. If it does NOT exist, or is stale, you MUST execute this command in the terminal to fetch it:
11
+ `mkdir -p $(dirname {{ workflows_path }}) && curl -s -o {{ workflows_path }} {{ fetch_url }}`
12
+ 3. Explicitly read `{{ workflows_path }}` before writing any integration code.
13
+
14
+ ## Generation Rules
15
+ * **Strict Adherence:** Only use the sequences, HTTP methods, and payload structures explicitly defined in the versioned grammar.
16
+ * **Scope Awareness:** Distinguish between `[Core Service]` endpoints (call exactly as documented) and `[External/Mocked]` steps (write custom local logic to bridge the gap).
17
+ * **Telemetry:** Always inject the following HTTP header into generated API client code: `X-Agent-Grammar-Workflow: [Workflow-ID]`
@@ -0,0 +1,20 @@
1
+ # Gemini Code Assist — API Integration Directives
2
+
3
+ You are Gemini Code Assist. When generating code that integrates with our service, conform to the verified workflow grammar.
4
+
5
+ ## Target API Version: {{ api_version }}
6
+ *(If the user upgrades versions, update the URL and local path below.)*
7
+
8
+ ## Mandatory Pre-flight Synchronization
9
+ 1. Verify that `{{ workflows_path }}` exists in the workspace.
10
+ 2. If it is missing or stale, run:
11
+ ```bash
12
+ mkdir -p $(dirname {{ workflows_path }}) && curl -s -o {{ workflows_path }} {{ fetch_url }}
13
+ ```
14
+ 3. Read `{{ workflows_path }}` before writing any integration code.
15
+
16
+ ## Generation Rules
17
+ - **Strict adherence:** Only the documented sequences, methods, and payloads are valid.
18
+ - **Scope awareness:** `[Core Service]` endpoints are real; `[External/Mocked]` rows are implementer responsibilities — write local logic for those.
19
+ - **Bindings:** Wire request fields per the "Precise Parameter Bindings & Payloads" table.
20
+ - **Telemetry:** Inject `X-Agent-Grammar-Workflow: [Workflow-ID]` into every request you generate.
@@ -0,0 +1,6 @@
1
+ """Public surface of the testing SDK."""
2
+
3
+ from agent_grammar.testing.client import AgentTestClient
4
+ from agent_grammar.testing.decorators import step_boundary, workflow
5
+
6
+ __all__ = ["AgentTestClient", "step_boundary", "workflow"]
@@ -0,0 +1,46 @@
1
+ """AgentTestClient: a TestClient subclass that records HTTP traffic."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+ from starlette.testclient import TestClient
8
+
9
+ from agent_grammar._context import get_active_recorder
10
+ from agent_grammar._models import HttpStep
11
+
12
+
13
+ def _safe_response_json(response: Any) -> Any | None:
14
+ try:
15
+ return response.json()
16
+ except (ValueError, Exception):
17
+ return None
18
+
19
+
20
+ class AgentTestClient(TestClient):
21
+ """Drop-in replacement for ``starlette.testclient.TestClient``.
22
+
23
+ When invoked inside a ``@workflow``-decorated test, each request is
24
+ captured as an ``HttpStep`` and appended to the active recorder.
25
+ """
26
+
27
+ def request( # type: ignore[override]
28
+ self,
29
+ method: str,
30
+ url: str,
31
+ *args: Any,
32
+ **kwargs: Any,
33
+ ) -> Any:
34
+ response = super().request(method, url, *args, **kwargs)
35
+ recorder = get_active_recorder()
36
+ if recorder is not None:
37
+ recorder.add_http_step(
38
+ HttpStep(
39
+ method=method.upper(),
40
+ path=str(url),
41
+ request_json=kwargs.get("json"),
42
+ status_code=response.status_code,
43
+ response_json=_safe_response_json(response),
44
+ )
45
+ )
46
+ return response
@@ -0,0 +1,89 @@
1
+ """Workflow decorator and step boundary context manager."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import functools
7
+ from contextlib import contextmanager
8
+ from typing import Any, Callable, Iterator
9
+
10
+ from agent_grammar._context import (
11
+ WorkflowRecorder,
12
+ get_active_recorder,
13
+ reset_recorder,
14
+ set_active_recorder,
15
+ )
16
+ from agent_grammar._models import Binding, BoundaryStep
17
+
18
+ RECORDER_ATTR = "_agent_grammar_recorder"
19
+
20
+
21
+ def _normalize_bindings(
22
+ raw: list[dict[str, str] | Binding] | None,
23
+ ) -> list[Binding]:
24
+ if not raw:
25
+ return []
26
+ out: list[Binding] = []
27
+ for item in raw:
28
+ if isinstance(item, Binding):
29
+ out.append(item)
30
+ else:
31
+ out.append(Binding(source=item["source"], target=item["target"]))
32
+ return out
33
+
34
+
35
+ def workflow(
36
+ *,
37
+ name: str,
38
+ intent: str,
39
+ bindings: list[dict[str, str] | Binding] | None = None,
40
+ ) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
41
+ """Decorate a pytest test function to record its HTTP/boundary steps.
42
+
43
+ The recorder is stashed on the wrapper as ``_agent_grammar_recorder`` so the
44
+ pytest plugin can collect it after the test passes.
45
+ """
46
+
47
+ normalized = _normalize_bindings(bindings)
48
+
49
+ def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
50
+ recorder = WorkflowRecorder(name=name, intent=intent, bindings=normalized)
51
+
52
+ if asyncio.iscoroutinefunction(func):
53
+
54
+ @functools.wraps(func)
55
+ async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
56
+ token = set_active_recorder(recorder)
57
+ try:
58
+ result = await func(*args, **kwargs)
59
+ recorder.mark_complete()
60
+ return result
61
+ finally:
62
+ reset_recorder(token)
63
+
64
+ setattr(async_wrapper, RECORDER_ATTR, recorder)
65
+ return async_wrapper
66
+
67
+ @functools.wraps(func)
68
+ def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
69
+ token = set_active_recorder(recorder)
70
+ try:
71
+ result = func(*args, **kwargs)
72
+ recorder.mark_complete()
73
+ return result
74
+ finally:
75
+ reset_recorder(token)
76
+
77
+ setattr(sync_wrapper, RECORDER_ATTR, recorder)
78
+ return sync_wrapper
79
+
80
+ return decorator
81
+
82
+
83
+ @contextmanager
84
+ def step_boundary(*, domain: str, name: str) -> Iterator[None]:
85
+ """Mark a non-HTTP step (e.g. database query, external service)."""
86
+ recorder = get_active_recorder()
87
+ if recorder is not None:
88
+ recorder.add_boundary_step(BoundaryStep(domain=domain, name=name))
89
+ yield
@@ -0,0 +1,98 @@
1
+ """Pytest plugin that aggregates passing workflows and writes ``workflows.md``."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ import pytest
9
+
10
+ from agent_grammar.testing.decorators import RECORDER_ATTR
11
+ from agent_grammar.testing.registry import WorkflowRegistry
12
+ from agent_grammar.testing.renderer import MarkdownRenderer
13
+
14
+ DEFAULT_OUTPUT = "assets/workflows.md"
15
+ REGISTRY_ATTR = "_agent_grammar_registry"
16
+
17
+
18
+ def pytest_addoption(parser: pytest.Parser) -> None:
19
+ group = parser.getgroup("agent-grammar")
20
+ group.addoption(
21
+ "--agent-grammar-output",
22
+ action="store",
23
+ dest="agent_grammar_output",
24
+ default=None,
25
+ help="Path to write the compiled workflows.md (default: assets/workflows.md).",
26
+ )
27
+ group.addoption(
28
+ "--agent-grammar-disable",
29
+ action="store_true",
30
+ dest="agent_grammar_disable",
31
+ default=False,
32
+ help="Skip writing the compiled workflows.md file.",
33
+ )
34
+ parser.addini(
35
+ "agent_grammar_output",
36
+ help="Path to write the compiled workflows.md (default: assets/workflows.md).",
37
+ default=DEFAULT_OUTPUT,
38
+ )
39
+
40
+
41
+ def pytest_configure(config: pytest.Config) -> None:
42
+ setattr(config, REGISTRY_ATTR, WorkflowRegistry())
43
+
44
+
45
+ def _get_recorder(item: pytest.Item) -> Any:
46
+ obj = getattr(item, "obj", None)
47
+ if obj is None:
48
+ return None
49
+ return getattr(obj, RECORDER_ATTR, None)
50
+
51
+
52
+ @pytest.hookimpl(hookwrapper=True)
53
+ def pytest_runtest_makereport(
54
+ item: pytest.Item, call: pytest.CallInfo[None]
55
+ ) -> Any:
56
+ outcome = yield
57
+ report = outcome.get_result()
58
+ if report.when != "call" or report.outcome != "passed":
59
+ return
60
+ recorder = _get_recorder(item)
61
+ if recorder is None or not recorder.complete:
62
+ return
63
+ registry: WorkflowRegistry | None = getattr(
64
+ item.config, REGISTRY_ATTR, None
65
+ )
66
+ if registry is None:
67
+ return
68
+ registry.add(recorder.build())
69
+
70
+
71
+ def _resolve_output_path(config: pytest.Config) -> Path:
72
+ cli_value = config.getoption("agent_grammar_output", default=None)
73
+ if cli_value:
74
+ return Path(cli_value)
75
+ ini_value = config.getini("agent_grammar_output") or DEFAULT_OUTPUT
76
+ return Path(ini_value)
77
+
78
+
79
+ def pytest_sessionfinish(
80
+ session: pytest.Session, exitstatus: int
81
+ ) -> None:
82
+ config = session.config
83
+ if config.getoption("agent_grammar_disable", default=False):
84
+ return
85
+ registry: WorkflowRegistry | None = getattr(
86
+ config, REGISTRY_ATTR, None
87
+ )
88
+ if registry is None or len(registry) == 0:
89
+ return
90
+ output = _resolve_output_path(config)
91
+ output.parent.mkdir(parents=True, exist_ok=True)
92
+ rendered = MarkdownRenderer().render(registry.records)
93
+ output.write_text(rendered, encoding="utf-8")
94
+ reporter = config.pluginmanager.get_plugin("terminalreporter")
95
+ if reporter is not None:
96
+ reporter.write_line(
97
+ f"[agent-grammar] wrote {len(registry)} workflow(s) to {output}"
98
+ )
@@ -0,0 +1,29 @@
1
+ """Session-scoped registry of workflow records collected during a test run."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import warnings
6
+
7
+ from agent_grammar._models import WorkflowRecord
8
+
9
+
10
+ class WorkflowRegistry:
11
+ """Collects ``WorkflowRecord`` instances from passing tests."""
12
+
13
+ def __init__(self) -> None:
14
+ self._records: dict[str, WorkflowRecord] = {}
15
+
16
+ def add(self, record: WorkflowRecord) -> None:
17
+ if record.slug in self._records:
18
+ warnings.warn(
19
+ f"Duplicate workflow slug '{record.slug}'; overwriting previous record.",
20
+ stacklevel=2,
21
+ )
22
+ self._records[record.slug] = record
23
+
24
+ @property
25
+ def records(self) -> list[WorkflowRecord]:
26
+ return list(self._records.values())
27
+
28
+ def __len__(self) -> int:
29
+ return len(self._records)
@@ -0,0 +1,155 @@
1
+ """Render ``WorkflowRecord`` instances to the markdown wire format."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from agent_grammar._models import (
6
+ Binding,
7
+ BoundaryStep,
8
+ HttpStep,
9
+ WorkflowRecord,
10
+ )
11
+
12
+
13
+ class MarkdownRenderer:
14
+ """Serializes workflow records to the V2 markdown wire format."""
15
+
16
+ HEADER = "# API Workflow Grammar Blueprint"
17
+
18
+ GLOBAL_REQ = (
19
+ "### Global Code Generation Requirement\n"
20
+ "Whenever you generate code for these workflows, you MUST include the "
21
+ "following HTTP header in all requests to our service to assist with "
22
+ "our internal metrics:\n"
23
+ "`X-Agent-Grammar-Workflow: [Workflow-ID]`"
24
+ )
25
+
26
+ def render(self, records: list[WorkflowRecord]) -> str:
27
+ parts: list[str] = [self.HEADER, ""]
28
+ for record in records:
29
+ parts.append(self._render_workflow(record))
30
+ parts.append("")
31
+ return "\n".join(parts).rstrip() + "\n"
32
+
33
+ def _render_workflow(self, record: WorkflowRecord) -> str:
34
+ chunks: list[str] = []
35
+ chunks.append(f"## Workflow: {record.name}")
36
+ chunks.append(f"* **ID:** `{record.slug}`")
37
+ chunks.append(f"* **Intent:** {record.intent}")
38
+ chunks.append("* **Status:** Verified / Test-Gated")
39
+ chunks.append("")
40
+ chunks.append(self.GLOBAL_REQ)
41
+ chunks.append("")
42
+ chunks.append("### 1. Ordered Execution Sequence")
43
+ chunks.append(self._render_steps_table(record.steps))
44
+ chunks.append("")
45
+ chunks.append("### 2. Precise Parameter Bindings & Payloads")
46
+ chunks.append(self._render_bindings_table(record.bindings, record.steps))
47
+ return "\n".join(chunks)
48
+
49
+ def _render_steps_table(
50
+ self, steps: list[HttpStep | BoundaryStep]
51
+ ) -> str:
52
+ lines = [
53
+ "| Step | Domain / Boundary | Action | Description |",
54
+ "|---|---|---|---|",
55
+ ]
56
+ for idx, step in enumerate(steps, start=1):
57
+ lines.append(self._render_step_row(idx, step))
58
+ return "\n".join(lines)
59
+
60
+ def _render_step_row(
61
+ self, idx: int, step: HttpStep | BoundaryStep
62
+ ) -> str:
63
+ if isinstance(step, HttpStep):
64
+ domain = "`[Core Service]`"
65
+ action = f"`{step.method} {step.path}`"
66
+ description = self._http_description(step)
67
+ return f"| {idx} | {domain} | {action} | {description} |"
68
+ # BoundaryStep
69
+ domain = "`[External/Mocked]`"
70
+ action = f"`{step.domain} Query`" if step.domain else "`External Action`"
71
+ description = (
72
+ f"{step.name}. (Implementer must write local logic here)."
73
+ )
74
+ return f"| {idx} | {domain} | {action} | {description} |"
75
+
76
+ def _http_description(self, step: HttpStep) -> str:
77
+ path = step.path.lower()
78
+ if "/auth" in path or "/token" in path:
79
+ return "Obtain standard JWT authorization token."
80
+ if step.method in {"POST", "PUT", "PATCH"}:
81
+ return "Submit payload to the documented endpoint."
82
+ if step.method == "GET":
83
+ return "Fetch resource from the documented endpoint."
84
+ if step.method == "DELETE":
85
+ return "Remove resource via the documented endpoint."
86
+ return "Invoke the documented endpoint."
87
+
88
+ def _render_bindings_table(
89
+ self,
90
+ bindings: list[Binding],
91
+ steps: list[HttpStep | BoundaryStep],
92
+ ) -> str:
93
+ lines = [
94
+ "| Target Input Field | Source Reference Property | Logic for Generated Code |",
95
+ "|---|---|---|",
96
+ ]
97
+ if not bindings:
98
+ lines.append("| _(none)_ | _(none)_ | _(none)_ |")
99
+ return "\n".join(lines)
100
+ for binding in bindings:
101
+ target_field = self._format_binding_endpoint(binding.target, steps)
102
+ source_ref = self._format_binding_source(binding.source, steps)
103
+ logic = self._derive_logic(binding)
104
+ lines.append(f"| `{target_field}` | `{source_ref}` | {logic} |")
105
+ return "\n".join(lines)
106
+
107
+ def _format_binding_endpoint(
108
+ self,
109
+ target: str,
110
+ steps: list[HttpStep | BoundaryStep],
111
+ ) -> str:
112
+ # "Step 3.headers.Authorization" -> "POST /v1/materials.headers.Authorization"
113
+ if target.startswith("Step "):
114
+ try:
115
+ head, rest = target.split(".", 1)
116
+ step_num = int(head.split(" ", 1)[1])
117
+ except (ValueError, IndexError):
118
+ return target
119
+ if 1 <= step_num <= len(steps):
120
+ step = steps[step_num - 1]
121
+ if isinstance(step, HttpStep):
122
+ return f"{step.method} {step.path}.{rest}"
123
+ return target
124
+
125
+ def _format_binding_source(
126
+ self,
127
+ source: str,
128
+ steps: list[HttpStep | BoundaryStep],
129
+ ) -> str:
130
+ # "Step 2.mocked_db_result" -> "Step 2 Database Query Result" when step 2 is a boundary
131
+ if source.startswith("Step "):
132
+ try:
133
+ head, rest = source.split(".", 1)
134
+ step_num = int(head.split(" ", 1)[1])
135
+ except (ValueError, IndexError):
136
+ return source
137
+ if 1 <= step_num <= len(steps):
138
+ step = steps[step_num - 1]
139
+ if isinstance(step, BoundaryStep):
140
+ return f"Step {step_num} {step.domain} Query Result"
141
+ return source
142
+
143
+ def _derive_logic(self, binding: Binding) -> str:
144
+ target_lower = binding.target.lower()
145
+ source_lower = binding.source.lower()
146
+ if target_lower.endswith(".headers.authorization"):
147
+ return (
148
+ "Extract token from Step 1 response and prefix with 'Bearer '."
149
+ )
150
+ if "mocked" in source_lower or "db" in source_lower:
151
+ return (
152
+ "Store the external DB zone ID in a variable and map it to "
153
+ "the JSON payload."
154
+ )
155
+ return "Map the source value into the target field."
@@ -0,0 +1,136 @@
1
+ Metadata-Version: 2.4
2
+ Name: agent-grammar
3
+ Version: 0.1.2
4
+ Summary: Test-gated AI agent workflow documentation for HTTP APIs.
5
+ Author-email: dlfelps <dlfelps@gmail.com>
6
+ License: MIT
7
+ License-File: LICENSE
8
+ Keywords: agents,ai,api,documentation,fastapi,llm,pytest
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Framework :: Pytest
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Testing
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: click>=8.1
20
+ Requires-Dist: httpx>=0.24
21
+ Requires-Dist: jinja2>=3.1
22
+ Requires-Dist: pytest>=7.0
23
+ Requires-Dist: starlette>=0.27
24
+ Provides-Extra: dev
25
+ Requires-Dist: fastapi>=0.100; extra == 'dev'
26
+ Requires-Dist: mypy; extra == 'dev'
27
+ Requires-Dist: pytest-cov; extra == 'dev'
28
+ Requires-Dist: ruff; extra == 'dev'
29
+ Provides-Extra: fastapi
30
+ Requires-Dist: fastapi>=0.100; extra == 'fastapi'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # agent-grammar
34
+
35
+ **Test-gated AI agent workflow documentation for HTTP APIs.**
36
+
37
+ `agent-grammar` lets API developers attach machine-readable workflow
38
+ documentation to their existing `pytest` suite. When integration tests pass,
39
+ a `workflows.md` blueprint is auto-compiled and served at a versioned route so
40
+ that external developers' AI agents (Cursor, Claude, Copilot, Gemini) can
41
+ fetch it and generate accurate integration code — no hallucinated endpoints,
42
+ no missing parameter bindings.
43
+
44
+ > Single source of truth: when business logic changes and tests are updated,
45
+ > the AI documentation re-compiles automatically.
46
+
47
+ ## Install
48
+
49
+ ```bash
50
+ pip install agent-grammar # core (pytest + serve via Starlette)
51
+ pip install "agent-grammar[fastapi]" # adds FastAPI for serving
52
+ ```
53
+
54
+ ## Quick Start
55
+
56
+ ### 1. Annotate an integration test
57
+
58
+ ```python
59
+ from agent_grammar import AgentTestClient, step_boundary, workflow
60
+ from app.main import app
61
+
62
+ client = AgentTestClient(app)
63
+
64
+ @workflow(
65
+ name="Material Onboarding Lifecycle",
66
+ intent="Secure a token, query the local DB for a zone, and register an asset.",
67
+ bindings=[
68
+ {"source": "Step 1.response.access_token", "target": "Step 3.headers.Authorization"},
69
+ {"source": "Step 2.mocked_db_result", "target": "Step 3.body.assigned_zone"},
70
+ ],
71
+ )
72
+ def test_compile_material_onboarding():
73
+ auth = client.post("/v1/auth/token", json={"seed": "dev-token"})
74
+ assert auth.status_code == 200
75
+ token = auth.json()["access_token"]
76
+
77
+ with step_boundary(domain="Database", name="Query PostgreSQL for Zone UUID"):
78
+ zone_id = "mocked-uuid-from-db-query"
79
+
80
+ resp = client.post(
81
+ "/v1/materials",
82
+ json={"sku": "MAT-9901", "assigned_zone": zone_id},
83
+ headers={"Authorization": f"Bearer {token}"},
84
+ )
85
+ assert resp.status_code == 201
86
+ ```
87
+
88
+ ### 2. Run the test suite
89
+
90
+ ```bash
91
+ pytest --agent-grammar-output=assets/workflows.md
92
+ ```
93
+
94
+ When the test passes, `assets/workflows.md` is compiled. Failing tests are
95
+ excluded — that's the "Test-Gated" guarantee.
96
+
97
+ ### 3. Serve the compiled markdown
98
+
99
+ ```python
100
+ from fastapi import FastAPI
101
+ from agent_grammar.serve.fastapi import AgentTelemetryMiddleware, GrammarRouter
102
+
103
+ app = FastAPI(title="Core Engine API - v1")
104
+
105
+ def log_agent_metric(workflow_id: str) -> None:
106
+ print(f"METRIC: agent-driven request for workflow {workflow_id}")
107
+
108
+ app.add_middleware(AgentTelemetryMiddleware, on_detect=log_agent_metric)
109
+ app.include_router(
110
+ GrammarRouter(filepath="assets/workflows.md"),
111
+ prefix="/v1/agent-workflows",
112
+ )
113
+ ```
114
+
115
+ ### 4. Publish system prompts for external developers
116
+
117
+ ```bash
118
+ agent-grammar export-agent-docs \
119
+ --base-url https://api.production.com \
120
+ --api-version v1
121
+ ```
122
+
123
+ Writes `./agent-docs/{cursor,claude,copilot,gemini}-rules.md` ready for the
124
+ API host's developer portal.
125
+
126
+ ## Configuration
127
+
128
+ | Option | Where | Default |
129
+ |---|---|---|
130
+ | `--agent-grammar-output` | pytest CLI | `assets/workflows.md` |
131
+ | `agent_grammar_output` | `pyproject.toml` `[tool.pytest.ini_options]` | `assets/workflows.md` |
132
+ | `--agent-grammar-disable` | pytest CLI | off |
133
+
134
+ ## License
135
+
136
+ MIT
@@ -0,0 +1,24 @@
1
+ agent_grammar/__init__.py,sha256=3XkKr3eIszdRKc9ZBCrzlTUhMyB-lnLwn1GG8xg9E_w,253
2
+ agent_grammar/_context.py,sha256=1pe4mc4wMTG5e9HfYhSJqYQoUCiZIoY0qBBFVaMMgmc,1637
3
+ agent_grammar/_models.py,sha256=ne43Jkx_pmOhuFVydM3k8KUTClWHlLIgPP__LlF1ZCc,898
4
+ agent_grammar/cli/__init__.py,sha256=ODdeNq_CdsXXwmU9RZw0mynwpKADH3hjJwPpvmbaJhM,42
5
+ agent_grammar/cli/export.py,sha256=HlcoLZDGLZ4jM_GXz_EOHmmoQZVrJ7iSW1WVESjbaf0,2561
6
+ agent_grammar/cli/main.py,sha256=E9U0QTlugdR97QEkzxT4_BEZ_LYExWmt0Dujye0Dx9E,485
7
+ agent_grammar/serve/__init__.py,sha256=eXQOepHsVUytvlxW9JoUuQkJTIPFXcW9Oua_aSGq0J0,65
8
+ agent_grammar/serve/fastapi.py,sha256=x9TZQ6MexvOqRN0_0vN-oyqsOQiKTPs8eNSocz5c5R0,3076
9
+ agent_grammar/serve/flask.py,sha256=wzn2T3nk8LMzizDuf2NBEromiOfioRQyGZCBjWYSeT0,320
10
+ agent_grammar/templates/claude.md.j2,sha256=ZylZrC6_uXy5r9j2NcHGhmhHOzwVSY70YSCGC0H3w1k,1444
11
+ agent_grammar/templates/copilot.md.j2,sha256=cS4am7Nz3L6Xpj3iJSX-jEE1vDOxDF-hJgD40u6_9Vw,1152
12
+ agent_grammar/templates/cursor.md.j2,sha256=XABqqJYQSXIydJGtAl1Ec9olKC1PT60-RjCanas4tP4,1239
13
+ agent_grammar/templates/gemini.md.j2,sha256=92d7dQfKrxEotxytLWF7yTl_WW6oWnvBEMMm7u1ch5s,1062
14
+ agent_grammar/testing/__init__.py,sha256=C0GWIwKIRL4E8QSpBfnAsX6a-Fv21D2dyEf-jZok6G4,228
15
+ agent_grammar/testing/client.py,sha256=pSHJXNvmRLmMT-OdFUvuoddNXpThKlE6cAhwP7XxVN4,1344
16
+ agent_grammar/testing/decorators.py,sha256=9kp9Gfmrotb_gHqdZqgAhEpsR8L7cJH1VBxVpgabyYw,2657
17
+ agent_grammar/testing/plugin.py,sha256=TYFOtGNom4V7_UFXl8-MVSshGQd_wZehFRRaLCxAbW0,3014
18
+ agent_grammar/testing/registry.py,sha256=L6nWEIUfaXEHao71sz_zIln_09wtpqITZ4f53a9QKYs,832
19
+ agent_grammar/testing/renderer.py,sha256=WBL96DTZd8BDhxNa0vQYJpXjob6omynT_cwF3mLXbAo,6001
20
+ agent_grammar-0.1.2.dist-info/METADATA,sha256=fkuQeODdJuJw-kngzG_AFPelSBkKjwtSqB1jQgS8x5s,4261
21
+ agent_grammar-0.1.2.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
22
+ agent_grammar-0.1.2.dist-info/entry_points.txt,sha256=98vLHZyXMIO4huDbXQ7xDFUI-WcZzHxUdUd5QUPtgV0,118
23
+ agent_grammar-0.1.2.dist-info/licenses/LICENSE,sha256=B_zMWTTRwQ_C1XMGtTF9GjhxC6e1JdC-vHKD2XF5izc,1064
24
+ agent_grammar-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,5 @@
1
+ [console_scripts]
2
+ agent-grammar = agent_grammar.cli.main:cli
3
+
4
+ [pytest11]
5
+ agent_grammar = agent_grammar.testing.plugin
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dlfelps
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.