Skip to content

feat: auto-instrument LangGraph nodes and routing decisions - #35

Open
DudeFr0mMars wants to merge 1 commit into
theagentplane:mainfrom
DudeFr0mMars:fix/issue-22
Open

feat: auto-instrument LangGraph nodes and routing decisions#35
DudeFr0mMars wants to merge 1 commit into
theagentplane:mainfrom
DudeFr0mMars:fix/issue-22

Conversation

@DudeFr0mMars

@DudeFr0mMars DudeFr0mMars commented Jul 29, 2026

Copy link
Copy Markdown

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

  • Tests added or updated, and pytest -v passes locally
  • ruff check . is clean
  • Docs / CHANGELOG updated if user-facing
  • If the Envelope schema changed: bumped at least a minor version and included a fixture migration path (see RELEASING.md)
  • Commits are signed off for the DCO (git commit -s)

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LangGraph: auto-instrument a compiled graph and capture routing decisions

1 participant