Zongzi.Anchor (zongzi v0.3.0)

Copy Markdown

Intervention 结构在变基时的批量编排。

消费 edit batch(一组 interventions + 编辑后的 Timeline), 对每个 intervention 调其 strategy 的 rebase/4, 按决策分类为 :survived:conflicts

这是纯函数编排——不调用引擎、不比对 snapshot。 语义存活判定属于 Declaration.resolve(渲染时), 本模块只做结构锚的批量判定(编辑后)。

两阶段全景

  1. 编辑后Anchor.rebase_all/4(本模块) 结构锚 rebase → preserve/rebase/relocate/conflict 分类

  2. 渲染时 — 各引擎 renderDeclaration.resolve/2 snapshot 比对 → delta apply / conflict

谁调用

Caller(库外编排者)在 Timeline 写操作落地后调用本模块, 并注入 Anchor.Context(Note 快照等)。zongzi 不实现 Caller。

交互契约

# edit batch ───────────────────────────────────┐
#     │                                         │
#     ▼                                         │
# Timeline 状态落地(insert/split/merge/delete)│
#     │                                         │
#     ▼                                         │
# Anchor.rebase_all(ints, timeline, ctx)  ←── Caller 注入 Context
#     │
#     ├─ survived [Intervention.t()] ──→ 可进 render request
#     │
#     └─ conflicts [{int, reason}]  ──→ 上浮 UI

Summary

Functions

edit batch 对全部 intervention 跑结构 rebase,按决策分类。

Types

decision_label()

@type decision_label() :: :preserve | :rebase | :relocate | :split | :conflict

rebase_result()

@type rebase_result() :: %{
  survived: [Zongzi.Intervention.t()],
  conflicts: [{Zongzi.Intervention.t(), Zongzi.Anchor.Strategy.reason()}],
  decisions: %{optional(term()) => decision_label()}
}

Functions

rebase_all(interventions, timeline, context \\ Zongzi.Anchor.Context.new(), opts \\ [])

edit batch 对全部 intervention 跑结构 rebase,按决策分类。

参数

  • interventions — 需要 rebase 的 interventions(可以为空)
  • timeline编辑后的 Timeline(nodes/tombstones/seq_map 已更新)
  • context — Caller 注入的 Context(notes_by_seqseq_to_window 等)
  • opts:

分类规则

decision去向
{:ok, :preserve}survived(原样)
{:ok, {:rebase, updated}}survived(锚已更新)
{:ok, {:relocate, updated, meta}}survived(锚已重定位;strategy 的 meta 并入 on_rebase meta)
{:conflict, reason}conflicts

返回值

  • :survived — 存活 interventions(含 on_rebase split 出的子干预)
  • :conflicts{intervention, reason} 列表
  • :decisions%{intervention_id => :preserve | :rebase | :relocate | :split | :conflict}, 每条 intervention 的结构决策(split 标在子干预上),供 Caller 做指标/日志

on_rebase 钩子

策略决策成功后,若 intervention 的 declaration 实现了 on_rebase/4, 以 (int, meta, timeline, context) 调用——meta 含 %{decision, old_anchor, new_anchor} (relocate 时并入 strategy 的 meta),context 即本函数的 Caller 注入 Context (declaration 可用 notes_by_seq 等做 payload 的 tick 级维护)。 {:split, children} 的子干预不再过 strategy.rebase——子干预锚的正确性由 declaration 负责。