feat: add the Deliverable dependency graph and Ledger

Increment 3 of .scratch/solution-architect-agent/spec.md, test-first.

- app/execution/deliverables.py: four Phases, nine Deliverables and
  their Dependencies declared once, plus per-kind contracts (required
  headings, attachments) checked by check_submission.
- app/execution/ledger.py: pure Ledger enforcing both acceptance
  thresholds, Phase Approvals (including pre-approved mode and change
  requests), Revision cascade with withdrawn Approvals, the revision
  cap with escalation and user override, attempt numbers that reject
  stale submissions, an ordered event log, and dict round-tripping.
- 37 new tests; full suite 102 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 18:28:04 +01:00
parent 2976dfab21
commit c8410946b8
7 changed files with 1044 additions and 0 deletions

View File

View File

@@ -0,0 +1,155 @@
"""The dependency graph and Deliverable contracts."""
import pytest
from app.execution.deliverables import (
DEPENDENCY_GRAPH,
PHASE_ORDER,
DeliverableKind,
PhaseId,
check_submission,
deliverables_in,
)
def test_every_kind_is_declared_once_with_known_dependencies():
assert set(DEPENDENCY_GRAPH) == set(DeliverableKind)
for kind, spec in DEPENDENCY_GRAPH.items():
assert spec.kind is kind
assert set(spec.depends_on) <= set(DeliverableKind), kind
def test_dependencies_never_point_to_a_later_phase_and_form_no_cycle():
position = {phase: index for index, phase in enumerate(PHASE_ORDER)}
for spec in DEPENDENCY_GRAPH.values():
for dependency in spec.depends_on:
assert position[DEPENDENCY_GRAPH[dependency].phase] <= position[spec.phase]
visiting: set[DeliverableKind] = set()
done: set[DeliverableKind] = set()
def visit(kind: DeliverableKind) -> None:
assert kind not in visiting, f"cycle through {kind}"
if kind in done:
return
visiting.add(kind)
for dependency in DEPENDENCY_GRAPH[kind].depends_on:
visit(dependency)
visiting.discard(kind)
done.add(kind)
for kind in DeliverableKind:
visit(kind)
def test_every_phase_groups_at_least_one_deliverable_with_a_distinct_specialist():
assert PHASE_ORDER == (
PhaseId.REQUIREMENTS_DISCOVERY,
PhaseId.SOLUTION_ARCHITECTURE,
PhaseId.SOLUTION_VALIDATION,
PhaseId.PACKAGING,
)
for phase in PHASE_ORDER:
assert deliverables_in(phase), phase
specialists = [spec.specialist for spec in DEPENDENCY_GRAPH.values()]
assert len(specialists) == len(set(specialists))
def test_graph_matches_the_upstream_skill_dependencies():
depends = {kind: set(spec.depends_on) for kind, spec in DEPENDENCY_GRAPH.items()}
K = DeliverableKind
assert depends[K.REQUIREMENTS] == set()
assert depends[K.TECHNICAL_DECOMPOSITION] == {K.REQUIREMENTS}
assert depends[K.PRODUCT_SELECTION] == {K.TECHNICAL_DECOMPOSITION}
assert depends[K.ARCHITECTURE_DIAGRAM] == {K.PRODUCT_SELECTION}
assert depends[K.ARCHITECTURE_DESCRIPTION] == {
K.ARCHITECTURE_DIAGRAM,
K.PRODUCT_SELECTION,
}
assert depends[K.DESIGN_RECOMMENDATIONS] == {K.PRODUCT_SELECTION, K.REQUIREMENTS}
assert depends[K.DEPLOYMENT_GUIDANCE] == {
K.PRODUCT_SELECTION,
K.DESIGN_RECOMMENDATIONS,
}
assert depends[K.VALIDATION_RESULTS] == {K.DEPLOYMENT_GUIDANCE}
assert depends[K.SOLUTION_GUIDE] == set(
deliverables_in(PhaseId.SOLUTION_VALIDATION)
)
def test_submission_meeting_the_contract_has_nothing_missing():
content = (
"# Deployment guidance\n"
"## 6.1. Deployment prerequisites\nEnable APIs.\n"
"## 6.2. Step-by-step deployment instructions\nRun terraform.\n"
)
attachments = {"terraform/main.tf": "resource {}", "terraform/variables.tf": ""}
assert (
check_submission(DeliverableKind.DEPLOYMENT_GUIDANCE, content, attachments)
== ()
)
def test_submission_reports_missing_sections_and_attachments():
missing = check_submission(
DeliverableKind.DEPLOYMENT_GUIDANCE,
"## Deployment prerequisites\n",
{"terraform/main.tf": "resource {}"},
)
assert missing == (
"section: Step-by-step deployment instructions",
"attachment: terraform/variables.tf",
)
@pytest.mark.parametrize(
"heading",
[
"### 5.1. Security, privacy, and compliance",
"## security, privacy, and compliance",
"# 5.1 Security, Privacy, and Compliance:",
],
)
def test_section_matching_ignores_numbering_case_and_trailing_punctuation(heading):
content = (
f"{heading}\n"
"## Reliability\n"
"## Operational excellence\n"
"## Cost optimization\n"
"## Performance efficiency\n"
"## Sustainability\n"
)
assert check_submission(DeliverableKind.DESIGN_RECOMMENDATIONS, content, {}) == ()
def test_section_matches_a_provider_prefixed_heading_but_not_a_longer_word():
content = (
"### 4.1. Google Cloud products and features mapping\n"
"## Non-functional requirements\n"
)
assert check_submission(DeliverableKind.PRODUCT_SELECTION, content, {}) == ()
assert "section: Functional requirements" in check_submission(
DeliverableKind.REQUIREMENTS, content, {}
)
def test_section_names_in_body_text_do_not_count_as_headings():
missing = check_submission(
DeliverableKind.REQUIREMENTS,
"Functional requirements, non-functional requirements, current state and "
"dependencies are all covered below.\n",
{},
)
assert "section: Functional requirements" in missing
def test_empty_content_is_refused():
assert check_submission(DeliverableKind.TECHNICAL_DECOMPOSITION, " \n", {}) == (
"content: must not be empty",
)

View File

@@ -0,0 +1,358 @@
"""The Ledger: Dependency acceptance, Approvals and Revision rules."""
import pytest
from app.execution.deliverables import DeliverableKind as K
from app.execution.deliverables import PhaseId as P
from app.execution.ledger import (
DeliverableStatus,
Ledger,
LedgerRefusal,
PhaseStatus,
)
def produce(ledger: Ledger, *kinds: K) -> None:
"""Delegate and submit each kind, in order."""
for kind in kinds:
attempt = ledger.start([kind], actor="orchestrator")[kind]
ledger.submit(kind, attempt, actor=f"specialist:{kind}")
def approve(ledger: Ledger, phase: P) -> None:
ledger.request_approval(phase, actor="orchestrator")
if ledger.phase_status(phase) is PhaseStatus.AWAITING_APPROVAL:
ledger.approve(phase, actor="user")
def through_phase_one(ledger: Ledger) -> None:
produce(ledger, K.REQUIREMENTS, K.TECHNICAL_DECOMPOSITION)
approve(ledger, P.REQUIREMENTS_DISCOVERY)
def through_phase_two(ledger: Ledger) -> None:
through_phase_one(ledger)
produce(
ledger,
K.PRODUCT_SELECTION,
K.ARCHITECTURE_DIAGRAM,
K.ARCHITECTURE_DESCRIPTION,
K.DESIGN_RECOMMENDATIONS,
K.DEPLOYMENT_GUIDANCE,
)
approve(ledger, P.SOLUTION_ARCHITECTURE)
def through_phase_three(ledger: Ledger) -> None:
through_phase_two(ledger)
produce(ledger, K.VALIDATION_RESULTS)
approve(ledger, P.SOLUTION_VALIDATION)
# --- ready work and the two acceptance thresholds -------------------------------
def test_new_execution_has_only_requirements_ready():
ledger = Ledger()
assert ledger.ready_work() == (K.REQUIREMENTS,)
assert all(ledger.phase_status(p) is PhaseStatus.OPEN for p in P)
def test_same_phase_dependency_is_met_once_submitted():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS)
assert ledger.ready_work() == (K.TECHNICAL_DECOMPOSITION,)
def test_cross_phase_dependency_needs_the_earlier_phase_approved():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS, K.TECHNICAL_DECOMPOSITION)
assert K.PRODUCT_SELECTION not in ledger.ready_work()
approve(ledger, P.REQUIREMENTS_DISCOVERY)
assert ledger.ready_work() == (K.PRODUCT_SELECTION,)
def test_independent_work_becomes_ready_together():
ledger = Ledger()
through_phase_one(ledger)
produce(ledger, K.PRODUCT_SELECTION)
assert set(ledger.ready_work()) == {
K.ARCHITECTURE_DIAGRAM,
K.DESIGN_RECOMMENDATIONS,
}
produce(ledger, K.ARCHITECTURE_DIAGRAM)
assert set(ledger.ready_work()) == {
K.ARCHITECTURE_DESCRIPTION,
K.DESIGN_RECOMMENDATIONS,
}
def test_ready_phases_lists_open_phases_with_every_deliverable_submitted():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS)
assert ledger.phases_ready_for_approval() == ()
produce(ledger, K.TECHNICAL_DECOMPOSITION)
assert ledger.phases_ready_for_approval() == (P.REQUIREMENTS_DISCOVERY,)
# --- delegation and submission ---------------------------------------------------
def test_start_refuses_work_that_is_not_ready_and_changes_nothing():
ledger = Ledger()
with pytest.raises(LedgerRefusal, match="technical_decomposition"):
ledger.start([K.REQUIREMENTS, K.TECHNICAL_DECOMPOSITION], actor="orchestrator")
assert ledger.status(K.REQUIREMENTS) is DeliverableStatus.PENDING
def test_start_moves_ready_work_in_progress_and_returns_attempts():
ledger = Ledger()
attempts = ledger.start([K.REQUIREMENTS], actor="orchestrator")
assert ledger.status(K.REQUIREMENTS) is DeliverableStatus.IN_PROGRESS
assert attempts == {K.REQUIREMENTS: 1}
assert ledger.ready_work() == ()
def test_submit_requires_the_current_attempt():
ledger = Ledger()
attempt = ledger.start([K.REQUIREMENTS], actor="orchestrator")[K.REQUIREMENTS]
with pytest.raises(LedgerRefusal, match="attempt"):
ledger.submit(K.REQUIREMENTS, attempt + 1, actor="requirements-analyst")
ledger.submit(K.REQUIREMENTS, attempt, actor="requirements-analyst")
assert ledger.status(K.REQUIREMENTS) is DeliverableStatus.SUBMITTED
def test_late_submission_from_a_superseded_attempt_is_refused_after_restart():
ledger = Ledger()
first = ledger.start([K.REQUIREMENTS], actor="orchestrator")[K.REQUIREMENTS]
ledger.release(K.REQUIREMENTS, first, reason="needs input")
second = ledger.start([K.REQUIREMENTS], actor="orchestrator")[K.REQUIREMENTS]
with pytest.raises(LedgerRefusal, match="attempt"):
ledger.submit(K.REQUIREMENTS, first, actor="requirements-analyst")
ledger.submit(K.REQUIREMENTS, second, actor="requirements-analyst")
assert ledger.status(K.REQUIREMENTS) is DeliverableStatus.SUBMITTED
def test_submit_refuses_work_not_in_progress():
ledger = Ledger()
with pytest.raises(LedgerRefusal, match="in_progress"):
ledger.submit(K.REQUIREMENTS, 1, actor="requirements-analyst")
def test_release_returns_work_to_pending_without_a_revision():
ledger = Ledger()
attempt = ledger.start([K.REQUIREMENTS], actor="orchestrator")[K.REQUIREMENTS]
ledger.release(K.REQUIREMENTS, attempt, reason="needs input")
assert ledger.status(K.REQUIREMENTS) is DeliverableStatus.PENDING
assert ledger.revisions(K.REQUIREMENTS) == 0
assert ledger.ready_work() == (K.REQUIREMENTS,)
with pytest.raises(LedgerRefusal, match="attempt"):
ledger.submit(K.REQUIREMENTS, attempt, actor="requirements-analyst")
# --- approvals ---------------------------------------------------------------------
def test_request_approval_refuses_until_every_deliverable_is_submitted():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS)
with pytest.raises(LedgerRefusal, match="technical_decomposition"):
ledger.request_approval(P.REQUIREMENTS_DISCOVERY, actor="orchestrator")
assert ledger.phase_status(P.REQUIREMENTS_DISCOVERY) is PhaseStatus.OPEN
def test_approval_moves_through_awaiting_to_approved_by_the_user():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS, K.TECHNICAL_DECOMPOSITION)
ledger.request_approval(P.REQUIREMENTS_DISCOVERY, actor="orchestrator")
assert (
ledger.phase_status(P.REQUIREMENTS_DISCOVERY) is PhaseStatus.AWAITING_APPROVAL
)
ledger.approve(P.REQUIREMENTS_DISCOVERY, actor="user")
assert ledger.phase_status(P.REQUIREMENTS_DISCOVERY) is PhaseStatus.APPROVED
assert ledger.events[-1].type == "approved"
assert ledger.events[-1].actor == "user"
def test_approve_refuses_a_phase_not_awaiting_approval():
ledger = Ledger()
with pytest.raises(LedgerRefusal, match="awaiting_approval"):
ledger.approve(P.REQUIREMENTS_DISCOVERY, actor="user")
def test_pre_approved_ledger_grants_approval_immediately():
ledger = Ledger(pre_approved=True)
produce(ledger, K.REQUIREMENTS, K.TECHNICAL_DECOMPOSITION)
ledger.request_approval(P.REQUIREMENTS_DISCOVERY, actor="orchestrator")
assert ledger.phase_status(P.REQUIREMENTS_DISCOVERY) is PhaseStatus.APPROVED
assert [e.type for e in ledger.events[-2:]] == ["approval_requested", "approved"]
assert ledger.events[-1].actor == "pre-approved"
def test_request_changes_reopens_the_phase_for_further_work():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS, K.TECHNICAL_DECOMPOSITION)
ledger.request_approval(P.REQUIREMENTS_DISCOVERY, actor="orchestrator")
ledger.request_changes(P.REQUIREMENTS_DISCOVERY, "Add the RTO target", actor="user")
assert ledger.phase_status(P.REQUIREMENTS_DISCOVERY) is PhaseStatus.OPEN
assert ledger.events[-1].type == "changes_requested"
assert ledger.events[-1].detail == "Add the RTO target"
def test_finish_is_allowed_only_once_packaging_is_approved():
ledger = Ledger()
through_phase_three(ledger)
assert not ledger.can_finish()
produce(ledger, K.SOLUTION_GUIDE)
approve(ledger, P.PACKAGING)
assert ledger.can_finish()
# --- revisions ---------------------------------------------------------------------
def test_reopen_cascades_to_dependents_and_withdraws_affected_approvals():
ledger = Ledger()
through_phase_three(ledger)
result = ledger.reopen(
K.DEPLOYMENT_GUIDANCE, "terraform plan failed", actor="orchestrator"
)
assert result.escalation is None
# solution_guide was still pending, so its status doesn't change and it isn't reset.
assert set(result.reset) == {K.DEPLOYMENT_GUIDANCE, K.VALIDATION_RESULTS}
assert ledger.status(K.DEPLOYMENT_GUIDANCE) is DeliverableStatus.PENDING
assert ledger.status(K.VALIDATION_RESULTS) is DeliverableStatus.PENDING
assert ledger.status(K.PRODUCT_SELECTION) is DeliverableStatus.SUBMITTED
assert ledger.phase_status(P.REQUIREMENTS_DISCOVERY) is PhaseStatus.APPROVED
assert ledger.phase_status(P.SOLUTION_ARCHITECTURE) is PhaseStatus.OPEN
assert ledger.phase_status(P.SOLUTION_VALIDATION) is PhaseStatus.OPEN
assert set(result.withdrawn_approvals) == {
P.SOLUTION_ARCHITECTURE,
P.SOLUTION_VALIDATION,
}
assert ledger.revisions(K.DEPLOYMENT_GUIDANCE) == 1
assert ledger.revisions(K.VALIDATION_RESULTS) == 0
assert ledger.feedback(K.DEPLOYMENT_GUIDANCE) == ("terraform plan failed",)
assert ledger.feedback(K.VALIDATION_RESULTS) == (
"Dependency deployment_guidance was revised: terraform plan failed",
)
assert ledger.ready_work() == (K.DEPLOYMENT_GUIDANCE,)
def test_reopen_invalidates_attempts_of_dependents_already_in_progress():
ledger = Ledger()
through_phase_one(ledger)
produce(ledger, K.PRODUCT_SELECTION)
attempt = ledger.start([K.ARCHITECTURE_DIAGRAM], actor="orchestrator")[
K.ARCHITECTURE_DIAGRAM
]
ledger.reopen(K.PRODUCT_SELECTION, "user prefers GKE", actor="orchestrator")
assert ledger.status(K.ARCHITECTURE_DIAGRAM) is DeliverableStatus.PENDING
with pytest.raises(LedgerRefusal, match="attempt"):
ledger.submit(K.ARCHITECTURE_DIAGRAM, attempt, actor="diagrammer")
def test_reopen_refuses_deliverables_that_were_never_submitted():
ledger = Ledger()
with pytest.raises(LedgerRefusal, match="submitted"):
ledger.reopen(K.REQUIREMENTS, "why", actor="orchestrator")
def test_reopen_escalates_at_the_revision_cap_without_changing_state():
ledger = Ledger(max_revisions=2)
produce(ledger, K.REQUIREMENTS)
for _ in range(2):
ledger.reopen(K.REQUIREMENTS, "again", actor="orchestrator")
produce(ledger, K.REQUIREMENTS)
result = ledger.reopen(K.REQUIREMENTS, "one more", actor="orchestrator")
assert result.reset == ()
assert result.escalation is not None
assert "requirements" in result.escalation
assert ledger.status(K.REQUIREMENTS) is DeliverableStatus.SUBMITTED
assert ledger.revisions(K.REQUIREMENTS) == 2
assert ledger.events[-1].type == "escalated"
def test_user_can_override_the_revision_cap():
ledger = Ledger(max_revisions=1)
produce(ledger, K.REQUIREMENTS)
ledger.reopen(K.REQUIREMENTS, "first", actor="orchestrator")
produce(ledger, K.REQUIREMENTS)
result = ledger.reopen(
K.REQUIREMENTS, "user insists", actor="user", override_cap=True
)
assert result.escalation is None
assert ledger.revisions(K.REQUIREMENTS) == 2
def test_default_revision_cap_is_three():
assert Ledger().max_revisions == 3
# --- records and persistence -------------------------------------------------------
def test_transitions_are_recorded_as_ordered_events():
ledger = Ledger()
produce(ledger, K.REQUIREMENTS)
assert [(e.sequence, e.type, e.subject) for e in ledger.events] == [
(1, "started", "requirements"),
(2, "submitted", "requirements"),
]
def test_round_trips_through_plain_data():
ledger = Ledger(max_revisions=5)
through_phase_one(ledger)
produce(ledger, K.PRODUCT_SELECTION)
ledger.reopen(K.PRODUCT_SELECTION, "cost", actor="user")
restored = Ledger.from_dict(ledger.to_dict())
assert restored.to_dict() == ledger.to_dict()
assert restored.ready_work() == ledger.ready_work()
assert restored.max_revisions == 5
assert restored.events == ledger.events