Worker service
The worker service is a long-running HTTP API built with Express.js and managed natively by Bun. It processes observations through the Claude Agent SDK separately from hook execution to prevent timeout issues.Overview
Technology
Runtime
smart-install.js)Port
CLAUDE_MEM_WORKER_PORTModel
CLAUDE_MEM_MODEL environment variable (default: sonnet)| Property | Value |
|---|---|
| Source | src/services/worker-service.ts |
| Built output | plugin/scripts/worker-service.cjs |
| Process manager | Native Bun ProcessManager |
| PID file | ~/.claude-mem/worker.pid |
REST API endpoints
The worker service exposes 22 HTTP endpoints organized into six categories.Viewer and health endpoints
GET / — Viewer UI
GET / — Viewer UI
- Real-time memory stream visualization
- Infinite scroll pagination
- Project filtering
- SSE-based live updates
- Theme toggle (light/dark, v5.1.2+)
GET /health — Health check
GET /health — Health check
GET /stream — Server-Sent Events
GET /stream — Server-Sent Events
| Event | When fired |
|---|---|
observation-created | New observation added |
session-summary-created | New summary generated |
user-prompt-created | New prompt recorded |
Data retrieval endpoints
GET /api/prompts — Get prompts (paginated)
GET /api/prompts — Get prompts (paginated)
| Parameter | Default | Description |
|---|---|---|
project | — | Filter by project name |
limit | 20 | Number of results |
offset | 0 | Pagination offset |
GET /api/observations — Get observations (paginated)
GET /api/observations — Get observations (paginated)
| Parameter | Default | Description |
|---|---|---|
project | — | Filter by project name |
limit | 20 | Number of results |
offset | 0 | Pagination offset |
GET /api/summaries — Get summaries (paginated)
GET /api/summaries — Get summaries (paginated)
| Parameter | Default | Description |
|---|---|---|
project | — | Filter by project name |
limit | 20 | Number of results |
offset | 0 | Pagination offset |
GET /api/observation/:id — Get observation by ID
GET /api/observation/:id — Get observation by ID
id (required) — observation IDResponse:{"error": "Observation #123 not found"}POST /api/observations/batch — Get observations by IDs
POST /api/observations/batch — Get observations by IDs
get_observations MCP tool.Request body:| Field | Required | Description |
|---|---|---|
ids | Yes | Array of observation IDs |
orderBy | No | date_desc or date_asc (default: date_desc) |
limit | No | Maximum results to return |
project | No | Filter by project name |
orderBy.Error responses:400:{"error": "ids must be an array of numbers"}400:{"error": "All ids must be integers"}
GET /api/session/:id — Get session by ID
GET /api/session/:id — Get session by ID
{"error": "Session #456 not found"}GET /api/prompt/:id — Get prompt by ID
GET /api/prompt/:id — Get prompt by ID
{"error": "Prompt #1 not found"}GET /api/stats — Database statistics
GET /api/stats — Database statistics
GET /api/projects — List projects
GET /api/projects — List projects
Search endpoints
GET /api/search — Full-text search
GET /api/search — Full-text search
search MCP tool. Queries FTS5 virtual tables via the SessionSearch service.Query parameters:| Parameter | Description |
|---|---|
query | Full-text search query |
limit | Maximum results (default: 20) |
type | Filter by observation type |
project | Filter by project name |
dateStart, dateEnd | Date range filters |
offset | Pagination offset |
orderBy | Sort order |
GET /api/timeline — Timeline context
GET /api/timeline — Timeline context
timeline MCP tool.Query parameters:| Parameter | Description |
|---|---|
anchor | Observation ID to center timeline around |
query | Search query to find anchor automatically |
depth_before | Observations before anchor (default: 3) |
depth_after | Observations after anchor (default: 3) |
project | Filter by project name |
anchor or query must be provided.Response: Chronological view showing what happened before, during, and after the anchor point.Settings endpoints
GET /api/settings — Retrieve settings
GET /api/settings — Retrieve settings
POST /api/settings — Save settings
POST /api/settings — Save settings
{"success": true}Queue management endpoints
GET /api/pending-queue — Queue status
GET /api/pending-queue — Queue status
| Status | Meaning |
|---|---|
pending | Queued, not yet processed |
processing | Currently being processed by SDK agent |
processed | Completed successfully |
failed | Failed after 3 retry attempts |
processing status for more than 5 minutes are counted in stuckCount.POST /api/pending-queue/process — Trigger manual recovery
POST /api/pending-queue/process — Trigger manual recovery
sessionLimit is optional (default: 10, max: 100).Response:Session management endpoints
POST /sessions/:sessionDbId/init — Initialize session
POST /sessions/:sessionDbId/init — Initialize session
{"success": true, "session_id": "abc-123"}POST /sessions/:sessionDbId/observations — Add observation
POST /sessions/:sessionDbId/observations — Add observation
{"success": true, "observation_id": 123}POST /sessions/:sessionDbId/summarize — Generate summary
POST /sessions/:sessionDbId/summarize — Generate summary
{"trigger": "stop"}Response: {"success": true, "summary_id": 456}GET /sessions/:sessionDbId/status — Session status
GET /sessions/:sessionDbId/status — Session status
DELETE /sessions/:sessionDbId — Delete session
DELETE /sessions/:sessionDbId — Delete session
{"success": true}SSE real-time updates
The/stream endpoint implements Server-Sent Events to push live updates to the viewer UI. Clients connect once and receive a continuous event stream without polling.
Bun process management
The worker is managed by the nativeProcessManager class, which handles:
- Process spawning with the Bun runtime
- PID file tracking at
~/.claude-mem/worker.pid - Health checks with automatic retry
- Graceful shutdown with SIGTERM / SIGKILL fallback
Commands
Auto-start behavior
The worker service auto-starts when the SessionStart hook (context-hook) fires. Manual start is optional.
Bun installation
Bun is required to run the worker service. If Bun is not present,smart-install.js installs it automatically on first run:
- macOS / Linux
- Windows
Async observation processing pipeline
The worker routes observations to the Claude Agent SDK for AI-powered processing asynchronously, preventing hook timeout issues.Observation queue
save-hook posts to /sessions/:id/observations.SDK processing
src/sdk/prompts.ts.XML parsing
src/sdk/parser.ts to extract structured fields: title, subtitle, narrative, facts, concepts, type, files_read, files_modified.SDK components
| File | Responsibility |
|---|---|
src/sdk/prompts.ts | Builds XML-structured prompts for Claude |
src/sdk/parser.ts | Parses Claude’s XML responses |
src/sdk/worker.ts | Main SDK agent loop |
Model configuration
| Shorthand | Description |
|---|---|
haiku | Fast, cost-efficient |
sonnet | Balanced (default) |
opus | Most capable |
Port allocation
- Default: Port 37777
- Override:
CLAUDE_MEM_WORKER_PORTenvironment variable - Port file:
${CLAUDE_PLUGIN_ROOT}/data/worker.port
Data storage
Error handling
The worker implements graceful degradation:| Error type | Behavior |
|---|---|
| Database errors | Logged but do not crash the service |
| SDK errors | Retried with exponential backoff |
| Network errors | Logged and skipped |
| Invalid input | Validated and rejected with error response |