yf176 commited on
Commit ·
7bedcbd
1
Parent(s): 2753d3a
fix(router): bind assignments to current event records
Browse files
src/polyhedra/agents/quant/market_watchers.py
CHANGED
|
@@ -1690,7 +1690,7 @@ class EventRouter(Agent):
|
|
| 1690 |
else:
|
| 1691 |
payload = EventRoutePayload(assignments=[], unassigned_event_ids=[])
|
| 1692 |
|
| 1693 |
-
|
| 1694 |
verify_evidence(payload, sources)
|
| 1695 |
output = EventRouterOutput(
|
| 1696 |
schema_version="8.0",
|
|
@@ -1841,11 +1841,11 @@ def _event_route_sources(
|
|
| 1841 |
return sources, input_refs
|
| 1842 |
|
| 1843 |
|
| 1844 |
-
def
|
| 1845 |
inputs: EventRouterInput,
|
| 1846 |
payload: EventRoutePayload,
|
| 1847 |
unusual_events_ref: JsonArtifactRef,
|
| 1848 |
-
) ->
|
| 1849 |
events = inputs.dependencies.unusual_events.payload.meaningful_events
|
| 1850 |
event_by_id = {event.event_id: event for event in events}
|
| 1851 |
pointer_by_id = {
|
|
@@ -1859,15 +1859,20 @@ def _validate_event_route_payload(
|
|
| 1859 |
if represented != set(event_by_id):
|
| 1860 |
raise ValueError("route must account for every and only meaningful event")
|
| 1861 |
|
|
|
|
| 1862 |
for assignment in payload.assignments:
|
| 1863 |
event = event_by_id[assignment.event_id]
|
| 1864 |
if assignment.ticker not in event.affected_tickers:
|
| 1865 |
raise ValueError("event assignment targets a ticker the event does not affect")
|
| 1866 |
if assignment.relevance <= 0:
|
| 1867 |
raise ValueError("event assignment requires positive relevance")
|
| 1868 |
-
|
| 1869 |
-
|
| 1870 |
-
|
| 1871 |
-
|
| 1872 |
-
|
| 1873 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1690 |
else:
|
| 1691 |
payload = EventRoutePayload(assignments=[], unassigned_event_ids=[])
|
| 1692 |
|
| 1693 |
+
payload = _bind_event_route_evidence(inputs, payload, input_refs[0])
|
| 1694 |
verify_evidence(payload, sources)
|
| 1695 |
output = EventRouterOutput(
|
| 1696 |
schema_version="8.0",
|
|
|
|
| 1841 |
return sources, input_refs
|
| 1842 |
|
| 1843 |
|
| 1844 |
+
def _bind_event_route_evidence(
|
| 1845 |
inputs: EventRouterInput,
|
| 1846 |
payload: EventRoutePayload,
|
| 1847 |
unusual_events_ref: JsonArtifactRef,
|
| 1848 |
+
) -> EventRoutePayload:
|
| 1849 |
events = inputs.dependencies.unusual_events.payload.meaningful_events
|
| 1850 |
event_by_id = {event.event_id: event for event in events}
|
| 1851 |
pointer_by_id = {
|
|
|
|
| 1859 |
if represented != set(event_by_id):
|
| 1860 |
raise ValueError("route must account for every and only meaningful event")
|
| 1861 |
|
| 1862 |
+
bound_assignments = []
|
| 1863 |
for assignment in payload.assignments:
|
| 1864 |
event = event_by_id[assignment.event_id]
|
| 1865 |
if assignment.ticker not in event.affected_tickers:
|
| 1866 |
raise ValueError("event assignment targets a ticker the event does not affect")
|
| 1867 |
if assignment.relevance <= 0:
|
| 1868 |
raise ValueError("event assignment requires positive relevance")
|
| 1869 |
+
bound_assignments.append(assignment.model_copy(update={
|
| 1870 |
+
"evidence_refs": [EvidenceRef(
|
| 1871 |
+
artifact=unusual_events_ref,
|
| 1872 |
+
field_path=pointer_by_id[assignment.event_id],
|
| 1873 |
+
)],
|
| 1874 |
+
}))
|
| 1875 |
+
return EventRoutePayload(
|
| 1876 |
+
assignments=bound_assignments,
|
| 1877 |
+
unassigned_event_ids=payload.unassigned_event_ids,
|
| 1878 |
+
)
|
tests/test_event_router_typed.py
CHANGED
|
@@ -105,6 +105,7 @@ def setup_router(
|
|
| 105 |
scope: list[str] | None = None,
|
| 106 |
meaningful: bool = True,
|
| 107 |
status: str = "COMPLETE",
|
|
|
|
| 108 |
):
|
| 109 |
ticker_scope = scope or ["BTC"]
|
| 110 |
session = tmp_path / "session1"
|
|
@@ -117,6 +118,16 @@ def setup_router(
|
|
| 117 |
meaningful=meaningful,
|
| 118 |
status=status,
|
| 119 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
unusual_files = ScopedFileWriter(
|
| 121 |
writer,
|
| 122 |
agent="UnusualEventDetector",
|
|
@@ -267,17 +278,57 @@ async def test_router_rejects_ticker_outside_event_affected_scope(tmp_path: Path
|
|
| 267 |
|
| 268 |
|
| 269 |
@pytest.mark.asyncio
|
| 270 |
-
async def
|
| 271 |
-
|
| 272 |
llm = ControlledLLM(route_payload(
|
| 273 |
unusual_ref,
|
| 274 |
field_path="/payload/meaningful_events/0/significance",
|
| 275 |
))
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
scope: list[str] | None = None,
|
| 106 |
meaningful: bool = True,
|
| 107 |
status: str = "COMPLETE",
|
| 108 |
+
second_event: bool = False,
|
| 109 |
):
|
| 110 |
ticker_scope = scope or ["BTC"]
|
| 111 |
session = tmp_path / "session1"
|
|
|
|
| 118 |
meaningful=meaningful,
|
| 119 |
status=status,
|
| 120 |
)
|
| 121 |
+
if second_event:
|
| 122 |
+
first = unusual.payload.meaningful_events[0]
|
| 123 |
+
unusual = unusual.model_copy(update={
|
| 124 |
+
"payload": unusual.payload.model_copy(update={
|
| 125 |
+
"meaningful_events": [
|
| 126 |
+
first,
|
| 127 |
+
first.model_copy(update={"event_id": "btc-news-catalyst"}),
|
| 128 |
+
],
|
| 129 |
+
}),
|
| 130 |
+
})
|
| 131 |
unusual_files = ScopedFileWriter(
|
| 132 |
writer,
|
| 133 |
agent="UnusualEventDetector",
|
|
|
|
| 278 |
|
| 279 |
|
| 280 |
@pytest.mark.asyncio
|
| 281 |
+
async def test_router_binds_exact_current_event_record(tmp_path: Path):
|
| 282 |
+
session, files, publisher, inputs, unusual_ref = setup_router(tmp_path)
|
| 283 |
llm = ControlledLLM(route_payload(
|
| 284 |
unusual_ref,
|
| 285 |
field_path="/payload/meaningful_events/0/significance",
|
| 286 |
))
|
| 287 |
|
| 288 |
+
await EventRouter(llm).execute_typed(
|
| 289 |
+
inputs,
|
| 290 |
+
files=files,
|
| 291 |
+
publisher=publisher,
|
| 292 |
+
now=lambda: AT,
|
| 293 |
+
)
|
| 294 |
+
output = EventRouterOutput.model_validate_json(
|
| 295 |
+
(session / "quant/runs/run1/event_route.json").read_text(), strict=True,
|
| 296 |
+
)
|
| 297 |
+
assert output.payload.assignments[0].evidence_refs[0].artifact == unusual_ref
|
| 298 |
+
assert output.payload.assignments[0].evidence_refs[0].field_path == (
|
| 299 |
+
"/payload/meaningful_events/0"
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
@pytest.mark.asyncio
|
| 304 |
+
async def test_router_binds_each_assignment_to_its_event_index(tmp_path: Path):
|
| 305 |
+
session, files, publisher, inputs, unusual_ref = setup_router(
|
| 306 |
+
tmp_path, second_event=True,
|
| 307 |
+
)
|
| 308 |
+
payload = EventRoutePayload.model_validate({
|
| 309 |
+
"assignments": [
|
| 310 |
+
{
|
| 311 |
+
"event_id": event.event_id,
|
| 312 |
+
"ticker": "BTC",
|
| 313 |
+
"relevance": 0.7,
|
| 314 |
+
"evidence_refs": [{
|
| 315 |
+
"artifact": unusual_ref,
|
| 316 |
+
"field_path": "/payload/meaningful_events/0/significance",
|
| 317 |
+
}],
|
| 318 |
+
"rationale": "The event affects BTC.",
|
| 319 |
+
}
|
| 320 |
+
for event in inputs.dependencies.unusual_events.payload.meaningful_events
|
| 321 |
+
],
|
| 322 |
+
"unassigned_event_ids": [],
|
| 323 |
+
})
|
| 324 |
+
|
| 325 |
+
await EventRouter(ControlledLLM(payload)).execute_typed(
|
| 326 |
+
inputs, files=files, publisher=publisher, now=lambda: AT,
|
| 327 |
+
)
|
| 328 |
+
output = EventRouterOutput.model_validate_json(
|
| 329 |
+
(session / "quant/runs/run1/event_route.json").read_text(), strict=True,
|
| 330 |
+
)
|
| 331 |
+
assert [ref.evidence_refs[0].field_path for ref in output.payload.assignments] == [
|
| 332 |
+
"/payload/meaningful_events/0",
|
| 333 |
+
"/payload/meaningful_events/1",
|
| 334 |
+
]
|