Files
gcp_solution_architecture_a…/app/tools/schema_review.py
Jonathan Boniface e5e1647ff1
Some checks failed
validation / verify (push) Failing after 13s
feat: prove the lessons, check overlays in evaluation, and settle the cost steps
- Schema review: a dotted path is judged only when it starts at a name the
  resource type has, so a database flag or a service name is no longer read
  as a missing setting. Lesson L-04, which worked around that, is retired.
- Skill routing: in a Change request the question says what changed.
- Evaluation: a case may list code_standards the infrastructure code must
  meet; case 06 checks its overlay's regions, naming and labels.
- Model tiers (iteration 5 step 6): a lighter model for a role's first
  attempt, named in the provider bundle. Measured on case 03 and left off:
  Flash saved five cents on the IaC author and took twice the requests.
- Steps 7 and 9 decided against on the numbers, recorded in the plans.
- Plans: the runs of cases 05 and 06, and the first proof of the lessons on
  case 03 (no Revision, 22 minutes against 28, quality held).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:27:09 +01:00

162 lines
6.3 KiB
Python

"""A review of the Target architecture against the provider's schema, in code.
A Check that names a setting its Element's resource type does not have cannot be met
by any code. Left alone it costs a whole Revision: the code's author finds it, the
design is reopened, and everything after it is done again. In the run of 2026-10-07
that happened twice, with a schema lookup available to the Specialist that wrote the
Checks.
This reads the settings each Check names and looks them up, so the design is told
before it is submitted. It is a review, not a rule: a Check is prose, and a word that
looks like a setting may be a value. What it raises is returned once.
"""
from __future__ import annotations
import difflib
import logging
import re
from collections.abc import Mapping, Sequence
from typing import Any
from app.adk.runtime import ToolResult
from app.tools.local import TerraformRunner
from app.workflows.deliverables import DeliverableKind
from app.workflows.target_architecture import GRAPH_ATTACHMENT, GraphError, parse_graph
logger = logging.getLogger(__name__)
_QUOTED = re.compile(r"\"[^\"]*\"|'[^']*'")
_CODE = re.compile(r"`([^`]+)`")
# snake_case with at least one underscore, or a dotted path of lower-case names.
_NAME = re.compile(
r"(?<![\w/.\-])"
r"([a-z][a-z0-9]*(?:_[a-z0-9]+)+(?:\.[a-z][a-z0-9_]*)*"
r"|[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+)"
r"(?![\w/\-])"
)
_PATH = re.compile(r"^[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)*$")
def named_settings(statement: str) -> tuple[str, ...]:
"""The settings a Check's statement names, as written.
Anything in backticks that reads as a name or a dotted path, and bare
snake_case names and dotted paths. Quoted text is a value, so it is skipped,
as are paths such as roles/x.y.
"""
found: list[str] = []
for span in _CODE.findall(statement):
candidate = span.strip().split("=")[0].strip()
if _PATH.match(candidate):
found.append(candidate)
bare = _QUOTED.sub(" ", _CODE.sub(" ", statement))
found += [match.group(1).rstrip(".") for match in _NAME.finditer(bare)]
return tuple(dict.fromkeys(found))
def _names(block: Mapping[str, Any]) -> set[str]:
"""Every argument and block name anywhere in a resource type's schema."""
names = set(block.get("attributes") or {})
for name, nested in (block.get("block_types") or {}).items():
names.add(name)
names |= _names(nested.get("block") or {})
return names
def _is_missing(setting: str, known: set[str]) -> bool:
"""Whether a name a Check uses is a setting the resource type lacks.
A path is judged by the setting at its end, and only when it starts at a name
the type has. One that does not start there is a value written with dots, such
as a database flag (`cloudsql.iam_authentication`) or a service
(`run.googleapis.com`): both were raised as missing settings on 2026-10-09.
"""
first, _, rest = setting.partition(".")
if rest and first not in known:
return False
return setting.split(".")[-1] not in known
class SchemaReview:
def __init__(self, runner: TerraformRunner) -> None:
self._runner = runner
self._names: dict[str, set[str]] = {}
async def __call__(
self,
kind: DeliverableKind,
attachments: Mapping[str, str],
upstream: Mapping[str, Mapping[str, Any]] | None,
scope: str = "",
) -> Sequence[str]:
"""Resource types the provider does not have, and Checks naming a setting
their Element's resource type does not have."""
if kind is not DeliverableKind.TARGET_ARCHITECTURE:
return ()
try:
graph = parse_graph(attachments.get(GRAPH_ATTACHMENT, ""))
except GraphError:
return ()
schemas = await self._runner._load_schemas()
if isinstance(schemas, ToolResult):
logger.info("Schema review skipped: %s", schemas.message[:120])
return ()
concerns: list[str] = []
types = {element.id: element.resource_type for element in graph.elements}
for element in graph.elements:
if element.resource_type not in schemas:
close = difflib.get_close_matches(
element.resource_type, schemas, n=3, cutoff=0.6
)
hint = f" Close matches: {', '.join(close)}." if close else ""
concerns.append(
f"Element {element.id}: {element.resource_type} is not a "
f"resource type of the provider.{hint}"
)
for check in graph.checks:
resource_type = types.get(check.applies_to)
if resource_type not in schemas:
continue # a Check on a Relationship, or a type already raised
known = self._names.setdefault(
resource_type, _names(schemas[resource_type][1])
)
missing = [
setting
for setting in named_settings(check.statement)
if setting not in schemas and _is_missing(setting, known)
]
if missing:
concerns.append(
f"Check {check.id}: {resource_type} has no setting named "
f"{', '.join(missing)}, so no code can meet it there. Put the "
"Check on the Element whose resource type has that setting, "
"or reword it. Leave it if that word is a value, not a setting"
)
if concerns:
logger.info("[%s] schema review raised %d item(s)", scope, len(concerns))
return tuple(concerns)
class Reviews:
"""Several reviews as one: everything any of them raises."""
def __init__(self, reviews: Sequence[Any]) -> None:
self._reviews = tuple(reviews)
async def __call__(
self,
kind: Any,
attachments: Mapping[str, str],
upstream: Mapping[str, Mapping[str, Any]] | None,
scope: str = "",
) -> Sequence[str]:
raised: list[str] = []
for review in self._reviews:
try:
raised += await review(kind, attachments, upstream, scope)
except Exception: # one review failing must not lose the others
logger.exception("A submission review failed; continuing")
return tuple(raised)