Skip to content

[AAASM-1181] ✨ (langgraph): Add delegation_reason, ToolNode detection, and config parent-id read#38

Merged
Chisanan232 merged 4 commits into
masterfrom
v0.0.1/AAASM-1181/feat/langgraph_delegation_reason
May 9, 2026
Merged

[AAASM-1181] ✨ (langgraph): Add delegation_reason, ToolNode detection, and config parent-id read#38
Chisanan232 merged 4 commits into
masterfrom
v0.0.1/AAASM-1181/feat/langgraph_delegation_reason

Conversation

@Chisanan232
Copy link
Copy Markdown
Contributor

Summary

  • Adds delegation_reason: str | None field to SpawnContext to record the context in which a child agent was spawned (e.g., langgraph_node:my_node, langgraph_tool:retriever)
  • Adds _extract_agent_id_from_runnable_config to read aaasm_agent_id from LangGraph's RunnableConfig.configurable
  • Adds _is_tool_node duck-type detector for LangGraph ToolNode objects
  • Adds _wrap_tool_node_subgraphs to intercept compiled-subgraph tools nested inside a ToolNode, setting spawned_by_tool + delegation_reason=langgraph_tool:<tool_name>
  • Updates _wrap_node_map to check _is_tool_node before callable() (ToolNode is also callable) and sets delegation_reason=langgraph_node:<node_name> for plain compiled-subgraph nodes

Motivation

Closes AAASM-1181. This is a follow-up to PR #37 which delivered the ContextVar backbone. The delegation_reason field enables downstream audit and observability to distinguish how a child agent was spawned — whether from a plain subgraph node, a tool call via ToolNode, or another mechanism.

How to verify

uv sync
.venv/bin/python -m pytest test/unit/core/test_spawn_context.py test/unit/adapters/langchain/test_langgraph_spawn_patch.py -v

All 265 tests pass, ruff clean, mypy clean on changed files.

Closes #AAASM-1181

Enriches the lineage context with the reason a child agent was spawned
(e.g., langgraph_node:my_node, langgraph_tool:retriever) for finer-grained
audit and observability.
…agent-id read

- _extract_agent_id_from_runnable_config: reads aaasm_agent_id from RunnableConfig.configurable
- _is_tool_node: duck-type detector (tools_by_name dict, no nodes attr)
- _wrap_tool_node_subgraphs: intercepts compiled-subgraph tools inside ToolNode,
  setting spawned_by_tool + delegation_reason=langgraph_tool:<name>
- _make_subgraph_spawn_wrapper: accepts spawned_by_tool + delegation_reason params
- _wrap_node_map: checks _is_tool_node before callable() (ToolNode is also callable);
  sets delegation_reason=langgraph_node:<name> for plain compiled-subgraph nodes
…tests

Covers delegation_reason default None, explicit value storage, and the
init_assembly auto-population of parent_agent_id/depth/spawned_by_tool
from the active _SPAWN_CTX ContextVar.
…nd config reader

New test classes: TestExtractAgentIdFromRunnableConfig, TestIsToolNode,
TestWrapToolNodeSubgraphs, TestWrapNodeMapDelegationReason.
Updated TestMakeSubgraphSpawnWrapper to assert delegation_reason propagation
and the plain-subgraph node spawned_by_tool=None behavior.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

❌ Patch coverage is 93.61702% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
agent_assembly/adapters/langgraph/patch.py 93.33% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 9, 2026

@Chisanan232 Chisanan232 merged commit f24e9bb into master May 9, 2026
24 checks passed
@Chisanan232 Chisanan232 deleted the v0.0.1/AAASM-1181/feat/langgraph_delegation_reason branch May 9, 2026 03:35
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.

1 participant