yf176 commited on
Commit ·
3698528
1
Parent(s): ca9ba0a
fix: bind risk gaps to direct evidence
Browse files
src/polyhedra/agents/quant/riskanalyst.py
CHANGED
|
@@ -24,7 +24,7 @@ from polyhedra.agents.quant.io_registry import governed_evidence_context, quant_
|
|
| 24 |
from polyhedra.agents.quant.llm_errors import format_llm_failure
|
| 25 |
from polyhedra.agents.quant.routing import LLM_ROUTING_PROMPT, parse_llm_routing
|
| 26 |
from polyhedra.agents.quant.yolo_prompt_context import build_yolo_prompt_context
|
| 27 |
-
from polyhedra.core.artifacts import Availability, DataIssue, JsonArtifactRef, RiskVerdict
|
| 28 |
from polyhedra.core.dependency_executor import AgentCompletion
|
| 29 |
from polyhedra.core.token_budget import TokenBudget
|
| 30 |
from polyhedra.services.artifact_publisher import ArtifactPublisher
|
|
@@ -95,7 +95,7 @@ class RiskAnalystAgent(Agent):
|
|
| 95 |
clock = now or (lambda: datetime.now(tz=UTC))
|
| 96 |
sources, input_refs, baseline_sources = _risk_sources(inputs, files)
|
| 97 |
publisher.record_input("RiskAnalyst", inputs)
|
| 98 |
-
upstream_gaps = _risk_input_gaps(inputs)
|
| 99 |
backtest_results = inputs.dependencies.backtests.payload.results
|
| 100 |
open_positions = inputs.optional_context.open_positions
|
| 101 |
|
|
@@ -117,8 +117,8 @@ class RiskAnalystAgent(Agent):
|
|
| 117 |
payload = RiskPayload.model_validate({
|
| 118 |
**payload.model_dump(mode="json"),
|
| 119 |
"data_gaps": [gap.model_dump(mode="json") for gap in _dedupe_risk_gaps([
|
| 120 |
-
*upstream_gaps,
|
| 121 |
*payload.data_gaps,
|
|
|
|
| 122 |
])],
|
| 123 |
})
|
| 124 |
else:
|
|
@@ -384,24 +384,34 @@ def _risk_baseline_context(sources: list[EvidenceSource]) -> str:
|
|
| 384 |
)
|
| 385 |
|
| 386 |
|
| 387 |
-
def _risk_input_gaps(
|
|
|
|
|
|
|
|
|
|
| 388 |
outputs = [inputs.dependencies.backtests]
|
| 389 |
if inputs.optional_context.trend is not None:
|
| 390 |
outputs.append(inputs.optional_context.trend)
|
| 391 |
if inputs.optional_context.macro_context is not None:
|
| 392 |
outputs.append(inputs.optional_context.macro_context)
|
|
|
|
| 393 |
gaps = [
|
| 394 |
-
gap
|
|
|
|
|
|
|
|
|
|
| 395 |
for output in outputs
|
| 396 |
-
for gap in output.data_gaps
|
| 397 |
if gap.ticker is None or gap.ticker in inputs.ticker_scope
|
| 398 |
]
|
| 399 |
assets = inputs.optional_context.asset_analyses
|
| 400 |
if assets is not None:
|
| 401 |
gaps.extend(
|
| 402 |
-
gap
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
| 405 |
)
|
| 406 |
return _dedupe_risk_gaps(gaps)
|
| 407 |
|
|
@@ -512,7 +522,7 @@ def _validate_risk_payload(
|
|
| 512 |
raise ValueError("unvalidated signals cannot support numerical loss estimates")
|
| 513 |
required_gaps = {
|
| 514 |
(gap.component, gap.code, gap.ticker)
|
| 515 |
-
for gap in _risk_input_gaps(inputs)
|
| 516 |
}
|
| 517 |
payload_gaps = {
|
| 518 |
(gap.component, gap.code, gap.ticker)
|
|
|
|
| 24 |
from polyhedra.agents.quant.llm_errors import format_llm_failure
|
| 25 |
from polyhedra.agents.quant.routing import LLM_ROUTING_PROMPT, parse_llm_routing
|
| 26 |
from polyhedra.agents.quant.yolo_prompt_context import build_yolo_prompt_context
|
| 27 |
+
from polyhedra.core.artifacts import Availability, DataIssue, EvidenceRef, JsonArtifactRef, RiskVerdict
|
| 28 |
from polyhedra.core.dependency_executor import AgentCompletion
|
| 29 |
from polyhedra.core.token_budget import TokenBudget
|
| 30 |
from polyhedra.services.artifact_publisher import ArtifactPublisher
|
|
|
|
| 95 |
clock = now or (lambda: datetime.now(tz=UTC))
|
| 96 |
sources, input_refs, baseline_sources = _risk_sources(inputs, files)
|
| 97 |
publisher.record_input("RiskAnalyst", inputs)
|
| 98 |
+
upstream_gaps = _risk_input_gaps(inputs, input_refs)
|
| 99 |
backtest_results = inputs.dependencies.backtests.payload.results
|
| 100 |
open_positions = inputs.optional_context.open_positions
|
| 101 |
|
|
|
|
| 117 |
payload = RiskPayload.model_validate({
|
| 118 |
**payload.model_dump(mode="json"),
|
| 119 |
"data_gaps": [gap.model_dump(mode="json") for gap in _dedupe_risk_gaps([
|
|
|
|
| 120 |
*payload.data_gaps,
|
| 121 |
+
*upstream_gaps,
|
| 122 |
])],
|
| 123 |
})
|
| 124 |
else:
|
|
|
|
| 384 |
)
|
| 385 |
|
| 386 |
|
| 387 |
+
def _risk_input_gaps(
|
| 388 |
+
inputs: RiskAnalystInput,
|
| 389 |
+
input_refs: list[JsonArtifactRef],
|
| 390 |
+
) -> list[DataIssue]:
|
| 391 |
outputs = [inputs.dependencies.backtests]
|
| 392 |
if inputs.optional_context.trend is not None:
|
| 393 |
outputs.append(inputs.optional_context.trend)
|
| 394 |
if inputs.optional_context.macro_context is not None:
|
| 395 |
outputs.append(inputs.optional_context.macro_context)
|
| 396 |
+
references = {ref.producer: ref for ref in input_refs}
|
| 397 |
gaps = [
|
| 398 |
+
gap.model_copy(update={"record_refs": [EvidenceRef(
|
| 399 |
+
artifact=references[output.producer],
|
| 400 |
+
field_path=f"/data_gaps/{index}",
|
| 401 |
+
)]})
|
| 402 |
for output in outputs
|
| 403 |
+
for index, gap in enumerate(output.data_gaps)
|
| 404 |
if gap.ticker is None or gap.ticker in inputs.ticker_scope
|
| 405 |
]
|
| 406 |
assets = inputs.optional_context.asset_analyses
|
| 407 |
if assets is not None:
|
| 408 |
gaps.extend(
|
| 409 |
+
gap.model_copy(update={"record_refs": [EvidenceRef(
|
| 410 |
+
artifact=references["Runtime"],
|
| 411 |
+
field_path=f"/contributions/{contribution_index}/data_gaps/{gap_index}",
|
| 412 |
+
)]})
|
| 413 |
+
for contribution_index, contribution in enumerate(assets.contributions)
|
| 414 |
+
for gap_index, gap in enumerate(contribution.data_gaps)
|
| 415 |
)
|
| 416 |
return _dedupe_risk_gaps(gaps)
|
| 417 |
|
|
|
|
| 522 |
raise ValueError("unvalidated signals cannot support numerical loss estimates")
|
| 523 |
required_gaps = {
|
| 524 |
(gap.component, gap.code, gap.ticker)
|
| 525 |
+
for gap in _risk_input_gaps(inputs, input_refs)
|
| 526 |
}
|
| 527 |
payload_gaps = {
|
| 528 |
(gap.component, gap.code, gap.ticker)
|
tests/test_riskanalyst_typed.py
CHANGED
|
@@ -56,6 +56,10 @@ def backtest_output(
|
|
| 56 |
"code": "HISTORICAL_ENGINE_NOT_CONFIGURED",
|
| 57 |
"message": "No approved historical execution engine is configured.",
|
| 58 |
"ticker": None,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}])
|
| 60 |
result_items = []
|
| 61 |
if results:
|
|
@@ -382,3 +386,5 @@ async def test_unavailable_empty_backtests_remain_unavailable(tmp_path: Path):
|
|
| 382 |
strict=True,
|
| 383 |
)
|
| 384 |
assert output.data_gaps[0].code == "HISTORICAL_ENGINE_NOT_CONFIGURED"
|
|
|
|
|
|
|
|
|
| 56 |
"code": "HISTORICAL_ENGINE_NOT_CONFIGURED",
|
| 57 |
"message": "No approved historical execution engine is configured.",
|
| 58 |
"ticker": None,
|
| 59 |
+
"record_refs": [{
|
| 60 |
+
"artifact": ref("QuantResearcher", "signals.json"),
|
| 61 |
+
"field_path": "/payload/data_gaps/0",
|
| 62 |
+
}],
|
| 63 |
}])
|
| 64 |
result_items = []
|
| 65 |
if results:
|
|
|
|
| 386 |
strict=True,
|
| 387 |
)
|
| 388 |
assert output.data_gaps[0].code == "HISTORICAL_ENGINE_NOT_CONFIGURED"
|
| 389 |
+
assert output.data_gaps[0].record_refs[0].artifact.producer == "Backtester"
|
| 390 |
+
assert output.data_gaps[0].record_refs[0].field_path == "/data_gaps/0"
|