Skip to content

Commit 09de772

Browse files
committed
docs: integrate memory system into graph runtime documentation, detailing usage patterns, core components, and troubleshooting
1 parent 7284472 commit 09de772

File tree

2 files changed

+86
-240
lines changed

2 files changed

+86
-240
lines changed

docs/core-concepts/graph-system.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,90 @@ async def run_crypto_analysis(query: str) -> Dict[str, Any]:
406406

407407
---
408408

409+
## Memory System Integration
410+
411+
The graph runtime builds on the SpoonOS Memory System to persist context, metadata, and execution state across runs. Every compiled graph can attach a `Memory` store so routers, reducers, and agents reason over accumulated history without bespoke plumbing.
412+
413+
### Overview
414+
415+
- Persistent JSON-backed storage keyed by `session_id`
416+
- Chronological message history with metadata enrichment
417+
- Query helpers for search and time-based filtering
418+
- Automatic wiring inside `GraphAgent` and high-level APIs
419+
420+
### Core Components
421+
422+
```python
423+
from spoon_ai.graph.agent import Memory
424+
425+
# Use default storage path (~/.spoon_ai/memory)
426+
default_memory = Memory()
427+
428+
# Customize location and session isolation
429+
scoped_memory = Memory(storage_path="./custom_memory", session_id="my_session")
430+
```
431+
432+
- **Persistent storage** keeps transcripts and state checkpoints on disk
433+
- **Session management** separates contexts per agent or user
434+
- **Metadata fields** let reducers store structured state
435+
- **Search helpers** (`search_messages`, `get_recent_messages`) surface relevant history
436+
437+
### Basic Usage Patterns
438+
439+
```python
440+
message = {"role": "user", "content": "Hello, how can I help?"}
441+
scoped_memory.add_message(message)
442+
443+
all_messages = scoped_memory.get_messages()
444+
recent = scoped_memory.get_recent_messages(hours=24)
445+
metadata = scoped_memory.get_metadata("last_topic")
446+
```
447+
448+
Use metadata to thread routing hints and conversation topics, and prune history with retention policies or manual cleanup (`memory.clear()`).
449+
450+
### Graph Workflow Integration
451+
452+
`GraphAgent` wires memory automatically and exposes statistics for monitoring:
453+
454+
```python
455+
from spoon_ai.graph import GraphAgent, StateGraph
456+
457+
agent = GraphAgent(
458+
name="crypto_analyzer",
459+
graph=my_graph,
460+
memory_path="./agent_memory",
461+
session_id="crypto_session"
462+
)
463+
464+
result = await agent.run("Analyze BTC trends")
465+
stats = agent.get_memory_statistics()
466+
print(stats["total_messages"])
467+
```
468+
469+
Switch between sessions to isolate experiments (`agent.load_session("research_session")`) or inject custom `Memory` subclasses for domain-specific validation.
470+
471+
### Advanced Patterns
472+
473+
- Call `memory.get_statistics()` to monitor file size, last update time, and record counts
474+
- Implement custom subclasses to enforce schemas or add enrichment hooks
475+
- Use time-window retrieval for reducers that need the most recent facts only
476+
- Build automated cleanup jobs for oversized stores (>10MB) to keep execution tight
477+
478+
### Troubleshooting
479+
480+
```python
481+
import json
482+
try:
483+
with open(scoped_memory.session_file, "r") as fh:
484+
json.load(fh)
485+
except json.JSONDecodeError:
486+
scoped_memory.clear() # Reset corrupted memory files
487+
```
488+
489+
Conflicts typically trace back to duplicated session IDs—compose unique identifiers with timestamps or agent names to avoid contention.
490+
491+
---
492+
409493
## Best Practices
410494

411495
- **Use declarative templates**: `GraphTemplate` + `NodeSpec` for maintainable workflows
@@ -460,7 +544,8 @@ async def run_crypto_analysis(query: str) -> Dict[str, Any]:
460544
- **[MCP Protocol](../core-concepts/mcp-protocol.md)** - Explore dynamic tool discovery and execution
461545

462546
### 📖 **Additional Resources**
463-
547+
- **[State Management](../api-reference/graph/state-graph.md)** - Reducer configuration guide
548+
- **[Agents Detailed](./agents-detailed.md)** - Long-lived agent design patterns
464549
- **[Graph Builder API](../api-reference/graph/)** - Complete declarative API documentation
465550
- **[Performance Optimization](../troubleshooting/performance.md)** - Graph performance tuning guides
466551
- **[Troubleshooting](../troubleshooting/common-issues.md)** - Common issues and solutions

docs/core-concepts/memory-system.md

Lines changed: 0 additions & 239 deletions
This file was deleted.

0 commit comments

Comments
 (0)