Files

180 lines
7.2 KiB
Python

"""Helpers for tests about what comes after scoping.
Every Execution now begins with the scoping Phase (ADR-0008). A test about
requirements, design or validation is not about that, so these run scoping ahead
of the test's own script and leave the test where it always started: requirements
ready, nothing recorded of how the scope was agreed.
The work breakdown structure (iteration 10) is produced beside the solution guide.
A script written before it existed delegates the guide alone; here the plan is
delegated with it, and planned without a script.
"""
from __future__ import annotations
import dataclasses
from typing import Any
from execution_engine.adk.fake_runtime import FakeAgentRuntime
from execution_engine.adk.runtime import Submitted
from execution_engine.agent import ExecutionEngine, Interaction
from tests.reference_workflow.workflow import (
DEPENDENCY_GRAPH,
WORKFLOW,
DeliverableKind,
PhaseId,
sample_submission,
)
SCOPE_ANALYST = DEPENDENCY_GRAPH[DeliverableKind.SCOPE].specialist
PLANNER = DEPENDENCY_GRAPH[DeliverableKind.WORK_BREAKDOWN].specialist
ORCHESTRATOR = "orchestrator"
def _with_the_plan(tool): # type: ignore[no-untyped-def]
"""`delegate`, delegating the work breakdown structure whenever the solution
guide is delegated."""
if tool.name != "delegate":
return tool
guide, plan = (
DeliverableKind.SOLUTION_GUIDE.value,
DeliverableKind.WORK_BREAKDOWN.value,
)
async def delegate(kinds, **rest): # type: ignore[no-untyped-def]
if guide in kinds and plan not in kinds:
kinds = [*kinds, plan]
return await tool.handler(kinds=kinds, **rest)
return dataclasses.replace(tool, handler=delegate)
class ScopedRuntime(FakeAgentRuntime):
"""A scripted runtime that first has the scope produced and its Approval asked
for, outside the script and outside `runs`."""
def __init__(self, turns: Any) -> None:
super().__init__(turns)
self._next = "delegate"
async def run(self, role, task, tools): # type: ignore[no-untyped-def]
by_name = {tool.name: tool for tool in tools}
if role.name == PLANNER and not self.remaining(PLANNER):
call = sample_submission(DeliverableKind.WORK_BREAKDOWN)
return Submitted(call.args["content"], call.args["attachments"])
if role.name == ORCHESTRATOR and "delegate" in by_name:
tools = [_with_the_plan(tool) for tool in tools]
if role.name == SCOPE_ANALYST and not self.remaining(SCOPE_ANALYST):
# Submitted as it stands: a test about a later Deliverable's draft is
# not asking whether the scope's draft was shown first.
call = sample_submission(DeliverableKind.SCOPE)
return Submitted(call.args["content"], call.args["attachments"])
if role.name == ORCHESTRATOR and self._next == "delegate":
self._next = "approve"
asked = await by_name["delegate"].handler(
kinds=[DeliverableKind.SCOPE.value]
)
return asked.outcome
if role.name == ORCHESTRATOR and self._next == "approve":
self._next = "done"
asked = await by_name["request_approval"].handler(
phase=PhaseId.SCOPING.value, summary="The scope is ready."
)
if asked.outcome is not None:
return asked.outcome # a user is present: their Approval is awaited
return await super().run(role, task, tools)
class ScopedEngine(ExecutionEngine):
"""An engine whose `start` grants the scoping Approval when it is asked for, so
an interactive test meets the first prompt it was written about. It runs the
example Workflow unless a test gives another."""
def __init__(self, **options: Any) -> None:
options.setdefault("workflow", WORKFLOW)
super().__init__(**options)
async def start(self, *args: Any, **kwargs: Any) -> Interaction:
interaction = await super().start(*args, **kwargs)
approval = (interaction.prompt or {}).get("approval") or {}
if approval.get("phase") == PhaseId.SCOPING.value:
interaction = await self.resume(
interaction.execution_id,
{"approval": {"phase": PhaseId.SCOPING.value, "decision": "approve"}},
)
return interaction
def scoped_ledger(**options: Any): # type: ignore[no-untyped-def]
"""A Ledger whose scope is agreed: where requirements discovery begins."""
from execution_engine.states.ledger import Ledger, PhaseStatus
ledger = Ledger(WORKFLOW, **options)
attempt = ledger.start([DeliverableKind.SCOPE], actor=ORCHESTRATOR)
ledger.submit(
DeliverableKind.SCOPE, attempt[DeliverableKind.SCOPE], actor=SCOPE_ANALYST
)
ledger.request_approval(PhaseId.SCOPING, actor=ORCHESTRATOR)
if ledger.phase_status(PhaseId.SCOPING) is PhaseStatus.AWAITING_APPROVAL:
ledger.approve(PhaseId.SCOPING, actor="user")
return ledger
def carrying(sources: list[str]) -> dict[str, list]:
"""Scripted turns for the Specialists along the Requirements chain, in which one
more requirement, NFR-2, carries what the customer stated (`sources`), and a
Check, K-3, holds the design to it."""
import json
from execution_engine.adk.fake_runtime import Call
from tests.reference_workflow.workflow import SAMPLE_DATA
def submission(kind: DeliverableKind, **files: str) -> Call:
call = sample_submission(kind)
return Call(
call.tool,
{**call.args, "attachments": {**call.args["attachments"], **files}},
)
requirements = json.loads(SAMPLE_DATA["requirements.json"])
requirements.append(
{
"id": "NFR-2",
"kind": "non_functional",
"text": "The design honours what the customer stated.",
"conditions": ["Each stated choice is configured as stated."],
"from": sources,
}
)
components = json.loads(SAMPLE_DATA["components.json"])
components[0]["requirements"].append("NFR-2")
graph = json.loads(SAMPLE_DATA["target-architecture.json"])
graph["checks"].append(
{
"id": "K-3",
"applies_to": "E-1",
"source": "NFR-2",
"statement": "the stated choices are configured",
}
)
implementation = json.loads(SAMPLE_DATA["implementation.json"])
implementation.append(
{"check": "K-3", "file": "terraform/main.tf", "address": "example.run"}
)
verdicts = json.loads(SAMPLE_DATA["verdicts.json"])
verdicts["checks"].append(
{"check": "K-3", "verdict": "pass", "evidence": "Configured as stated."}
)
K = DeliverableKind
files = {
K.REQUIREMENTS: {"requirements.json": json.dumps(requirements)},
K.TECHNICAL_DECOMPOSITION: {"components.json": json.dumps(components)},
K.TARGET_ARCHITECTURE: {"target-architecture.json": json.dumps(graph)},
K.DEPLOYMENT_GUIDANCE: {"implementation.json": json.dumps(implementation)},
K.VALIDATION_RESULTS: {"verdicts.json": json.dumps(verdicts)},
}
return {
DEPENDENCY_GRAPH[kind].specialist: [[submission(kind, **attached)]]
for kind, attached in files.items()
}