# Runnable audit examples

## MoonBit 最终上下文门禁示例

[release_gate/main.mbt](../examples/release_gate/main.mbt) 从仓库指定提交中的
[审查规则](../examples/release_gate/fixtures/review-policy.md)和
[代码变更](../examples/release_gate/fixtures/change.diff)读取证据，组装最终
模型上下文。`gate/git` 使用受审查策略中的提交、路径和指纹，从 Git blob
而不是可变工作区读取正文，并拒绝符号链接；`gate` 核验摘录和具体系统规则，
再对**最终要发布的同一字符串**运行文本审计。通过时实际写入
`_build/release_gate/approved-context.md`，复读文件并核对内容指纹。CI 只在
门禁成功后上传该示例构建物；没有真实模型调用或客户数据。

从仓库根目录分别运行：

~~~text
moon run examples/release_gate -- pass
moon run examples/release_gate -- stale
moon run examples/release_gate -- unsafe
moon run examples/release_gate -- leak
~~~

第一条打印源指纹、最终文本指纹和 `RELEASE_ARTIFACT`，退出码为 0。
后三条分别模拟错误的源指纹、越界路径、最终问题含 `TODO` 和 `sk-`；
都打印 `BLOCKED`，返回 1，且不会留下旧产物。测试验证真实 Git blob
读取、审计指纹和三种失败路径。工作区中的 fixture 改动不会偷偷影响
固定快照；使用新的来源内容须审查并更新策略中的提交与指纹。

这里的指纹不是签名。`gate/git` 信任代码中配置的策略与本地 Git 仓库；
它不验证远端仓库身份，也不能强制外部应用一定使用获批构建物。字符预算
不是 tokenizer 的 token 计数。见[信任边界](trust-boundary.md)。

## CLI 构建物示例

The audit examples are ordinary Markdown files:
[context.md](../examples/audit/context.md) and
[context-minimal.md](../examples/audit/context-minimal.md). They can be edited by a prompt
author, generated by an existing template engine, or checked out from a
knowledge-base build. No custom source format is required.

~~~sh
moon run cmd/main -- audit examples/audit/context.md \
  --policy examples/audit/policy.json
~~~

The same check in machine-readable form is suitable for a release job:

~~~sh
moon run cmd/main -- audit \
  examples/audit/context.md examples/audit/context-minimal.md \
  --policy examples/audit/policy.json \
  --json --report _build/context-audit-batch.json
~~~

Batch output keeps one report per input in order and adds a summary of passed,
warning, failed, and unreadable artifacts.

To review a report change between two builds:

~~~sh
moon run cmd/main -- diff \
  _build/context-audit-before.json _build/context-audit-after.json \
  --json --report _build/context-audit-diff.json
~~~

The diff aligns artifacts by path and records additions, removals, fingerprint
changes, issue-code changes, and newly unreadable inputs. A changed report exits
with status 1 so a release job can require an explicit review.

To exercise the failure path, lower the budget or require a marker that is not
present:

~~~sh
moon run cmd/main -- audit examples/audit/context.md \
  --budget 20 --require "A citation policy"
~~~

## Complete fresh-checkout verification

~~~sh
moon update
moon fmt --check
moon check --target all --deny-warn --warn-list=-implicit_impl_as_method-test_unqualified_package
moon build --target all
moon test --target all
moon run cmd/main -- audit examples/audit/context.md --budget 1800
~~~

See [the audit contract](audit.md), [production workflows](production-workflows.md),
and [the release guide](releasing.md) for the report format and CI checklist.
