MusePi

会话树计划

English 中文

参考:session.md

本文档描述当前会话树导航的实现方式:内存中的 tree model、leaf movement rules、branching behavior,以及 extension/event integration。

子系统定位

会话以 append-only entry log 持久化,但运行时行为是 tree-based:

关键文件:

SessionManager 中的 tree data model

Runtime indices 存放在 SessionEntryIndex helper 中,作为 SessionManager 上的 #index 持有,并与 journal array #entries 保持同步:

Tree APIs:

getTree() 是 runtime projection;持久化仍是 append-only JSONL entries。

Leaf movement semantics

共有三种 leaf movement primitives:

  1. branch(entryId)
    • 校验 entry 存在
    • 设置 leafId = entryId
    • 不写入新 entry
  2. resetLeaf()
    • 设置 leafId = null
    • 下一次 append 会创建新的 root entry(parentId = null
  3. branchWithSummary(branchFromId, summary, details?, fromExtension?)
    • 接受 branchFromId: string | null
    • 设置 leafId = branchFromId
    • 追加一个 branch_summary entry 作为该 leaf 的子节点
    • branchFromIdnull 时,fromId 被持久化为 "root"

/tree navigation behavior(同一 session file)

AgentSession.navigateTree() 是 navigation,不是 file forking。

流程:

  1. 校验 target 并计算 abandoned path(collectEntriesForBranchSummary
  2. 携带 TreePreparation emit session_before_tree
  3. 可选总结 abandoned entries(hook-provided summary 或 built-in summarizer)
  4. 计算新 leaf target:
    • 选中 user message:leaf 移到其 parent,message text 返回用于 editor prefill
    • 选中 custom_message:与 user message 同规则(leaf = parent,text prefills editor)
    • 选中其他 entry:leaf = 选中 entry id
  5. 应用 leaf move:
    • 带 summary:branchWithSummary(newLeafId, ...)
    • 不带 summary 且 newLeafId === nullresetLeaf()
    • 其他情况:branch(newLeafId)
  6. 从新 leaf 重建 agent context 并 emit session_tree

重要:summary entries 附加在新导航位置,不在 abandoned branch tail。

/branch behavior(新 session file)

/branch/tree 有意不同:

用户可见 /branch 流程(SelectorController.showUserMessageSelectorAgentSession.branch):

SessionManager.createBranchedSession(leafId) 细节:

Context reconstruction 与 summary/custom 集成

buildSessionContext()(在 session-context.ts 中,通过 SessionManager.buildSessionContext() 暴露)解析活跃 root→leaf path 并构建有效 LLM context state:

session/messages.ts 随后把这些消息类型映射为 model input:

因此 tree movement 通过改变活跃 leaf path 来改变 context,而不是通过修改旧 entries。

Labels 与 tree UI behavior

Label persistence:

Tree selector behavior(tree-selector.ts):

Command routing:

Extension 和 hook touchpoints

Command-time extension API(ExtensionCommandContext):

Tree navigation 相关 events:

相邻但相关的 lifecycle hooks:

真实约束与边界条件

Plan approval session naming

当用户从 plan mode(InteractiveMode.#approvePlan)批准 plan 时,approval handler 会用 plan 的 title 作为 session name 的 seed,使生成的(fresh 或 compacted)session 不会保持 unnamed。

Trigger:

Naming source:

humanizePlanTitle 示例:

Legacy compatibility still present

Session migrations 在 load 时仍会运行:

迁移后的当前 runtime behavior 是 version-3 tree semantics。