状态·记忆·会话State, memory & sessions · 单元Unit 06
状态与记忆:让会话可恢复、可扩展State and memory: recoverable, scalable conversations
状态决定进程重启后能否接着聊,记忆决定跨会话能否记得。学完本单元,你能区分两者并避免多实例覆盖、并发写冲突和上下文膨胀。State decides whether a conversation survives a restart; memory decides whether it survives across sessions. After this unit you can separate them and avoid multi-instance overwrites, write conflicts and context bloat.
学习目标Learning goals
- 区分 AgentState 与会话记忆Separate AgentState from conversational memory
- 把状态落到外部存储并处理并发写入Persist state externally and handle concurrent writes
- 设计长期记忆的写入与检索边界Design write and retrieval boundaries for long-term memory
概念闪卡Concept cards
点击卡片翻面查看解释。Click a card to flip it and reveal the explanation.
关键 APIKey APIs
点击进入对应的 Python 或 Java 参考页。Open the exact Python or Java reference for each type.
问题巩固Check yourself
完成 10 道题;答错会自动进入错题本。Answer all ten questions. Wrong answers are added to your wrong-answer book automatically.
01Python 中保存可恢复会话状态的核心类型是?Which Python type stores recoverable conversation state?
解析Why AgentState 包含上下文、reply id、迭代计数和未完成工具调用,可持久化恢复。AgentState contains context, reply id, iteration counters and unfinished tool calls, and can be persisted.
02生产环境中 Agent 状态应该放在哪里?Where should agent state live in production?
解析Why 状态外置才能水平扩容和重启恢复;实例字段只适合单进程演示。Externalized state enables horizontal scaling and restart recovery; instance fields only suit single-process demos.
03Java 中持久化 AgentState 的接口是?Which Java interface persists AgentState?
解析Why AgentStateStore 提供 save、get、exists、delete 和版本化写入。AgentStateStore provides save, get, exists, delete and versioned writes.
04Java 中对话记忆的抽象接口是?What is Java's memory abstraction?
解析Why Memory 负责消息的增删查清空;LongTermMemory 负责跨会话语义记忆。Memory handles message add/delete/query/clear; LongTermMemory handles cross-session semantic recall.
05Java 提供的内存版记忆实现是?Which Java class is the in-memory memory implementation?
解析Why InMemoryMemory 适合测试和单进程;StateBackedMemory 把记忆绑定到状态存储。InMemoryMemory suits tests and single processes; StateBackedMemory binds memory to the state store.
06跨会话的长期记忆接口是?Which interface represents cross-session long-term memory?
解析Why LongTermMemory 提供 record 与 retrieve,需要自行设计写入、召回和遗忘策略。LongTermMemory provides record and retrieve; you design the write, recall and forgetting policies.
07多个实例并发写同一会话时,Java 可能抛出?What may Java throw when multiple instances write the same session concurrently?
解析Why 该异常表示版本冲突,应重试、合并或改用以会话为单位的串行处理。This exception indicates a version conflict; retry, merge or serialize work per session.
08会话隔离键应该由什么组成?What should the session isolation key consist of?
解析Why 只用 user_id 会让同一用户的多个会话互相污染;两者组合才能精确定位。Using only user_id mixes a user's sessions; combining both locates a session precisely.
09Python 中工具执行附带的缓存与上下文位于?Where does Python keep tool-related cache and context?
解析Why ToolContext 提供文件缓存、激活工具组等工具执行期上下文。ToolContext provides file caches, activated tool groups and other tool-execution context.
10水平扩容 Agent 服务的前提是什么?What is required to scale agent services horizontally?
解析Why 无状态进程才能被任意调度;否则请求落到别的实例就会丢失上下文。Only stateless processes can be scheduled arbitrarily; otherwise a request routed elsewhere loses context.