yf176 commited on
Commit
200133b
·
1 Parent(s): 1341b26

fix(strategy): bind parent evidence citations

Browse files
src/polyhedra/agents/quant/publisher.py CHANGED
@@ -137,6 +137,7 @@ class PublisherAgent(Agent):
137
  *upstream_gaps, *payload.data_gaps,
138
  ])],
139
  })
 
140
  _validate_publisher_payload(
141
  inputs,
142
  payload,
@@ -524,6 +525,25 @@ def _dedupe_publisher_gaps(gaps: list[DataIssue]) -> list[DataIssue]:
524
  return list(unique.values())
525
 
526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  def _validate_publisher_payload(
528
  inputs: PublisherInput,
529
  payload: PublisherPayload,
 
137
  *upstream_gaps, *payload.data_gaps,
138
  ])],
139
  })
140
+ payload = _bind_current_strategy_evidence(payload, input_refs[0])
141
  _validate_publisher_payload(
142
  inputs,
143
  payload,
 
525
  return list(unique.values())
526
 
527
 
528
+ def _bind_current_strategy_evidence(
529
+ payload: PublisherPayload,
530
+ strategy_ref: JsonArtifactRef,
531
+ ) -> PublisherPayload:
532
+ evidence_refs = [
533
+ evidence.model_dump(mode="json")
534
+ for evidence in payload.evidence_refs
535
+ if evidence.artifact != strategy_ref
536
+ ]
537
+ evidence_refs.append({
538
+ "artifact": strategy_ref.model_dump(mode="json"),
539
+ "field_path": "/payload",
540
+ })
541
+ return PublisherPayload.model_validate({
542
+ **payload.model_dump(mode="json"),
543
+ "evidence_refs": evidence_refs,
544
+ })
545
+
546
+
547
  def _validate_publisher_payload(
548
  inputs: PublisherInput,
549
  payload: PublisherPayload,
src/polyhedra/agents/quant/quantstrategist.py CHANGED
@@ -162,6 +162,8 @@ class QuantStrategistAgent(Agent):
162
  ])],
163
  })
164
 
 
 
165
  _validate_strategy_llm_payload(
166
  inputs,
167
  llm_payload,
@@ -683,6 +685,25 @@ def _dedupe_strategy_gaps(gaps: list[DataIssue]) -> list[DataIssue]:
683
  return list(unique.values())
684
 
685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
686
  def _validate_strategy_llm_payload(
687
  inputs: QuantStrategistInput,
688
  payload: QuantStrategistLLMPayload,
 
162
  ])],
163
  })
164
 
