Repository navigation
tool parallel safety
LangGraph's
ToolNodestarts every tool call in one model batch at once. Sherry replaces it with a batch-aware node and two resource-lock layers, so calls that share a file are ordered while independent calls stay parallel.
A model can return several tool calls in one response. ToolNode gathers them with asyncio.gather, which is correct for independent reads and dangerous for two writes, a read racing a write, or a shell command racing another agent's shell command. An all-or-nothing rule (serialize the whole batch if any tool writes) is safe but throws away parallelism for unrelated files. The design therefore needs a resource key, not just a boolean.
agent/tools/parallel_safety.py owns the decision:
-
READ_ONLY_TOOLSlists tools with no side effects. - A tool's metadata may add
concurrent_safe(explicitly safe) orside_effect_scope(none,workspace,system,session,user_interaction, ...). - Unknown tools and missing metadata are unsafe.
-
destructiveoverrides every allow declaration. -
idempotentis deliberately not used:memoryis idempotent but writes session state, whilesessions_spawnis non-idempotent but can run concurrently.
For every ToolNode batch, plan_batch produces groups:
-
read_file,write_file,patch_fileare file operations keyed by their canonicalfile_path. - A path group with a write runs in the model's
tool_callsorder; a group of reads may run concurrently. - Different paths are independent.
- Any non-file unsafe call (for example
terminal) makes the whole batch a barrier; its target resource cannot be inferred from arguments. - A file path that cannot be resolved is also a barrier.
The ordering rule is the model's call order: read_file(A) before write_file(A) reads the old content and licenses the write; the reverse writes first and the later read sees the new content. Two writes to the same path run in order, and the last one wins.
The batch planner orders conflicts inside one batch. The file layer protects concurrent agents and processes:
-
agent/tools/pub_base/path_lock.pyprovidespath_read_lock(shared) andpath_write_lock(exclusive, writer preferred). -
agent/tools/pub_base/file_lock.pyadds a sharedflock(LOCK_SH) and keeps the exclusive one (LOCK_EX). -
read_filetakesfile_read_lock;write_fileandpatch_filekeepfile_write_lock. - The fixed lock order is in-process gate, then cross-process
flock.
Two readers of one path overlap; a reader and a writer of one path never do; different paths never contend. Across batches/agents the order is lock-acquisition order, not the model's call order.
agent/middlewares/tool_resource_lock.py covers the two resources a path cannot describe:
-
side_effect_scope: "system"(terminal,python_repl,execute_code,ptc) takes a process-wide lock;TOOL_PARALLEL["system_lock"]is OFF by default because it serializes all shell work. -
side_effect_scope: "user_interaction"(question,exit_plan_mode) takes a per-session lock;interaction_lockis ON by default so one HITL channel handles one prompt at a time. -
workspace,sessionandnetworkare not re-locked: file locks, the memory store, SQLite stores and the browser manager own those resources.
create_agent has no tool_node parameter. agent/core.py patches langchain.agents.factory.ToolNode once at import, gated by TOOL_PARALLEL["enabled"], so main, subagent and auxiliary graphs all use SafeToolNode. The subagent middleware chain also carries ToolResourceLockMiddleware. TOOL_PARALLEL["enforce"] = False keeps native concurrency and only logs the plan; True applies it. A compatibility test pins the private ToolNode methods the node depends on, so a LangGraph upgrade fails loudly.
-
terminal,python_repl,execute_codeandptcremain whole-batch barriers: their file targets cannot be reliably parsed from command strings. - Cross-agent ordering is not guaranteed; only mutual exclusion is.
system_lockis the explicit exception when enabled. - The batch planner uses the canonical path; a path that cannot be resolved falls back to a barrier rather than guessing.
- The file locks are local-filesystem locks (
flock); NFS semantics are out of scope.
-
agent/safe_tool_node.py:SafeToolNode,_afunc/_func, factory installation. -
agent/tools/parallel_safety.py: classification,file_resource_key,plan_batch. -
agent/tools/pub_base/path_lock.py: in-process read/write gate. -
agent/tools/pub_base/file_lock.py:file_read_lock/file_write_lock,flock_path(shared=...). -
agent/middlewares/tool_resource_lock.py: process-wide and per-session resource locks. -
agent/tools/file_tools/read_file.py: the read-lock call site. -
tests/agent/test_safe_tool_node.py,tests/agent/tools/test_parallel_safety.py,tests/agent/tools/file_tools/test_file_rw_lock.py,tests/agent/middlewares/test_tool_resource_lock.py: the behavior and invariants above.
Generated from the repository's wiki/ tree. Source