File size: 6,441 Bytes
32e44f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Common quant contracts reject ambiguous identities before artifact access."""

import json

import pytest
from pydantic import TypeAdapter, ValidationError

from polyhedra.core.artifacts import (
    AgentArtifact, AgentInput, ArtifactRef, DataIssue, Direction, EvidenceRef,
    JsonArtifactRef, RenderingRef, TradeDirection, TypedModel,
)


def ref(**updates):
    data = dict(
        format="JSON", schema_version="8.0", artifact_id="current-data",
        session_id="session1", run_id="run1", producer="DataFetcher",
        path="quant/runs/run1/raw/SPY.json",
    )
    data.update(updates)
    return JsonArtifactRef.model_validate(data)


class EmptyContext(TypedModel):
    pass


class Dependencies(TypedModel):
    market_data: JsonArtifactRef


class Payload(TypedModel):
    interpretation: str


def input_data(**updates):
    data = dict(
        run_id="run1", session_id="session1", run_mode="BASELINE",
        as_of="2026-09-09T12:00:00Z", ticker_scope=["SPY"],
        dependencies={"market_data": ref().model_dump(mode="json")}, optional_context={},
    )
    data.update(updates)
    return data


def artifact_data(**updates):
    data = dict(
        schema_version="8.0", artifact_id="trend", run_id="run1", session_id="session1",
        producer="TrendAnalyst", ticker_scope=["SPY"], created_at="2026-09-09T12:01:00Z",
        status="COMPLETE", input_refs=[ref().model_dump(mode="json")], baseline_refs=[],
        data_gaps=[], payload={"interpretation": "Stable prices"},
    )
    data.update(updates)
    return data


def test_typed_envelope_json_round_trip():
    inputs = AgentInput[Dependencies, EmptyContext].model_validate_json(json.dumps(input_data()), strict=True)
    assert inputs.dependencies.market_data.producer == "DataFetcher"
    artifact = AgentArtifact[Payload].model_validate_json(json.dumps(artifact_data()), strict=True)
    assert AgentArtifact[Payload].model_validate_json(artifact.model_dump_json(), strict=True) == artifact


@pytest.mark.parametrize("changes", [
    {"schema_version": "7.1"}, {"path": "C:/secret.json"},
    {"path": "quant/runs/run1/../secret.json"}, {"path": "quant/runs/run2/raw/SPY.json"},
    {"path": "quant/runs/run1/raw/SPY*.json"}, {"path": "quant/runs/run1/raw/SPY.json:secret"},
    {"path": "quant/runs/run1/raw/spy.json"}, {"producer": "TrendAnalyst"},
    {"path": "quant/runs/run1/raw/SPY_summary.md"}, {"producer": "human"},
])
def test_bad_json_reference(changes):
    with pytest.raises(ValidationError):
        ref(**changes)


def test_rendering_has_format_and_source_instead_of_schema_version():
    rendering = RenderingRef(
        format="MARKDOWN", artifact_id="summary", session_id="session1", run_id="run1",
        producer="DataFetcher", path="quant/runs/run1/raw/SPY_summary.md", source=ref(),
    )
    adapter = TypeAdapter(ArtifactRef)
    assert adapter.validate_json(rendering.model_dump_json(), strict=True) == rendering
    with pytest.raises(ValidationError):
        RenderingRef.model_validate({**rendering.model_dump(), "schema_version": "8.0"})
    for changes in (
        {"session_id": "other"}, {"artifact_id": "current-data"},
        {"path": "quant/runs/run1/raw/QQQ_summary.md"}, {"format": "PNG"},
    ):
        with pytest.raises(ValidationError):
            RenderingRef.model_validate({**rendering.model_dump(), **changes})


def test_evidence_requires_exact_locator():
    assert EvidenceRef(artifact=ref(), field_path="/ohlcv/0/close").field_path
    assert EvidenceRef(artifact=ref(), record_id="2026-09-08").record_id
    with pytest.raises(ValidationError):
        EvidenceRef(artifact=ref())
    with pytest.raises(ValidationError):
        EvidenceRef(artifact=ref(), field_path="/invalid~escape")


@pytest.mark.parametrize("changes", [
    {"ticker_scope": []}, {"ticker_scope": ["SPY", "SPY"]}, {"ticker_scope": ["SPY*"]},
    {"as_of": "2026-09-09T12:00:00"}, {"next_role": "human"},
    {"optional_context": {"thread_history": "undeclared"}},
    {"baseline_refs": [ref().model_dump(mode="json")]},
])
def test_bad_input_envelope(changes):
    with pytest.raises(ValidationError):
        AgentInput[Dependencies, EmptyContext].model_validate(input_data(**changes))


def test_delta_reference_preserves_prior_run_and_session():
    prior = ref(run_id="prior", path="quant/runs/prior/raw/SPY.json", artifact_id="prior-data")
    model = AgentInput[Dependencies, EmptyContext]
    assert model.model_validate(input_data(run_mode="DELTA", baseline_refs=[prior])).baseline_refs == [prior]
    for references in ([ref()], [prior, prior], [prior.model_copy(update={"session_id": "other"})]):
        with pytest.raises(ValidationError):
            model.model_validate(input_data(run_mode="DELTA", baseline_refs=references))


@pytest.mark.parametrize("changes", [
    {"status": "FAILED"}, {"input_refs": [ref(session_id="other")]},
    {"input_refs": [ref(), ref()]}, {"baseline_refs": [ref()]},
    {"data_gaps": [DataIssue(component="NEWS", code="UNAVAILABLE", message="Missing", ticker="QQQ")]},
    {"payload": {"interpretation": "Stable", "next_role": "Publisher"}},
])
def test_bad_output_envelope(changes):
    with pytest.raises(ValidationError):
        AgentArtifact[Payload].model_validate(artifact_data(**changes))


def test_render_reference_must_point_to_own_artifact():
    source = ref(producer="TrendAnalyst", artifact_id="trend", path="quant/runs/run1/trend_analysis.json")
    rendering = RenderingRef(
        format="MARKDOWN", artifact_id="trend-render", session_id="session1", run_id="run1",
        producer="TrendAnalyst", path="quant/runs/run1/trend_analysis.md", source=source,
    )
    model = AgentArtifact[Payload]
    assert model.model_validate(artifact_data(render_ref=rendering)).render_ref == rendering
    with pytest.raises(ValidationError):
        model.model_validate(artifact_data(artifact_id="different", render_ref=rendering))


def test_trade_direction_is_not_market_direction():
    with pytest.raises(ValidationError):
        TypeAdapter(TradeDirection).validate_python("BULLISH")
    with pytest.raises(ValidationError):
        TypeAdapter(Direction).validate_python("LONG")


def test_runtime_aggregate_has_explicit_owner():
    assert ref(producer="Runtime", path="quant/runs/run1/asset_analysis.json").producer == "Runtime"
    with pytest.raises(ValidationError):
        ref(producer="Runtime", path="quant/runs/run1/trend_analysis.json")