yf176 commited on
Commit
07a9bce
·
1 Parent(s): 81d2128

fix: bind Finley gaps to direct evidence

Browse files
src/polyhedra/agents/quant/publisher.py CHANGED
@@ -29,7 +29,7 @@ from polyhedra.agents.quant.product_contracts import (
29
  )
30
  from polyhedra.agents.quant.routing import LLM_ROUTING_PROMPT, parse_llm_routing
31
  from polyhedra.agents.quant.yolo_prompt_context import build_yolo_prompt_context
32
- from polyhedra.core.artifacts import Availability, DataIssue, DeltaState, JsonArtifactRef
33
  from polyhedra.core.dependency_executor import AgentCompletion
34
  from polyhedra.core.token_budget import TokenBudget
35
  from polyhedra.services.artifact_publisher import ArtifactPublisher
@@ -117,7 +117,7 @@ class PublisherAgent(Agent):
117
  clock = now or (lambda: datetime.now(tz=UTC))
118
  sources, input_refs, baseline_sources = _publisher_sources(inputs, files)
119
  publisher.record_input("Publisher", inputs)
120
- upstream_gaps = _publisher_input_gaps(inputs)
121
  binding = quant_io_binding("Publisher")
122
  prompt = binding.build_prompt(inputs)
123
  prompt += governed_evidence_context(input_refs)
@@ -135,7 +135,7 @@ class PublisherAgent(Agent):
135
  payload = PublisherPayload.model_validate({
136
  **payload.model_dump(mode="json"),
137
  "data_gaps": [gap.model_dump(mode="json") for gap in _dedupe_publisher_gaps([
138
- *upstream_gaps, *payload.data_gaps,
139
  ])],
140
  })
141
  payload = _bind_current_strategy_evidence(payload, input_refs[0])
@@ -493,7 +493,10 @@ def _publisher_baseline_context(sources: list[EvidenceSource]) -> str:
493
  )
494
 
495
 
496
- def _publisher_input_gaps(inputs: PublisherInput) -> list[DataIssue]:
 
 
 
497
  outputs = [inputs.dependencies.strategy]
498
  for value in (
499
  inputs.optional_context.trend,
@@ -503,18 +506,25 @@ def _publisher_input_gaps(inputs: PublisherInput) -> list[DataIssue]:
503
  ):
504
  if value is not None:
505
  outputs.append(value)
 
506
  gaps = [
507
- gap
 
 
 
508
  for output in outputs
509
- for gap in output.data_gaps
510
  if gap.ticker is None or gap.ticker in inputs.ticker_scope
511
  ]
512
  assets = inputs.optional_context.asset_analyses
513
  if assets is not None:
514
  gaps.extend(
515
- gap
516
- for contribution in assets.contributions
517
- for gap in contribution.data_gaps
 
 
 
518
  )
519
  return _dedupe_publisher_gaps(gaps)
520
 
@@ -598,7 +608,7 @@ def _validate_publisher_payload(
598
 
599
  required_gaps = {
600
  (gap.component, gap.code, gap.ticker)
601
- for gap in _publisher_input_gaps(inputs)
602
  }
603
  payload_gaps = {
604
  (gap.component, gap.code, gap.ticker)
 
29
  )
30
  from polyhedra.agents.quant.routing import LLM_ROUTING_PROMPT, parse_llm_routing
31
  from polyhedra.agents.quant.yolo_prompt_context import build_yolo_prompt_context
32
+ from polyhedra.core.artifacts import Availability, DataIssue, DeltaState, EvidenceRef, JsonArtifactRef
33
  from polyhedra.core.dependency_executor import AgentCompletion
34
  from polyhedra.core.token_budget import TokenBudget
35
  from polyhedra.services.artifact_publisher import ArtifactPublisher
 
117
  clock = now or (lambda: datetime.now(tz=UTC))
118
  sources, input_refs, baseline_sources = _publisher_sources(inputs, files)
119
  publisher.record_input("Publisher", inputs)
120
+ upstream_gaps = _publisher_input_gaps(inputs, input_refs)
121
  binding = quant_io_binding("Publisher")
122
  prompt = binding.build_prompt(inputs)
123
  prompt += governed_evidence_context(input_refs)
 
135
  payload = PublisherPayload.model_validate({
136
  **payload.model_dump(mode="json"),
137
  "data_gaps": [gap.model_dump(mode="json") for gap in _dedupe_publisher_gaps([
138
+ *payload.data_gaps, *upstream_gaps,
139
  ])],
140
  })
141
  payload = _bind_current_strategy_evidence(payload, input_refs[0])
 
493
  )
