---
title: "CLI"
---

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

# CLI

## 命令

```sh
koine check model.koi [more.koi ...] [--strict-docs]
koine parse model.koi
koine doccheck model.koi [--strict]
koine graph model.koi refund_flow [--depth N] [--max-worlds N]
koine run model.koi refund_flow
koine history model.koi refund_flow
koine observe model.koi refund_flow --at 3 --view customer
koine diff model.koi refund_flow --from 0 --to 3
koine diff model.koi refund_flow --from 0 --to 3 --view finance
koine explore model.koi --start s0 [--depth 10] [--max-worlds 1000] [--timeout ms] [--include-stutter]
koine find model.koi --start s0 --where 'status(o1) == Refunded' [--depth 10] [--max-worlds 1000] [--timeout ms]
koine compare model.koi refund_flow chargeback_flow
```

| 命令 | 输出 |
| --- | --- |
| `check` | 诊断数组（JSON）；有 error 时退出码非 0；多个文件合并为一个平面模型检查 |
| `parse` | AST 调试输出 |
| `doccheck` | 文档注释 lint 诊断数组（JSON）；默认迁移期（缺 `@gloss` 是 warning），`--strict` 升为 error |
| `graph` | 场景 run 路径的世界图（JSON，带版本号）；覆盖范围内有拒绝步时退出码 1 |
| `run` | 场景步骤 trace + 最终世界（JSON）；任一步被拒绝退出码 1 |
| `history` | 场景步骤时间轴（JSON，与 run 同构；可加 final） |
| `observe` | 第 `--at` 步世界按 `--view` 过滤（JSON） |
| `diff` | 第 `--from` / `--to` 步两个世界的增删集（JSON），可选 `--view` |
| `explore` | 从 state / `init` 出发的有界可达世界/边集合（JSON） |
| `find` | 第一个满足 `--where` 的世界与路径：`found` / `not-found` / `unknown`（JSON） |
| `compare` | 两个场景的 trace、最终世界与差异（JSON）；任一场景失败输出诊断、退出码 1 |

JSON 结果一律到 stdout；人类可读错误到 stderr。检查/运行失败仍输出 JSON 诊断对象到 stdout，退出码非 0。JSON 的 key 与元组按字典序稳定排序。

## `run` 输出

```json
{
  "scenario": "refund_flow",
  "steps": [
    {
      "index": 0,
      "transition": "place",
      "binding": {"order": "o1", "actor": "c1"},
      "time": "2024-01-01T00:00:00Z",
      "accepted": true,
      "diff": {
        "added": [{"relation": "status", "tuple": ["o1", "Placed"]}],
        "removed": [{"relation": "status", "tuple": ["o1", "Draft"]}],
        "attrs": []
      },
      "diagnostics": []
    }
  ],
  "final": {
    "time": "2024-01-01T00:00:00Z",
    "entities": {"Order": ["o1"]},
    "relations": {"status": [["o1", "Placed"]]}
  },
  "diagnostics": []
}
```

- `accepted` 步骤的 `time` 是目标世界时间；被拒绝步骤的 `time` 是当前世界时间，`diff` 为 `null`，`diagnostics` 含底层原因（如 `koine.eval.division-by-zero`、`koine.axiom.violated`）。
- 被拒绝的步骤不产生新世界，回放即停；顶层 `diagnostics` 含 `koine.run.step-rejected`（带步骤 index、transition、绑定与原因）。
- 拒绝时退出码 1；`final` 是最后一个被接受的世界。
- 如果场景开始前就失败（例如 init/start 世界违反 axiom），输出 `scenario`、`steps: []`、`final: null` 和 `diagnostics`，退出码 1。

## `observe` 输出

```json
{
  "view": "customer",
  "time": "2024-01-01T00:00:00Z",
  "relations": {
    "status": [["o1", "Placed"]],
    "paid": [],
    "refunded": []
  }
}
```

view 里的关系都会出现（没有行的关系是空数组）；不在 view 里的关系不出现。

## `diff` 输出

```json
{
  "added": [{"relation": "status", "tuple": ["o1", "Placed"]}],
  "removed": [{"relation": "status", "tuple": ["o1", "Draft"]}],
  "attrs": [{"name": "time", "from": "2024-01-01T00:00:00Z", "to": "2024-02-01T00:00:00Z"}]
}
```

