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.
Flow and ownership
Section titled “Flow and ownership”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_contentprovides replacement text. The tool sanitizes braces usingsanitizeUnknownTemplatePlaceholders()and validates length viavalidateMemoryContent(). - Deletion. Supply the ID and scope. The tool rejects
memory_contenton a delete.
Scope determination and resolution
Section titled “Scope determination and resolution”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 theserver_memoriestable scoped to(server_id, persona_lineage_id). - Personal scope. With
memory_scope: "target_user", providetarget_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.PARTIALorPrivacyLevel.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.
Database operations and notifications
Section titled “Database operations and notifications”Following parameter and scope checks, execution routes to the matching repository:
- Database execution:
- Server update:
serverMemoryRepository.updateByIdWithLineage()updates content inserver_memories. - Server deletion:
serverMemoryRepository.removeByIdWithLineage()removes the row and returns its prior content. - Personal update:
personalMemoryRepository.updateByIdForUserAndLineage()updates content inpersonal_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 leavemanually_writtenunchanged. Every successful edit-form save through/memoriesor/personal memoriessets 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.
- Server update:
- Cache invalidation:
- Server operations call
invalidateTomoriStateCache(serverDiscId)immediately after successful database execution. - Personal operations call
invalidateUserCache(targetUserId)immediately after successful database execution.
- Server operations call
- 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.
- Updates dispatch an amber notice embed using
The tool returns a successful ToolResult detailing the outcome.
Constraints and rationale
Section titled “Constraints and rationale”- Lineage protection: all repository updates and deletes enforce
persona_lineage_idmatching, 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.
Source pointers
Section titled “Source pointers”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.tsandsrc/utils/cache/userCache.ts: cache invalidation functions.