trustgraph/docs/tech-specs
cybermaggedon 312174eb88
Adding explainability to the ReACT agent (#689)
* Added tech spec

* Add provenance recording to React agent loop

Enables agent sessions to be traced and debugged using the same
explainability infrastructure as GraphRAG. Agent traces record:
- Session start with query and timestamp
- Each iteration's thought, action, arguments, and observation
- Final answer with derivation chain

Changes:
- Add session_id and collection fields to AgentRequest schema
- Add agent predicates (TG_THOUGHT, TG_ACTION, etc.) to namespaces
- Create agent provenance triple generators in provenance/agent.py
- Register explainability producer in agent service
- Emit provenance triples during agent execution
- Update CLI tools to detect and render agent traces alongside GraphRAG

* Updated explainability taxonomy:

GraphRAG: tg:Question → tg:Exploration → tg:Focus → tg:Synthesis

Agent: tg:Question → tg:Analysis(s) → tg:Conclusion

All entities also have their PROV-O type (prov:Activity or prov:Entity).

Updated commit message:

Add provenance recording to React agent loop

Enables agent sessions to be traced and debugged using the same
explainability infrastructure as GraphRAG.

Entity types follow human reasoning patterns:
- tg:Question - the user's query (shared with GraphRAG)
- tg:Analysis - each think/act/observe cycle
- tg:Conclusion - the final answer

Also adds explicit TG types to GraphRAG entities:
- tg:Question, tg:Exploration, tg:Focus, tg:Synthesis

All types retain their PROV-O base types (prov:Activity, prov:Entity).

Changes:
- Add session_id and collection fields to AgentRequest schema
- Add explainability entity types to namespaces.py
- Create agent provenance triple generators
- Register explainability producer in agent service
- Emit provenance triples during agent execution
- Update CLI tools to detect and render both trace types

* Document RAG explainability is now complete. Here's a summary of the
changes made:

Schema Changes:
- trustgraph-base/trustgraph/schema/services/retrieval.py: Added
  explain_id and explain_graph fields to DocumentRagResponse
- trustgraph-base/trustgraph/messaging/translators/retrieval.py:
  Updated translator to handle explainability fields

Provenance Changes:
- trustgraph-base/trustgraph/provenance/namespaces.py: Added
  TG_CHUNK_COUNT and TG_SELECTED_CHUNK predicates
- trustgraph-base/trustgraph/provenance/uris.py: Added
  docrag_question_uri, docrag_exploration_uri, docrag_synthesis_uri
  generators
- trustgraph-base/trustgraph/provenance/triples.py: Added
  docrag_question_triples, docrag_exploration_triples,
  docrag_synthesis_triples builders
- trustgraph-base/trustgraph/provenance/__init__.py: Exported all
  new Document RAG functions and predicates

Service Changes:
- trustgraph-flow/trustgraph/retrieval/document_rag/document_rag.py:
  Added explainability callback support and triple emission at each
  phase (Question → Exploration → Synthesis)
- trustgraph-flow/trustgraph/retrieval/document_rag/rag.py:
  Registered explainability producer and wired up the callback

Documentation:
- docs/tech-specs/agent-explainability.md: Added Document RAG entity
  types and provenance model documentation

Document RAG Provenance Model:
Question (urn:trustgraph:docrag:{uuid})
    │
    │  tg:query, prov:startedAtTime
    │  rdf:type = prov:Activity, tg:Question
    │
    ↓ prov:wasGeneratedBy
    │
Exploration (urn:trustgraph:docrag:{uuid}/exploration)
    │
    │  tg:chunkCount, tg:selectedChunk (multiple)
    │  rdf:type = prov:Entity, tg:Exploration
    │
    ↓ prov:wasDerivedFrom
    │
Synthesis (urn:trustgraph:docrag:{uuid}/synthesis)
    │
    │  tg:content = "The answer..."
    │  rdf:type = prov:Entity, tg:Synthesis

* Specific subtype that makes the retrieval mechanism immediately
obvious:

System: GraphRAG
TG Types on Question: tg:Question, tg:GraphRagQuestion
URI Pattern: urn:trustgraph:question:{uuid}
────────────────────────────────────────
System: Document RAG
TG Types on Question: tg:Question, tg:DocRagQuestion
URI Pattern: urn:trustgraph:docrag:{uuid}
────────────────────────────────────────
System: Agent
TG Types on Question: tg:Question, tg:AgentQuestion
URI Pattern: urn:trustgraph:agent:{uuid}
Files modified:
- trustgraph-base/trustgraph/provenance/namespaces.py - Added
TG_GRAPH_RAG_QUESTION, TG_DOC_RAG_QUESTION, TG_AGENT_QUESTION
- trustgraph-base/trustgraph/provenance/triples.py - Added subtype to
question_triples and docrag_question_triples
- trustgraph-base/trustgraph/provenance/agent.py - Added subtype to
agent_session_triples
- trustgraph-base/trustgraph/provenance/__init__.py - Exported new types
- docs/tech-specs/agent-explainability.md - Documented the subtypes

This allows:
- Query all questions: ?q rdf:type tg:Question
- Query only GraphRAG: ?q rdf:type tg:GraphRagQuestion
- Query only Document RAG: ?q rdf:type tg:DocRagQuestion
- Query only Agent: ?q rdf:type tg:AgentQuestion

* Fixed tests
2026-03-11 15:28:15 +00:00
..
__TEMPLATE.md Add template (#463) 2025-08-21 14:50:57 +01:00
agent-explainability.md Adding explainability to the ReACT agent (#689) 2026-03-11 15:28:15 +00:00
architecture-principles.md More config cli (#466) 2025-08-22 13:36:10 +01:00
cassandra-consolidation.md Feature/consolidate cassandra config (#483) 2025-09-03 23:41:22 +01:00
cassandra-performance-refactor.md Collection delete pt. 3 (#542) 2025-09-30 16:02:33 +01:00
collection-management.md Address legacy issues in storage management (#595) 2026-01-05 13:45:14 +00:00
document-embeddings-chunk-id.md Document chunks not stored in vector store (#665) 2026-03-07 23:10:45 +00:00
embeddings-batch-processing.md Batch embeddings (#668) 2026-03-08 18:36:54 +00:00
entity-centric-graph.md Fix Cassandra schema and graph filter semantics (#680) 2026-03-10 12:52:51 +00:00
explainability-cli.md Add explainability CLI tools (#688) 2026-03-11 13:44:29 +00:00
extraction-flows.md Remove redundant metadata (#685) 2026-03-11 10:51:39 +00:00
extraction-time-provenance.md Extract-time provenance (#661) 2026-03-05 18:36:10 +00:00
flow-class-definition.md Fix config inconsistency (#609) 2026-01-14 12:31:40 +00:00
flow-configurable-parameters.md Fix config inconsistency (#609) 2026-01-14 12:31:40 +00:00
graph-contexts.md Changed schema for Value -> Term, majorly breaking change (#622) 2026-01-27 13:48:08 +00:00
graphql-query.md Feature/graphql table query (#486) 2025-09-03 23:39:11 +01:00
graphrag-performance-optimization.md Graph rag optimisations (#527) 2025-09-23 21:05:51 +01:00
import-export-graceful-shutdown.md Fix import export graceful shutdown (#476) 2025-08-28 13:39:28 +01:00
jsonl-prompt-output.md Feature/prompts jsonl (#619) 2026-01-26 17:38:00 +00:00
large-document-loading.md Incremental / large document loading (#659) 2026-03-04 16:57:58 +00:00
logging-strategy.md Loki logging (#586) 2025-12-09 23:24:41 +00:00
mcp-tool-arguments.md More config cli (#466) 2025-08-22 13:36:10 +01:00
mcp-tool-bearer-token.md MCP auth for the simple case (#557) 2025-11-11 12:28:53 +00:00
minio-to-s3-migration.md Change MinIO integration options in librarian to be more generic - to support a Garage integration (#594) 2025-12-27 18:01:51 +00:00
more-config-cli.md More config cli (#466) 2025-08-22 13:36:10 +01:00
multi-tenant-support.md Address legacy issues in storage management (#595) 2026-01-05 13:45:14 +00:00
neo4j-user-collection-isolation.md Feature/neo4j user collection isolation (#509) 2025-09-10 22:11:21 +01:00
ontology-extract-phase-2.md Feature/improve ontology extract (#576) 2025-12-03 13:36:10 +00:00
ontology.md OntoRAG: Ontology-Based Knowledge Extraction and Query Technical Specification (#523) 2025-11-12 20:38:08 +00:00
ontorag.md Update tech spec (#558) 2025-11-13 16:29:20 +00:00
openapi-spec.md REST API OpenAPI spec (#612) 2026-01-15 11:04:37 +00:00
pubsub.md Messaging fabric plugins (#592) 2025-12-17 21:40:43 +00:00
python-api-refactor.md Python API implements streaming interfaces (#577) 2025-12-04 17:38:57 +00:00
query-time-explainability.md Update tech spec (#678) 2026-03-10 10:07:37 +00:00
rag-streaming-support.md Streaming rag responses (#568) 2025-11-26 19:47:39 +00:00
schema-refactoring-proposal.md More config cli (#466) 2025-08-22 13:36:10 +01:00
streaming-llm-responses.md Feature/streaming llm phase 1 (#566) 2025-11-26 09:59:10 +00:00
structured-data-2.md Row embeddings APIs exposed (#646) 2026-02-23 21:52:56 +00:00
structured-data-descriptor.md Structured data loader cli (#498) 2025-09-05 15:38:18 +01:00
structured-data-schemas.md More config cli (#466) 2025-08-22 13:36:10 +01:00
structured-data.md Remove graphql collection param (#489) 2025-09-04 10:04:09 +01:00
structured-diag-service.md Structure data diagnosis service (#518) 2025-09-16 21:43:23 +01:00
tool-group.md Feature/tool group (#484) 2025-09-03 23:39:49 +01:00
tool-services.md Tool services - dynamically pluggable tool implementations for agent frameworks (#658) 2026-03-04 14:51:32 +00:00
vector-store-lifecycle.md Fix hard coded vector size (#555) 2025-11-10 16:56:51 +00:00