Files
gcp_solution_architecture_a…/app/tools/schema_review.py
Jonathan Boniface 58e778ca7a feat: record verdicts in pieces and check the design against the schema
From the case 02 run cancelled at 66 minutes on 2026-10-07:

- The validator records verdicts a few Checks at a time (verdict_put) and
  they are attached on submission. It timed out five times writing them in
  one reply. Verdicts on the same code survive a run cut short; verdicts on
  changed code are dropped. Its model timeout is 300 s.
- A schema review at submission, in code: every setting a Check names is
  looked up on its Element's resource type, and an unknown resource type is
  reported. Two Revisions in that run were for Checks no code could meet.
  On the two designs that passed validation it raises 2 of 96 Checks, both
  one metric name used as a value, so it is returned once and never blocks.
- Reviews run together; the Jev coverage review stays off.

Plans record both steps and the cancelled run.

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

150 lines
5.8 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
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)
# A resource type named in the statement is not a setting, and a
# path is judged by the setting at its end.
if setting not in schemas and setting.split(".")[-1] not in 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)