# 软件工厂的 lint rule 应该是怎样的

> 从需求、实现、验证、审查到发布，哪些判断可以写成自动检查，哪些仍要人来做。

Author: Shumin Fu  
Published: 2026-11-23  
Updated: 2026-11-23  
Tags: ai-agents, workflow  
Canonical: https://funfun.zone/posts/software-factory-lint-rules/

<div class="html-embed" lang="zh-CN">
<article>
  <p class="lede">从需求、实现、验证、审查到发布，哪些判断可以写成自动检查，哪些仍要人来做。</p>
  <p>软件写得越快，人就越来越看不过来。于是问题会变得很具体：哪些判断可以写成反复执行的检查，哪些仍然必须由人来做。<strong>能写成检查的，就不要让人反复看同一类错误；不能写成检查的，就不要假装测试通过等于事情已经做对。</strong>先把内循环做稳，再去放大外层自动化。这里说的软件工厂，不是一座不必看代码、也不必开灯的黑箱，而是一条能把重复判断做成检查、又能把关键判断留给人的工程流程。</p>
  <p><strong>要把规则变成会执行、会报错的检查，而不只是写在说明里。</strong></p>
  <p>从需求、实现、验证、审查到发布，每个阶段都可以分出这两类判断。下图是这条流水线的概要。后文的规则字段和代码例子是综合建议，不是 Tan、Claude Code 团队或 Pi 对谈已经统一的标准。</p>
  <figure>
    [image: 流程图：需求、实现、验证、审查、发布五个阶段自上而下排列。浅蓝色表示可自动检查，浅米色表示由人判断。每个阶段左侧是可自动检查的事项，右侧是由人判断的事项。图底是反馈循环：误报过多、规则过期或事故发生之后，回到源头改规则或清理旧例子，再进入下一轮。]
    <figcaption>从需求到发布，每个阶段都同时有自动检查和人工判断；底部的反馈循环把误报、过期规则和事故送回源头。</figcaption>
  </figure>
  <h2>从需求到发布，边界在哪里</h2>
  <p>图中左侧是可以交给机器反复执行的检查，右侧是仍然要人做的判断。</p>
  <p><strong>需求</strong>阶段，机器最多能查这次改动有没有写出可验收的条件；要不要做这件事，以及用户真正需要什么，仍然要人来判断。</p>
  <p><strong>实现</strong>阶段，已经反复出现、而且能把边界说清楚的错误，可以写成检查；系统设计、接口取舍，以及会随数据体量变化的权衡，仍然要人来做。</p>
  <p><strong>验证</strong>阶段，机器可以查行为有没有被实际跑过；这些检查有没有量对东西，一次成功路径能不能代表其他组合，仍要人判断。</p>
  <p><strong>审查</strong>阶段，常规缺陷和已经写清失败长什么样的约定可以自动抓；阅读证据、判断某种架构会不会成为被复制的模板，以及为结果签字，必须由人来做。</p>
  <p><strong>发布</strong>阶段，构建、测试和回退说明可以自动查；上线后果，以及事故之后回看哪一层本该抓住它，仍然要人承担。</p>
  <p>如果误报过多、规则过期，或事故已经发生，就应回到源头改规则或清理旧例子，再进入下一轮。不要只在下游关掉报错，也不要在内循环还不稳时，先去放大外层自动化。</p>
  <h2>一条规则至少要写清这些</h2>
  <p>综合建议是：软件工厂里的 lint rule，不该只是代码风格清单。它更像一份带条件的检查契约。下面这个模板用来把反复出现的审查意见写成可执行、可维护的规则。它不是 Tan 或 Claude Code 团队公布的格式。</p>
  <div class="rule">
    <p class="rule-title">建议中的规则字段</p>
    <ul>
      <li>要挡住的失败：用一句话说清这条规则在拦什么。</li>
      <li>适用条件：它在哪些目录、哪些改动类型、哪些数据规模下成立。条件本身也要写进说明里，避免以后环境变了，旧规则还在把人带偏。</li>
      <li>证据：报错必须落到文件和行号，或者落到一条可复现的命令及其输出，而不能只凭函数名猜测。</li>
      <li>严重程度：是挡住合并，还是标为次要问题，或只是提示。风格和清理类意见，不应默认获得和缺陷同样的阻断力。</li>
      <li>例外：什么情况下可以不适用，谁有权确认例外，例外是否必须附理由并到期复查。</li>
      <li>报错文本：直接指向该修的根因，而不是只写「出现了违规」。如果正确做法是改用某个 API，就把这条路写进报错。</li>
      <li>失效和维护：这条规则依据什么假设；假设失效时如何发现、由谁修订或下线。如果自动审查器开始反复误报，就应回到源头改规则，而不是靠人一次次关闭工单。</li>
    </ul>
  </div>
  <p>这样写，是为了避开三种常见负担：防御性检查把系统堆复杂，重要缺口反而看不清；误报过多，审查无法收敛；规则和文档过期，还继续指导人和模型。</p>
  <h2>两个可运行的例子</h2>
  <p>下面两个例子用来说明：一条规则怎样把边界写成确定性检查，以及例外怎样带上到期条件并被机器校验。它们是建议写法，不是现成的行业标准，也不是上述来源里的原样代码。</p>
  <h3>用检查守住依赖方向</h3>
  <p>渲染进程不该直接引用主进程代码。这类边界适合写成确定性检查。下面这个小检查器用 Python 的 <code>ast</code> 读取 import，守住这一边界。它是教学模拟，不是对真实 JavaScript 或 TypeScript Electron 工程的检查，也不是完整的静态分析。它不依赖某个 lint 工具的配置格式，因此也不把未经核对的配置写成既有用法。</p>
  <p>违规文件 <code>electron_renderer/bad_ui.py</code> 直接引用了主进程模块：</p>
