21. Session Memory

What the Session Memory Code Is Doing

The services/SessionMemory/ files reveal a background note-maintenance system. It initializes a dedicated markdown memory file, watches thresholds, and periodically uses a forked subagent to summarize useful information from the active session without interrupting the main loop.

Important Mechanisms

  • Threshold-based initialization and update cadence
  • Shared state for “last summarized message” and token counts
  • Timeout and stale-extraction protection
  • Read/write integration with the same tool and permission framework as the rest of the app

Main Lesson

Long-session products need a separate memory-maintenance loop. The user conversation cannot be the only place where durable state lives.

Source Navigation

Recommended Source Files

  • source/services/SessionMemory/sessionMemory.ts
  • source/services/SessionMemory/sessionMemoryUtils.ts
  • source/services/SessionMemory/prompts.ts
  • source/history.ts
  • source/assistant/sessionHistory.ts

Next Files to Read

  • source/services/compact/sessionMemoryCompact.ts
  • source/services/compact/compact.ts
  • source/services/extractMemories/extractMemories.ts