fix: one schema lookup for a whole design, and larger graph batches
The target architect exhausted its 45 tool calls on a first attempt in three of five runs: 31 schema lookups and 29 graph_put calls for one design. Each time it carried on from its working graph, at a cost of four to ten minutes. - terraform_schema takes resource_types, a list, and outlines every argument and block each accepts, by name, in one reply. - The architect is told to make that one call once the Elements are decided, and to build the graph in a handful of calls of ten to twenty items. Case 04 then completed at 100% for the first time, the architect finishing in one attempt with 11 lookups and 13 graph calls. Plans record the run and correct three step statuses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,7 +17,14 @@ import json
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
from collections.abc import Awaitable, Callable, Iterable, Mapping, MutableMapping
|
||||
from collections.abc import (
|
||||
Awaitable,
|
||||
Callable,
|
||||
Iterable,
|
||||
Mapping,
|
||||
MutableMapping,
|
||||
Sequence,
|
||||
)
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path, PurePosixPath
|
||||
from typing import Any
|
||||
@@ -25,6 +32,8 @@ from typing import Any
|
||||
from app.adk.runtime import FILES_SCHEMA, ToolResult, ToolSpec, files_from_argument
|
||||
|
||||
_MAX_OUTPUT = 8_000
|
||||
_MAX_OUTLINE = 4_000 # one resource type, when several are looked up together
|
||||
_MAX_TYPES = 20 # resource types in one lookup
|
||||
_TERRAFORM_SUFFIXES = (".tf", ".tfvars", ".tf.json")
|
||||
_VARIABLE_NAME = re.compile(r"^[A-Za-z_][A-Za-z0-9_-]*$")
|
||||
_MERMAID_TYPES = (
|
||||
@@ -172,12 +181,52 @@ class TerraformRunner:
|
||||
)
|
||||
|
||||
async def schema(
|
||||
self, resource_type: str, block: str = "", find: str = ""
|
||||
self,
|
||||
resource_type: str = "",
|
||||
block: str = "",
|
||||
find: str = "",
|
||||
resource_types: Sequence[str] = (),
|
||||
) -> ToolResult:
|
||||
"""What a resource type accepts, from the pinned provider's own schema."""
|
||||
"""What a resource type accepts, from the pinned provider's own schema.
|
||||
|
||||
Several resource types at once come back as outlines: every argument and
|
||||
block each accepts, by name. That is one call for a whole design, where one
|
||||
call per type or per setting is what used up a Specialist's budget.
|
||||
"""
|
||||
loaded = await self._load_schemas()
|
||||
if isinstance(loaded, ToolResult):
|
||||
return loaded
|
||||
wanted = list(
|
||||
dict.fromkeys(
|
||||
name
|
||||
for name in [*resource_types, *re.split(r"[,\s]+", resource_type)]
|
||||
if name
|
||||
)
|
||||
)
|
||||
if not wanted:
|
||||
return ToolResult("Refused: name at least one resource type.")
|
||||
if len(wanted) > 1:
|
||||
parts = []
|
||||
for name in wanted[:_MAX_TYPES]:
|
||||
if name not in loaded:
|
||||
close = difflib.get_close_matches(name, loaded, n=3, cutoff=0.6)
|
||||
hint = f" Close matches: {', '.join(close)}." if close else ""
|
||||
parts.append(
|
||||
f"{name}: not a resource type of {self._provider_labels()}."
|
||||
f"{hint}"
|
||||
)
|
||||
continue
|
||||
parts.append(_outline_only(name, loaded[name][1]))
|
||||
if len(wanted) > _MAX_TYPES:
|
||||
parts.append(
|
||||
f"[{len(wanted) - _MAX_TYPES} more not shown: ask for at most "
|
||||
f"{_MAX_TYPES} at a time]"
|
||||
)
|
||||
return ToolResult(
|
||||
f"{self._provider_labels()}. * marks a required argument.\n\n"
|
||||
+ "\n\n".join(parts)
|
||||
)
|
||||
resource_type = wanted[0]
|
||||
if resource_type not in loaded:
|
||||
close = difflib.get_close_matches(resource_type, loaded, n=5, cutoff=0.6)
|
||||
hint = f" Close matches: {', '.join(close)}." if close else ""
|
||||
@@ -415,6 +464,31 @@ def _render_schema(heading: str, node: Mapping[str, Any]) -> str:
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _outline_only(resource_type: str, root: Mapping[str, Any]) -> str:
|
||||
"""One resource type as names alone: its arguments on a line, then every block
|
||||
with its arguments. Enough to see whether a setting exists and where."""
|
||||
lines = [resource_type, " " + (_names_line(root) or "-")]
|
||||
|
||||
def outline(parent: Mapping[str, Any], depth: int) -> None:
|
||||
for name, nested in sorted((parent.get("block_types") or {}).items()):
|
||||
inner = nested.get("block") or {}
|
||||
lines.append(f"{' ' * depth}{name} {{ }}: {_names_line(inner) or '-'}")
|
||||
outline(inner, depth + 1)
|
||||
|
||||
outline(root, 1)
|
||||
text = "\n".join(lines)
|
||||
if len(text) > _MAX_OUTLINE:
|
||||
text = text[:_MAX_OUTLINE] + "\n [cut short: look this type up alone]"
|
||||
return text
|
||||
|
||||
|
||||
def _names_line(node: Mapping[str, Any]) -> str:
|
||||
return ", ".join(
|
||||
f"{name}*" if attribute.get("required") else name
|
||||
for name, attribute in _arguments(node).items()
|
||||
)
|
||||
|
||||
|
||||
def _find_in_schema(title: str, root: Mapping[str, Any], wanted: str) -> str:
|
||||
"""Every argument or block whose name contains `wanted`, with where it sits."""
|
||||
needle = wanted.lower()
|
||||
@@ -512,10 +586,11 @@ LOCAL_TOOLS: dict[str, tuple[str, str]] = {
|
||||
"general",
|
||||
(
|
||||
"Look up what a Terraform resource type accepts, from the provider's own "
|
||||
"schema. With `resource_type` alone: every argument and block it has, "
|
||||
"which is enough to see whether a setting exists. `block` gives one "
|
||||
"block in full. `find` says where named settings sit, or that the "
|
||||
"resource type has none."
|
||||
"schema. With `resource_types`, a list: an outline of every argument "
|
||||
"and block each one accepts, in one call. With `resource_type` alone: "
|
||||
"the same for one type, with descriptions. `block` gives one block in "
|
||||
"full. `find` says where named settings sit, or that the resource type "
|
||||
"has none."
|
||||
),
|
||||
),
|
||||
}
|
||||
@@ -586,6 +661,14 @@ def local_tools(
|
||||
"type": "string",
|
||||
"description": "For example google_cloud_run_v2_service",
|
||||
},
|
||||
"resource_types": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": (
|
||||
"Several resource types in one call: each comes back "
|
||||
"as an outline of every argument and block it accepts"
|
||||
),
|
||||
},
|
||||
"block": {
|
||||
"type": "string",
|
||||
"description": "A nested block, such as template.containers",
|
||||
@@ -598,7 +681,7 @@ def local_tools(
|
||||
),
|
||||
},
|
||||
},
|
||||
"required": ["resource_type"],
|
||||
"required": [],
|
||||
},
|
||||
runner.schema,
|
||||
tier="general",
|
||||
|
||||
@@ -251,7 +251,7 @@ def graph_tools(working: MutableMapping[str, Any]) -> list[ToolSpec]:
|
||||
ToolSpec(
|
||||
"graph_put",
|
||||
"Add items to your working graph, or replace the ones with the same id. "
|
||||
"Call it several times with a few items each. Your working graph is "
|
||||
"Put ten to twenty items in a call, not one at a time. Your working graph is "
|
||||
"attached as target-architecture.json when you submit.",
|
||||
_schema(lists, []),
|
||||
graph_put,
|
||||
|
||||
Reference in New Issue
Block a user