"""Dependency execution uses publication state, not linear mention routing.""" import asyncio import pytest from polyhedra.core.active_routing import build_active_routing_plan from polyhedra.core.artifacts import JsonArtifactRef from polyhedra.core.dependency_executor import AgentCompletion, DependencyExecutor from polyhedra.core.quant_contracts import load_quant_contracts def plan(tickers): return build_active_routing_plan(run_id="run1", execution_context="YOLO", tickers=list(tickers)) def completion(agent, status="COMPLETE", *, run="run1", session="session1"): contract = load_quant_contracts().for_agent(agent) path = next(path for path in contract.permissions.create if path.endswith(".json")) path = path.replace("{run_id}", run) return AgentCompletion( result_status=status, artifact_ref=JsonArtifactRef( schema_version="8.0", artifact_id=f"{agent}-{run}", run_id=run, session_id=session, producer=agent, path=path, ), ) @pytest.mark.asyncio async def test_ready_agents_overlap_and_join_waits_for_all_applicable_dependencies(): active = plan(["MSFT"]) data_done = asyncio.Event() branch_started = {"TrendAnalyst": asyncio.Event(), "SentimentAnalyst": asyncio.Event()} branch_release = asyncio.Event() starts = [] async def runner(item): starts.append(item.agent) if item.agent == "DataFetcher": data_done.set() elif item.agent in branch_started: assert data_done.is_set() branch_started[item.agent].set() await branch_release.wait() elif item.agent == "QuantResearcher": assert all(event.is_set() for event in branch_started.values()) return completion(item.agent) task = asyncio.create_task(DependencyExecutor( active, session_id="session1", runner=runner, ).run()) await asyncio.wait_for(data_done.wait(), timeout=1) await asyncio.wait_for(branch_started["TrendAnalyst"].wait(), timeout=1) await asyncio.wait_for(branch_started["SentimentAnalyst"].wait(), timeout=1) assert "QuantResearcher" not in starts branch_release.set() result = await asyncio.wait_for(task, timeout=1) assert all(record.state == "COMPLETED" for record in result.records) assert starts.index("TrendAnalyst") < starts.index("QuantResearcher") assert starts.index("SentimentAnalyst") < starts.index("QuantResearcher") @pytest.mark.asyncio async def test_unavailable_typed_result_satisfies_dependency_completion(): async def runner(item): status = "UNAVAILABLE" if item.agent == "TrendAnalyst" else "COMPLETE" return completion(item.agent, status) result = await DependencyExecutor( plan(["MSFT"]), session_id="session1", runner=runner, ).run() assert result.for_agent("TrendAnalyst").state == "COMPLETED" assert result.for_agent("TrendAnalyst").result_status == "UNAVAILABLE" assert result.for_agent("QuantResearcher").state == "COMPLETED" @pytest.mark.asyncio async def test_failure_blocks_dependents_but_unrelated_root_finishes(): completed = [] async def runner(item): if item.agent == "DataFetcher": raise RuntimeError("provider normalization failed") completed.append(item.agent) return completion(item.agent) result = await DependencyExecutor( plan(["SPY"]), session_id="session1", runner=runner, ).run() assert result.for_agent("DataFetcher").state == "FAILED" assert result.for_agent("OptionsFlowWatcher").state == "COMPLETED" assert completed == ["OptionsFlowWatcher"] assert result.for_agent("TrendAnalyst").blocked_by == ("DataFetcher",) assert result.for_agent("Publisher").state == "BLOCKED" @pytest.mark.asyncio async def test_wrong_publication_identity_is_execution_failure(): async def runner(item): if item.agent == "DataFetcher": return completion(item.agent, run="other") return completion(item.agent) result = await DependencyExecutor( plan(["MSFT"]), session_id="session1", runner=runner, ).run() record = result.for_agent("DataFetcher") assert record.state == "FAILED" assert "identity contradicts" in record.error assert result.for_agent("TrendAnalyst").state == "BLOCKED" @pytest.mark.asyncio async def test_each_active_agent_is_invoked_once(): calls = {} async def runner(item): calls[item.agent] = calls.get(item.agent, 0) + 1 return completion(item.agent) active = plan(["SPY", "BTC"]) result = await DependencyExecutor( active, session_id="session1", runner=runner, ).run() assert set(calls) == {item.agent for item in active.active_agents} assert set(calls.values()) == {1} assert len(result.records) == len(active.active_agents) @pytest.mark.asyncio async def test_inputs_are_captured_before_ready_tasks_are_scheduled(): captured = {} runner_mutations = [] def capture(item, dependencies): captured[item.agent] = ( tuple(record.agent for record in dependencies), tuple(runner_mutations), ) return f"snapshot:{item.agent}" async def runner(item, snapshot): assert snapshot == f"snapshot:{item.agent}" runner_mutations.append(item.agent) return completion(item.agent) result = await DependencyExecutor( plan(["MSFT"]), session_id="session1", runner=runner, input_capture=capture, ).run() assert all(record.state == "COMPLETED" for record in result.records) assert captured["DataFetcher"] == ((), ()) assert captured["TrendAnalyst"][0] == ("DataFetcher",) assert captured["SentimentAnalyst"][0] == ("DataFetcher",) assert captured["TrendAnalyst"][1] == captured["SentimentAnalyst"][1] @pytest.mark.asyncio async def test_input_capture_failure_blocks_only_dependent_branch(): ran = [] def capture(item, dependencies): if item.agent == "DataFetcher": raise ValueError("invalid provider artifact") return tuple(record.agent for record in dependencies) async def runner(item, _snapshot): ran.append(item.agent) return completion(item.agent) result = await DependencyExecutor( plan(["SPY"]), session_id="session1", runner=runner, input_capture=capture, ).run() assert result.for_agent("DataFetcher").state == "FAILED" assert "invalid provider artifact" in result.for_agent("DataFetcher").error assert result.for_agent("OptionsFlowWatcher").state == "COMPLETED" assert ran == ["OptionsFlowWatcher"] assert result.for_agent("Publisher").state == "BLOCKED" @pytest.mark.asyncio async def test_failed_execution_record_redacts_provider_credentials(): async def runner(item): raise RuntimeError( "https://provider.invalid/run?api_key=secret-value&mode=test " "Bearer private-token" ) result = await DependencyExecutor( plan(["SPY"]), session_id="session1", runner=runner, ).run() error = result.for_agent("DataFetcher").error assert "secret-value" not in error assert "private-token" not in error assert error.count("") == 2