跳转至

上下文即环境:面向长程智能体的程序化上下文管理

文章背景与核心概要

随着大语言模型(LLM)智能体处理的任务日益复杂且周期变长,交互历史往往会超出模型的标准上下文窗口。传统方法通常依赖于压缩历史记录或将信息提取为静态记忆,但这要求系统在预知未来需求之前就做出“保留哪些信息”的决策,存在明显的局限性。

本文提出了 Scroll,这是一种创新的上下文管理器,它将智能体的每个会话视为一个可执行的“会话环境”。Scroll 通过结合不可变的事件日志与持久化的 Python 内核,允许模型通过生成代码来动态搜索、转换和管理状态。这种方法将上下文管理从简单的文本拼接转化为程序化任务,显著提升了智能体在长程任务中的表现。


📌 摘要

随着大语言模型(LLM)智能体处理的任务日益复杂且周期变长,交互历史往往会超出模型的标准上下文窗口。传统方法通常试图通过压缩历史交互或将选择性信息提取为静态记忆表示来解决这一问题——这迫使系统在真正了解未来需求之前,就必须预先决定需要保留哪些信息。

As LLM agents tackle increasingly complex, long-running tasks, their interaction histories frequently exceed standard model context windows. Traditional approaches attempt to solve this by compressing historical interactions or extracting selective information into static memory representations—forcing a commitment to what needs to be preserved before future requirements are actually known.

为了克服这一局限性,作者引入了 Scroll,这是一种新颖的上下文管理器,它将每个智能体任务会话视为一个可执行的 会话环境 (Session Environment)

To overcome this limitation, the authors introduce Scroll, a novel context manager that treats each agent session as an executable Session Environment.

Scroll 的核心创新:

  • 仅追加事件日志与 Python 内核: 会话环境将不可变的、无损的事件日志与沙盒化的持久 Python 内核配对。
  • 类型化命名空间与变量绑定: 工具输出、检索到的历史记录和派生状态被绑定到跨模型调用的类型化命名空间中的变量,而不是每次都盲目地序列化到提示词中。
  • 程序化状态操作: 模型使用生成的代码通过 exec 来搜索、具体化和转换会话状态。只有显式打印的投影才会进入模型的工作视图以供后续调用,从而有效地将上下文管理转化为利用 LLM 编码能力的编程任务。
  • 基于精确地标的驱逐机制: 当工作视图接近上下文预算时,陈旧的片段会被驱逐,但仍然完全可恢复。驱逐索引维护了直接绑定到精确事件日志地址的紧凑地标,使智能体能够精确导航到被驱逐的区域,而无需进行全日志搜索。

Key Innovations of Scroll:

  • Append-Only Event Log & Python Kernel: The session environment pairs an immutable, lossless Event Log with a sandboxed, persistent Python kernel.
  • Typed Namespace & Variable Binding: Tool outputs, retrieved history, and derived states are bound to variables in a typed namespace across model calls, rather than blindly serialized into the prompt every time.
  • Programmatic State Manipulation: Models use generated code to search, materialize, and transform session state via exec. Only explicitly printed projections enter the model’s working view for subsequent calls, effectively turning context management into a programming task that leverages the LLM's coding abilities.
  • Eviction with Exact Landmarks: As the working view approaches its context budget, stale spans are evicted but remain fully recoverable. An eviction index maintains compact landmarks tied directly to exact Event Log addresses, allowing the agent to navigate precisely to evicted regions without resorting to full-log searches.

📊 性能亮点

Qwen3.8-Max 作为骨干模型,Scroll 在主要的长期任务基准测试中取得了最先进的成果:

Powered by Qwen3.8-Max as the backbone, Scroll achieves state-of-the-art results across major long-horizon benchmarks:

  • LongMemEval_S: 94.8%
  • BEAM_10M: 73.1% (超过了之前发布的最佳记忆系统 5.1 个百分点)
  • LOCA_256K: 86.7% (超过了之前发布的最佳长程智能体 37.4 个百分点)
  • LongMemEval_S: 94.8%
  • BEAM_10M: 73.1% (surpassing the best previously published memory system by 5.1 points)
  • LOCA_256K: 86.7% (surpassing the best previously published long-horizon agent by 37.4 points)

🔗 链接与资源