# What Is Symbolic Memory Encoding Using Mermaid Syntax in TencentDB Agent Memory

> Discover symbolic memory encoding in TencentDB Agent Memory. Learn how Mermaid syntax transforms meaningful memories into compact, machine-readable flowcharts for agent cognitive snapshots.

- Repository: [Tencent Cloud/TencentDB-Agent-Memory](https://github.com/TencentCloud/TencentDB-Agent-Memory)
- Tags: deep-dive
- Published: 2026-08-21

---

**In the TencentDB Agent Memory system, symbolic memory encoding using Mermaid syntax is the process of filtering out purely symbolic text fragments and representing the remaining semantically meaningful memories as a Mermaid flowchart that serves as a compact, machine-readable snapshot of the agent's cognitive state machine.**

The TencentCloud/TencentDB-Agent-Memory repository implements a multi-layered memory architecture where raw conversational data undergoes strict quality filtering before being structured into visual representations. This encoding mechanism ensures that only substantive knowledge—free from noise like punctuation clusters or special character sequences—gets preserved in the agent's long-term memory graph.

## Understanding Symbolic Memory Filtering

In this system, **symbolic memory** refers to text fragments consisting solely of non-word, non-space, non-CJK characters—such as `"!!!"`, `"@@@"`, or other punctuation-heavy sequences that carry no semantic information. These fragments are identified and discarded during the L0 to L1 extraction phase to prevent pollution of the downstream memory store.

### The Pure Symbol Detection Logic

The critical quality check resides in [`MemoryCore/src/utils/sanitize.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/utils/sanitize.ts) within the `shouldExtractL1()` function. This predicate uses a strict regular expression to detect and reject purely symbolic strings between 1 and 5 characters in length:

```typescript
// MemoryCore/src/utils/sanitize.ts
export function shouldExtractL1(text: string): boolean {
  if (!shouldCaptureL0(text)) return false;

  // Purely symbolic strings (1-5 characters) are rejected
  if (/^[^\w\s\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]{1,5}$/.test(text))
    return false;
  // … additional checks omitted for brevity …
  return true;
}

```

The regex `/^[^\w\s\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]{1,5}$/` specifically targets strings that contain only special symbols—excluding word characters (`\w`), whitespace (`\s`), and CJK Unicode ranges for Chinese, Japanese, and Korean characters. Only when `shouldExtractL1(text)` returns `true` does the content proceed to the L2 encoding stage.

## The L2 Mermaid Generation Pipeline

Once filtered, meaningful memory entries enter the **L2 Mermaid Generation pipeline**, which transforms offloaded tool-call logs and user intents into structured Mermaid flowcharts of type `flowchart TD` (top-down). This pipeline converts discrete memory fragments into a visual cognitive state machine that the LLM can parse and manipulate.

### From Offload Entries to Mermaid Nodes

In [`MemoryCore/src/offload/pipelines/l2-mermaid.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/offload/pipelines/l2-mermaid.ts), each valid `OffloadEntry` is converted into a diagram node via the `entryToMermaidNode()` function. The system automatically assigns node identifiers, status flags, and textual summaries:

```typescript
// MemoryCore/src/offload/pipelines/l2-mermaid.ts
import { OffloadEntry } from '../types';

function entryToMermaidNode(entry: OffloadEntry, idx: number): string {
  const id = `N${idx}`;
  const status = entry.done ? 'done' : (entry.inProgress ? 'doing' : 'todo');
  const label = entry.summary.replace(/"/g, '\\"');
  return `${id}[${status}]:::${status} -->|${label}| ${id}`;
}

```

Each node receives a status classification—`todo`, `doing`, or `done`—creating a task-oriented visualization of the agent's workflow. The `:::${status}` syntax applies CSS class styling to distinguish completion states visually.

### Constructing the Cognitive State Diagram

The `buildMermaidDiagram()` function aggregates individual nodes into a complete flowchart structure:

```typescript
// MemoryCore/src/offload/pipelines/l2-mermaid.ts
export function buildMermaidDiagram(entries: OffloadEntry[]): string {
  const nodes = entries.map(entryToMermaidNode).join('\n');
  return `flowchart TD\n${nodes}`;
}

```

This produces a directed graph where edges represent temporal or logical relationships between memory entries, enabling the LLM to trace the evolution of task contexts and dependencies through the agent's operational history.

## Integrating Mermaid Diagrams into LLM Prompts

The generated Mermaid syntax is embedded within LLM prompts to provide a structured, scippable representation of the agent's current knowledge state. In [`MemoryCore/src/offload/local-llm/prompts/l2-prompt.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/offload/local-llm/prompts/l2-prompt.ts), the diagram is wrapped in a fenced code block and injected into the conversation context:

```typescript
// MemoryCore/src/offload/local-llm/prompts/l2-prompt.ts
const parts = [];
parts.push('\n## Existing Mermaid content:');

parts.push('```mermaid\n' + currentMmd + '\n```');
// … LLM is asked to update the diagram and return JSON …

```

This integration allows the LLM to perform two critical operations:

- **Update** the diagram by adding new nodes or modifying existing status flags as tasks progress
- **Query** the diagram to understand current task goals, completed steps, and pending work without parsing unstructured text

## Summary

- **Symbolic memory** in TencentDB Agent Memory refers to noise-like text fragments containing only 1-5 special characters that lack semantic value.
- The `shouldExtractL1()` function in [`MemoryCore/src/utils/sanitize.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/utils/sanitize.ts) filters these symbols using a regex that excludes word characters and CJK scripts.
- Valid memories are encoded as **Mermaid flowcharts** (`flowchart TD`) via the L2 pipeline in [`MemoryCore/src/offload/pipelines/l2-mermaid.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/offload/pipelines/l2-mermaid.ts).
- Each memory entry becomes a node with an auto-generated ID, a status flag (`todo`/`doing`/`done`), and a descriptive label.
- The resulting diagram is embedded in LLM prompts to create a **visual cognitive state machine** that supports both updates and queries by the agent.

## Frequently Asked Questions

### What constitutes symbolic memory in this system?

Symbolic memory refers to text strings composed exclusively of non-word, non-space, non-CJK characters—such as `"!!!"` or `"@@@"`—that carry virtually no semantic information for downstream processing. These fragments are detected by the regex `/^[^\w\s\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]{1,5}$/` in [`MemoryCore/src/utils/sanitize.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/utils/sanitize.ts) and are filtered out before storage.

### How does the system distinguish between symbolic and meaningful content?

The system uses the `shouldExtractL1()` function to apply a length-based symbolic check (1-5 characters) combined with Unicode category exclusion. Strings containing only special symbols are rejected, while those including word characters, whitespace, or CJK scripts proceed to the L2 Mermaid encoding pipeline.

### What Mermaid diagram type does the system use for memory encoding?

The system generates **top-down flowcharts** using the `flowchart TD` declaration. Each memory entry becomes a node with directional edges representing relationships, and nodes are styled with status classes (`todo`, `doing`, `done`) to indicate task progression within the agent's cognitive state machine.

### Can the LLM modify the generated Mermaid diagram?

Yes. The LLM receives the Mermaid diagram wrapped in a fenced code block within the L2 prompt defined in [`MemoryCore/src/offload/local-llm/prompts/l2-prompt.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/offload/local-llm/prompts/l2-prompt.ts). The model can update the diagram by adding new nodes, changing status flags, or restructuring edges, then return the modified syntax as part of its JSON response to reflect the current agent state.