Skip to content

[Module] Agent 分层记忆系统:热层 Markdown 目录与冷层 FTS 检索 #6

Closed

Description

@SATA260

[Module] Agent 分层记忆系统:热层 Markdown 目录与冷层 FTS 检索

功能职责

记忆跨 Session 保存目录和专题,并按工作区检索已落库消息;不决定记什么、怎么改。

  • 每个 user / workspace 一篇目录加多篇专题;目录是指针,正文在专题里
  • 目录超过 200 行或 25KB 仍写入,标 OverBudget
  • 新 Session 或对话压缩后装冻结目录,同 Session 内复用,不重读
  • 写消息时建索引;模型按需检索,不自动塞进每一轮
  • 用户可看/删目录和专题,不可从这边直接写;写入只走记忆工具

边界

  • 记什么、目录要不要改短、细节要不要挪专题,由模型和记忆工具决定;本模块只存字符串
  • 写入新目录不改当前 Session 已冻结的前缀;新 Session 或压缩后再带上
  • 检索范围是工作区,不是 Session
  • 检索命中只经工具回传,不每个 Turn / Run 自动检索再注入
  • 用户侧只能看/删,不暴露消息索引
  • 目录不拼进静态 prompt,不当事条消息入库
  • 不做向量检索,不自动建专题

内部拆分

目录与专题(TextMemory)

热层:每个 scope 一篇目录(name 固定 index)加多篇专题。管增删改查、长度和超限标记。不解析 Markdown,不决定正文怎么改。

type TextMemory struct {
    Scope      TextMemoryScope // user | workspace
    ScopeID    string
    Kind       TextMemoryKind  // index 为目录,topic 为专题。
    Name       string          // 目录固定为 index。
    Content    string
    ByteLen    int
    OverBudget bool            // 目录超过 200 行或 25KB。
}

type TextMemoryKey struct {
    Scope   TextMemoryScope
    ScopeID string
    Kind    TextMemoryKind
    Name    string
}

func Get(key TextMemoryKey) (TextMemory, error)                        // 读一篇目录或专题。
func Upsert(item TextMemory) (TextMemory, error)                       // 覆盖写入;用户侧不调,只给记忆工具用。
func Delete(key TextMemoryKey) error                                   // 删一篇目录或专题。
func List(scope TextMemoryScope, scopeID string) ([]TextMemory, error) // 列出某 scope 下的目录和专题。
func ByteLen(content string) int                                       // 按字节算 Markdown 长度。
func IndexOverBudget(content string) bool                              // 目录是否超过 200 行或 25KB。
func ClipIndex(content string) string                                  // 把目录裁到上限以内。

消息检索(Search)

冷层:同一工作区内已落库消息的检索。管建索引和按关键词查。不写热层,不把命中塞进 prompt。

type ContextMessage struct {
    ID          string
    WorkspaceID string // 检索范围,不是 Session。
    SessionID   string
    RunID       string
    Role        string
    Content     string
}

type Search struct {
    WorkspaceID string
    Query       string
    Limit       int
}

type MessageHit struct {
    Message ContextMessage
    Rank    float64
}

func IndexMessage(msg ContextMessage) error              // 写消息时建检索索引;不暴露给工具和用户。
func SearchMessages(search Search) ([]MessageHit, error) // 按工作区关键词检索已索引消息。

冻结目录(FrozenIndex)

新 Session 或对话压缩后,把 user 目录和工作区目录冻成两段字节,本 Session 内复用。不管专题,不拼静态 prompt。

func Load(session Session) (userIndex, workspaceIndex string) // 读出两份目录并冻结;没有则空串。
func Get(sessionID string) (userIndex, workspaceIndex string) // 取本 Session 已冻结的目录,不重读。

记忆工具(MemoryTool)

模型按需读写目录 / 专题、检索旧消息。user 用会话的 user,workspace 用会话的工作区。不装当前对话,不自动检索。

func Read(scope, name string) (TextMemory, error)           // 按 scope 和 name 读一篇。
func Write(scope, name, content string) (TextMemory, error) // 覆盖写入一篇,立刻落库。
func Search(query string) ([]MessageHit, error)             // 按当前工作区检索旧消息。

流程

用户新开一个会话,希望带上目录;模型按需读专题或检索旧消息,也可以改目录;用户可以看或删自己的记忆。

// 用户发起会话
FrozenIndex.Load(...)        // 冻成独立 system 消息,不拼进静态 prompt。

// 模型要读一篇
MemoryTool.Read(...)
TextMemory.Get(...)

// 模型要写一篇
MemoryTool.Write(...)
TextMemory.Upsert(...)
TextMemory.IndexOverBudget(...)

// 写消息时
Search.IndexMessage(...)

// 模型要搜旧消息
MemoryTool.Search(...)
Search.SearchMessages(...)

// 对话压缩后
FrozenIndex.Load(...)        // 再装一次;不改压缩前已冻结的前缀。

// 用户查看 / 删除
TextMemory.List(...)
TextMemory.Get(...)
TextMemory.Delete(...)

Activity

  1. changed the title [-]记忆模块[/-] [+]Agent 记忆模块[/+] on Aug 28, 2026
  2. added
    module
    and removed on Aug 28, 2026
  3. changed the title [-]Agent 记忆模块[/-] [+][Module] Agent 分层记忆系统:热层 Markdown 目录与冷层 FTS 检索[/+] on Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    module

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions