"""The work breakdown structure: a plan checked, scheduled and drawn by code.""" import json from langgraph.checkpoint.memory import InMemorySaver from execution_engine.adk.fake_runtime import Call, FakeAgentRuntime from execution_engine.adk.runtime import NoDecider, RoleSpec from execution_engine.agent import ExecutionEngine from execution_engine.nodes.router import Router, Thresholds from execution_engine.recorder import InMemoryRecorder from tests.reference_workflow.work_breakdown import ( check_work_breakdown, mermaid, render, schedule, summary, ) from tests.reference_workflow.workflow import ( DEPENDENCY_GRAPH, SAMPLE_DATA, WORKFLOW, sample_submission, ) from tests.reference_workflow.workflow import DeliverableKind as K GRAPH = SAMPLE_DATA["target-architecture.json"] # Elements E-1 and E-2 PLANNER = DEPENDENCY_GRAPH[K.WORK_BREAKDOWN].specialist def upstream(**delivery) -> dict: scope = {"stated": {"delivery": delivery}, "assumed": []} return { "target_architecture": {"attachments": {"target-architecture.json": GRAPH}}, "scope": {"attachments": {"scope.json": json.dumps(scope)}}, } def plan(packages=None, activities=None) -> dict[str, str]: data = json.loads(SAMPLE_DATA["work-breakdown.json"]) if packages is not None: data["work_packages"] = packages if activities is not None: data["activities"] = activities return {"work-breakdown.json": json.dumps(data)} def activity(ident: str, days: float, *after: str, **more) -> dict: return { "id": ident, "work_package": "WP-1", "name": f"Do {ident}", "days": days, "after": list(after), **more, } PACKAGE = json.loads(SAMPLE_DATA["work-breakdown.json"])["work_packages"][0] # --- the plan is checked against the design --------------------------------------------- def test_a_plan_that_covers_the_design_is_accepted(): assert check_work_breakdown(plan(), upstream()) == [] def test_every_element_of_the_design_belongs_to_a_work_package(): only_one = [{**PACKAGE, "elements": ["E-1"]}] invented = [{**PACKAGE, "elements": ["E-1", "E-2", "E-9"]}] (left_out,) = check_work_breakdown(plan(only_one), upstream()) (unknown,) = check_work_breakdown(plan(invented), upstream()) assert "no work package delivers E-2" in left_out assert "WP-1 delivers E-9, which is not an Element" in unknown def test_a_plan_that_cannot_be_scheduled_is_refused_with_the_reason(): def problems(*activities): return check_work_breakdown(plan(activities=list(activities)), upstream()) assert any( "depend on each other in a circle" in p for p in problems(activity("A-1", 1, "A-2"), activity("A-2", 1, "A-1")) ) assert any( "comes after A-9, which is not an activity" in p for p in problems(activity("A-1", 1, "A-9")) ) assert any( "needs `days`, a number above 0" in p for p in problems(activity("A-1", 0)) ) assert any("split anything over" in p for p in problems(activity("A-1", 400))) assert any( "owner must be one of" in p for p in problems(activity("A-1", 1, owner="vendor")) ) assert any( "is in WP-7, not a work package" in p for p in problems({**activity("A-1", 1), "work_package": "WP-7"}) ) def test_a_work_package_holds_work_or_other_work_packages(): parent = {**PACKAGE, "id": "WP-0", "elements": []} child = {**PACKAGE, "parent": "WP-0"} idle = {**PACKAGE, "id": "WP-2", "elements": []} assert check_work_breakdown(plan([parent, child]), upstream()) == [] assert any( "WP-2 has no activity and no work package beneath it" in p for p in check_work_breakdown( plan([child | {"parent": None}, idle]), upstream() ) ) assert any( "has parent WP-9, which does not exist" in p for p in check_work_breakdown(plan([{**PACKAGE, "parent": "WP-9"}]), upstream()) ) # --- the schedule is computed ----------------------------------------------------------- def test_an_activity_starts_when_what_it_follows_has_finished(): placed = schedule( [activity("A-1", 2), activity("A-2", 3, "A-1"), activity("A-3", 1, "A-2")] ) assert [(p.start, p.finish) for p in placed.values()] == [(0, 2), (2, 5), (5, 6)] assert all(p.critical for p in placed.values()) def test_independent_work_runs_at_once_up_to_the_size_of_the_team(): work = [activity("A-1", 4), activity("A-2", 4), activity("A-3", 4)] two = schedule(work, team_size=2) three = schedule(work, team_size=3) assert max(p.finish for p in two.values()) == 8 assert sorted(p.start for p in two.values()) == [0, 0, 4] assert max(p.finish for p in three.values()) == 4 def test_what_the_customer_owns_takes_nobody_from_the_team(): work = [ activity("A-1", 10, owner="customer"), activity("A-2", 3), activity("A-3", 2, "A-1", "A-2"), ] placed = schedule(work, team_size=1) # The lead time and the team's work run together; the end waits on the longer. assert placed["A-2"].start == 0 and placed["A-3"].start == 10 assert placed["A-1"].critical and placed["A-3"].critical assert not placed["A-2"].critical def test_when_people_are_short_the_longest_chain_goes_first(): work = [activity("A-1", 1), activity("A-2", 1), activity("A-3", 5, "A-2")] placed = schedule(work, team_size=1) assert placed["A-2"].start == 0 and placed["A-1"].start > 0 # --- the views are written from the plan ------------------------------------------------ def test_the_three_views_are_written_from_the_data(): text = render(plan(), upstream(team_size=2)) assert "## WBS dictionary" in text # Acceptance is the Checks on the work package's Elements, read from the design. assert ( "| WP-1 | Ingress | The service and its identity. | a running service | E-1, E-2 | K-1, K-2 |" in text ) assert ( "| A-2 | Deploy the service | WP-1 | 2 | A-1 | Day 1 | Day 3 | team | yes |" in text ) assert "## Timeline" in text and "Day 0" in text assert "3 working days, about 0.6 weeks, with a team of 2 and a 5-day week." in text assert "indicative" in text def test_a_plan_over_the_delivery_window_says_so_and_names_the_critical_path(): long = plan(activities=[activity("A-1", 20), activity("A-2", 15, "A-1")]) over = render(long, upstream(team_size=2, window_weeks=4)) inside = render(long, upstream(team_size=2, window_weeks=10)) assert "**That is 3.0 weeks over the 4-week delivery window.**" in over assert "The critical path is A-1 -> A-2" in over assert "inside the 10-week delivery window, with 3.0 weeks to spare" in inside assert summary(long, upstream(window_weeks=4))["within_window"] is False def test_an_assumed_team_size_is_said_to_be_assumed(): assert "(assumed; the scope gives no team size)" in render(plan(), upstream()) def test_a_customers_lead_time_is_shown_as_theirs(): waiting = plan( activities=[ activity( "A-1", 30, owner="customer", assumption="The carrier takes six weeks." ), activity("A-2", 2, "A-1"), ] ) text = render(waiting, upstream(team_size=2)) assert "The customer owns these, and the plan waits on them:" in text assert "- A-1 Do A-1: 30 days. The carrier takes six weeks." in text assert "░" in text and "█" in text def test_calendar_dates_and_a_chart_appear_only_with_a_start_date(): dated = upstream(team_size=2, target_start_date="2026-11-02") # a Monday assert mermaid(plan(), upstream(team_size=2)) == "" assert "Dates" not in render(plan(), upstream(team_size=2)) chart = mermaid(plan(), dated) assert chart.startswith("gantt\n dateFormat YYYY-MM-DD") assert "Deploy the service :crit, A-2, 2026-11-03, 2d" in chart assert "| 2026-11-03 to 2026-11-04 |" in render(plan(), dated) def test_a_week_of_working_days_skips_the_weekend(): week = plan(activities=[activity("A-1", 5), activity("A-2", 1, "A-1")]) text = render(week, upstream(team_size=1, target_start_date="2026-11-02")) # Five working days from Monday end on Friday; the next starts on Monday. assert "| 2026-11-02 to 2026-11-06 |" in text assert "| 2026-11-09 to 2026-11-09 |" in text # --- in an Execution -------------------------------------------------------------------- async def test_an_execution_delivers_a_plan_and_adds_its_timeline_to_the_guide( tmp_path, ): roles = { "orchestrator": RoleSpec("orchestrator", "Unused.", budget=40), **{ spec.specialist: RoleSpec(spec.specialist, "Produce.", budget=15) for spec in DEPENDENCY_GRAPH.values() }, } runtime = FakeAgentRuntime( { spec.specialist: [[sample_submission(kind)]] for kind, spec in DEPENDENCY_GRAPH.items() } ) engine = ExecutionEngine( workflow=WORKFLOW, runtime=runtime, roles=roles, recorder=InMemoryRecorder(), checkpointer=InMemorySaver(), output_root=tmp_path, orchestrator=Router(NoDecider(), None, Thresholds()), ) result = await engine.start("Ingest", customer_case="acme", pre_approved=True) assert result.status == "completed" out = next(tmp_path.rglob("execution.json")).parent breakdown = (out / "deliverables" / "work_breakdown.md").read_text() assert "## WBS dictionary" in breakdown and "## Activity sequence" in breakdown assert (out / "work-breakdown.json").is_file() guide = (out / "solution-architecture-guide.md").read_text() assert "## Delivery plan" in guide and "A-2 Deploy the service" in guide manifest = json.loads((out / "execution.json").read_text()) assert manifest["delivery_plan"]["working_days"] == 3 assert manifest["delivery_plan"]["critical_path"] == ["A-1", "A-2"] # The planner was given the design and the code to plan from, in full. planner = next(run for run in runtime.runs if run.role == PLANNER) given = planner.task.context["dependencies"] assert "content" in given["target_architecture"] assert "content" in given["deployment_guidance"] async def test_a_plan_that_leaves_an_element_out_is_returned_to_the_planner(tmp_path): from execution_engine.tools.execution import specialist_tools (submit,) = ( tool for tool in specialist_tools( WORKFLOW, K.WORK_BREAKDOWN, (), allow_cloud_read=False, upstream=upstream() ) if tool.name == "submit_deliverable" ) partial = Call( "submit_deliverable", { "content": "## Approach\nIn order.\n", "attachments": plan([{**PACKAGE, "elements": ["E-1"]}]), }, ) refused = await submit.handler(**partial.args) assert refused.outcome is None assert "no work package delivers E-2" in refused.message