"""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. """ from __future__ import annotations from typing import Any from app.adk.fake_runtime import FakeAgentRuntime, sample_submission from app.adk.runtime import Submitted from app.agent import ExecutionEngine, Interaction from app.workflows.deliverables import DEPENDENCY_GRAPH, DeliverableKind, PhaseId SCOPE_ANALYST = DEPENDENCY_GRAPH[DeliverableKind.SCOPE].specialist ORCHESTRATOR = "orchestrator" 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 == 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.""" 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 app.states.ledger import Ledger, PhaseStatus ledger = Ledger(**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