---
title: "把 market 的 paid / refunded / charged_back 改为派生关系"
---

> Documentation Index
> Fetch the complete documentation index at: https://koine.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# 把 market 的 paid / refunded / charged_back 改为派生关系

## 背景

`market` 示例里 `paid` / `refunded` / `charged_back` 三个一元历史标志，与对应的事件时间关系 `paid_at` / `refunded_at` / `charged_back_at` 成对出现：

- `pay` 总是同时写 `paid(order)` 与 `paid_at(order, now())`；`refund`、`chargeback` 同理。
- 迁移前，三个历史标志是 base state relation，但按 [K-026](https://koine.systems/reference/spec/derived/) 的语义它们是可派生事实：`paid(o) ⇔ ∃t. paid_at(o, t)`（其余两个同理）。
- [关系分析报告](https://koine.systems/notes/relation-analysis-report/) 3.1 / 3.6 / 3.7 在 s0 可达世界内把三者判为等价与冗余（holds-in-scope），并指出消除方向已由 [ADR-0016](https://koine.systems/notes/decisions/0016-derived-relations-are-virtual/) 给出，但模型未改。
- [ADR-0016](https://koine.systems/notes/decisions/0016-derived-relations-are-virtual/) 明确 derived relation 是 virtual、只读、可进 view/diff；[ADR-0019](https://koine.systems/notes/decisions/0019-derived-relation-key/) 说明一元 relation 是成员测试。迁移前继续把三者存为 base，等于为“曾经发生过”重复存一份可由 `*_at` 推出的数据，还要维护 transition 双写与正向 axiom 的一致性。

问题：是否把 `paid` / `refunded` / `charged_back` 改为 derived relation。

## 选项

- **保持 base**：三者继续作为 base state relation，由 transition 显式写入，与 `*_at` 并行维护。最直接，但保留双写与冗余。
- **改为 virtual derived**：把三者改成 `derived relation`，公式为 `exists (t: Timestamp). *_at(order, t)`；transition 不再写它们，只写 `*_at`。符合 ADR-0016 的方向。
- **materialized derived**：派生扩展缓存进存储。消除重复语义，但引入缓存失效与双写一致性成本；规范只定义 virtual，物化是实现选择（见[运行时边界](https://koine.systems/reference/spec/derived/#运行时边界)）。

## 决定

决定改为 **virtual derived relation**：

```koine
derived relation paid(order: Order) {
  exists (t: Timestamp). paid_at(order, t)
}

derived relation refunded(order: Order) {
  exists (t: Timestamp). refunded_at(order, t)
}

derived relation charged_back(order: Order) {
  exists (t: Timestamp). charged_back_at(order, t)
}
```

- 删除三个 base `state relation` 声明，改成上述 derived 声明。
- `pay` / `refund` / `chargeback` 不再写 `paid(order)` / `refunded(order)` / `charged_back(order)`，只保留对应 `*_at` 与 `status`、`invoice_status` 等 base 写入。
- 不引入 materialized；物化留作实现优化。
- 本 ADR 已接受，按“迁移顺序与回退”一节执行模型与测试改动。

## 理由

- 单一事实来源：`*_at` 是事件时间的唯一 base 记录，历史标志是它的投影，不重复存储。
- 与 ADR-0016 / ADR-0019 一致：virtual、只读、可进 view/diff，消除“为了 view/diff 而存 base”的重复。
- axiom / define / require 都允许引用 derived relation（见[变化](https://koine.systems/reference/spec/transitions/)与[定义](https://koine.systems/reference/spec/definitions/)），现有引用无需改写。
- 可观察性不损失：view 与查询层 diff 仍能看到三者；只有 base world 与 trace diff 不再出现它们，这正是 virtual derived 的语义。
- 世界身份不变：derived 不进入 canonical JSON，可达世界集合与边数不变。

## 后果

### transition 写入

`pay` / `refund` / `chargeback` 各删一行 base 写入：

- `pay` 删 `paid(order)`；保留 `paid_at(order, now())`。
- `refund` 删 `refunded(order)`；保留 `refunded_at(order, now())`。
- `chargeback` 删 `charged_back(order)`；保留 `charged_back_at(order, now())`。

transition 不能写 derived（`koine.derive.not-writable`），这三行必须删，否则 `check` 报错。

### view 与 diff

- `paid` / `refunded` / `charged_back` 不再出现在 `world.relations`、`explore` 的世界输出、`run` 的最终世界和每步 trace diff；只保留 base `*_at`。
- 三者仍可进 view：`customer` / `finance` / `risk` 里的引用不变，`observe` 仍按扩展输出。
- 查询层 diff（`diff` 命令、`compare`）仍按扩展计算三者；每步 trace diff 只记 base 增量。见[观察规范](https://koine.systems/reference/spec/observations/)与[派生关系规范](https://koine.systems/reference/spec/derived/#运行时边界)。

### axiom / define / require

- 六个 axiom 不变：`RefundRequiresPayment`、`ChargebackRequiresPayment`、`NoDoubleRefund`、`PaidStatusRequiresPayment`、`RefundedStatusRequiresRefund`、`ChargedBackStatusRequiresChargeback` 继续成立——axiom 可引用 derived relation，且派生扩展与旧 base 标志在 s0 可达世界内等价。
- 关键链条仍闭合：`refund` 要求 `refundable` → `status == Paid` → axiom `PaidStatusRequiresPayment` → `paid`（即 `∃t. paid_at`）；`refundable` 自身也直接要求 `paid_at`。`NoDoubleRefund` 文本不变；因为 `refunded` / `charged_back` 改为派生，其扩展等价于对应 `*_at` 存在，语义上等于两个事件时间关系不能共存。
- `define refundable` 与 `require refundable(order)` 不改；其中 `not refunded(order)` / `not charged_back(order)` 展开为“不存在对应 `*_at` 行”，求值结果一致。
- `PaidStatusRequiresPayment` 在派生后更关键：它把 `status = Paid` 与 `paid_at` 绑定，防止出现“状态已付但无支付时间”的世界。

### 测试与场景

- 四个 scenario（`refund_flow` / `late_refund` / `chargeback_flow` / `subscribe_flow`）不直接引用这三个标志，步骤与接受性不变；`state s0` / `init` 不列这三个标志，也无需改。
- 最终世界输出变化：`final.relations` 不再有 `paid` / `refunded` / `charged_back`，只有 `paid_at` / `refunded_at` / `charged_back_at` 等 base 关系；`refund_flow` 的 `status(o1, Refunded)`、`chargeback_flow` 的 `status(o1, ChargedBack)` 不变。
- 依赖 base world / trace diff 中出现这三个标志的断言（若有）需要改为断言 `*_at`，或改到 `observe --view` / 查询层 `diff`。
- `explore` 可达世界 12 / 边 16 不变（world id 由 base canonical JSON 决定，derived 不参与）。

### 迁移顺序与回退

迁移：

1. 把 `market` 中三个 base `state relation` 改为 `derived relation`（同名替换，避免重名与 `koine.derive.not-writable`）。
2. 同步删掉 `pay` / `refund` / `chargeback` 里对三者的写入；第 1、2 步必须一起落地，半迁移状态会被 `check` 拒绝。
3. 运行 `mise run check`（必要时加 `impl:check`），确认 axiom 与场景通过、世界/边数不变。
4. 更新依赖 `world.relations` / trace diff 中出现三者的测试与快照。

回退条件：出现以下任一情况即回退为 base，并保留本 ADR 记录：

- 派生扩展与旧 base 标志在可达世界内不再等价（例如新增直接写 `*_at` 却不写 `status` 的 transition，或新增“支付但不记录时间”的路径）。
- 查询层 diff / view 出现不可接受的回归，且 virtual 无法满足（此时评估 materialized，另开 ADR）。

## 结论

按“改为 virtual derived”执行：改 `market` 模型、同步更新测试断言，并运行 `mise run check` 与相关实现测试。已按上节迁移顺序落地。

不在本 ADR 范围：`settled_amount` / `applied_rate` 的固化，以及 `status Refunded ≡ refunded` 这类过渡性等价（关系分析报告 3.1 / 3.8），留待后续。

## 相关

- [ADR-0016：派生关系是只读虚拟关系](https://koine.systems/notes/decisions/0016-derived-relations-are-virtual/)
- [ADR-0019：派生关系可以声明 key](https://koine.systems/notes/decisions/0019-derived-relation-key/)
- [ADR-0015：历史用快照序列和路径表达](https://koine.systems/notes/decisions/0015-history-as-snapshot-sequence/)
- [派生关系规范](https://koine.systems/reference/spec/derived/)
- [观察规范](https://koine.systems/reference/spec/observations/)
- [变化](https://koine.systems/reference/spec/transitions/)
- [市场模型关系分析报告](https://koine.systems/notes/relation-analysis-report/)
- [市场示例](https://koine.systems/examples/market/)

Source: https://koine.systems/notes/decisions/0029-payment-flags-derived/index.mdx
