4.2 KiB
Prompt 12: Wire Up Services Layer (Analytics, Policy, Settings, Sessions)
Context
You are working in /workspaces/claude-code. The CLI has several background services that run during operation:
- Analytics/Telemetry — GrowthBook feature flags, OpenTelemetry traces
- Policy Limits — rate limiting, quota enforcement from Anthropic backend
- Remote Managed Settings — server-pushed configuration
- Session Memory — persistent conversation history across invocations
- Bootstrap Data — initial config fetched from API on startup
Most of these talk to Anthropic's backend servers and will fail in our dev build. The goal is to make them fail gracefully (not crash the app) or provide stubs.
Key Files
src/services/analytics/growthbook.ts— GrowthBook feature flag clientsrc/services/analytics/— Telemetry, event loggingsrc/services/policyLimits/— Rate limit enforcementsrc/services/remoteManagedSettings/— Server-pushed settingssrc/services/SessionMemory/— Conversation persistencesrc/services/api/bootstrap.ts— Initial data fetchsrc/entrypoints/init.ts— Where most services are initializedsrc/cost-tracker.ts— Token usage and cost tracking
Task
Part A: Map the initialization sequence
Read src/entrypoints/init.ts carefully. Document:
- What services are initialized, in what order?
- Which are blocking (must complete before app starts)?
- Which are fire-and-forget (async, can fail silently)?
- What happens if each one fails?
Part B: Make GrowthBook optional
Read src/services/analytics/growthbook.ts:
- How is GrowthBook initialized?
- Where is it called from? (feature flag checks throughout the codebase)
- What happens if initialization fails?
Goal: Make GrowthBook fail silently — all feature flag checks should return false (default) if GrowthBook is unavailable. This may already be handled, but verify it.
Part C: Stub policy limits
Read src/services/policyLimits/:
- What limits does it enforce? (messages per minute, tokens per day, etc.)
- What happens when a limit is hit?
- Where is
loadPolicyLimits()called?
Goal: Make the app work without policy limits. Either:
- Stub the service to return "no limits" (allow everything)
- Or catch and ignore errors from the API call
Part D: Make remote settings optional
Read src/services/remoteManagedSettings/:
- What settings does it manage?
- What's the fallback when the server is unreachable?
Goal: Ensure the app works with default settings when the remote endpoint fails.
Part E: Handle bootstrap data
Read src/services/api/bootstrap.ts:
- What data does it fetch?
- What uses this data?
- What happens if the fetch fails?
Goal: Provide sensible defaults when bootstrap fails (no API key = no bootstrap).
Part F: Verify session memory
Read src/services/SessionMemory/:
- Where is session data stored? (filesystem path)
- How are sessions identified?
- Does it work with the local filesystem?
Goal: Session memory should work out of the box since it's local filesystem.
Part G: Wire up cost tracking
Read src/cost-tracker.ts:
- How are costs calculated?
- Where is usage reported?
- Does it persist across sessions?
Goal: Cost tracking should work locally (just display, no remote reporting needed).
Part H: Create a services smoke test
Create scripts/test-services.ts:
// scripts/test-services.ts
// Test that all services initialize without crashing
// Usage: bun scripts/test-services.ts
import './src/shims/preload.js'
async function main() {
console.log('Testing service initialization...')
// Try to run the init sequence
try {
const { init } = await import('./src/entrypoints/init.js')
await init()
console.log('✅ Services initialized')
} catch (err: any) {
console.error('❌ Init failed:', err.message)
// Document which service failed and why
}
}
main()
Verification
bun scripts/test-services.tscompletes without crashing (warnings are fine)- Missing remote services log warnings, not crashes
- Session memory reads/writes to the local filesystem
- Cost tracking displays locally
- The app can start even when Anthropic's backend is unreachable (with just an API key)