2026-03-11 14:25:28 +00:00
# Agent Explainability: Provenance Recording
## Overview
Add provenance recording to the React agent loop so agent sessions can be traced and debugged using the same explainability infrastructure as GraphRAG.
**Design Decisions:**
- Write to `urn:graph:retrieval` (generic explainability graph)
2026-03-11 15:05:32 +00:00
- Linear dependency chain for now (analysis N → wasDerivedFrom → analysis N-1)
2026-03-11 14:25:28 +00:00
- Tools are opaque black boxes (record input/output only)
- DAG support deferred to future iteration
2026-03-11 15:05:32 +00:00
## Entity Types
Both GraphRAG and Agent use PROV-O as the base ontology with TrustGraph-specific subtypes:
### GraphRAG Types
| Entity | PROV-O Type | TG Type | Description |
|--------|-------------|---------|-------------|
| Question | `prov:Activity` | `tg:Question` | The user's query |
| Exploration | `prov:Entity` | `tg:Exploration` | Edges retrieved from knowledge graph |
| Focus | `prov:Entity` | `tg:Focus` | Selected edges with reasoning |
| Synthesis | `prov:Entity` | `tg:Synthesis` | Final answer |
### Agent Types
| Entity | PROV-O Type | TG Type | Description |
|--------|-------------|---------|-------------|
| Question | `prov:Activity` | `tg:Question` | The user's query (same as GraphRAG) |
| Analysis | `prov:Entity` | `tg:Analysis` | Each think/act/observe cycle |
| Conclusion | `prov:Entity` | `tg:Conclusion` | Final answer |
2026-03-11 14:25:28 +00:00
## Provenance Model
```
2026-03-11 15:05:32 +00:00
Question (urn:trustgraph:agent:{uuid})
2026-03-11 14:25:28 +00:00
│
│ tg:query = "User's question"
│ prov:startedAtTime = timestamp
2026-03-11 15:05:32 +00:00
│ rdf:type = prov:Activity, tg:Question
2026-03-11 14:25:28 +00:00
│
2026-03-11 15:05:32 +00:00
↓ prov:wasDerivedFrom
2026-03-11 14:25:28 +00:00
│
2026-03-11 15:05:32 +00:00
Analysis1 (urn:trustgraph:agent:{uuid}/i1)
2026-03-11 14:25:28 +00:00
│
│ tg:thought = "I need to query the knowledge base..."
│ tg:action = "knowledge-query"
│ tg:arguments = {"question": "..."}
│ tg:observation = "Result from tool..."
2026-03-11 15:05:32 +00:00
│ rdf:type = prov:Entity, tg:Analysis
2026-03-11 14:25:28 +00:00
│
↓ prov:wasDerivedFrom
│
2026-03-11 15:05:32 +00:00
Analysis2 (urn:trustgraph:agent:{uuid}/i2)
2026-03-11 14:25:28 +00:00
│ ...
↓ prov:wasDerivedFrom
│
2026-03-11 15:05:32 +00:00
Conclusion (urn:trustgraph:agent:{uuid}/final)
2026-03-11 14:25:28 +00:00
│
│ tg:answer = "The final response..."
2026-03-11 15:05:32 +00:00
│ rdf:type = prov:Entity, tg:Conclusion
2026-03-11 14:25:28 +00:00
```
## Changes Required
### 1. Schema Changes
**File:** `trustgraph-base/trustgraph/schema/services/agent.py`
Add `session_id` and `collection` fields to `AgentRequest` :
```python
@dataclass
class AgentRequest:
question: str = ""
state: str = ""
group: list[str] | None = None
history: list[AgentStep] = field(default_factory=list)
user: str = ""
collection: str = "default" # NEW: Collection for provenance traces
streaming: bool = False
session_id: str = "" # NEW: For provenance tracking across iterations
```
**File:** `trustgraph-base/trustgraph/messaging/translators/agent.py`
Update translator to handle `session_id` and `collection` in both `to_pulsar()` and `from_pulsar()` .
### 2. Add Explainability Producer to Agent Service
**File:** `trustgraph-flow/trustgraph/agent/react/service.py`
Register an "explainability" producer (same pattern as GraphRAG):
```python
from ... base import ProducerSpec
from ... schema import Triples
# In __init__:
self.register_specification(
ProducerSpec(
name = "explainability",
schema = Triples,
)
)
```
### 3. Provenance Triple Generation
2026-03-11 15:05:32 +00:00
**File:** `trustgraph-base/trustgraph/provenance/agent.py`
2026-03-11 14:25:28 +00:00
Create helper functions (similar to GraphRAG's `question_triples` , `exploration_triples` , etc.):
```python
def agent_session_triples(session_uri, query, timestamp):
2026-03-11 15:05:32 +00:00
"""Generate triples for agent Question."""
2026-03-11 14:25:28 +00:00
return [
2026-03-11 15:05:32 +00:00
Triple(s=session_uri, p=RDF_TYPE, o=PROV_ACTIVITY),
Triple(s=session_uri, p=RDF_TYPE, o=TG_QUESTION),
2026-03-11 14:25:28 +00:00
Triple(s=session_uri, p=TG_QUERY, o=query),
Triple(s=session_uri, p=PROV_STARTED_AT_TIME, o=timestamp),
]
def agent_iteration_triples(iteration_uri, parent_uri, thought, action, arguments, observation):
2026-03-11 15:05:32 +00:00
"""Generate triples for one Analysis step."""
2026-03-11 14:25:28 +00:00
return [
2026-03-11 15:05:32 +00:00
Triple(s=iteration_uri, p=RDF_TYPE, o=PROV_ENTITY),
Triple(s=iteration_uri, p=RDF_TYPE, o=TG_ANALYSIS),
2026-03-11 14:25:28 +00:00
Triple(s=iteration_uri, p=TG_THOUGHT, o=thought),
Triple(s=iteration_uri, p=TG_ACTION, o=action),
Triple(s=iteration_uri, p=TG_ARGUMENTS, o=json.dumps(arguments)),
Triple(s=iteration_uri, p=TG_OBSERVATION, o=observation),
Triple(s=iteration_uri, p=PROV_WAS_DERIVED_FROM, o=parent_uri),
]
def agent_final_triples(final_uri, parent_uri, answer):
2026-03-11 15:05:32 +00:00
"""Generate triples for Conclusion."""
2026-03-11 14:25:28 +00:00
return [
2026-03-11 15:05:32 +00:00
Triple(s=final_uri, p=RDF_TYPE, o=PROV_ENTITY),
Triple(s=final_uri, p=RDF_TYPE, o=TG_CONCLUSION),
2026-03-11 14:25:28 +00:00
Triple(s=final_uri, p=TG_ANSWER, o=answer),
Triple(s=final_uri, p=PROV_WAS_DERIVED_FROM, o=parent_uri),
]
```
2026-03-11 15:05:32 +00:00
### 4. Type Definitions
2026-03-11 14:25:28 +00:00
**File:** `trustgraph-base/trustgraph/provenance/namespaces.py`
2026-03-11 15:05:32 +00:00
Add explainability entity types and agent predicates:
2026-03-11 14:25:28 +00:00
```python
2026-03-11 15:05:32 +00:00
# Explainability entity types (used by both GraphRAG and Agent)
TG_QUESTION = TG + "Question"
TG_EXPLORATION = TG + "Exploration"
TG_FOCUS = TG + "Focus"
TG_SYNTHESIS = TG + "Synthesis"
TG_ANALYSIS = TG + "Analysis"
TG_CONCLUSION = TG + "Conclusion"
# Agent predicates
2026-03-11 14:25:28 +00:00
TG_THOUGHT = TG + "thought"
TG_ACTION = TG + "action"
TG_ARGUMENTS = TG + "arguments"
TG_OBSERVATION = TG + "observation"
2026-03-11 15:05:32 +00:00
TG_ANSWER = TG + "answer"
2026-03-11 14:25:28 +00:00
```
2026-03-11 15:05:32 +00:00
## Files Modified
2026-03-11 14:25:28 +00:00
| File | Change |
|------|--------|
| `trustgraph-base/trustgraph/schema/services/agent.py` | Add session_id and collection to AgentRequest |
| `trustgraph-base/trustgraph/messaging/translators/agent.py` | Update translator for new fields |
2026-03-11 15:05:32 +00:00
| `trustgraph-base/trustgraph/provenance/namespaces.py` | Add entity types and agent predicates |
| `trustgraph-base/trustgraph/provenance/triples.py` | Add TG types to GraphRAG triple builders |
| `trustgraph-base/trustgraph/provenance/__init__.py` | Export new types and predicates |
2026-03-11 14:25:28 +00:00
| `trustgraph-flow/trustgraph/agent/react/service.py` | Add explainability producer + recording logic |
| `trustgraph-cli/trustgraph/cli/show_explain_trace.py` | Handle agent trace types |
| `trustgraph-cli/trustgraph/cli/list_explain_traces.py` | List agent sessions alongside GraphRAG |
2026-03-11 15:05:32 +00:00
## Files Created
2026-03-11 14:25:28 +00:00
| File | Purpose |
|------|---------|
2026-03-11 15:05:32 +00:00
| `trustgraph-base/trustgraph/provenance/agent.py` | Agent-specific triple generators |
## CLI Updates
2026-03-11 14:25:28 +00:00
2026-03-11 15:05:32 +00:00
**Detection:** Both GraphRAG and Agent Questions have `tg:Question` type. Distinguished by:
1. URI pattern: `urn:trustgraph:agent:` vs `urn:trustgraph:question:`
2. Derived entities: `tg:Analysis` (agent) vs `tg:Exploration` (GraphRAG)
2026-03-11 14:25:28 +00:00
**`list_explain_traces.py` :**
2026-03-11 15:05:32 +00:00
- Shows Type column (Agent vs GraphRAG)
2026-03-11 14:25:28 +00:00
**`show_explain_trace.py` :**
2026-03-11 15:05:32 +00:00
- Auto-detects trace type
- Agent rendering shows: Question → Analysis step(s) → Conclusion
2026-03-11 14:25:28 +00:00
## Backwards Compatibility
- `session_id` defaults to `""` - old requests work, just won't have provenance
- `collection` defaults to `"default"` - reasonable fallback
- CLI gracefully handles both trace types
## Verification
```bash
# Run an agent query
tg-invoke-agent -q "What is the capital of France?"
2026-03-11 15:05:32 +00:00
# List traces (should show agent sessions with Type column)
2026-03-11 14:25:28 +00:00
tg-list-explain-traces -U trustgraph -C default
2026-03-11 15:05:32 +00:00
# Show agent trace
2026-03-11 14:25:28 +00:00
tg-show-explain-trace "urn:trustgraph:agent:xxx"
```
## Future Work (Not This PR)
2026-03-11 15:05:32 +00:00
- DAG dependencies (when analysis N uses results from multiple prior analyses)
2026-03-11 14:25:28 +00:00
- Tool-specific provenance linking (KnowledgeQuery → its GraphRAG trace)
2026-03-11 15:05:32 +00:00
- Document RAG explainability
2026-03-11 14:25:28 +00:00
- Streaming provenance emission (emit as we go, not batch at end)