←
TRCKR-280
Created: 2025-12-12
•
Updated: 2025-12-16
Relationships
Loading...
Attachments
Loading...
Comments (1)
agent
·
2025-12-12
## Work Completed
Successfully created API endpoints for agents in server/routes/agents.py.
### Endpoints Implemented
1. **POST /api/agents/heartbeat** - Upsert agent status (main endpoint)
- Derives stable agent ID from hostname, machine_address, and repo_path
- Creates new agent if doesn't exist, updates if it does
- ID format: agt_<8-char sha256 hash>
- Returns full agent record with timestamps and version
2. **GET /api/agents** - List all agents
- Supports filtering by status and hostname
- Returns paginated list with total count
- Sorted by last_seen_at (most recent first)
3. **GET /api/agents/{agent_id}** - Get specific agent
- Returns full agent record by ID
- Returns 404 if agent not found
4. **DELETE /api/agents/{agent_id}** - Remove stale agent
- Hard delete from database
- Returns 204 on success, 404 if not found
### Implementation Details
**Agent ID Derivation** (server/routes/agents.py:25-42):
- Uses SHA-256 hash of "hostname:machine_address:repo_path"
- Deterministic - same characteristics always produce same ID
- Format: agt_ prefix + first 8 chars of hash
**Database Helpers**:
- `_get_agent()` - Fetch agent by ID with JSON field parsing
- `_list_agents()` - Fetch all agents with sorting
- `_upsert_agent()` - Create or update agent record
- `_delete_agent()` - Remove agent from database
**JSON Field Handling**:
- issue_ids and capabilities stored as JSON TEXT in database
- Automatically serialized on write, deserialized on read
- Default to empty arrays if null/missing
### Testing
Manual testing confirmed all endpoints work correctly:
```
✓ POST /api/agents/heartbeat (create) - 200 OK
✓ GET /api/agents (list) - 200 OK, total: 1
✓ GET /api/agents/{id} - 200 OK, version: 1
✓ POST /api/agents/heartbeat (update) - 200 OK, version: 2
✓ DELETE /api/agents/{id} - 204 No Content
✓ GET /api/agents/{id} (after delete) - 404 Not Found
```
All existing tests pass: 661 passed, 11 skipped
### Files Changed
- **server/routes/agents.py** (new): 340 lines with all endpoints
- **server/routes/__init__.py**: Added agents_router export
- **server/main.py**: Registered agents_router in app
### Patterns Followed
- Used existing route patterns from issues.py and comments.py
- All endpoints require Bearer token authentication
- Proper error handling with HTTPException
- Logging at debug/info/warning levels
- Response models from server/models.py (AgentResponse, AgentListResponse, etc.)
### Commit
- Branch: TRCKR-280
- Commit: 2c8330a
- Merged to master
- Pushed to remote: origin/master