Files
stack/docs/scratchpads/71-graph-data-api.md
Jason Woltje 12abdfe81d feat(#93): implement agent spawn via federation
Implements FED-010: Agent Spawn via Federation feature that enables
spawning and managing Claude agents on remote federated Mosaic Stack
instances via COMMAND message type.

Features:
- Federation agent command types (spawn, status, kill)
- FederationAgentService for handling agent operations
- Integration with orchestrator's agent spawner/lifecycle services
- API endpoints for spawning, querying status, and killing agents
- Full command routing through federation COMMAND infrastructure
- Comprehensive test coverage (12/12 tests passing)

Architecture:
- Hub → Spoke: Spawn agents on remote instances
- Command flow: FederationController → FederationAgentService →
  CommandService → Remote Orchestrator
- Response handling: Remote orchestrator returns agent status/results
- Security: Connection validation, signature verification

Files created:
- apps/api/src/federation/types/federation-agent.types.ts
- apps/api/src/federation/federation-agent.service.ts
- apps/api/src/federation/federation-agent.service.spec.ts

Files modified:
- apps/api/src/federation/command.service.ts (agent command routing)
- apps/api/src/federation/federation.controller.ts (agent endpoints)
- apps/api/src/federation/federation.module.ts (service registration)
- apps/orchestrator/src/api/agents/agents.controller.ts (status endpoint)
- apps/orchestrator/src/api/agents/agents.module.ts (lifecycle integration)

Testing:
- 12/12 tests passing for FederationAgentService
- All command service tests passing
- TypeScript compilation successful
- Linting passed

Refs #93

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-03 14:37:06 -06:00

163 lines
5.5 KiB
Markdown

# Issue #71: [KNOW-019] Graph Data API
## Objective
Create API endpoints to retrieve knowledge graph data for visualization, including nodes (entries) and edges (relationships) with filtering and statistics capabilities.
## Approach
1. Review existing knowledge schema and relationships table
2. Define DTOs for graph data structures (nodes, edges, filters)
3. Write tests for graph endpoints (TDD approach)
4. Implement GraphService for data aggregation and processing
5. Create graph controller with three endpoints
6. Implement orphan detection, filtering, and node limiting
7. Test with sample data
8. Run quality checks and commit
## Progress
- [x] Review schema and existing code
- [x] Define DTOs for graph structures
- [x] Write tests for graph endpoints (RED)
- [x] Implement GraphService (GREEN)
- [x] Create graph controller endpoints (GREEN)
- [x] Implement orphan detection
- [x] Add filtering capabilities
- [x] Add node count limiting
- [x] Run code review
- [x] Run QA checks
- [x] Commit changes
- [x] Close issue
## Completion Summary
Issue #71 has been successfully completed with all acceptance criteria met:
1. **GET /api/knowledge/graph** - Full knowledge graph endpoint implemented
- Returns all entries and links with optional filtering
- Supports filtering by tags, status
- Includes node count limit option
- Orphan detection included
2. **GET /api/knowledge/graph/:slug** - Entry-centered subgraph endpoint implemented
- Returns subgraph centered on specific entry
- Supports depth parameter (1-5, default 1)
- Uses BFS traversal for connected nodes
3. **GET /api/knowledge/graph/stats** - Graph statistics endpoint implemented
- Returns total entries and links
- Detects and counts orphan entries
- Calculates average links per entry
- Shows top 10 most connected entries
- Provides tag distribution
All tests passing (21 tests), code quality gates passed, and changes committed to develop branch.
## API Endpoints
1. `GET /api/knowledge/graph` - Return full knowledge graph with filters
2. `GET /api/knowledge/graph/:slug` - Return subgraph centered on entry
3. `GET /api/knowledge/graph/stats` - Return graph statistics
## Graph Data Format
```typescript
{
nodes: [
{
id: string,
slug: string,
title: string,
type: string,
status: string,
tags: string[],
isOrphan: boolean
}
],
edges: [
{
source: string, // node id
target: string, // node id
type: string // relationship type
}
]
}
```
## Testing
- Unit tests for GraphService methods
- Integration tests for graph endpoints
- Test filtering, orphan detection, and node limiting
- Verify graph statistics calculation
## Notes
### Existing Code Analysis
- GraphService already exists with `getEntryGraph()` method for entry-centered graphs
- GraphNode and GraphEdge interfaces defined in entities/graph.entity.ts
- GraphQueryDto exists but only for entry-centered view (depth parameter)
- KnowledgeLinks table connects entries (source_id, target_id, resolved flag)
- No full graph endpoint exists yet
- No orphan detection implemented yet
- No graph statistics endpoint yet
### Implementation Plan
1. Create new graph.controller.ts for graph endpoints
2. Extend GraphService with:
- getFullGraph(workspaceId, filters) - full graph with optional filters
- getGraphStats(workspaceId) - graph statistics including orphan detection
3. Create new DTOs:
- GraphFilterDto - for filtering by tags, status, limit
- GraphStatsResponse - for statistics response
- FullGraphResponse - for full graph response
4. Add tests for new service methods (TDD)
5. Wire up controller to module
### Implementation Summary
**Files Created:**
- `/apps/api/src/knowledge/graph.controller.ts` - New controller with 3 endpoints
- `/apps/api/src/knowledge/graph.controller.spec.ts` - Controller tests (7 tests, all passing)
**Files Modified:**
- `/apps/api/src/knowledge/dto/graph-query.dto.ts` - Added GraphFilterDto
- `/apps/api/src/knowledge/entities/graph.entity.ts` - Extended interfaces with isOrphan, status fields, added FullGraphResponse and GraphStatsResponse
- `/apps/api/src/knowledge/services/graph.service.ts` - Added getFullGraph(), getGraphStats(), getEntryGraphBySlug()
- `/apps/api/src/knowledge/services/graph.service.spec.ts` - Added 7 new tests (14 total, all passing)
- `/apps/api/src/knowledge/knowledge.module.ts` - Registered KnowledgeGraphController
- `/apps/api/src/knowledge/dto/index.ts` - Exported GraphFilterDto
**API Endpoints Implemented:**
1. `GET /api/knowledge/graph` - Returns full knowledge graph
- Query params: tags[], status, limit
- Returns: nodes[], edges[], stats (totalNodes, totalEdges, orphanCount)
2. `GET /api/knowledge/graph/stats` - Returns graph statistics
- Returns: totalEntries, totalLinks, orphanEntries, averageLinks, mostConnectedEntries[], tagDistribution[]
3. `GET /api/knowledge/graph/:slug` - Returns entry-centered subgraph
- Query params: depth (1-5, default 1)
- Returns: centerNode, nodes[], edges[], stats
**Key Features:**
- Orphan detection: Identifies entries with no incoming or outgoing links
- Filtering: By tags, status, and node count limit
- Performance optimizations: Uses raw SQL for aggregate queries
- Tag distribution: Shows entry count per tag
- Most connected entries: Top 10 entries by link count
- Caching: Leverages existing cache service for entry-centered graphs
**Test Coverage:**
- 21 total tests across service and controller
- All tests passing
- Coverage includes orphan detection, filtering, statistics calculation