feat: implement ForumAgent service for server-side PDS writes (ATB-18) (#31)
* docs: add ATB-18 ForumAgent implementation plan
Detailed TDD-based plan with 10 bite-sized tasks:
1. Add forum credentials to config
2. Create ForumAgent class with status types
3. Implement error classification and retry logic
4. Implement proactive session refresh
5. Integrate into AppContext
6. Create health endpoint
7. Update test context
8. Manual integration testing
9. Update documentation
10. Update Linear issue and project plan
Each task follows TDD: write test → verify fail → implement → verify pass → commit
* feat(appview): add forum credentials to config
- Add forumHandle and forumPassword to AppConfig
- Load from FORUM_HANDLE and FORUM_PASSWORD env vars
- Make credentials optional (undefined if not set)
- Add tests for loading forum credentials
* feat(appview): create ForumAgent class with basic structure
- Define ForumAgentStatus and ForumAgentState types
- Implement initialization with status tracking
- Add getStatus(), isAuthenticated(), getAgent() methods
- Add shutdown() for resource cleanup
- Add tests for initialization and successful auth
* fix(appview): address ForumAgent code quality issues
Critical fixes:
- Logging: Wrap all console logs in JSON.stringify() per project standards
- Type safety: Remove non-null assertions by making agent non-nullable
- Test lifecycle: Add shutdown() calls in afterEach for proper cleanup
- Test coverage: Add test verifying shutdown() cleans up resources
All ForumAgent tests pass.
* feat(appview): implement error classification and retry logic in ForumAgent
- Add isAuthError() and isNetworkError() helpers
- Auth errors fail permanently (no retry, prevent lockouts)
- Network errors retry with exponential backoff (10s, 20s, 40s, 80s, 160s)
- Stop retrying after 5 failed attempts
- Update status to 'retrying' during retry attempts
- Add comprehensive tests for error classification and retry behavior
* fix(appview): correct retry backoff intervals to match spec (10s, 30s, 1m, 5m, 10m)
* feat(appview): implement proactive session refresh in ForumAgent
- Add scheduleRefresh() to run every 30 minutes
- Add refreshSession() using agent.resumeSession()
- Fall back to full re-auth if refresh fails
- Add tests for session refresh and refresh failure handling
* fix(appview): address ForumAgent session refresh critical issues
- Add isRefreshing flag to prevent concurrent refresh execution
- Add session existence check to guard clause in refreshSession()
- Set status='retrying' immediately on refresh failure (before attemptAuth)
- Remove non-null assertion by checking session in guard clause
Fixes race conditions and status consistency issues identified in code review.
* feat(appview): integrate ForumAgent into AppContext
- Add forumAgent to AppContext interface (nullable)
- Initialize ForumAgent in createAppContext if credentials provided
- Clean up ForumAgent in destroyAppContext
- Add tests for ForumAgent integration with AppContext
- Use structured logging with JSON.stringify for missing credentials warning
* fix(appview): add missing forumDid field to AppContext test config
- Add required forumDid field to test configuration
- Extend sessionSecret to meet 32-character minimum
* fix(appview): improve AppContext ForumAgent integration logging and tests
- Use JSON.stringify for structured logging (follows project convention)
- Add assertion that initialize() is called when ForumAgent created
- Add console.warn spy assertion for missing credentials path
- Improves test coverage of critical initialization path
* feat(appview): add comprehensive health endpoint with service status reporting
- Create GET /api/health endpoint (public, no auth required)
- Report status: healthy, degraded, unhealthy
- Include database status with latency measurement
- Include firehose connection status
- Include ForumAgent status (authenticated, retrying, failed, etc)
- Expose granular ForumAgent states with retry countdown
- Security: no sensitive data exposed (no DIDs, handles, credentials)
- Add comprehensive tests for all health states
- Maintain backward compatibility with /api/healthz legacy endpoints
* fix(appview): make health endpoint spec-compliant with FirehoseService API
- Add isRunning() and getLastEventTime() methods to FirehoseService
- Track last event timestamp in firehose
- Update health endpoint to use spec-compliant methods
- Add last_event_at field to firehose health response
- Update tests to use new API methods
Fixes spec deviation where health endpoint was using getHealthStatus()
instead of the specified isRunning() and getLastEventTime() methods.
* refactor(appview): improve health endpoint code quality
- Replace 'any' type with proper HealthResponse interface
- Remove dead code (isHealthy, getHealthStatus methods)
- Eliminate redundant isRunning() call (use cached value)
- Add test coverage for last_event_at field
- Improve type safety and reduce maintenance burden
* test(appview): add ForumAgent to test context
- Add forumAgent: null to test context return value
- Ensures route tests have proper AppContext shape
- Mock ForumAgent is null by default (can be overridden in tests)
* docs: mark ATB-18 implementation complete and add usage guide
- Update acceptance criteria to show all items complete
- Add usage examples for checking ForumAgent availability in routes
- Add monitoring examples for health endpoint
- Document required environment variables
* docs: mark ATB-18 complete in project plan
ForumAgent service implemented with:
- Graceful degradation and smart retry logic
- Proactive session refresh
- Health endpoint integration
- Comprehensive test coverage
* docs: update Bruno collection for /api/health endpoint
Update existing Check Health.bru to document the new comprehensive
health endpoint at /api/health instead of the legacy /api/healthz.
Changes:
- Updated URL from /api/healthz to /api/health
- Added comprehensive documentation of response structure
- Documented all three overall status types (healthy/degraded/unhealthy)
- Documented all five ForumAgent status values
- Added assertions for response fields
- Included example responses for each health state
- Added usage examples for monitoring and alerting
- Noted security guarantee (no sensitive data exposure)
Addresses code review feedback on PR #31.