<pre><code>from electron_main.db import query
<span></span>
<span></span>
def render_status():
    return f&quot;status={query().get(&#x27;ok&#x27;)}&quot;
</code></pre>
  <p>合规文件 <code>electron_renderer/good_ui.py</code> 只使用传入的数据，不跨越进程边界：</p>
<pre><code>def render_status(data):
    return f&quot;status={data.get(&#x27;ok&#x27;)}&quot;
</code></pre>
  <p>检查器会遍历目录里的 Python 文件。只要 <code>electron_renderer/</code> 中出现对 <code>electron_main</code> 的引用，它就在具体行号上指出根因。</p>
  <p>在包含上述两个文件的目录里运行 <code>python3 check_boundary.py .</code>，输出为：</p>
<pre class="output"><code>electron_renderer/bad_ui.py:1: 不要在 electron_renderer 中直接引用 electron_main.db。主进程代码应留在 electron_main/，通过明确的 IPC 或共享契约与界面通信，而不是把主线程工作引进渲染进程。
found 1 error(s)
</code></pre>
  <p>把违规文件拿掉后再运行，检查通过，输出为：</p>
<pre class="output"><code>ok: no renderer-to-main imports
</code></pre>
  <details>
    <summary>完整检查器</summary>
<pre><code>#!/usr/bin/env python3
&quot;&quot;&quot;Forbid electron_renderer from importing electron_main.
<span></span>
A small suggested checker for a process boundary, not a published standard.
&quot;&quot;&quot;
from __future__ import annotations
<span></span>
import ast
import sys
from pathlib import Path
<span></span>
FORBIDDEN_FROM = &quot;electron_renderer&quot;
FORBIDDEN_TO = &quot;electron_main&quot;
<span></span>
<span></span>
def check_file(path: Path, root: Path) -&gt; list[str]:
    rel = path.relative_to(root).as_posix()
    if not rel.startswith(FORBIDDEN_FROM + &quot;/&quot;):
        return []
    tree = ast.parse(path.read_text(encoding=&quot;utf-8&quot;), filename=str(path))
    errors: list[str] = []
    for node in ast.walk(tree):
        names: list[str] = []
        if isinstance(node, ast.Import):
            names = [alias.name for alias in node.names]
        elif isinstance(node, ast.ImportFrom) and node.module:
            names = [node.module]
        for name in names:
            if name == FORBIDDEN_TO or name.startswith(FORBIDDEN_TO + &quot;.&quot;):
                errors.append(
                    f&quot;{rel}:{node.lineno}: 不要在 {FORBIDDEN_FROM} 中直接引用 {name}。&quot;
                    f&quot;主进程代码应留在 {FORBIDDEN_TO}/，通过明确的 IPC 或共享契约与界面通信，&quot;
                    &quot;而不是把主线程工作引进渲染进程。&quot;
                )
    return errors
<span></span>
<span></span>
def main(argv: list[str]) -&gt; int:
    root = Path(argv[1] if len(argv) &gt; 1 else Path(__file__).parent).resolve()
    files = sorted(root.rglob(&quot;*.py&quot;))
    files = [p for p in files if p.name != Path(__file__).name]
    errors: list[str] = []
    for path in files:
        errors.extend(check_file(path, root))
    if errors:
        print(&quot;\n&quot;.join(errors))
        print(f&quot;found {len(errors)} error(s)&quot;)
        return 1
    print(&quot;ok: no renderer-to-main imports&quot;)
    return 0
<span></span>
<span></span>
if __name__ == &quot;__main__&quot;:
    raise SystemExit(main(sys.argv))
</code></pre>
  </details>
  <h3>让例外到期可被机器校验</h3>
  <p>例外如果只写在注释或口头约定里，过期之后仍可能继续把人带偏。下面的例外格式是建议写法：每条例外都要有规则编号、路径、理由、批准人和到期日。校验器会抓住缺字段和已过期的例外。下面这次运行以 2026-11-23 为校验日。</p>
<pre><code>[
  {
    &quot;rule_id&quot;: &quot;no-product-useeffect&quot;,
    &quot;path&quot;: &quot;product/legacy_chart.py&quot;,
    &quot;reason&quot;: &quot;旧图表尚待迁到框架封装&quot;,
    &quot;approved_by&quot;: &quot;framework-owners&quot;,
    &quot;expires&quot;: &quot;2027-03-01&quot;
  },
  {
    &quot;rule_id&quot;: &quot;no-product-useeffect&quot;,
    &quot;path&quot;: &quot;product/old_dashboard.py&quot;,
    &quot;reason&quot;: &quot;历史页面仍在迁移&quot;,
    &quot;approved_by&quot;: &quot;framework-owners&quot;,
    &quot;expires&quot;: &quot;2026-10-01&quot;
  },
  {
    &quot;rule_id&quot;: &quot;no-product-useeffect&quot;,
    &quot;path&quot;: &quot;product/temp_panel.py&quot;,
    &quot;reason&quot;: &quot;临时联调&quot;,
    &quot;approved_by&quot;: &quot;&quot;
  }
]
</code></pre>
  <p>运行 <code>python3 check_exceptions.py exceptions.json 2026-11-23</code>，输出为：</p>
<pre class="output"><code>exceptions[0]: ok product/legacy_chart.py until 2027-03-01 (approved_by=framework-owners)
exceptions[1]: 例外已过期（product/old_dashboard.py，expires=2026-10-01，today=2026-11-23）。过期后应收回例外，或先扩展框架路径，而不是继续关闭报错。
exceptions[2]: 缺少必填字段 approved_by, expires
found 2 error(s)
</code></pre>
  <p>第一条例外仍然有效。第二条已经过期，第三条缺少批准人和到期日，因此不能继续当作有效例外。</p>
  <details>
    <summary>完整校验器</summary>
<pre><code>#!/usr/bin/env python3
&quot;&quot;&quot;Validate a suggested exception record format.
<span></span>
This format is a writing suggestion, not a published standard.
Required fields: rule_id, path, reason, approved_by, expires (YYYY-MM-DD).
An exception fails if a required field is missing, or if expires is today or earlier.
&quot;&quot;&quot;
from __future__ import annotations
<span></span>
import json
import sys
from datetime import date, datetime
from pathlib import Path
<span></span>
REQUIRED = (&quot;rule_id&quot;, &quot;path&quot;, &quot;reason&quot;, &quot;approved_by&quot;, &quot;expires&quot;)
<span></span>
<span></span>
def check_record(item: dict, today: date, index: int) -&gt; list[str]:
    errors: list[str] = []
    label = f&quot;exceptions[{index}]&quot;
    missing = [key for key in REQUIRED if not str(item.get(key) or &quot;&quot;).strip()]
    if missing:
        errors.append(f&quot;{label}: 缺少必填字段 {&#x27;, &#x27;.join(missing)}&quot;)
        return errors
    try:
        expires = datetime.strptime(str(item[&quot;expires&quot;]), &quot;%Y-%m-%d&quot;).date()
    except ValueError:
        errors.append(f&quot;{label}: expires 必须是 YYYY-MM-DD，当前是 {item[&#x27;expires&#x27;]!r}&quot;)
        return errors
    if expires &lt;= today:
        errors.append(
            f&quot;{label}: 例外已过期（{item[&#x27;path&#x27;]}，expires={expires.isoformat()}，today={today.isoformat()}）。&quot;
            &quot;过期后应收回例外，或先扩展框架路径，而不是继续关闭报错。&quot;
        )
    else:
        print(
            f&quot;{label}: ok {item[&#x27;path&#x27;]} until {expires.isoformat()} (approved_by={item[&#x27;approved_by&#x27;]})&quot;
        )
    return errors
<span></span>
<span></span>
def main(argv: list[str]) -&gt; int:
    path = Path(argv[1] if len(argv) &gt; 1 else Path(__file__).parent / &quot;exceptions.json&quot;)
    today = date.fromisoformat(argv[2]) if len(argv) &gt; 2 else date.today()
    records = json.loads(path.read_text(encoding=&quot;utf-8&quot;))
    errors: list[str] = []
    for index, item in enumerate(records):
        errors.extend(check_record(item, today, index))
    if errors:
        print(&quot;\n&quot;.join(errors))
        print(f&quot;found {len(errors)} error(s)&quot;)
        return 1
    print(&quot;ok: all exceptions are complete and unexpired&quot;)
    return 0
<span></span>
<span></span>
if __name__ == &quot;__main__&quot;:
    raise SystemExit(main(sys.argv))
</code></pre>
  </details>
  <h2>规则要保护的是理解和判断</h2>
  <p>如果规则只服务于更快地自动合并，流程也许能转起来，却可能把理解一起转没。自动检查越强，人越需要知道这些检查会漏掉什么。如果说不清这一点，就不该把一次快速批准当成审查已经完成。</p>
  <h2>三份来源分别提供了什么依据</h2>
  <p>Tan 把问题说成信任，但不是盲目信任。她用一座倒金字塔来安排投入：最宽的一层是代码库本身，也就是好的 API 设计、架构和重构；接着是静态分析，包括 lint、类型检查、编译器和测试；再往下是运行时验证和形式化验证；最窄的一层才是书面规则和技能。</p>
  <p>代码库是 agent 看到的最大输入之一，留下的权宜之计会被下一份改动当成既有做法，因此一条反复出现的审查意见应当变成确定性的检查。官方阅读稿里的顺序是：先加 lint，再清理旧例子，然后给 agent 一条能够验证行为的路径。如果内层的修改和检查循环本身就不稳，再把外层自动化放大，只会把修修补补的成本一起放大。<sup><a class="cite" href="#ref-1" id="cite-1">[1]</a></sup></p>
  <p>Claude Code 团队把代码审查分成两件事：在发布前发现并修好问题，以及在团队里建立对代码库的共享理解。他们不再把「有没有人读过这行代码」当成充分标准，而改问这次改动可能在哪里出错，哪一种检查最适合抓住它。人的时间要用在人才能判断的事上，例如要不要做、高风险代码、会被别人复制的架构、阅读证据，以及为结果承担责任。</p>
  <p>他们对 <code>REVIEW.md</code> 的要求是：写清失败长什么样，要求给出文件和行号作为证据，并另外检查审查器是否真的遵守这些说明。每一条发现都必须被回应，同时审查必须收敛，因为作为作者的 agent 和作为审查者的 agent 可能无限地互相打转。风险等级由改动本身决定，机器人的批准不能填补必须由人批准的空位。<sup><a class="cite" href="#ref-2" id="cite-2">[2]</a></sup></p>
  <p>在已发布的对谈笔记里，Mario Zechner 和 Armin Ronacher 并没有反对检查本身。他们担心的是另一种自我安慰：只要 agent 写了一大堆检查规则和测试，系统就一定是好的。单个模块都通过，也不等于这些模块组合起来仍然通过。模型被大量「最佳实践」训练过，其中不少其实可能加重系统负担，例如到处做防御性检查。</p>
  <p>Armin 举过一份过期文档的例子：文档没有写清自己成立的条件，过期之后就会把人带偏。他们听过同行在事故会上的情形：没人知道发生了什么，所有人都在问自己的模型。Mario 认为这非常可怕。这些都是现场判断，不是对照实验的结论，但和前两份来源一样，都认为检查不能替代人对系统和用户的理解。<sup><a class="cite" href="#ref-3" id="cite-3">[3]</a></sup></p>
  <h2>来源</h2>
  <ol class="sources">
    <li id="ref-1"><a class="ref-back" href="#cite-1">[1]</a> Lauren Tan，《All Code Is Slop (Except Mine)》，AI Engineer CODE 2026，2026 年 11 月 11 日。<a href="https://www.youtube.com/watch?v=O_zcc6Xl12g">原视频</a>，<a href="https://ai.engineer/talks/O_zcc6Xl12g-all-code-is-slop-except-mine">官方演讲阅读稿</a>，<a href="https://ai.engineer/code/2026/talks/lauren-tan">会议页</a>。</li>
    <li id="ref-2"><a class="ref-back" href="#cite-2">[2]</a> Addy Osmani、Morgan Westlee Lunt、Qing Wang，《How the Claude Code team does code review》，2026 年 10 月 15 日：<a href="https://claude.dev/blog/solving-code-review-at-anthropic/">Claude团队原文</a>。</li>
    <li id="ref-3"><a class="ref-back" href="#cite-3">[3]</a> FunFun Zone，《Pi 作者谈 AI 编程：测试通过之后，人还要判断什么》：<a href="https://funfun.zone/posts/pi-creators-code-quality-notes/">Pi访谈笔记</a>。<a href="https://www.youtube.com/watch?v=ZBTegd8DUZM">原视频</a>。</li>
  </ol>
</article>
</div>
