127 lines
4.2 KiB
Python
127 lines
4.2 KiB
Python
"""Which references a Specialist is told to read first (iteration 5 step 8)."""
|
|
|
|
from pathlib import Path
|
|
|
|
from langgraph.checkpoint.memory import InMemorySaver
|
|
|
|
from execution_engine.adk.fake_runtime import FakeAgentRuntime, FakeDecider
|
|
from execution_engine.adk.runtime import Answer, NoDecider, RoleSpec
|
|
from execution_engine.agent import ExecutionEngine
|
|
from execution_engine.nodes.router import Router, Thresholds
|
|
from execution_engine.recorder import InMemoryRecorder
|
|
from execution_engine.tools.skill_routing import read_first, references_of, routable
|
|
from tests.reference_workflow.workflow import (
|
|
DEPENDENCY_GRAPH,
|
|
WORKFLOW,
|
|
sample_submission,
|
|
)
|
|
from tests.reference_workflow.workflow import DeliverableKind as K
|
|
|
|
PATTERNS = {
|
|
"references/cloud-sql.md": "Cloud SQL. A private PostgreSQL instance.",
|
|
"references/pubsub.md": "Pub/Sub. A topic and a subscription.",
|
|
}
|
|
|
|
|
|
def skill(root: Path, name: str, references: dict[str, str]) -> Path:
|
|
folder = root / name
|
|
(folder / "references").mkdir(parents=True)
|
|
(folder / "SKILL.md").write_text(f"---\nname: {name}\ndescription: d\n---\n")
|
|
for file, text in references.items():
|
|
(folder / "references" / file).write_text(text)
|
|
return folder
|
|
|
|
|
|
def test_a_reference_is_described_by_its_title_and_first_paragraph(tmp_path):
|
|
folder = skill(
|
|
tmp_path,
|
|
"patterns",
|
|
{"kms.md": "# Cloud KMS\n\nA key ring and a key.\n\n- A rule.\n"},
|
|
)
|
|
|
|
assert references_of(folder) == {
|
|
"references/kms.md": "Cloud KMS. A key ring and a key"
|
|
}
|
|
|
|
|
|
def test_only_a_skill_with_enough_references_is_routed_within(tmp_path):
|
|
many = skill(tmp_path, "many", {f"r{n}.md": f"# R{n}\n\nText.\n" for n in range(4)})
|
|
few = skill(tmp_path, "few", {"one.md": "# One\n\nText.\n"})
|
|
none = tmp_path / "none"
|
|
none.mkdir()
|
|
|
|
assert list(routable({"many": many, "few": few, "none": none})) == ["many"]
|
|
|
|
|
|
async def test_the_references_a_task_needs_are_named():
|
|
decider = FakeDecider(
|
|
{
|
|
"patterns/references/cloud-sql.md": Answer(1.8, 0.9),
|
|
"patterns/references/pubsub.md": Answer(0.1, 0.9),
|
|
}
|
|
)
|
|
|
|
chosen = await read_first(decider, "Write the code.", {"patterns": PATTERNS})
|
|
|
|
assert chosen == [{"skill": "patterns", "resource": "references/cloud-sql.md"}]
|
|
_, questions, _ = decider.asked[0]
|
|
assert "Cloud SQL. A private PostgreSQL instance." in (
|
|
questions["patterns/references/cloud-sql.md"].instructions
|
|
)
|
|
|
|
|
|
async def test_without_a_decision_nothing_is_named():
|
|
assert await read_first(NoDecider(), "Write.", {"patterns": PATTERNS}) is None
|
|
|
|
|
|
async def test_a_specialist_with_routable_skills_is_told_where_to_start(tmp_path):
|
|
roles = {
|
|
"orchestrator": RoleSpec("orchestrator", "Unused.", budget=40),
|
|
**{
|
|
spec.specialist: RoleSpec(spec.specialist, "Produce.", budget=15)
|
|
for spec in DEPENDENCY_GRAPH.values()
|
|
},
|
|
}
|
|
seen: dict[str, object] = {}
|
|
|
|
def recording(kind):
|
|
def turn(task):
|
|
seen[kind.value] = task.context["read_first"]
|
|
return [sample_submission(kind)]
|
|
|
|
return turn
|
|
|
|
decider = FakeDecider(
|
|
{
|
|
"patterns/references/cloud-sql.md": Answer(0.2, 0.9),
|
|
"patterns/references/pubsub.md": Answer(1.9, 0.9),
|
|
}
|
|
)
|
|
author = DEPENDENCY_GRAPH[K.DEPLOYMENT_GUIDANCE].specialist
|
|
engine = ExecutionEngine(
|
|
workflow=WORKFLOW,
|
|
runtime=FakeAgentRuntime(
|
|
{
|
|
spec.specialist: [recording(kind)]
|
|
for kind, spec in DEPENDENCY_GRAPH.items()
|
|
}
|
|
),
|
|
roles=roles,
|
|
recorder=InMemoryRecorder(),
|
|
checkpointer=InMemorySaver(),
|
|
output_root=tmp_path,
|
|
orchestrator=Router(NoDecider(), None, Thresholds()),
|
|
decider=decider,
|
|
skill_references={author: {"patterns": PATTERNS}},
|
|
)
|
|
|
|
await engine.start("Ingest webhooks", pre_approved=True)
|
|
|
|
assert seen[K.DEPLOYMENT_GUIDANCE.value] == [
|
|
{"skill": "patterns", "resource": "references/pubsub.md"}
|
|
]
|
|
assert seen[K.REQUIREMENTS.value] is None
|
|
state, _, _ = decider.asked[0]
|
|
assert state.startswith("Write the deployment guidance Deliverable")
|
|
assert "Elements of the Target architecture:" in state
|