Download tests/test_quant_io_registry.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 11.2 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_quant_io_registry.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/tests/test_quant_io_registry.py
-
curl -L -o test_quant_io_registry.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_quant_io_registry.py
11.2 kB
| """Every registered quant agent has one concrete structured I/O binding.""" | |
| from typing import get_args | |
| import pytest | |
| from pydantic import BaseModel | |
| from polyhedra.agents.quant.analysis_contracts import TrendPayload | |
| from polyhedra.agents.quant.asset_contracts import ( | |
| BTCAssetAnalysis, GLDAssetAnalysis, QQQAssetAnalysis, SPYAssetAnalysis, | |
| ) | |
| from polyhedra.agents.quant.decision_contracts import QuantStrategistLLMPayload | |
| from polyhedra.agents.quant.io_registry import ( | |
| QUANT_IO_REGISTRY, _project_asset_input, _project_options_summary, | |
| _project_quantstrategist_input, | |
| _project_trend_input, quant_artifact_schema, quant_io_binding, | |
| ) | |
| from polyhedra.agents.quant.market_data_contracts import DataFetcherInput | |
| from polyhedra.agents.quant.product_contracts import PublisherPayload | |
| from polyhedra.core.quant_contracts import AgentName | |
| AT = "2026-09-10T12:00:00Z" | |
| class ProjectionInput(BaseModel): | |
| dependencies: dict | |
| optional_context: dict | None = None | |
| def test_registry_covers_each_agent_exactly_once(): | |
| assert set(QUANT_IO_REGISTRY) == set(get_args(AgentName)) | |
| assert len(QUANT_IO_REGISTRY) == 19 | |
| for name, binding in QUANT_IO_REGISTRY.items(): | |
| assert binding.agent == name | |
| assert binding.input_schema.model_json_schema()["type"] == "object" | |
| assert binding.output_schema.model_json_schema()["type"] == "object" | |
| artifact = quant_artifact_schema(name) | |
| assert artifact.model_json_schema()["type"] == "object" | |
| assert "run_id" in artifact.model_fields | |
| def test_prompts_are_agent_specific_and_accept_only_typed_input(): | |
| prompts = [binding.prompt_template for binding in QUANT_IO_REGISTRY.values()] | |
| assert len(set(prompts)) == 19 | |
| for name, binding in QUANT_IO_REGISTRY.items(): | |
| assert name in binding.prompt_template | |
| assert binding.prompt_template.count("{input_json}") == 1 | |
| assert "do not add routing fields" in binding.prompt_template | |
| assert "for BASELINE, every delta_state must be NOT_COMPARED" in binding.prompt_template | |
| assert "include either a JSON Pointer field_path" in binding.prompt_template | |
| assert "do not copy a nested reference unless" in binding.prompt_template | |
| assert "containing record in the direct dependency artifact" in binding.prompt_template | |
| assert "use a visible zero-based JSON Pointer" in binding.prompt_template | |
| event_prompt = quant_io_binding("MarketEventWatcher").prompt_template | |
| assert "DataFetcher NEWS provider as the approved event feed" in event_prompt | |
| assert "code EXTERNAL_EVENT_FEED_NOT_CONFIGURED" in event_prompt | |
| assert "into the context's source_refs list" in event_prompt | |
| macro_prompt = quant_io_binding("MacroContextWatcher").prompt_template | |
| assert "direct current MarketEventWatcher or OptionsFlowWatcher artifact" in macro_prompt | |
| trend_prompt = quant_io_binding("TrendAnalyst").prompt_template | |
| assert "exact per-ticker MarketDataArtifact identity" in trend_prompt | |
| assert "Do not reconstruct an artifact identity" in trend_prompt | |
| unusual_prompt = quant_io_binding("UnusualEventDetector").prompt_template | |
| assert "exact direct artifact identity of MarketEventWatcher" in unusual_prompt | |
| assert "never copy a nested DataFetcher" in unusual_prompt | |
| fetch_input = DataFetcherInput( | |
| run_id="run1", session_id="session1", run_mode="BASELINE", | |
| as_of=AT, ticker_scope=["SPY"], baseline_refs={}, | |
| ) | |
| prompt = quant_io_binding("DataFetcher").build_prompt(fetch_input) | |
| assert '"ticker_scope": [' in prompt | |
| with pytest.raises(TypeError): | |
| quant_io_binding("TrendAnalyst").build_prompt(fetch_input) | |
| def test_specialized_payload_schemas_keep_distinct_ticker_types(): | |
| expected = { | |
| "BTCAnalyst": BTCAssetAnalysis, | |
| "SPYAnalyst": SPYAssetAnalysis, | |
| "QQQAnalyst": QQQAssetAnalysis, | |
| "GLDAnalyst": GLDAssetAnalysis, | |
| } | |
| for agent, analysis_type in expected.items(): | |
| annotation = QUANT_IO_REGISTRY[agent].output_schema.model_fields["analysis"].annotation | |
| assert annotation is analysis_type | |
| def test_strategy_binding_excludes_runtime_owned_performance_fields(): | |
| schema = quant_io_binding("QuantStrategist").output_schema | |
| assert schema is QuantStrategistLLMPayload | |
| assert "committee_weights" not in schema.model_fields | |
| assert "committee_aggregation" not in schema.model_fields | |
| assert "weight_updates" not in schema.model_fields | |
| def test_strategy_prompt_names_complete_contract_and_distinguishes_votes(): | |
| prompt = quant_io_binding("QuantStrategist").prompt_template | |
| for field in ( | |
| "outcome", "proposals", "no_trade_or_watch_rationale", "exclusions", | |
| "committee_votes", "proposal_updates", "evidence_refs", "data_gaps", | |
| ): | |
| assert field in prompt | |
| assert "committee members are not proposals" in prompt | |
| assert "Include every field" in prompt | |
| def test_strategy_projection_keeps_decisions_without_provenance_envelopes(): | |
| source = ProjectionInput( | |
| dependencies={"risk": { | |
| "schema_version": "8.0", | |
| "artifact_id": "risk-1", | |
| "input_refs": [{"path": "backtest_results.json"}], | |
| "data_gaps": [{ | |
| "component": "BACKTEST", | |
| "code": "NOT_TESTABLE", | |
| "message": "No historical engine.", | |
| "record_refs": [{"field_path": "/payload/results/0"}], | |
| }], | |
| "payload": {"signal_assessments": [{ | |
| "signal_id": "spy-signal", | |
| "verdict": "REJECT", | |
| "rationale": "Not testable.", | |
| "evidence_refs": [{"field_path": "/payload/results/0"}], | |
| }]}, | |
| }}, | |
| optional_context={"committee_performance": { | |
| "weights": {"risk_skeptic": 1.0}, | |
| "recorded_at": AT, | |
| }}, | |
| ) | |
| projected = _project_quantstrategist_input(source) | |
| risk = projected["dependencies"]["risk"] | |
| assert "schema_version" not in risk | |
| assert "artifact_id" not in risk | |
| assert "input_refs" not in risk | |
| assert risk["payload"]["signal_assessments"] == [{ | |
| "signal_id": "spy-signal", | |
| "verdict": "REJECT", | |
| "rationale": "Not testable.", | |
| }] | |
| assert risk["data_gaps"][0]["message"] == "No historical engine." | |
| assert "record_refs" not in risk["data_gaps"][0] | |
| assert projected["optional_context"]["committee_performance"]["weights"] == { | |
| "risk_skeptic": 1.0, | |
| } | |
| def test_fixed_markdown_renderer_is_deterministic(): | |
| trend = TrendPayload(ticker_analyses=[]) | |
| rendered = quant_io_binding("TrendAnalyst").render_markdown(trend) | |
| assert rendered == "# Trend Analyst\n\n## Ticker Analyses\n\nNone recorded.\n" | |
| def test_publisher_renderer_returns_validated_slack_body(): | |
| payload = PublisherPayload( | |
| headline="SPY has no validated setup", market_summary="Evidence remains mixed.", | |
| what_to_watch=[], displayed_proposal_ids=[], no_proposal_reason="TRUE_NO_TRADE", | |
| disclaimer="Paper trading simulation only. Not financial advice.", | |
| approximate_word_count=12, | |
| slack_rendering="*SPY has no validated setup*\nEvidence remains mixed.\n", | |
| delta_state="NOT_COMPARED", evidence_refs=[], data_gaps=[], | |
| ) | |
| assert quant_io_binding("Publisher").render_markdown(payload) == payload.slack_rendering | |
| def test_registry_is_read_only_and_rejects_unknown_name(): | |
| with pytest.raises(TypeError): | |
| QUANT_IO_REGISTRY["Unknown"] = QUANT_IO_REGISTRY["DataFetcher"] | |
| with pytest.raises(KeyError): | |
| quant_io_binding("Unknown") | |
| def test_downstream_options_projection_omits_only_raw_contract_rows(): | |
| for agent in ("MacroContextWatcher", "UnusualEventDetector", "EventRouter"): | |
| assert quant_io_binding(agent).prompt_projector is _project_options_summary | |
| for agent in ("SPYAnalyst", "QQQAnalyst", "GLDAnalyst"): | |
| assert quant_io_binding(agent).prompt_projector is _project_asset_input | |
| assert quant_io_binding("OptionsFlowWatcher").prompt_projector is None | |
| source = ProjectionInput(dependencies={"options_flow": { | |
| "artifact_id": "options-1", | |
| "payload": {"ticker_results": [{ | |
| "ticker": "SPY", | |
| "contracts": [{"contract_symbol": "SPY-CALL"}], | |
| "market_context": {"contracts_sampled": 1}, | |
| "flow_candidates": [{ | |
| "candidate_id": "candidate-1", | |
| "evidence_refs": [{"field_path": "/contracts/0"}], | |
| }], | |
| "unusual_flows": [], | |
| "capabilities": {"trade_side": False}, | |
| "data_gaps": [], | |
| }]}, | |
| }}) | |
| projected = _project_options_summary(source) | |
| result = projected["dependencies"]["options_flow"]["payload"]["ticker_results"][0] | |
| assert "contracts" not in result | |
| assert "raw_contracts" not in result | |
| assert projected["prompt_only"] == { | |
| "non_citable_options_contract_counts": {"SPY": 1}, | |
| } | |
| assert result["market_context"] == {"contracts_sampled": 1} | |
| assert result["flow_candidates"] == [{"candidate_id": "candidate-1"}] | |
| assert source.dependencies["options_flow"]["payload"]["ticker_results"][0]["contracts"] | |
| def test_trend_projection_omits_nontechnical_records_only(): | |
| source = ProjectionInput(dependencies={"market_data": [{ | |
| "ticker": "SPY", | |
| "ohlcv": [{"close": 650.0}], | |
| "indicators": {"points": [{"rsi_14": 58.0}]}, | |
| "news": [{"headline": "Not used for technical analysis"}], | |
| "earnings": [{"fiscal_period": "Q3"}], | |
| "provenance": [{"component": "OHLCV"}], | |
| "quality_issues": [], | |
| }]}) | |
| projected = _project_trend_input(source) | |
| market = projected["dependencies"]["market_data"][0] | |
| assert "news" not in market | |
| assert "earnings" not in market | |
| assert market["omitted_nontechnical_records"] == {"news": 1, "earnings": 1} | |
| assert market["ohlcv"] == [{"close": 650.0}] | |
| assert market["indicators"] == {"points": [{"rsi_14": 58.0}]} | |
| assert source.dependencies["market_data"][0]["news"] | |
| def test_specialized_options_projection_reads_optional_context(): | |
| source = ProjectionInput(dependencies={}, optional_context={"options_flow": { | |
| "artifact_id": "options-1", | |
| "payload": {"ticker_results": [{ | |
| "ticker": "SPY", | |
| "contracts": [ | |
| {"contract_symbol": f"SPY-CALL-{index}"} for index in range(100) | |
| ], | |
| "market_context": {"contracts_sampled": 100}, | |
| "flow_candidates": [], | |
| "unusual_flows": [], | |
| "capabilities": {"trade_side": False}, | |
| "data_gaps": [], | |
| }]}, | |
| }}) | |
| projected = _project_options_summary(source) | |
| result = projected["optional_context"]["options_flow"]["payload"]["ticker_results"][0] | |
| assert "contracts" not in result | |
| assert "raw_contracts" not in result | |
| assert projected["prompt_only"]["non_citable_options_contract_counts"]["SPY"] == 100 | |
| assert result["market_context"] == {"contracts_sampled": 100} | |
| assert source.optional_context["options_flow"]["payload"]["ticker_results"][0]["contracts"] | |