- `added` / `removed` 的单位是元组；同 key 替换表现为一删一增。
- `attrs` 只含变化的 world 属性（当前只有 `time`）。
- `--from` / `--to` 是路径 step 下标（0 = 起点，steps 数 = 终点）；重建途中某步被拒绝则报错。
- 查询层 diff 包含 derived relation（按扩展计算）；scenario run 的每步 trace diff 只记录 base 增量（见[派生关系](https://koine.systems/reference/spec/derived/#运行时边界)）。

## `explore` 输出

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

- `--start` 是 state 名或 `init`；`--depth` 默认 10，`--max-worlds` 默认 1000，`--timeout` 默认无（单位 ms）。
- 世界身份 `id` 是内容决定的 canonical JSON；重复世界只出现一次；worlds 按 id、edges 按 (from, to, transition) 稳定排序。
- 命中边界或参数域无法枚举时 `complete: false`，`reason` 为 `depth` / `max-worlds` / `timeout` / `unknown`。
- 没有可用 transition 的世界有隐式 stutter 自环（世界不变）；默认隐藏，`--include-stutter` 显示为 `transition: "stutter"`、空 binding；stutter 不产生新世界。
- `worlds[].relations` 只有 base relation；derived relation 不出现在这里。

## `find` 输出

```json
{
  "status": "found",
  "start": "s0",
  "where": "status(o1) == Refunded",
  "path": [{"transition": "place", "binding": {"order": "o1"}}],
  "world": {"id": "{...}", "time": "...", "relations": {}},
  "bounds": {"depth": 10, "max-worlds": 1000, "timeout_ms": null},
  "reason": null
}
```

- `where` 是当前世界的 Bool 表达式（无自由变量；可用 relation/define/aggregate/量词/now），复用解析器（`koine::parse_expression`）。
- 找到：`found` + 路径（BFS 序最短）+ 世界快照；完整搜索无命中：`not-found`（path 空、world null）。
- 边界耗尽或后继无法枚举：`unknown` + `reason`（`depth` / `max-worlds` / `timeout` / `unknown`），绝不写 `not-found`。
- 谓词求值失败（非 Bool、求值失败）报诊断、退出码 1；find 不改变世界。

## `history` 输出

与 `run` 同构（`scenario` / `steps` / `final` / `diagnostics`）：每一步是时间轴上的一个事件（index、transition、binding、time、accepted、diff）。任一步被拒绝退出码 1。

## `compare` 输出

```json
{
  "scenarios": [
    {"scenario": "refund_flow", "steps": [...], "final": {...}},
    {"scenario": "chargeback_flow", "steps": [...], "final": {...}}
  ],
  "final": {"refund_flow": {...}, "chargeback_flow": {...}},
  "diff": {"added": [...], "removed": [...], "attrs": [...]}
}
```

- 执行两个场景并比较最终世界；`diff` 是查询层差异（含 derived relation）。
- 任一场景失败（某步被拒绝）输出该场景的诊断，退出码 1。

## `doccheck` 输出

`doccheck` 只做文档注释 lint（K-025），不做类型检查；契约见[文档注释](https://koine.systems/reference/spec/doc-comments/#lint-契约)。输出与 `check` 相同的诊断数组；有 error 退出码 1。

- 默认是迁移期：`relation` / `state relation` 缺 `@gloss`、`@example` 只有关系实例、文档注释未附着，都是 warning。
- `--strict` 把这三类升为 error；其余检查（未知 tag、占位符、元数等）始终是 error。
- `@example` 里只对同文件已声明的关系名做元数校验；未声明的名字按自然语言文本处理。
- `koine check --strict-docs` 在常规检查后追加 strict 模式的 doc lint。

## `graph` 输出

`graph` 导出场景 run 路径的世界图，不做 explore；结构变更只增不改（ADR-0014，版本策略见 ADR-0027）。

```json
{
  "version": "0.1",
  "scenario": "refund_flow",
  "worlds": [
    {"id": "<内容决定的 canonical JSON>", "time": "2024-01-01T00:00:00Z", "relations": {"status": [["o1", "Draft"]]}}
  ],
  "edges": [
    {"from": "<world id>", "to": "<world id>", "transition": "place", "binding": {"order": "o1", "actor": "c1"}}
  ],
  "diagnostics": []
}
```

- `worlds[0]` 是 start 世界，之后每个被接受的 step 一个世界；世界 `id` 是内容决定的 canonical JSON（与[观察接口](https://koine.systems/reference/spec/observations/#观察接口)一致）。
- 被拒绝的 step 不产生新世界、没有边，原因进 `diagnostics`（含 `koine.run.step-rejected`）；覆盖范围内有拒绝时退出码 1。
- `--depth N` 最多执行 N 步（默认全部）；`--max-worlds N` 最多导出 N 个世界，边只保留两端都在导出集合里的。
- prepare 失败或场景不存在时输出空 `worlds` / `edges` 加诊断，退出码 1。

## 值的 JSON 表示

| 值 | JSON |
| --- | --- |
| `Bool` / `Int` / `Decimal` | JSON 布尔/数字（任意精度，不丢精度） |
| `Text` | 字符串 |
| `Date` / `Time` | `"2024-01-01"` / `"12:30:00"` |
| `Timestamp` | RFC 3339，UTC 用 `Z`：`"2024-01-01T00:00:00Z"` |
| entity | 实体名字符串（如 `"o1"`） |
| 枚举 | 成员名字符串（如 `"Paid"`） |
| 产品值 | 字段对象：`{"amount": 100, "currency": "CNY"}` |
| newtype | 底层值的 JSON（类型身份不进 JSON） |
| quantity / Duration | `{"dimension": "Length", "magnitude": 10000}`；Duration 的量纲是 `Time`，canonical 单位是秒 |

实体名与 Text 字符串在 JSON 里不区分类型；显示层需要类型时参考模型声明。

## 检查语义

`koine check` 做 parse → resolve → check → world check → 静态世界验证（state / init / scenario start 的加载与 axiom 求值）。axiom 违反报 `koine.axiom.violated`，求值失败报原诊断码（如 `koine.eval.division-by-zero`）。

多个文件传给 `check` 时合并为一个平面模型检查（语言保持平面，见[组织](https://koine.systems/reference/spec/organization/)）：跨文件引用直接成立，跨文件重名报 `koine.resolve.duplicate-name`；任一文件有 parse error 时只报 parse 诊断。诊断的 `span.file` 指出来源文件。

## 时间轴

每个世界有 `time`。路径可以渲染成时间轴：

```text
2024-01-01  place(o1)
2024-01-01  pay(o1)
2024-02-01  wait(31 days)
```

时间轴是表现层，不改变语义。

## 尚未实现

`run --at`（等价命令是 `observe --view` 与 `diff`）属于后续里程碑；`graph` 只导出 run 路径，不做 explore / import。

## 相关

- [驱动层](https://koine.systems/reference/spec/driving/)
- [观察](https://koine.systems/reference/spec/observations/)
- [错误码](https://koine.systems/reference/errors/)

Source: https://koine.systems/reference/cli/index.mdx