165
+ if inputs.dependencies.risk.status != Availability.UNAVAILABLE:
166
+ llm_payload = _bind_current_risk_evidence(llm_payload, input_refs[0])
167
  _validate_strategy_llm_payload(
168
  inputs,
169
  llm_payload,
 
685
  return list(unique.values())
686
 
687
 
688
+ def _bind_current_risk_evidence(
689
+ payload: QuantStrategistLLMPayload,
690
+ risk_ref: JsonArtifactRef,
691
+ ) -> QuantStrategistLLMPayload:
692
+ evidence_refs = [
693
+ evidence.model_dump(mode="json")
694
+ for evidence in payload.evidence_refs
695
+ if evidence.artifact != risk_ref
696
+ ]
697
+ evidence_refs.append({
698
+ "artifact": risk_ref.model_dump(mode="json"),
699
+ "field_path": "/payload",
700
+ })
701
+ return QuantStrategistLLMPayload.model_validate({
702
+ **payload.model_dump(mode="json"),
703
+ "evidence_refs": evidence_refs,
704
+ })
705
+
706
+
707
  def _validate_strategy_llm_payload(
708
  inputs: QuantStrategistInput,
709
  payload: QuantStrategistLLMPayload,
tests/test_publisher_typed.py CHANGED
@@ -285,15 +285,24 @@ async def test_finley_rejects_pending_label_without_pending_gap(tmp_path: Path):
285
 
286
 
287
  @pytest.mark.asyncio
288
- async def test_finley_requires_current_strategy_citation(tmp_path: Path):
289
- _, publisher, inputs, files, strategy_ref = setup_publisher(
290
  tmp_path, outcome="NO_TRADE",
291
  )
292
  payload = finley_payload(
293
  strategy_ref, outcome="NO_TRADE", reason="TRUE_NO_TRADE",
294
  ).model_copy(update={"evidence_refs": []})
295
 
296
- with pytest.raises(ValueError, match="current strategy"):
297
- await PublisherAgent(ControlledLLM(payload)).execute_typed(
298
- inputs, files=files, publisher=publisher, now=lambda: AT,
299
- )
 
 
 
 
 
 
 
 
 
 
285
 
286
 
287
  @pytest.mark.asyncio
288
+ async def test_finley_binds_current_strategy_citation(tmp_path: Path):
289
+ session, publisher, inputs, files, strategy_ref = setup_publisher(
290
  tmp_path, outcome="NO_TRADE",
291
  )
292
  payload = finley_payload(
293
  strategy_ref, outcome="NO_TRADE", reason="TRUE_NO_TRADE",
294
  ).model_copy(update={"evidence_refs": []})
295
 
296
+ await PublisherAgent(ControlledLLM(payload)).execute_typed(
297
+ inputs,
298
+ files=files,
299
+ publisher=publisher,
300
+ now=lambda: AT,
301
+ )
302
+
303
+ output = PublisherOutput.model_validate_json(
304
+ (session / "quant/runs/run1/post.json").read_text(),
305
+ strict=True,
306
+ )
307
+ assert output.payload.evidence_refs[0].artifact == strategy_ref
308
+ assert output.payload.evidence_refs[0].field_path == "/payload"
tests/test_quantstrategist_typed.py CHANGED
@@ -242,6 +242,26 @@ async def test_typed_strategy_publishes_all_rejected_no_trade(tmp_path: Path):
242
  assert state["quant_state"]["token_log"][0]["completion_tokens"] == 27
243
 
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  @pytest.mark.asyncio
246
  async def test_typed_strategy_uses_trusted_weights_for_aggregation(tmp_path: Path):
247
  weights = {
@@ -280,6 +300,23 @@ async def test_strategy_rejects_incomplete_committee(tmp_path: Path):
280
  )
281
 
282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  @pytest.mark.asyncio
284
  async def test_strategy_cannot_propose_rejected_signal(tmp_path: Path):
285
  _, publisher, inputs, files, risk_ref = setup_strategy(tmp_path)
 
242
  assert state["quant_state"]["token_log"][0]["completion_tokens"] == 27
243
 
244
 
245
+ @pytest.mark.asyncio
246
+ async def test_strategy_binds_current_risk_citation(tmp_path: Path):
247
+ session, publisher, inputs, files, risk_ref = setup_strategy(tmp_path)
248
+ payload = no_trade_payload(risk_ref).model_copy(update={"evidence_refs": []})
249
+
250
+ await QuantStrategistAgent(ControlledLLM(payload)).execute_typed(
251
+ inputs,
252
+ files=files,
253
+ publisher=publisher,
254
+ now=lambda: AT,
255
+ )
256
+
257
+ output = QuantStrategistOutput.model_validate_json(
258
+ (session / "quant/runs/run1/trade_proposals.json").read_text(),
259
+ strict=True,
260
+ )
261
+ assert output.payload.evidence_refs[0].artifact == risk_ref
262
+ assert output.payload.evidence_refs[0].field_path == "/payload"
263
+
264
+
265
  @pytest.mark.asyncio
266
  async def test_typed_strategy_uses_trusted_weights_for_aggregation(tmp_path: Path):
267
  weights = {
 
300
  )
301
 
302
 
303
+ @pytest.mark.asyncio
304
+ async def test_strategy_invalidates_vote_without_current_evidence(tmp_path: Path):
305
+ _, publisher, inputs, files, risk_ref = setup_strategy(tmp_path)
306
+ payload = no_trade_payload(risk_ref)
307
+ votes = list(payload.committee_votes)
308
+ votes[0] = votes[0].model_copy(update={"cited_evidence": []})
309
+ payload = payload.model_copy(update={"committee_votes": votes})
310
+
311
+ with pytest.raises(ValueError, match="committee vote requires current cited evidence"):
312
+ await QuantStrategistAgent(ControlledLLM(payload)).execute_typed(
313
+ inputs,
314
+ files=files,
315
+ publisher=publisher,
316
+ now=lambda: AT,
317
+ )
318
+
319
+
320
  @pytest.mark.asyncio
321
  async def test_strategy_cannot_propose_rejected_signal(tmp_path: Path):
322
  _, publisher, inputs, files, risk_ref = setup_strategy(tmp_path)