Overview
The Graph Expander is a long-running background worker that autonomously discovers and adds related historical events to the knowledge graph. It uses an LLM to suggest contextually relevant events from “frontier nodes” (sparsely connected events).The Expander must be explicitly enabled via the
EXPANSION_ENABLED feature flag and requires a valid OPENROUTER_API_KEY.Configuration
Parameters
Environment Variables
Worker Lifecycle
The Expander runs continuously, executing expansion cycles at regular intervals:Startup
Inapp/main.py, the Expander is initialized during application startup:
Shutdown
Expansion Algorithm
Each expansion cycle follows this workflow:1. Find Frontier Nodes
2. Generate Related Events
The Expander uses an LLM to suggest 3-5 related historical events:3. Add Events to Graph
Each suggested event is added as a new node:_add_event method:
- Constructs a URL path for the event
- Checks if the event already exists
- Creates the node with metadata
- Adds an edge from the source node
Edge Types
The Expander creates edges with semantic relationships:Example Response Parsing
The Expander handles markdown-wrapped JSON responses:Node Metadata
Expanded nodes are tagged with provenance:Error Handling
The Expander gracefully handles API failures:- LLM timeout: 120-second timeout on OpenRouter requests
- Invalid JSON: Malformed LLM responses are logged and skipped
- Duplicate events: Existing events are silently ignored
- Edge creation errors: Caught with
ValueErrorand passed
Monitoring
Watch expansion activity via logs:/health endpoint: