Skip to content

LTM 02: Memory Update & Delete

The memory update and delete stage handles model-initiated modifications or removals of existing persistent memories using the integer identifier shown in prompt context.

The model calls manage_long_term_memory with action: "update" or action: "delete" and the memory ID.

The action controls which fields the tool accepts.

  • Update: memory_content provides replacement text. The tool sanitizes braces using sanitizeUnknownTemplatePlaceholders() and validates length via validateMemoryContent().
  • Deletion. Supply the ID and scope. The tool rejects memory_content on a delete.

The required memory_scope selects the repository. Server and personal IDs are independent.

  • Server scope. With memory_scope: "server_wide", the tool updates or deletes records in the server_memories table scoped to (server_id, persona_lineage_id).
  • Personal scope. With memory_scope: "target_user", provide target_user. resolveUserTarget() (src/utils/discord/targetResolver.ts) matches the display name.
    • Bridge users reject personal updates with an error because bridge users only support server-wide memories.
    • Personal updates targeting the bot itself return an error.
    • The resolved user must belong to the current guild or direct message channel.
    • Updates and deletions check the target’s privacy and message-proxy host protection before loading personal memories. PrivacyLevel.PARTIAL or PrivacyLevel.FULL, an unresolved proxy host, or a host with full privacy or a server blacklist entry blocks the tool.
    • Owners can still delete stored memories after opting out through /personal memories, whose interactions bind the owner to the invoking account or a managed proxy identity.

Following parameter and scope checks, execution routes to the matching repository:

  1. Database execution:
    • Server update: serverMemoryRepository.updateByIdWithLineage() updates content in server_memories.
    • Server deletion: serverMemoryRepository.removeByIdWithLineage() removes the row and returns its prior content.
    • Personal update: personalMemoryRepository.updateByIdForUserAndLineage() updates content in personal_memories.
    • Personal deletion: personalMemoryRepository.removeByIdForUserAndLineage() removes the row and returns its prior content. Content-changing updates from either the tool or memory panels delete every embedding family in the same transaction while preserving decay metadata. Model edits leave manually_written unchanged. Every successful edit-form save through /memories or /personal memories sets it to true in the content-update transaction, including channel-only, tag-only, and unchanged-content saves. This protects the memory from automatic eviction while allowing it to become dormant. Unchanged content keeps its vectors. Deleting a memory cascades to its vectors; post-turn fill rebuilds edited content only when it is visible to recall.
  2. Cache invalidation:
    • Server operations call invalidateTomoriStateCache(serverDiscId) immediately after successful database execution.
    • Personal operations call invalidateUserCache(targetUserId) immediately after successful database execution.
  3. Discord embed confirmation:
    • Updates dispatch an amber notice embed using sendMemoryEmbedWithExpand().
    • Deletions dispatch a red notice embed displaying the deleted memory content for user confirmation.

The tool returns a successful ToolResult detailing the outcome.

  • Lineage protection: all repository updates and deletes enforce persona_lineage_id matching, preventing one character persona from mutating or deleting memories belonging to another.
  • Accidental deletion transparency: deletion operations fetch the memory text prior to row removal, allowing the confirmation notice to display the exact content that was removed.
  • Cache freshness guarantee: cache invalidation occurs immediately after database success, so subsequent context builds reload fresh records directly from the database.
  • src/tools/functionCalls/updateLongTermMemoryTool.ts: changeLongTermMemory() and the scoped write paths.
  • src/utils/db/repositories/ServerMemoryRepository.ts: queries for updating and removing server memories with lineage checks.
  • src/utils/db/repositories/PersonalMemoryRepository.ts: queries for updating and removing personal memories with lineage checks.
  • src/utils/cache/tomoriStateCache.ts and src/utils/cache/userCache.ts: cache invalidation functions.