[RFC] OpenViking Session 上下文优化:Turn-aware Retention、Pending/Failed 恢复与预算控制 #3330
Closed
yeshion23333
started this conversation in
RFC
Replies: 1 comment
|
本方案已经实现 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
RFC:OpenViking Session 上下文优化
关联背景:VikingBot 保留原始 assistant/tool 关系 #3236
要解决的三个问题
commit_count隐藏;failed 原文退出上下文目标
checkpoint_source_message_ids对应的 Assistant/Tool 前缀,不能从全局 overview 截取。不保存模型私有 reasoning,也不保证所有 assistant/tool 历史永久以 raw message 保留。
改动一:优化 retained messages(Turn + Token)
两个原子单元
Tool-only 的
role=usertransport message 不开启新 Turn。选择算法
优先级:
超长 Turn 如何处理
例如:
A1...A14的原文仍保存在 archivemessages.jsonl;checkpoint_summaries[];A15...final保持原始 text/tool/result 关系。最小 LLM 契约
核心原则:代码决定“摘要谁”,LLM 只负责“写摘要”。
输入:只增加范围标记
Phase 1 已经知道
checkpoint_source_message_ids。Phase 2 格式化现有 archive messages 时,在对应的连续 Assistant/Tool Steps 外增加标记:source 原文只出现一次,同时服务于全局 Working Memory 和 checkpoint,不重复拼接。LLM 只看到稳定的序号
index,不接收真实 message ID、anchor ID、archive URI 或动态 token budget。输出:只新增一个字符串数组
{ "...现有 Working Memory 输出...": "保持原协议", "checkpoint_summaries": [ "检查了 OpenViking peers 目录,结果为空。" ] }checkpoint_summaries: list[str]sections更新操作checkpoint_summaries: list[str]数组下标与输入标记对应。多个 partial Turn 或 failed roll-forward 使用多个
index,仍在同一次调用中返回。LLM 不复制任何 provenance 字段。OpenViking 本地补齐映射
OpenViking 本地负责:
index映射回turn_anchor_message_id和source_message_ids;ContextPart,写入 archive metadata;生成约束只保留语义要求:摘要应覆盖执行意图、关键动作、工具结论、错误和未完成状态;不保存模型私有 reasoning。该方案不增加 LLM 调用次数,但 completion tokens 会小幅增加。
Checkpoint 持久化
checkpoint 不写入 root
messages.jsonl,也不新增独立状态文件;作为可选字段合并写入对应 archive 的.meta.json:{ "checkpoints": [ { "turn_anchor_message_id": "msg_user_1", "source_message_ids": ["msg_a1", "msg_tool_1", "msg_a14"], "abstract": "检查了连接池指标,确认等待队列持续增长……", "estimated_tokens": 96 } ] }上述 provenance 和 token 字段全部由 OpenViking 本地生成,不是 LLM 输出。
只有 overview 和所需 checkpoint 都成功落盘后才写
.done。对于启用了 Working Memory 的 partial Turn,checkpoint 生成或解析失败时 Phase 2 失败,archive raw 继续作为 uncovered raw 返回;working_memory=false保持原行为,不生成 checkpoint,也不将 completed archive raw 放回 active。旧 archive 没有
checkpoints字段时继续使用latest_archive_overview,但不再从 overview 合成重复 checkpoint。Checkpoint 对外仍使用现有
ContextPart:{ "role": "assistant", "message_kind": "checkpoint", "parts": [{ "type": "context", "context_type": "memory", "uri": "viking://.../history/archive_003", "abstract": "已确认连接池耗尽,当前正在验证回收配置。" }] }Tool 输出限制必须提升到 Turn 范围
当前单个 Assistant message 内:
消息展开后,10 个 Steps × 10,000 chars 可能全部绕过限制。新逻辑应在完整逻辑 Turn 内聚合计算,再决定哪些 Tool 输出 externalize;原文继续存入
tool-results/。改动二:修复 Archive 状态与消息恢复
状态定义
.done,无.failed.json.done且 overview 可读.failed.json,未被后续 coverage 覆盖.done但 overview 损坏commit_count只保留为统计字段,不再判断 pending/completed boundary。Context 始终按下面的公式组装:
最后按
message.idstable dedup,防止崩溃恢复或并发切换时重复。Pending:为什么不能使用
commit_count当前 Phase 1 时序:
因此
commit_count=2只能说明两个 Phase 1 已接受,不能说明archive_001/002已被 overview 覆盖。两个 rapid commits 的正确行为:
latest_archive_overviewFailed:恢复为逻辑 live
“恢复到 live”指 failed archive 原文重新进入
get_session_context().messages,不需要把文件物理搬回 root:这样可以避免 Phase 2 期间物理 prepend 导致重复、乱序或覆盖新消息。
Failed raw 不能永久留在 active。后续 archive 成功时必须 roll-forward:
建议扩展
.done,记录 coverage:{ "starting_message_id": "msg_001", "ending_message_id": "msg_020", "coverage_start_archive": "archive_001", "coverage_end_archive": "archive_002", "covered_failed_archives": ["archive_001"] }旧
.done没有 coverage 字段时,默认只覆盖自身。结构差异
commit_count+ marker.donecoverage 吸收详细改动
1. Message:增加可选语义字段
旧 Message 没有这些字段时继续推断;推断不确定时扩大原子范围,不能拆 call/result。
2. Commit API:新模式不改变旧参数语义
{ "retention_mode": "turn_budget", "keep_recent_turn_count": 3, "retained_message_token_budget": 12000, "min_raw_tail_steps": 1 }keep_recent_count;3. Session:抽出纯 Retention Planner
输出至少包括:
Planner 不访问 filesystem、不调用 LLM,可以独立单测。
4. Archive:统一状态扫描
新增统一
_scan_archive_states(),供以下逻辑共用:删除
_get_pending_archive_messages()对self._meta.commit_count的正确性依赖,重构为_get_uncovered_archive_messages()。5. Phase 1 / Phase 2
checkpoint_summaries[];本地补齐 ID/预算,全部落盘后最后写.doneoverview + uncovered raw + root;stable dedup;读取 archive.meta.json.checkpoints,在仍 live 的 User anchor 后插入对应 checkpointPhase 2 写
overview_tokens和checkpoints时必须 merge archive.meta.json,不能覆盖 Phase 1 保存的 retention plan。checkpoint request 应覆盖当前 archive 以及本次 roll-forward 吸收的 failed partial archives。必须满足的不变量
.done必须最后写;只有.done + readable overview才表示 raw 已覆盖。.failed.json是终态,但不是“消息已压缩”的证明。assistant text + tool calls + results永远不跨 retained/archive 边界拆分。source_message_ids必须完全属于被替代的 Assistant/Tool 前缀。checkpoint_summaries[]。测试与验收
User + Turn-local checkpoint + raw tail,Step 不拆分;checkpoint 只总结 source messagescommit_count无关.donecoverage 吸收 failed rawcheckpoint_summaries[];不新增专用调用完成标准:
latest_archive_overview截取,且能由本地映射按 source IDs 溯源;落地顺序
checkpoint_summaries[],本地完成映射和预算控制。待确认
turn_id/message_kind进入公共 Message schema,还是先放 optional metadata?tool-results/,还是提供通用 message result store?strict_budget?All reactions