让大模型像编程一样管理长期任务上下文,实现超长记忆与高效恢复。
Context as an Environment: Programmatic Context Management for Long-Horizon Agents

- 将上下文视为可执行的环境,用事件日志和沙箱内核保存状态变量。
- 在多个长序列任务测试中表现优异,最高超越现有最佳系统37.4分。
- 支持失活内容可恢复,通过索引直接跳转,不依赖全量搜索。
大型语言模型代理越来越多地承担长期运行的任务,其历史记录远超单次模型上下文窗口。现有方法要么压缩早期交互,要么提取信息到固定记忆表征,但需预先决定保留什么,而未来需求未知。本文提出 Scroll,一种将每个代理会话视为可执行的会话环境的上下文管理器。该环境基于追加只读的事件日志和沙箱化的持久化 Python 内核,内核在模型调用间维持类型化的命名空间,使工具输出、检索的历史和推导状态绑定为变量而非每次序列化进提示词。模型编写的代码通过 exec 搜索、生成并转换会话状态,仅显式打印的投影进入下一次调用的模型工作视图。上下文管理因此成为继承大模型日益增强的编码能力的编程任务,事件日志则保存无损的历史真实数据。当工作视图接近预算时,过期片段被逐出但仍可恢复:逐出索引保留与事件日志精确地址关联的紧凑地标,使代理可直接导航至被逐出区域,无需遍历完整日志。以 Qwen3.8-Max 为骨干,Scroll 在 LongMemEval_S 上达 94.8%,在 BEAM_10M 上达 73.1%(超越此前最优 5.1 分),在 LOCA_256K 上达 86.7%(超越此前最优 37.4 分)。
原文摘要 · Abstract (English)
LLM agents increasingly take on long-running tasks whose history grows far beyond a single model context window. Existing approaches compress earlier interactions or extract selected information into fixed memory representations, committing to what to preserve before future needs are known. We present Scroll, a context manager that treats each agent session as an executable Session Environment. The environment is backed by an append-only Event Log and a sandboxed, persistent Python kernel. The kernel maintains a typed namespace across model calls, allowing tool outputs, retrieved history, and derived state to be bound to variables rather than serialized into the prompt at each call. Model-written code searches, materializes, and transforms session state through exec; only explicitly printed projections enter the model's working view for the next call. Context management thus becomes a programming task that inherits the improving coding abilities of LLMs, while the Event Log preserves lossless historical ground truth. As the working view approaches its budget, stale spans are evicted but remain recoverable: an eviction index keeps compact landmarks tied to exact Event Log addresses, so that the agent navigates directly to evicted regions instead of searching the full log. With Qwen3.8-Max as the backbone, Scroll achieves 94.8% on LongMemEval_S; 73.1% on BEAM_10M, surpassing the best published memory system by 5.1 points; and 86.7% on LOCA_256K, exceeding the best published long-horizon agent by 37.4 points.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。