---
title: "观察"
---

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

# 观察

## 视角

> 特性：K-017

**view 是一个角色能看到的关系集合。**

```koine
view customer = { status, paid, refunded }

view finance  = { paid, refunded, charged_back }

view risk     = { charged_back }
```

规则：

- 花括号里只能是 relation 或 state relation，不能是 transition。
- 关系必须存在。
- 空 view 合法。
- view 名字在独立命名空间。

view 只做列级可见性：

- 不改变世界。
- 不做行级过滤。
- 不做计算、聚合、重命名。
- 不在 view 里的关系，查询结果里不出现。

使用：

```koine
observe(world, view customer)
```

命令行用法见 [CLI](https://koine.systems/reference/cli/)。

## 路径与差异

> 特性：K-018, K-019

### 路径

> 特性：K-018

**路径是从一个世界出发，按顺序走的一串 transition。**

```text
path = { start: world, steps: [(transition, binding), ...] }
```

用途：回放、分享、比较。路径不存中间世界，按需重建。查询可以带前缀参数：`path(start, steps, k)`。

路径是压缩的快照序列；确定性回放可以重建中间世界。历史是路径在目标/关系上的投影；拒绝/重试等运行时事件由 trace 记录，不产生新世界。见 [ADR-0015](https://koine.systems/notes/decisions/0015-history-as-snapshot-sequence/)。

### 差异

> 特性：K-019

**差异是两个世界之间，哪些元组被加了、哪些被删了。**

```text
diff = { added: [(relation, tuple), ...], removed: [(relation, tuple), ...] }
```

单位是元组。查询可以带视角参数：`diff(a, b, view)`。

## 观察接口

只有两个原语：世界、transition。其余都是查询。

节点是具体世界，不是世界集合。世界集合是 `find` / `explore` 的结果，不是图上的点。

```text
World:
  id          -> 内容哈希
  time        -> Timestamp
  relations   -> 每个状态关系的完整元组集合

Edge:
  source      -> World
  target      -> World
  transition  -> 规则名 + 源位置
  binding     -> 参数绑定
  delta       -> { added, removed, attrs }

查询:
  successors(world)        -> 边
  predecessors(world)      -> 边
  path(start, steps)       -> 世界序列
  diff(world_a, world_b)   -> 增删集
```

接口只回答"有什么"，不回答"怎么存"。存储可以增量，接口必须给完整世界。

### find 与 explore

> 特性：K-020, K-021

`find` 回答“是否存在一个世界或路径满足条件”，属于描述层。`explore` 回答“从起点实际能走到哪些世界”，属于驱动层。两者的失败模式和成本不同，不合并。

### explore 的查询边界

> 特性：K-021

`explore(start, bounds)` 从 start（state 或 `init`）出发，BFS 遍历可达世界：

- 后继 = 全部 transition 的合法绑定。绑定参数域来自当前世界：entity 取实体集合，枚举取全部成员，Bool/Rounding 取固定域，其他值类型取 base relations 的活动域。
- 每个目标世界必须通过 require、total key 与全部 axiom；被拒绝的绑定不产生边。
- 世界身份由内容决定（canonical JSON）；重复世界只访问一次，边仍记录。
- stutter：没有可用 transition 时世界不变（自环，不产生新世界）；默认隐藏，`--include-stutter` 显示。stutter 不是无限循环。
- 边界：`--depth`（默认 10）、`--max-worlds`（默认 1000）、`--timeout`（默认无）。
- 命中边界（含参数域无法枚举）时 `complete=false` 并给 `reason`（`depth` | `max-worlds` | `timeout` | `unknown`），不假装完整。
- 输出按 world id、edge (from, to, transition) 稳定排序。

### find 的查询边界

> 特性：K-020

`find(start, where, bounds)` 在 explore 的可达世界集合里按 BFS 序找第一个满足 `where` 的世界：

- `where` 是当前世界的 Bool 表达式，无自由变量；可用 relation、define、aggregate、量词与 `now`。
- 找到：`found` + 路径（start 到目标世界的一串 transition/binding）+ 世界快照。
- 完整搜索无命中：`not-found`。
- 边界耗尽（depth / max-worlds / timeout）或后继无法枚举：`unknown` + `reason`，绝不写 `not-found`。
- 谓词求值失败（非 Bool、求值失败）不是 false：报诊断、查询失败。
- find 是描述层查询，不改变世界。

三态语义见 [ADR-0028](https://koine.systems/notes/decisions/0028-find-is-bounded-descriptive-query/)。

Source: https://koine.systems/reference/spec/observations/index.mdx
