---
title: "6. 探索"
---

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

# 6. 探索

不指定路径，让工具枚举所有可达世界：`explore` 画可达空间，`find` 找第一个满足条件的世界和路径。

## 一个退款场景

```koine part
// 正常退款：下单 -> 支付 -> 退款。
scenario refund_flow {
  start s0
  do place(o1, c1)
  do pay(o1)
  do refund(o1, c1)
}
```

## explore：有界可达空间

```sh
koine explore model.koi --start s0 --depth 6 --max-worlds 1000
```

从 s0 出发，在 depth 和 max-worlds 内枚举每一步所有可能的 transition 绑定（特性 [K-021](https://koine.systems/reference/features/)）。输出示意：

```json
{
  "start": "s0",
  "complete": true,
  "bounds": {"depth": 6, "max_worlds": 1000, "timeout_ms": null},
  "worlds": [
    {"id": "…", "relations": {"status": [["o1", "Draft"]]}, "time": "2024-01-01T00:00:00Z"}
  ],
  "edges": [
    {"from": "…", "to": "…", "transition": "place", "binding": {"order": "o1", "actor": "c1"}}
  ]
}
```

world 的 `id` 和 edge 的 `from` / `to` 是完整世界的 JSON 编码；`complete: true` 表示在边界内遍历完。

## find：条件 + 路径

```sh
koine find model.koi --start s0 --where 'status(o1) == Refunded' --depth 6
```

`--where` 是表达式，在到达的世界里求值。结果三态（特性 [K-020](https://koine.systems/reference/features/)）：

- `found`：找到第一个满足的世界和到达路径。
- `not-found`：在边界内遍历完，没有满足的世界。
- `unknown`：触到 depth / max-worlds 上限，没找完。

输出示意：

```json
{
  "status": "found",
  "where": "status(o1) == Refunded",
  "path": [
    {"transition": "place", "binding": {"order": "o1", "actor": "c1"}},
    {"transition": "pay", "binding": {"order": "o1"}},
    {"transition": "refund", "binding": {"order": "o1", "actor": "c1"}}
  ]
}
```

把 `--where` 改成 `status(o1) == ChargedBack`，结果是 `not-found`：这个模型根本没有拒付。

## 边界

- 探索总是有界的；没有边界参数的“全空间”查询不存在（[ADR-0028](https://koine.systems/notes/decisions/0028-find-is-bounded-descriptive-query/)）。
- 触到 depth / max-worlds 上限时 `complete: false`；边界外的部分未知，不能当成“不存在”。
- `wait` 这类只改时间的 transition 会产生大量同构世界；`--include-stutter` 默认关闭这类步骤。
- 模型里的 `Duration`、`Decimal` 等连续值不参与枚举；世界按离散事实区分。

## 本章完整模型

```koine full
// 教程模型：一张订单从草稿走到支付，可以退款。
module orders {
  // 订单有身份：两张一样的订单也是两张。
  entity Order
  // 客户有身份。
  entity Customer
}

// 订单状态：四种取值，没有身份。
value OrderStatus = { Draft, Placed, Paid, Refunded }

// 订单归属客户：o1 由 c1 下单。
relation placed_by(order: Order, customer: Customer)

// 订单当前状态：每个订单恰好一行。
state relation status(order: Order, value: OrderStatus) {
  key total (order)
}

// 下单时间：草稿订单还没有这一行。
state relation placed_at(order: Order, value: Timestamp) {
  key partial (order)
}

// 支付时间：可能还没有。
state relation paid_at(order: Order, value: Timestamp) {
  key partial (order)
}

// 退款时间：可能还没有。
state relation refunded_at(order: Order, value: Timestamp) {
  key partial (order)
}

// 曾经下单 = 存在下单时间。
derived relation placed(order: Order) {
  exists (t: Timestamp). placed_at(order, t)
}

// 曾经支付 = 存在支付时间。
derived relation paid(order: Order) {
  exists (t: Timestamp). paid_at(order, t)
}

// 曾经退款 = 存在退款时间。
derived relation refunded(order: Order) {
  exists (t: Timestamp). refunded_at(order, t)
}

// 退款资格：已支付、30 天窗口内、未退款。
define refundable(order: Order) -> Bool {
  status(order) == Paid
  and exists (t: Timestamp). paid_at(order, t) and now() - t <= 30 days
  and not refunded(order)
}

// 下单：只有本人草稿订单可以提交。
transition place(order: Order, actor: Customer) {
  require placed_by(order, actor)
  require status(order) == Draft
  status(order, Placed)
  placed_at(order, now())
}

// 支付：已下单订单支付成功。
transition pay(order: Order) {
  require status(order) == Placed
  status(order, Paid)
  paid_at(order, now())
}

// 退款：本人订单在窗口内退款。
transition refund(order: Order, actor: Customer) {
  require placed_by(order, actor)
  require refundable(order)
  status(order, Refunded)
  refunded_at(order, now())
}

// 推进世界时间：退款窗口、截止时间等规则都靠它。
transition wait(d: Duration) {
  time = now() + d
}

// 世界约束：退款必须支付过。
axiom RefundRequiresPayment {
  forall (o: Order). refunded(o) implies paid(o)
}

// 客户视角：状态和支付退款事实。
view customer = { status, paid, refunded }

// 财务视角：只看时间戳。
view finance = { placed_at, paid_at, refunded_at }

// 起点世界：世界时间、实体和事实都写清楚。
state s0 {
  time 2024-01-01T00:00:00Z
  c1: Customer
  o1: Order
  placed_by(o1, c1)
  status(o1, Draft)
}

// 默认从 s0 出发。
init s0

// 主路径：下单 -> 支付。
scenario happy_path {
  start s0
  do place(o1, c1)
  do pay(o1)
}

// 31 天后退款：窗口已过，这步被拒绝。
scenario late_refund {
  start s0
  do place(o1, c1)
  do pay(o1)
  do wait(31 days)
  do refund(o1, c1)
}

// 正常退款：下单 -> 支付 -> 退款。
scenario refund_flow {
  start s0
  do place(o1, c1)
  do pay(o1)
  do refund(o1, c1)
}
```

## 下一步

- 完整业务模型：[示例](https://koine.systems/examples/)，从订阅市场开始。
- 构造的准确定义：[语言规范](https://koine.systems/reference/spec/)。
- 设计理由：[设计笔记](https://koine.systems/notes/)。

Source: https://koine.systems/learn/explore/index.mdx
