fix runtime journal run lifecycle events (#3470)

This commit is contained in:
Nan Gao
2026-06-10 02:33:29 +02:00
committed by GitHub
parent ae9e8bc0bf
commit a57d05fe0a
2 changed files with 16 additions and 4 deletions
@@ -164,7 +164,18 @@ class RunJournal(BaseCallbackHandler):
metadata={"caller": caller, **(metadata or {})}, metadata={"caller": caller, **(metadata or {})},
) )
def on_chain_end(self, outputs: Any, *, run_id: UUID, **kwargs: Any) -> None: def on_chain_end(
self,
outputs: Any,
*,
run_id: UUID,
parent_run_id: UUID | None = None,
**kwargs: Any,
) -> None:
# Nested chain ends fire for internal graph nodes; only the root chain
# represents the user-visible run lifecycle.
if parent_run_id is not None:
return
self._put(event_type="run.end", category="outputs", content=outputs, metadata={"status": "success"}) self._put(event_type="run.end", category="outputs", content=outputs, metadata={"status": "success"})
self._flush_sync() self._flush_sync()
+4 -3
View File
@@ -179,15 +179,16 @@ class TestLifecycleCallbacks:
assert "run.end" in types assert "run.end" in types
@pytest.mark.anyio @pytest.mark.anyio
async def test_nested_chain_no_run_start(self, journal_setup): async def test_nested_chain_no_run_lifecycle_events(self, journal_setup):
"""Nested chains (parent_run_id set) should NOT produce run.start.""" """Nested chains (parent_run_id set) should NOT produce root run lifecycle events."""
j, store = journal_setup j, store = journal_setup
parent_id = uuid4() parent_id = uuid4()
j.on_chain_start({}, {}, run_id=uuid4(), parent_run_id=parent_id) j.on_chain_start({}, {}, run_id=uuid4(), parent_run_id=parent_id)
j.on_chain_end({}, run_id=uuid4()) j.on_chain_end({}, run_id=uuid4(), parent_run_id=parent_id)
await j.flush() await j.flush()
events = await store.list_events("t1", "r1") events = await store.list_events("t1", "r1")
assert not any(e["event_type"] == "run.start" for e in events) assert not any(e["event_type"] == "run.start" for e in events)
assert not any(e["event_type"] == "run.end" for e in events)
class TestToolCallbacks: class TestToolCallbacks: