agora.plan.api_spec
Parse the plan-builder’s shared API spec into structured module specs.
Sprint 7.5 — the single source of truth for API surface. The plan-builder’s
define_api task writes plan/api_spec.md with one section per module:
## module: src/core_domain.py
- class URLShortener:
def __init__(self) -> None: … def shorten(self, url: str) -> str: …
def helper(x: int) -> int: …
## module: src/persistence.py
def save(obj: URLShortener, path: str) -> None: … def load(path: str) -> URLShortener: …
This module parses that format and hands a structured view to the test scaffolder (for real imports) AND the impl-stub scaffolder (for matching signature stubs). Both agents see the SAME API — the “tester hallucinates, implementer disagrees” coordination failure becomes structurally impossible.
Functions
|
Return ONE |
|
Walk |
|
Parse the markdown API spec into a list of |
|
Render the src/ file stub from a |
|
Emit |
|
Remove any |
Classes
Class signature + its methods. |
|
A |
|
Top-level function signature. |
|
A single module's API surface. |
- class agora.plan.api_spec.ClassSpec[source]
Bases:
objectClass signature + its methods.
- methods: list[FunctionSpec]
- __init__(name, methods=<factory>)
- class agora.plan.api_spec.DeclaredModule[source]
Bases:
objectA
## module:section as declared in the source spec text.Exposes the raw body + parse status so validators can catch the case where
parse_api_spec()silently dropped a section because its body didn’t parse as Python. Validators can then surface “section for path X at line N failed to parse: <reason>” instead of letting the broken section vanish from downstream consumers.- parsed: ModuleSpec | None = None
- __init__(path, header_line, body, parsed=None, parse_error='')
- class agora.plan.api_spec.FunctionSpec[source]
Bases:
objectTop-level function signature.
- __init__(name, source)
- class agora.plan.api_spec.ModuleSpec[source]
Bases:
objectA single module’s API surface.
- functions: list[FunctionSpec]
- source: str = ''
Full parsed python source (with stub bodies) — the scaffolders can re-emit this directly to create matching src/ stub files.
- __init__(path, classes=<factory>, functions=<factory>, source='')
- agora.plan.api_spec.extract_declared_modules(spec_text)[source]
Return ONE
DeclaredModuleper## module:header.Unlike
parse_api_spec()which filters out unparseable sections, this includes every section along with its parse status. Used byapi_spec_is_validto catch silently-dropped sections, duplicate module paths, and stray prose that failed to parse.
- agora.plan.api_spec.find_unknown_method_calls(source, modules)[source]
Walk
sourceAST; return violations where code calls a method that doesn’t exist on a spec class.Sprint 7.6(g) — stops the recurring coordination failure where the tester’s test body calls
URLShortener().add(url)when the spec declaresadd_url. Framework validates method names at the tool boundary so the model can’t silently invent them.- Detection scope:
ClassName().method_name(...)— direct constructor chain.var = ClassName(...); var.method_name(...)— single-assignment aliases.ClassName.method_name(...)— class-method/staticmethod style.
- Ignored:
_nameand__name__— private/dunder are implementation detail.Attribute access on return values of unknown calls.
Any symbol not resolvable to a spec class (too noisy to flag).
Returns a list of human-readable violation messages (empty = OK).
- agora.plan.api_spec.parse_api_spec(spec_text)[source]
Parse the markdown API spec into a list of
ModuleSpec.Each
## module: <path>header starts a new module. The body until the next header (or EOF) is parsed as Python source —ast.parseextracts classes + top-level functions. Method signatures inside classes are captured viaClassSpec.methods.Returns empty list when no module sections are found (caller should fall back to free-form scaffolding). Unparseable module bodies are skipped with a best-effort partial parse — see
extract_declared_modules()when callers need to know about silently-dropped sections (e.g. theapi_spec_is_validpredicate).
- agora.plan.api_spec.render_impl_stub(module)[source]
Render the src/ file stub from a
ModuleSpec.The stub contains the exact signatures from the spec + placeholder bodies that raise
NotImplementedError. The implementer’s job is to replace each placeholder with a real body — the add_class_method and add_function tools (Sprint 7.4) upsert-by-name, so replacing a stub body is a single tool call per method/function.- Why
raise NotImplementedErrorinstead ofpass: Makes contract tests FAIL LOUDLY (not quietly pass) when the implementer hasn’t replaced the stub yet.
Catches “implementer forgot a method” at test time, not runtime.
- Why
- agora.plan.api_spec.render_test_imports(modules)[source]
Emit
from src.X import Y, Zlines for every named symbol across every module in the spec. Used by the contract test scaffolder so the test file’s imports WILL resolve against the generated src/ stubs.
- agora.plan.api_spec.strip_test_module_sections(spec_text)[source]
Remove any
## module: tests/...or## module: src/tests/...sections from an api_spec. Returns(cleaned_text, removed_paths).Test modules have no place in the api_spec — the test-authoring pipeline scaffolds them separately. Observed across runs (2026-04-22): GPT-4o-mini persistently adds a test module section even when the instruction and the C5 validator’s retry feedback both say “remove it”. Stripping at write time is always safe (the content is invalid if present) and saves the architect’s retry budget.
Deterministic line-level pass: preserves everything outside test sections verbatim, including blank lines and ordering.