yf176 commited on
Commit
8a94c13
·
1 Parent(s): 7221e98

fix(strategy): normalize empty no-trade decisions

Browse files
src/polyhedra/agents/quant/quantstrategist.py CHANGED
@@ -165,6 +165,11 @@ class QuantStrategistAgent(Agent):
165
  })
166
 
167
  if inputs.dependencies.risk.status != Availability.UNAVAILABLE:
 
 
 
 
 
168
  llm_payload = _bind_current_risk_evidence(llm_payload, input_refs[0])
169
  _validate_strategy_llm_payload(
170
  inputs,
@@ -743,6 +748,39 @@ def _bind_current_risk_evidence(
743
  })
744
 
745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
746
  def _validate_strategy_llm_payload(
747
  inputs: QuantStrategistInput,
748
  payload: QuantStrategistLLMPayload,
 
165
  })
166
 
167
  if inputs.dependencies.risk.status != Availability.UNAVAILABLE:
168
+ llm_payload = _materialize_non_trade_proposal(
169
+ inputs,
170
+ llm_payload,
171
+ input_refs[0],
172
+ )
173
  llm_payload = _bind_current_risk_evidence(llm_payload, input_refs[0])
174
  _validate_strategy_llm_payload(
175
  inputs,
 
748
  })
749
 
750
 
751
+ def _materialize_non_trade_proposal(
752
+ inputs: QuantStrategistInput,
753
+ payload: QuantStrategistLLMPayload,
754
+ risk_ref: JsonArtifactRef,
755
+ ) -> QuantStrategistLLMPayload:
756
+ if payload.proposals or payload.outcome not in {
757
+ AnalysisOutcome.NO_TRADE,
758
+ AnalysisOutcome.WATCH_FOR_CONFIRMATION,
759
+ }:
760
+ return payload
761
+ condition = (
762
+ "Re-run when a new risk-approved signal with testable evidence is available."
763
+ if payload.outcome == AnalysisOutcome.NO_TRADE
764
+ else "Re-run when new evidence satisfies the watch rationale and passes risk review."
765
+ )
766
+ proposal = NonTradeProposal(
767
+ proposal_id=f"{inputs.run_id}-{payload.outcome.value.lower()}",
768
+ outcome=payload.outcome,
769
+ ticker_scope=inputs.ticker_scope,
770
+ confidence="LOW",
771
+ rationale=payload.no_trade_or_watch_rationale,
772
+ confirmation_conditions=[condition],
773
+ evidence_refs=[{
774
+ "artifact": risk_ref.model_dump(mode="json"),
775
+ "field_path": "/payload/overall_risk_summary",
776
+ }],
777
+ )
778
+ return QuantStrategistLLMPayload.model_validate({
779
+ **payload.model_dump(mode="json"),
780
+ "proposals": [proposal.model_dump(mode="json")],
781
+ })
782
+
783
+
784
  def _validate_strategy_llm_payload(
785
  inputs: QuantStrategistInput,
786
  payload: QuantStrategistLLMPayload,
tests/test_quantstrategist_typed.py CHANGED
@@ -378,8 +378,8 @@ async def test_zero_signal_strategy_votes_cite_existing_risk_summary(tmp_path: P
378
 
379
 
380
  @pytest.mark.asyncio
381
- async def test_current_no_trade_strategy_requires_decision_proposal(tmp_path: Path):
382
- _, publisher, inputs, files, risk_ref = setup_strategy(
383
  tmp_path,
384
  assessments=False,
385
  )
@@ -388,13 +388,22 @@ async def test_current_no_trade_strategy_requires_decision_proposal(tmp_path: Pa
388
  "exclusions": [],
389
  })
390
 
391
- with pytest.raises(ValueError, match="explicit decision proposal"):
392
- await QuantStrategistAgent(ControlledLLM(payload)).execute_typed(
393
- inputs,
394
- files=files,
395
- publisher=publisher,
396
- now=lambda: AT,
397
- )
 
 
 
 
 
 
 
 
 
398
 
399
 
400
  @pytest.mark.asyncio
 
378
 
379
 
380
  @pytest.mark.asyncio
381
+ async def test_current_no_trade_strategy_materializes_legacy_empty_proposal(tmp_path: Path):
382
+ session, publisher, inputs, files, risk_ref = setup_strategy(
383
  tmp_path,
384
  assessments=False,
385
  )
 
388
  "exclusions": [],
389
  })
390
 
391
+ await QuantStrategistAgent(ControlledLLM(payload)).execute_typed(
392
+ inputs,
393
+ files=files,
394
+ publisher=publisher,
395
+ now=lambda: AT,
396
+ )
397
+
398
+ output = QuantStrategistOutput.model_validate_json(
399
+ (session / "quant/runs/run1/trade_proposals.json").read_text(),
400
+ strict=True,
401
+ )
402
+ proposal = output.payload.proposals[0]
403
+ assert proposal.outcome == "NO_TRADE"
404
+ assert proposal.ticker_scope == ["SPY"]
405
+ assert proposal.rationale == "The current signal did not pass risk review."
406
+ assert proposal.evidence_refs[0].artifact == risk_ref
407
 
408
 
409
  @pytest.mark.asyncio