feat: auto-instrument LangGraph nodes and routing decisions - #35
Open
DudeFr0mMars wants to merge 1 commit into
Open
feat: auto-instrument LangGraph nodes and routing decisions#35DudeFr0mMars wants to merge 1 commit into
DudeFr0mMars wants to merge 1 commit into
Conversation
chronicle.instrument(graph) wraps every node and every add_conditional_edges routing function on a StateGraph in one call, before or after .compile() — a CompiledStateGraph keeps a live reference to its builder, and node callables are mutated in place, so instrumenting late still reroutes already-compiled execution. Routing functions are recorded as kind="router" boundaries, so which branch was taken is captured and replays deterministically, not just each node's input/output. A sync-only node's auto-generated async executor shim closes over the original, unwrapped function, so it's rebuilt around the instrumented one instead — otherwise ainvoke/astream would silently skip recording. That shim also now copies the caller's contextvars into the executor thread (matching langgraph's own run_in_executor), since Chronicle scopes its session via ContextVar and threads don't inherit it. Closes theagentplane#22 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: DudeFromMars <varunmathur1990@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds chronicle.instrument(graph): one call auto-instruments every node and every add_conditional_edges routing function on a LangGraph StateGraph, before or after .compile().
Works on the builder's public attributes (nodes, branches), not langgraph's private compiled Pregel internals — more stable across langgraph versions.
Also works after .compile(): a CompiledStateGraph keeps a live reference to its builder (compiled.builder), and node callables are mutated in place, so late instrumentation still reroutes already-compiled execution.
Routing functions become kind="router" boundaries, so which branch was taken is recorded and replays deterministically from a fixture — not just each node's input/output. session.py gained a small kind="router" branch in the record/replay round-trip so a router's plain string/list return value survives stub replay faithfully (it isn't a dict, so it can't just be handed back as raw_response).
Handles async correctly, which needed two fixes verified by test, not assumed: (1) langgraph gives a sync-only node an auto-generated async executor shim that closes over the original function — rewiring only the sync side would silently skip recording under ainvoke/astream, so the shim is rebuilt around the instrumented function instead; (2) that shim runs in a thread pool, and ContextVars (how Chronicle scopes its session) don't cross threads automatically, so the caller's context is copied into the executor thread — the same fix langgraph's own private run_in_executor helper applies, for the same reason.
Idempotent: calling instrument() twice on the same graph wraps each node/router once.
Also adds examples/langgraph_demo/routing_demo.py (record → replay a 3-node graph with a conditional edge; replay reproduces the same completion with no node/router code executing).
Related issue
Closes #22
Checklist
Checklist
pytest -vpasses locallyruff check .is cleangit commit -s)