* docs: Update AI documentation for accurate codebase reflection - Replace obsolete POLLING_ARCHITECTURE.md with DATA_FETCHING_ARCHITECTURE.md - Rewrite API_NAMING_CONVENTIONS.md with file references instead of code examples - Condense ARCHITECTURE.md from 482 to 195 lines for clarity - Update ETAG_IMPLEMENTATION.md to reflect actual implementation - Update QUERY_PATTERNS.md to reflect completed Phase 5 (nanoid optimistic updates) - Add PRPs/stories/ to .gitignore All documentation now references actual files in codebase rather than embedding potentially stale code examples. * docs: Update CLAUDE.md and AGENTS.md with current patterns - Update CLAUDE.md to reference documentation files instead of embedding code - Replace Service Layer and Error Handling code examples with file references - Add proper distinction between DATA_FETCHING_ARCHITECTURE and QUERY_PATTERNS docs - Include ETag implementation reference - Update environment variables section with .env.example reference * docs: apply PR review improvements to AI documentation - Fix punctuation, hyphenation, and grammar issues across all docs - Add language tags to directory tree code blocks for proper markdown linting - Clarify TanStack Query integration (not replacing polling, but integrating it) - Add Cache-Control header documentation and browser vs non-browser fetch behavior - Reference actual implementation files for polling intervals instead of hardcoding values - Improve type-safety phrasing and remove line numbers from file references - Clarify Phase 1 removed manual frontend ETag cache (backend ETags remain)
5.8 KiB
Data Fetching Architecture
Overview
Archon uses TanStack Query v5 for all data fetching, caching, and synchronization. This replaces the former custom polling layer with a query‑centric design that handles caching, deduplication, and smart refetching (including visibility‑aware polling) automatically.
Core Components
1. Query Client Configuration
Location: archon-ui-main/src/features/shared/queryClient.ts
Centralized QueryClient with:
- 30-second default stale time
- 10-minute garbage collection
- Smart retry logic (skips 4xx errors)
- Request deduplication enabled
- Structural sharing for optimized re-renders
2. Smart Polling Hook
Location: archon-ui-main/src/features/ui/hooks/useSmartPolling.ts
Visibility-aware polling that:
- Pauses when browser tab is hidden
- Slows down (1.5x interval) when tab is unfocused
- Returns
refetchIntervalfor use with TanStack Query
3. Query Patterns
Location: archon-ui-main/src/features/shared/queryPatterns.ts
Shared constants:
DISABLED_QUERY_KEY- For disabled queriesSTALE_TIMES- Standardized cache durations (instant, realtime, frequent, normal, rare, static)
Feature Implementation Patterns
Query Key Factories
Each feature maintains its own query keys:
- Projects:
archon-ui-main/src/features/projects/hooks/useProjectQueries.ts(projectKeys) - Tasks:
archon-ui-main/src/features/projects/tasks/hooks/useTaskQueries.ts(taskKeys) - Knowledge:
archon-ui-main/src/features/knowledge/hooks/useKnowledgeQueries.ts(knowledgeKeys) - Progress:
archon-ui-main/src/features/progress/hooks/useProgressQueries.ts(progressKeys) - MCP:
archon-ui-main/src/features/mcp/hooks/useMcpQueries.ts(mcpKeys) - Documents:
archon-ui-main/src/features/projects/documents/hooks/useDocumentQueries.ts(documentKeys)
Data Fetching Hooks
Standard pattern across all features:
use[Feature]()- List queriesuse[Feature]Detail(id)- Single item queriesuseCreate[Feature]()- Creation mutationsuseUpdate[Feature]()- Update mutationsuseDelete[Feature]()- Deletion mutations
Backend Integration
ETag Support
Location: archon-ui-main/src/features/shared/apiWithEtag.ts
ETag implementation:
- Browser handles ETag headers automatically
- 304 responses reduce bandwidth
- TanStack Query manages cache state
API Structure
Backend endpoints follow RESTful patterns:
- Knowledge:
python/src/server/api_routes/knowledge_api.py - Projects:
python/src/server/api_routes/projects_api.py - Progress:
python/src/server/api_routes/progress_api.py - MCP:
python/src/server/api_routes/mcp_api.py
Optimistic Updates
Utilities: archon-ui-main/src/features/shared/optimistic.ts
All mutations use nanoid-based optimistic updates:
- Creates temporary entities with
_optimisticflag - Replaces with server data on success
- Rollback on error
- Visual indicators for pending state
Refetch Strategies
Smart Polling Usage
Implementation: archon-ui-main/src/features/ui/hooks/useSmartPolling.ts
Polling intervals are defined in each feature's query hooks. See actual implementations:
- Projects:
archon-ui-main/src/features/projects/hooks/useProjectQueries.ts - Tasks:
archon-ui-main/src/features/projects/tasks/hooks/useTaskQueries.ts - Knowledge:
archon-ui-main/src/features/knowledge/hooks/useKnowledgeQueries.ts - Progress:
archon-ui-main/src/features/progress/hooks/useProgressQueries.ts - MCP:
archon-ui-main/src/features/mcp/hooks/useMcpQueries.ts
Standard intervals from archon-ui-main/src/features/shared/queryPatterns.ts:
STALE_TIMES.instant: 0ms (always fresh)STALE_TIMES.frequent: 5 seconds (frequently changing data)STALE_TIMES.normal: 30 seconds (standard cache)
Manual Refetch
All queries expose refetch() for manual updates.
Performance Optimizations
Request Deduplication
Handled automatically by TanStack Query when same query key is used.
Stale Time Configuration
Defined in STALE_TIMES and used consistently:
- Auth/Settings:
Infinity(never stale) - Active operations:
0(always fresh) - Normal data:
30_000(30 seconds) - Rare updates:
300_000(5 minutes)
Garbage Collection
Unused data removed after 10 minutes (configurable in queryClient).
Migration from Polling
What Changed (Phases 1-5)
- Phase 1: Removed ETag cache layer
- Phase 2: Standardized query keys
- Phase 3: Fixed optimistic updates with UUIDs
- Phase 4: Configured request deduplication
- Phase 5: Removed manual invalidations
Deprecated Patterns
usePollinghook (removed)useCrawlProgressPolling(removed)- Manual cache invalidation with setTimeout
- Socket.IO connections
- Double-layer caching
Testing Patterns
Hook Testing
Example: archon-ui-main/src/features/projects/hooks/tests/useProjectQueries.test.ts
Standard mocking approach for:
- Service methods
- Query patterns (STALE_TIMES, DISABLED_QUERY_KEY)
- Smart polling behavior
Integration Testing
Use React Testing Library with QueryClientProvider wrapper.
Developer Guidelines
Adding New Data Fetching
- Create query key factory in
{feature}/hooks/use{Feature}Queries.ts - Use
useQuerywith appropriate stale time fromSTALE_TIMES - Add smart polling if real-time updates needed
- Implement optimistic updates for mutations
- Follow existing patterns in similar features
Common Patterns to Follow
- Always use query key factories
- Never hardcode stale times
- Use
DISABLED_QUERY_KEYfor conditional queries - Implement optimistic updates for better UX
- Add loading and error states
Future Considerations
- Server-Sent Events for true real-time (post-Phase 5)
- WebSocket fallback for critical updates
- GraphQL migration for selective field updates