Canon

09 — Memory Consolidation

How approved material consolidates into organizational memory scopes with provenance.

Overview

The Memory Consolidator is a scheduled Cloudflare Worker that aggregates approved triage items into consolidated memories. It bridges the gap between individual triage candidates and durable organizational knowledge.

File: services/knowledge/src/memory-consolidator.ts

Schedules

ScheduleTriggerWhat It Does
HourlyCron (every hour)Topic clustering of recent sessions
DailyCron (midnight UTC)Deep user/account consolidation + health signals
WeeklyCron (Sunday midnight UTC)Cross-account pattern extraction + best practices

Hourly Consolidation

  1. Fetch approved triage items from last hour
  2. Group by category/topic
  3. Generate topic summary using Think service (Workers AI fallback)
  4. Write consolidated_memories with type topic

Minimum threshold: 2+ sessions per topic.

Daily Consolidation

  1. Fetch approved triage items from last 24 hours
  2. User-level: Group by user, generate user summaries
  3. Account-level: Group by account, generate account summaries
  4. Send health signals for at-risk accounts
  5. Cleanup memories beyond retention period

Minimum threshold: 2+ sessions per user/account.

Weekly Consolidation

  1. Fetch consolidated memories from past week (topic + user + account types)
  2. Extract cross-cutting patterns using Think service
  3. Identify best practices
  4. Write pattern-type consolidated memory
  5. Update knowledge base with extracted patterns

Minimum threshold: 5+ consolidated memories.

Data Flow

Approved Triage Items
        |
        v
fetchApprovedTriageItems() — Knowledge Service API
        |
        v
Group by topic/user/account
        |
        v
Generate Summary (Think service / Workers AI)
        |
        v
storeConsolidatedMemory() — D1 direct write
        |
        v
consolidated_memories table

Lock Mechanism

The Consolidator uses a Durable Object lock to prevent duplicate processing:

  • acquire-lock with TTL (3600 seconds)
  • Release after processing completes
  • If lock not acquired, skip this cycle

Fallback Behavior

If the Think service is unavailable, the Consolidator uses a fallback summary:

function createFallbackSummary(sessions: SessionMemory[]): SummaryResult {
  return {
    summary: `Consolidated ${sessions.length} sessions`,
    insights: allInsights.slice(0, 5),
    themes: allEntities.slice(0, 5),
    actions: [],
    risk_level: avgSentiment < -0.3 ? "elevated" : "normal",
  };
}

Retention

Old consolidated memories are cleaned up based on MEMORY_RETENTION_DAYS configuration. Default cleanup runs during daily consolidation.