feat: add SharedState for sub-agent communication#35
Merged
Conversation
Add a shared key-value store that sub-agents can read/write via an injected tool, so large artifacts (CI logs, code, traces) are stored once and referenced by name instead of re-pasted into every prompt. - SharedState: Arc<RwLock<HashMap<String, String>>> with 10MB default cap - SharedStateTool: AgentTool impl with get/set/list/remove actions - SubAgentTool: opt-in .with_shared_state(state) builder injects tool and state summary into sub-agent system prompt - Core agent loop untouched (no changes to types.rs, agent_loop.rs, agent.rs) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add CLAUDE.md section covering SharedState architecture and usage. Add examples/shared_state.rs demonstrating parallel CI log analysis where 3 sub-agents share a single artifact via shared state. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add examples/code_review.rs — interactive code review agent that users run on their own files. 3 parallel sub-agents (bugs, quality, docs) share the source code via SharedState. Update docs site: - concepts/sub-agents.md: new Shared State section with usage examples - reference/tools.md: document SharedStateTool Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Wire up on_update callback so sub-agent text deltas stream to stderr in real-time with [bugs]/[quality]/[docs] prefixes. Users see all 3 reviewers working in parallel instead of waiting in silence. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Buffer per-agent text deltas and flush on newlines so parallel sub-agent output doesn't interleave mid-line. Tool call events flush the buffer and print on their own line. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `turn_delay` to `AgentLoopConfig` and `SubAgentTool` for throttling API calls between turns — needed for OAuth tokens with strict rate limits. Add `examples/rlm.rs` demonstrating true recursive language model: lead_analyst discovers files autonomously and delegates to file_analyst sub-agents, communicating through SharedState. Fix SubAgentTool silently swallowing provider errors by checking StopReason::Error before extracting text. Use Debug format for transport errors to preserve the full error chain. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ok RLM example Refactor SharedState to use a `SharedStateBackend` trait with two implementations: `MemoryBackend` (default, same behavior) and `FileBackend` (each key stored as a file for persistence and debugging). Custom backends (Redis, SQLite, etc.) can implement the trait. Add `with_model_config()` to SubAgentTool so sub-agents can use any provider (OpenAI, xAI, Groq, etc.) — previously hardcoded to None, silently breaking non-Anthropic sub-agents. Switch RLM example to Grok (grok-4-1-fast-reasoning) via xAI API, removing Anthropic OAuth rate-limit workarounds. Fix shared state summary format from ambiguous "9B" to "9 bytes". Update docs: sub-agents configuration table, multi-provider section, SubAgentTool API reference, nesting supported note, examples list. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Show commented-out FileBackend alternative alongside the default in-memory SharedState so users can easily switch. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
Code reviewFound 5 issues:
Lines 84 to 86 in 3312908
Lines 220 to 233 in 3312908
yoagent/docs/reference/configuration.md Lines 28 to 31 in 3312908
Lines 9 to 11 in 3312908
Lines 341 to 368 in 3312908 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
- Update CLAUDE.md to reflect Arc<dyn SharedStateBackend> instead of HashMap - Switch FileBackend to percent-encoding for reversible key mapping - Restore compaction_strategy field in configuration.md - Fix sub_agent.rs doc: nesting is supported, not prevented - Log I/O errors in SharedState public API instead of silent swallowing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add SharedState API section to api.md (constructors, methods, backends, trait) - Add FileBackend and custom backend docs to sub-agents.md - Add turn_delay to agent-loop.md struct and field table - Fix duplicate compaction_strategy in configuration.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Apr 27, 2026
Merged
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.
Summary
SharedState— a shared key-value store (Arc<RwLock<HashMap<String, String>>>) that sub-agents can read/write via an injectedshared_statetool.with_shared_state(state)onSubAgentTool— existing behavior unchangedtypes.rs,agent_loop.rs,agent.rs)New files
src/shared_state.rs—SharedStatetype with 10MB default cap, get/set/remove/keys/summarysrc/tools/shared_state_tool.rs—SharedStateToolimplementingAgentTool(get/set/list/remove)tests/shared_state_test.rs— 5 integration tests with MockProviderModified files
src/sub_agent.rs—shared_statefield + builder, injects tool + state summary into promptsrc/tools/mod.rs— register modulesrc/lib.rs— register module + re-exportUsage
Test plan
cargo fmt -- --checkcargo clippy --all-targets(with-Dwarnings)cargo test— 187 tests pass, 0 failures🤖 Generated with Claude Code