Download tests/test_published_inputs.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 6.16 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_published_inputs.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/tests/test_published_inputs.py
-
curl -L -o test_published_inputs.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_published_inputs.py
6.16 kB
| """Activation input capture sees only immutable, ACL-readable typed JSON.""" | |
| from pathlib import Path | |
| import pytest | |
| from polyhedra.agents.quant.analysis_contracts import TrendAnalystOutput | |
| from polyhedra.core.artifacts import JsonArtifactRef | |
| from polyhedra.services.artifact_publisher import ArtifactPublisher | |
| from polyhedra.services.file_writer import ( | |
| ArtifactAccessError, FileWriter, PublicationRegistry, ScopedFileWriter, | |
| ) | |
| from polyhedra.services.published_inputs import PublishedInputResolver | |
| AT = "2026-09-12T12:00:00Z" | |
| def ref(producer, filename, *, run="run1", artifact_id=None): | |
| return dict( | |
| schema_version="8.0", artifact_id=artifact_id or f"{producer}-{run}", run_id=run, | |
| session_id="session1", producer=producer, path=f"quant/runs/{run}/{filename}", | |
| ) | |
| def trend(*, run="run1", artifact_id=None): | |
| return TrendAnalystOutput.model_validate(dict( | |
| schema_version="8.0", artifact_id=artifact_id or f"TrendAnalyst-{run}", run_id=run, | |
| session_id="session1", producer="TrendAnalyst", ticker_scope=["SPY"], | |
| created_at=AT, status="COMPLETE", | |
| input_refs=[ref("DataFetcher", "data_fetch.json", run=run)], | |
| baseline_refs=[], data_gaps=[], render_ref=None, payload={"ticker_analyses": [dict( | |
| ticker="SPY", price_direction="NEUTRAL", price_change_percentage=0.0, | |
| measurement_days=5, support_levels=[], resistance_levels=[], volume_state="NORMAL", | |
| sma_20_state="UNKNOWN", ema_20_state="UNKNOWN", moving_average_crossover="NONE", | |
| rsi_14_state="UNKNOWN", macd_state="UNKNOWN", confidence="LOW", | |
| evidence_refs=[{"artifact": ref( | |
| "DataFetcher", "raw/SPY.json", run=run, artifact_id=f"SPY-{run}", | |
| ), "field_path": "/ohlcv/0/close"}], | |
| delta_state="NOT_COMPARED", data_gaps=[], | |
| )]}, | |
| )) | |
| def setup(tmp_path: Path): | |
| session = tmp_path / "session1" | |
| session.mkdir() | |
| writer = FileWriter(session, session_id="session1") | |
| publications = PublicationRegistry() | |
| producer = ScopedFileWriter( | |
| writer, agent="TrendAnalyst", run_id="run1", ticker_scope=["SPY"], | |
| publications=publications, | |
| ) | |
| consumer = ScopedFileWriter( | |
| writer, agent="Publisher", run_id="run1", ticker_scope=["SPY"], | |
| publications=publications, | |
| ) | |
| return writer, publications, producer, consumer | |
| def publish_trend(writer, producer): | |
| return ArtifactPublisher( | |
| writer, session_id="session1", run_id=producer.run_id, | |
| ).publish( | |
| producer, trend(run=producer.run_id), | |
| json_path=f"quant/runs/{producer.run_id}/trend_analysis.json", | |
| markdown_path=f"quant/runs/{producer.run_id}/trend_analysis.md", | |
| ) | |
| def test_capture_validates_required_reference_and_producer_schema(tmp_path: Path): | |
| writer, _, producer, consumer = setup(tmp_path) | |
| publication = publish_trend(writer, producer) | |
| snapshot = PublishedInputResolver(consumer).capture( | |
| required_refs=(publication.artifact_ref,), | |
| ) | |
| assert snapshot.references == (publication.artifact_ref,) | |
| assert snapshot.required[0].value == trend() | |
| assert snapshot.optional == () | |
| assert snapshot.baseline == () | |
| def test_optional_inputs_are_frozen_at_activation_time(tmp_path: Path): | |
| writer, _, producer, consumer = setup(tmp_path) | |
| resolver = PublishedInputResolver(consumer) | |
| before = resolver.capture() | |
| publication = publish_trend(writer, producer) | |
| after = resolver.capture() | |
| assert before.optional == () | |
| assert [item.reference for item in after.optional] == [publication.artifact_ref] | |
| assert before.optional == () | |
| def test_unpublished_or_acl_forbidden_artifacts_cannot_be_inputs(tmp_path: Path): | |
| writer, publications, _, _ = setup(tmp_path) | |
| path = "quant/runs/run1/trend_analysis.json" | |
| writer.write(path, trend().model_dump_json()) | |
| reference = ref("TrendAnalyst", "trend_analysis.json") | |
| typed_ref = JsonArtifactRef.model_validate(reference) | |
| consumer = ScopedFileWriter( | |
| writer, agent="Publisher", run_id="run1", ticker_scope=["SPY"], | |
| publications=publications, | |
| ) | |
| with pytest.raises(ArtifactAccessError, match="published"): | |
| PublishedInputResolver(consumer).capture(required_refs=(typed_ref,)) | |
| publications.publish(path, typed_ref) | |
| forbidden = ScopedFileWriter( | |
| writer, agent="OptionsFlowWatcher", run_id="run1", ticker_scope=["SPY"], | |
| publications=publications, | |
| ) | |
| with pytest.raises(ArtifactAccessError, match="cannot read"): | |
| PublishedInputResolver(forbidden).capture(required_refs=(typed_ref,)) | |
| def test_published_contents_must_match_registered_identity(tmp_path: Path): | |
| writer, publications, _, consumer = setup(tmp_path) | |
| path = "quant/runs/run1/trend_analysis.json" | |
| writer.write(path, trend(artifact_id="different").model_dump_json()) | |
| reference = JsonArtifactRef.model_validate(ref("TrendAnalyst", "trend_analysis.json")) | |
| publications.publish(path, reference) | |
| with pytest.raises(ValueError, match="contents contradict"): | |
| PublishedInputResolver(consumer).capture(required_refs=(reference,)) | |
| def test_selected_baseline_is_read_only_and_captured_separately(tmp_path: Path): | |
| session = tmp_path / "session1" | |
| session.mkdir() | |
| writer = FileWriter(session, session_id="session1") | |
| publications = PublicationRegistry() | |
| producer = ScopedFileWriter( | |
| writer, agent="TrendAnalyst", run_id="prior", ticker_scope=["SPY"], | |
| publications=publications, | |
| ) | |
| publication = publish_trend(writer, producer) | |
| consumer = ScopedFileWriter( | |
| writer, agent="TrendAnalyst", run_id="run1", ticker_scope=["SPY"], | |
| baseline_run_id="prior", publications=publications, | |
| ) | |
| snapshot = PublishedInputResolver(consumer).capture( | |
| baseline_refs=(publication.artifact_ref,), | |
| ) | |
| assert snapshot.required == () | |
| assert [item.reference for item in snapshot.baseline] == [publication.artifact_ref] | |
| with pytest.raises(ValueError, match="current run"): | |
| PublishedInputResolver(consumer).capture(required_refs=(publication.artifact_ref,)) | |