494
 
495
 
496
+ def _publisher_input_gaps(
497
+ inputs: PublisherInput,
498
+ input_refs: list[JsonArtifactRef],
499
+ ) -> list[DataIssue]:
500
  outputs = [inputs.dependencies.strategy]
501
  for value in (
502
  inputs.optional_context.trend,
 
506
  ):
507
  if value is not None:
508
  outputs.append(value)
509
+ references = {ref.producer: ref for ref in input_refs}
510
  gaps = [
511
+ gap.model_copy(update={"record_refs": [EvidenceRef(
512
+ artifact=references[output.producer],
513
+ field_path=f"/data_gaps/{index}",
514
+ )]})
515
  for output in outputs
516
+ for index, gap in enumerate(output.data_gaps)
517
  if gap.ticker is None or gap.ticker in inputs.ticker_scope
518
  ]
519
  assets = inputs.optional_context.asset_analyses
520
  if assets is not None:
521
  gaps.extend(
522
+ gap.model_copy(update={"record_refs": [EvidenceRef(
523
+ artifact=references["Runtime"],
524
+ field_path=f"/contributions/{contribution_index}/data_gaps/{gap_index}",
525
+ )]})
526
+ for contribution_index, contribution in enumerate(assets.contributions)
527
+ for gap_index, gap in enumerate(contribution.data_gaps)
528
  )
529
  return _dedupe_publisher_gaps(gaps)
530
 
 
608
 
609
  required_gaps = {
610
  (gap.component, gap.code, gap.ticker)
611
+ for gap in _publisher_input_gaps(inputs, input_refs)
612
  }
613
  payload_gaps = {
614
  (gap.component, gap.code, gap.ticker)
tests/test_publisher_typed.py CHANGED
@@ -56,6 +56,10 @@ def strategy_output(outcome: str) -> QuantStrategistOutput:
56
  "code": "RISK_ASSESSMENT_UNAVAILABLE",
57
  "message": "No usable current risk assessment is available.",
58
  "ticker": "SPY",
 
 
 
 
59
  }] if outcome == "ANALYSIS_UNAVAILABLE" else [])
60
  proposals = ([{
61
  "proposal_id": "spy-paper-long",
@@ -283,6 +287,8 @@ async def test_finley_labels_unavailable_strategy_as_data_loss(tmp_path: Path):
283
  )
284
  assert "DATA_LOSS" in output.payload.slack_rendering
285
  assert "No usable current risk assessment" in output.payload.slack_rendering
 
 
286
 
287
 
288
  @pytest.mark.asyncio
 
56
  "code": "RISK_ASSESSMENT_UNAVAILABLE",
57
  "message": "No usable current risk assessment is available.",
58
  "ticker": "SPY",
59
+ "record_refs": [{
60
+ "artifact": ref("RiskAnalyst", "risk_assessment.json"),
61
+ "field_path": "/data_gaps/0",
62
+ }],
63
  }] if outcome == "ANALYSIS_UNAVAILABLE" else [])
64
  proposals = ([{
65
  "proposal_id": "spy-paper-long",
 
287
  )
288
  assert "DATA_LOSS" in output.payload.slack_rendering
289
  assert "No usable current risk assessment" in output.payload.slack_rendering
290
+ assert output.data_gaps[0].record_refs[0].artifact.producer == "QuantStrategist"
291
+ assert output.data_gaps[0].record_refs[0].field_path == "/data_gaps/0"
292
 
293
 
294
  @pytest.mark.asyncio