diff --git a/.github/workflows/docs-check.yml b/.github/workflows/docs-check.yml index 7292d98..db4c0ca 100644 --- a/.github/workflows/docs-check.yml +++ b/.github/workflows/docs-check.yml @@ -13,26 +13,18 @@ concurrency: cancel-in-progress: true jobs: + policy: + uses: QuantStrategyLab/.github/.github/workflows/docs-acceptance.yml@b4966bdd8780cad3ae5154ae50d8b98259a16dc6 + with: + policy-revision: b4966bdd8780cad3ae5154ae50d8b98259a16dc6 + mode: enforce + docs: name: Documentation check + needs: policy + if: always() runs-on: ubuntu-latest - timeout-minutes: 5 + timeout-minutes: 2 steps: - - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 - with: - persist-credentials: false - path: target - - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 - with: - repository: QuantStrategyLab/.github - ref: cbfe3aedf927a9cf73dfb7813e872b38d432b5a1 - persist-credentials: false - path: policy - - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6 - with: - python-version: "3.12" - - name: Test the documentation checker - working-directory: policy - run: python scripts/test_style.py - - name: Check documentation rules - run: python policy/scripts/check_style.py --root target --phase 2 + - name: Require documentation policy acceptance + run: test "${{ needs.policy.result }}" = "success" diff --git a/AGENTS.md b/AGENTS.md index b25c783..dcc2254 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,7 +38,7 @@ uv run --no-sync python -m pytest -q CI also verifies the locked shared refs, the QPK pin and the package build. -Organization documentation rules are checked by `python scripts/check_style.py --phase 2` (`Documentation check` in CI). +Organization documentation rules run in CI via `QuantStrategyLab/.github/.github/workflows/docs-acceptance.yml` (`Documentation check`), or offline with a sibling `QuantStrategyLab/.github` checkout (`git clone https://github.com/QuantStrategyLab/.github ../.github && python3 ../.github/scripts/check_style.py --root . --phase 2`). ## Naming and documentation diff --git a/docs/README.md b/docs/README.md index 7f153eb..8133e58 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,9 +4,7 @@ - [Saved account history](account-archive-history.md) - [Configurable display names](display-names.md) -- [交易差异说明:FirstradePlatform](naming-trading-diff.zh-CN.md) - [Firstrade isolated paper command consumer](paper-execution-command-consumer.md) - [Shared dependency test imports](shared-test-imports.md) - [Statement reconciliation: fee split and valuation basis](statement-reconciliation.md) · [简体中文](statement-reconciliation.zh-CN.md) - [Unified daily notification (code only)](unified-daily-notification.md) -- [交易差异审查:FirstradePlatform](unified-notification-trading-diff-review.zh-CN.md) diff --git a/docs/naming-trading-diff.zh-CN.md b/docs/naming-trading-diff.zh-CN.md deleted file mode 100644 index 0afb943..0000000 --- a/docs/naming-trading-diff.zh-CN.md +++ /dev/null @@ -1,14 +0,0 @@ -# 交易差异说明:FirstradePlatform - -基线:`c3f4a2122aa535594703279f9a4a1b4222d57586`。作者自审与 AST 检查,不声称独立人工审查。 - -只改通知显示名解析、固定显示配置副本、测试和说明。当前显示名解析由 QPK 提供(`quant_platform_kit.common.display_names`,随 QPK pin `5181f2d6` 安装),仓库内 `notifications/display_names.py/json` 副本已删除;下表保留基线时的文件记录。账户 key、策略 profile、RuntimeTarget、fingerprint、release/continuity binding、Secret 名称、存储 key、通知去重键均不改。交易依赖及锁文件未改;SOXL 0.679 / 总仓位 0.97 / 现金 0.03 未改。不部署交易服务。 - -现有源码中的券商、订单、风险、状态和报告持久化调用表达式及其参数比较结果: - -| 文件 | 敏感调用数 | 参数不变 | -|---|---:|---| -| `notifications/display_names.py` | 0 | 是 | -| `notifications/telegram.py` | 0 | 是 | - -新增解析器只读同目录的生成 JSON 及可选显示环境配置,不联网、不调用券商、不写持久状态。显示值不得作为机器标识再输入交易调用。该 PR 的固定副本来自 QRS 的生成器,避免只为改名升级交易依赖。PR 保持 draft;即使 CI 通过,也未批准实际运行版本采用。 diff --git a/docs/unified-notification-trading-diff-review.zh-CN.md b/docs/unified-notification-trading-diff-review.zh-CN.md deleted file mode 100644 index ea45a83..0000000 --- a/docs/unified-notification-trading-diff-review.zh-CN.md +++ /dev/null @@ -1,25 +0,0 @@ -# 交易差异审查:FirstradePlatform - -审查方式:本次作者自审,辅以 AST 调用参数比较;不声称独立人工审查。 -基线:`946d8783d48e139e3df97ad7464a8fdcb6f55d83`。范围为当前 PR 相对基线的全部改动。 - -结论:源码差异仅涉及通知路由、附加的通知报告事实、已有监测入口的候选投影、通知配置透传、测试和说明。交易依赖、锁文件、UES/QPK pin、信号、仓位、资金限额、订单构造、券商调用、状态 claim 和风险门禁保持原样。 - -现有文件中的券商、订单、风险、交易状态和报告持久化调用,已对调用表达式及全部参数做 AST 比较: - -| 文件 | 敏感调用数量 | 参数完全相同 | -|---|---:|---| -| `application/rebalance_service.py` | 4 | 是 | -| `main.py` | 8 | 是 | - -新增模块不调用券商。只有既有云监测器在 `daily_digest` 且目标明确启用时,才读已有私有报告并写候选;不启用账户、不修改订单或交易排程。关闭模式保持既有行为。失败、未决订单、风险、认证及持久化问题保留即时通知;已确认的普通订单可通过 `QSL_DIGEST_ROUTINE_ORDERS=false` 继续即时发送。 - -通知代码取自 QPK `4cc5e55251269923771f90cd661bb62a5cfdf0cd`,有意不升级平台交易依赖。三个源文件逐字节相同: - -| 文件 | SHA256 | -|---|---| -| `digest_routing.py` | `60c3f258c4d1a03bc49c7d03b392c02c316541726dfacb0b1d4fae6dbd2e5017` | -| `digest_candidates.py` | `6272c0ca16ee63eecd9bcb25870df7f575d8fac3f487d3ab1ac69cc3831042a0` | -| `digest_archive.py` | `b9a92299adfddff42c62f7d493845e0cbde91df7e475a5cb6bf2114446045b49` | - -合并条件:相关验证通过、PR 当前精确 head 的 CI 全绿、当前远端主线的 push / workflow_run / 可复用 workflow 调用链无自动部署。否则保留草稿。此审查不批准从旧生产 revision 升级到整个当前主线;生产到候选的完整差异、UES 指纹与 baseline 仍须按后续发布方案单独审查。 diff --git a/scripts/check_style.py b/scripts/check_style.py deleted file mode 100755 index eb4302e..0000000 --- a/scripts/check_style.py +++ /dev/null @@ -1,811 +0,0 @@ -#!/usr/bin/env python3 -"""Check a repository against docs/STYLE_GUIDE.md. - -The style guide is 500 lines of prose and nothing enforced it. This script turns -the mechanically checkable parts into exit codes, so a pull request that breaks -them fails instead of relying on a reviewer remembering section 7. - -Checks come from the guide, not from invention: - - section 2 marketing words, filler openings, emoji in headings - section 4 language suffixes - section 5 content that stays out of public repositories - section 6 internal note paths - section 7 file naming - section 13 the grep list the guide already tells reviewers to run - -Usage: - python scripts/check_style.py [--root PATH] [--phase {1,2}] - -Phases exist because 26 repositories already carry violations. Turning all of -them into blocking failures on day one means every open pull request goes red -and the check gets deleted. Phase 1 blocks only the defects that leak private -data or break a reader's navigation; naming and README structure are reported -but do not fail. Phase 2 blocks everything, and should be switched on once the -naming cleanup is done. - -Exit code 0 when nothing in the blocking set is found. -""" - -from __future__ import annotations - -import argparse -import re -import subprocess -import sys -import unicodedata -import urllib.parse -from pathlib import Path -from typing import NamedTuple - -SKIP_DIRS = { - ".git", "node_modules", ".venv", "venv", "__pycache__", "build", "dist", - ".gradle", ".mypy_cache", ".pytest_cache", ".ruff_cache", "site-packages", - ".idea", ".vscode", -} - -# Section 7: these keep their uppercase spelling because tools and readers -# expect them. -CONVENTIONAL_UPPER = { - "README", "CHANGELOG", "SECURITY", "CONTRIBUTING", "CODE_OF_CONDUCT", - "LICENSE", "SUPPORT", "GOVERNANCE", "AUTHORS", "NOTICE", "AGENTS", - "CLAUDE", "THIRD_PARTY_NOTICES", "TRADEMARK", "ARCHITECTURE", - "CONFIGURATION", "GLOSSARY", "STYLE_GUIDE", "PULL_REQUEST_TEMPLATE", - "ISSUE_TEMPLATE", "FUNDING", "SKILL", "INDEX", -} - -DATE_IN_NAME = re.compile(r"(?" - -# A date in the name is a defect when the file describes something that keeps -# changing, because the date goes stale while the name stays. It is the -# identity of the record when the file is a snapshot of one date: two analyses -# of the same subject from different months are different documents, and -# dropping the date leaves them fighting over one name. -# -# Section 7 therefore exempts the two places that hold records. ADRs carry a -# sequence number rather than a date, but they are the same kind of thing. -DATE_BEARING_PATHS = ("docs/adr/", "docs/research/") - -# Section 3 outline. The guide calls it "a default, not a required template", -# so a missing section is never blocking. -README_OUTLINE = [ - "status", "what this is", "what this is not", "quick start", - "configuration", "architecture", "repository layout", "contributing", - "security", "license", -] - -# Section 2, "Habits to avoid". -# -# `robust` and `powerful` are deliberately absent. Both are technical terms in -# this corpus — a robust estimator, statistical power — and a rule that flags -# them reported twelve non-defects in the research docs and one quotation of -# Hansen's SPA test. A word that is marketing in a landing page and a term of -# art in a research note cannot be decided by a word list, so the list keeps -# only the words that have no technical sense here. -MARKETING_WORDS = [ - "seamless", "cutting-edge", "comprehensive", - "world-class", "next-generation", "state-of-the-art", "best-in-class", -] -FILLER_PHRASES = [ - "it is worth noting", "it's worth noting", "in summary", "let's dive in", - "needless to say", "at the end of the day", -] - -# Section 13 says "non-placeholder buckets" and section 7 line 104 says the -# placeholder form is ``, ``, `$PROJECTS_ROOT`. So a -# `gs://` URI whose bucket segment is a placeholder is the correct form, not a -# finding. `synthetic-` is on the list because the organisation's fixtures are -# named that way; without it every fixture a doc quotes becomes a finding the -# day the doc is written. -PLACEHOLDER_BUCKET = re.compile( - r"^[<{$]|[<>{}]|\.\.\." - r"|^(?:your|my|example|sample|placeholder|shared|synthetic)[-_]" - r"|^bucket(?:-|$)" -) - -# Section 13 asks for a placeholder wherever a deployed subdomain would appear, -# so `YOUR_SUBDOMAIN.workers.dev` is the correct form, not a finding. Matching -# the domain shape alone reports the guide's own advice as a leak, which is how -# a check earns the reputation that lets the real leak through. -PLACEHOLDER_SUBDOMAIN = re.compile( - r"^[<{$]|[<>{}]" - r"|^(?:your|my|example|sample|placeholder)[-_]" - r"|^subdomain(?:-|$)" - r"|^x{2,}$" -) - -URL_TOKEN = re.compile(r"https?://[^\s)\]\"'<>`]+") -FENCED_BLOCK = re.compile(r"^```.*?^```", re.DOTALL | re.MULTILINE) - - -class FileContext(NamedTuple): - """Per-file material the leak predicates need to judge a match.""" - - url_spans: list[tuple[int, int]] - prose: str # the text with fenced blocks blanked out, offsets preserved - masked: str # `prose` with inline code blanked out too - - -def phrase_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: - """A rule about phrasing describes prose; backticks name the phrase. - - The style guide writes `this round` when it means the words, and a value - rule such as `gs://` is the opposite case: a bucket inside backticks is - still a bucket. That is why this is a per-rule predicate and not a blanket - skip on code spans. - """ - return bool(ctx.masked[match.start():match.end()].strip()) - - -def bucket_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: - bucket = match.group(0)[len("gs://"):].split("/", 1)[0] - # A sentence-final period is punctuation, not part of the name. An ellipsis - # is the placeholder, so it must survive this. - if bucket.endswith(".") and not bucket.endswith("..."): - bucket = bucket[:-1] - return bool(bucket) and not PLACEHOLDER_BUCKET.search(bucket) - - -def subdomain_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: - """A placeholder subdomain is the form section 13 asks for, not a leak. - - What survives this predicate is a subdomain that was actually deployed, - which is the thing the rule is about. - - The fenced-block exemption is repeated here because `check_leaks` only - applies it to patterns without a predicate. Without this line, installing - the predicate would make an example that shows what to avoid start - reporting — a wider blocking set than the rule had before, which is not - what a narrowing fix should do. - """ - if not ctx.prose[match.start():match.end()].strip(): - return False - subdomain = match.group(0)[: -len(".workers.dev")] - return not PLACEHOLDER_SUBDOMAIN.search(subdomain.lower()) - - -def hex_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: - """Section 5 forbids 40-character hex *in prose* — CI head SHAs pasted in. - - A revision reference is a different thing and the guide asks for it: line - 377 writes `<40-char sha>` and the cross-repo links pin `blob//`. So - the three forms that carry a revision are exempt, and what is left is the - case the rule was written for. - """ - if ctx.prose[match.start()] == " ": - return False # inside a fenced block: a contract example, not prose - for start, end in ctx.url_spans: - if start <= match.start() < end: - return False # a link target - line_start = text.rfind("\n", 0, match.start()) + 1 - line_end = text.find("\n", match.end()) - line = text[line_start: line_end if line_end != -1 else len(text)] - column = match.start() - line_start - if line[:column].count("`") % 2 == 1: - return False # a quoted identifier - if text[match.end(): match.end() + 2] == "](": - return False # the label of a link whose target carries the same sha - return len(set(match.group(0))) != 1 # 000...0 and friends, a placeholder - - -# Section 13 grep list plus section 5 categories that are recognisable. -# The lookbehind keeps URL paths such as https://example.com/home/legal/ out of -# the results; "/home/" inside a URL is not a local path. -# -# Two categories, because they carry different weight. A real home path is -# evidence that something leaked from a workstation. The rest are the guide's -# own "prompt to check, not a certain failure" greps. -# -# The last column is a predicate deciding whether a match is a finding at all. -# Both heuristics in this list match far more than they mean: `gs://` matches -# `gs:///...`, which is the form section 13 asks for, and a 40-character -# hex string matches every commit SHA, which is how a cross-repository reference -# is supposed to be pinned. Without the predicates the two rules reported 522 -# findings across the organisation and not one of them was a defect, which is -# the same as having no rule: the report gets scrolled past. -LEAK_PATTERNS = [ - (re.compile(r"(?|]{2,}"), - "paths", "absolute home path (section 13)", None), - (re.compile(r"\bgs://[^\s`)\]\"'<>]+"), - "content", "non-placeholder bucket (section 13)", bucket_is_real), - (re.compile(r"\b[\w-]+\.workers\.dev\b"), - "publish", "personal subdomain (section 13)", subdomain_is_real), - (re.compile(r"\b[0-9a-f]{40}\b"), - "content", "40-character hex string in prose (section 5)", hex_is_prose), - # `| PR |` is a table header; `| PR #12 |` and `| #12 |` are the rows this - # rule is about. Matching the header too reported the guide's own row - # describing the rule. - (re.compile(r"\|\s*(?:PR\s*#?\d+|#\d+)"), - "content", "PR-number status table (section 5)", None), - (re.compile(r"\b(this round|this task|in this task)\b", re.IGNORECASE), - "content", "task-scope wording (section 5)", phrase_is_prose), - (re.compile(r"\b(the owner approved|approved by)\b", re.IGNORECASE), - "content", "owner approval note (section 5)", phrase_is_prose), -] - - -# Section 6: paths for notes that must never be committed. The four wildcard -# entries are spelled with [Aa] character classes because .gitignore is -# case-sensitive on some platforms and `*audit*.md` alone lets `Audit.md` past. -INTERNAL_NOTE_GITIGNORE = [ - "*[Aa][Uu][Dd][Ii][Tt]*.md", - "*[Hh][Aa][Nn][Dd][Oo][Ff][Ff]*.md", - "*-[Rr][Ee][Vv][Ii][Ee][Ww]-*.md", - "*_[Rr][Ee][Vv][Ii][Ee][Ww]_*.md", - "docs/audits/", - "docs/plans/", - "docs/handoffs/", - "docs/superpowers/", - "docs/changes/", - "docs/acceptance-policy.md", - ".claude/", - ".codex/", - ".local/", - "ops-notes/", - "*.local.md", - "*.internal.md", -] - -# Directories whose contents are working material, never reader-facing docs. -INTERNAL_NOTE_DIRS = re.compile( - r"(^|/)(docs/plans|docs/handoffs|docs/superpowers|docs/audits|docs/changes)/" -) - -DATED_TITLE = re.compile(r"(audit|acceptance).*20\d{2}-\d{2}-\d{2}", re.IGNORECASE) - -# Blocking categories per phase. -# -# Phase 1 blocks only what is unambiguous: a leaked local path, a link a reader -# cannot follow, a missing rules file, a .gitignore that has stopped keeping -# working notes out. Everything else is reported as NOTE. Phase 2 blocks the -# rest once the naming cleanup is done. README structure never blocks, because -# section 3 is a default and section 4 says so. -PHASE_BLOCKING = { - 1: {"paths", "links", "agents", "hygiene", "publish"}, - 2: {"paths", "links", "agents", "hygiene", "publish", "content", "naming", "language", "docs"}, -} - -# Rules whose purpose is to stop an identifier being published. -# -# The style guide governs published documentation. In a private repository -# nothing is published, and a deployment document may need the real host name to -# be usable at all — an OAuth callback URL with a placeholder in it cannot be -# configured. So these report as notes there and block in public repositories, -# which is the case the rule was written for. The default stays "public", so -# forgetting the flag cannot switch a leak rule off by accident. -PUBLICATION_CATEGORIES = {"paths", "publish"} - - -class Report: - def __init__(self, blocking: set[str]) -> None: - self.blocking = blocking - self.findings: list[tuple[str, str]] = [] - - def add(self, category: str, message: str) -> None: - self.findings.append((category, message)) - - def blocking_findings(self) -> list[tuple[str, str]]: - return [f for f in self.findings if f[0] in self.blocking] - - def advisory_findings(self) -> list[tuple[str, str]]: - return [f for f in self.findings if f[0] not in self.blocking] - - -def git_visible_files(root: Path) -> set[Path] | None: - """Return tracked and unignored working-tree files when root is a Git checkout.""" - if not (root / ".git").exists(): - return None - res = subprocess.run( - ["git", "ls-files", "--cached", "--others", "--exclude-standard", "-z"], - cwd=root, - capture_output=True, - check=False, - ) - if res.returncode != 0: - return None - visible: set[Path] = set() - for raw in res.stdout.split(b"\0"): - if not raw: - continue - rel = raw.decode("utf-8", errors="surrogateescape") - candidate = root / rel - if candidate.is_file(): - visible.add(candidate) - return visible - - -def is_untracked_gitignored(root: Path, rel_path: str) -> bool: - """Return True only when rel_path has no tracked/unignored files and is gitignored.""" - if not (root / ".git").exists(): - return False - listed = subprocess.run( - ["git", "ls-files", "--cached", "--others", "--exclude-standard", "-z", "--", rel_path], - cwd=root, - capture_output=True, - check=False, - ) - if listed.returncode == 0 and listed.stdout.strip(b"\0"): - return False - ignored = subprocess.run( - ["git", "check-ignore", "-q", "--", rel_path], - cwd=root, - check=False, - ) - return ignored.returncode == 0 - - -def iter_files(root: Path): - visible = git_visible_files(root) - if visible is not None: - for path in sorted(visible): - if any(part in SKIP_DIRS for part in path.parts): - continue - yield path - return - for path in sorted(root.rglob("*")): - if any(part in SKIP_DIRS for part in path.parts): - continue - if path.is_file(): - yield path - - -def is_markdown(path: Path) -> bool: - return path.suffix.lower() in {".md", ".markdown"} - - -def has_emoji(text: str) -> bool: - for char in text: - if ord(char) < 0x2000: - continue - if unicodedata.category(char) == "So": - return True - return False - - -def strip_fenced_blocks(text: str) -> str: - """Drop ``` fenced blocks. - - Documentation about documentation contains example links such as - `[简体中文](README.zh-CN.md)`. Those are illustrations, not links, and - checking them reports failures the author cannot fix. - """ - return re.sub(r"^```.*?^```", "", text, flags=re.DOTALL | re.MULTILINE) - - -# GitHub recognises these two template names with underscores only. Renaming -# them to hyphens silently disables the template, so they are exempt from the -# underscore rule while the lowercase rule still applies. -GITHUB_TEMPLATE_NAMES = {"pull_request_template", "issue_template"} - - -def check_naming(root: Path, report: Report) -> None: - for path in iter_files(root): - if not is_markdown(path): - continue - name = path.name - rel = path.relative_to(root).as_posix() - core = name[:-3] - - if " " in name: - report.add("naming", f"{rel}: file name contains a space") - - if core.endswith(".zh-CN"): - core = core[: -len(".zh-CN")] - elif core.endswith(".zh-cn"): - report.add("naming", f"{rel}: language suffix must be .zh-CN.md, not .zh-cn.md") - core = core[: -len(".zh-cn")] - elif core.endswith(".en"): - report.add( - "naming", - f"{rel}: English is the base name (section 4); .en.md is not a suffix", - ) - - if rel != "PROPOSAL.md" and core not in CONVENTIONAL_UPPER and core not in GITHUB_TEMPLATE_NAMES: - if core != core.lower(): - report.add("naming", f"{rel}: file name must be lowercase (section 7)") - if "_" in core: - report.add("naming", f"{rel}: use hyphens, not underscores (section 7)") - - if DATE_IN_NAME.search(core) and not rel.startswith(DATE_BEARING_PATHS): - report.add("naming", f"{rel}: dates belong in history, not file names (section 7)") - - -RE_PATCH_PROSE = re.compile( - r"\b(this change|this pr|in this pr|this policy pr|remains? unchanged)\b", - re.IGNORECASE, -) -RE_PHASE_HEADING = re.compile(r"^#{1,4}\s+.*\bP0[–-]P\d+\b", re.MULTILINE) -RE_LICENSE_HEADING = re.compile(r"^##\s+(?:License|许可证)\s*$") -RE_H2_HEADING = re.compile(r"^##\s+\S+") -MAX_AGENTS_LINES = 120 - - -def check_readme(root: Path, report: Report) -> None: - readme = root / "README.md" - if not readme.exists(): - report.add("readme", "README.md is missing") - else: - text = readme.read_text(encoding="utf-8", errors="ignore") - headings = [h.strip().lower() for h in re.findall(r"^#{2,3}\s+(.+)$", text, re.MULTILINE)] - missing = [ - section for section in README_OUTLINE - if not any(section in heading for heading in headings) - ] - if missing: - report.add( - "readme", - "README.md: section 3 outline not followed — missing " - + ", ".join(missing) - + " (a default, not a requirement)", - ) - - for name in ("README.md", "README.zh-CN.md"): - path = root / name - if not path.exists(): - continue - raw = path.read_text(encoding="utf-8", errors="ignore") - prose = blank_fenced_blocks(raw) - masked = blank_code_spans(prose) - seen_license = False - for lineno, (prose_line, masked_line) in enumerate( - zip(prose.splitlines(), masked.splitlines()), 1 - ): - match = RE_PATCH_PROSE.search(masked_line) - if match: - report.add( - "content", - f"{name}:{lineno}: PR-diff narration '{match.group(0)}' belongs in PR description, not README (section 2)", - ) - if RE_PHASE_HEADING.match(prose_line): - report.add( - "content", - f"{name}:{lineno}: phase code heading belongs outside reader-facing README (section 5)", - ) - if RE_LICENSE_HEADING.match(prose_line): - seen_license = True - elif seen_license and RE_H2_HEADING.match(prose_line): - report.add( - "content", - f"{name}:{lineno}: top-level section after License belongs above License (section 3)", - ) - - -def check_links(root: Path, report: Report) -> None: - for path in iter_files(root): - if not is_markdown(path): - continue - # A link written inside backticks is an example of how to write a link, - # not a link. `pr-attachments.md` documents `![alt](./shot.png)` so the - # reader can copy it; reporting that as a broken target makes the - # documentation of the feature impossible to pass the check. - text = blank_code_spans( - strip_fenced_blocks(path.read_text(encoding="utf-8", errors="ignore")) - ) - for match in re.finditer(r"\[[^\]]*\]\(([^)\s]+)\)", text): - target = match.group(1) - if target.startswith(("http://", "https://", "mailto:", "#", "<")): - continue - resolved = (path.parent / urllib.parse.unquote(target.split("#")[0])).resolve() - # A link that climbs out of the repository points at a sibling - # checkout. CI only has this repository, so the target cannot exist - # there — that is not a broken link, it is a reference this check - # cannot verify. Reporting it as broken makes the check unusable in - # CI while passing locally, which is the worst of both. - if not resolved.is_relative_to(root): - # `xref` is not in PHASE_BLOCKING, so this stays a note in - # both phases. `tools/check_cross_repo_links.py` resolves these - # against the sibling's default branch on a schedule; a check - # that says "not checked" has no standing to block. - report.add( - "xref", - f"{path.relative_to(root).as_posix()}: cross-repository reference, not checked -> {target}", - ) - continue - if not resolved.exists(): - report.add( - "links", - f"{path.relative_to(root).as_posix()}: broken relative link -> {target}", - ) - - -def check_agent_files(root: Path, report: Report) -> None: - tracked = subprocess.run( - ["git", "ls-files", "--cached", "-z", "--", "AGENTS.md", "CLAUDE.md"], - cwd=root, capture_output=True, text=True, check=False, - ) - names = set(tracked.stdout.split("\0")) if tracked.returncode == 0 else set() - - for name in ("AGENTS.md", "CLAUDE.md"): - path = root / name - if not path.exists(): - report.add("agents", f"{name} is missing") - elif name not in names: - report.add("agents", f"{name} is not tracked by Git; a local copy is not a committed rule") - - claude = root / "CLAUDE.md" - if claude.exists(): - content = claude.read_text(encoding="utf-8", errors="ignore").strip() - if content != "@AGENTS.md": - report.add("agents", "CLAUDE.md must contain the single line @AGENTS.md") - - if (root / ".github" / "engineering.md").exists() and not is_untracked_gitignored( - root, ".github/engineering.md" - ): - report.add( - "agents", - ".github/engineering.md is retired; consolidate rules in AGENTS.md", - ) - - agents = root / "AGENTS.md" - if agents.exists(): - text = agents.read_text(encoding="utf-8", errors="ignore") - if len(text.splitlines()) > MAX_AGENTS_LINES: - report.add( - "agents", - f"AGENTS.md exceeds {MAX_AGENTS_LINES} lines; keep agent rules concise", - ) - if CONTENT_BOUNDARY not in text: - report.add( - "agents", - "AGENTS.md is missing the content-boundary block, see section 5", - ) - - -def check_docs_baseline(root: Path, report: Report, visibility: str = "public") -> None: - """The file set a reader expects, split by repository visibility. - - Public repositories owe outside readers a license, contribution path, - vulnerability disclosure policy, and code of conduct. Private repositories - only require SECURITY.md for credential handling and rotation. - """ - if visibility == "public" or (root / "LICENSE").exists(): - required = ["LICENSE", "CONTRIBUTING.md", "SECURITY.md", "CODE_OF_CONDUCT.md"] - kind = "public" - else: - required = ["SECURITY.md"] - kind = "private" - missing = [name for name in required if not (root / name).exists()] - if missing: - report.add( - "docs", - f"{kind} repository is missing {', '.join(missing)} (section 14)", - ) - - -def check_doc_hygiene_advisory(root: Path, report: Report) -> None: - """Report non-blocking semantic hygiene cues (orphan docs, zh-CN-only files).""" - docs_dir = root / "docs" - if docs_dir.is_dir(): - doc_mds = [p for p in docs_dir.rglob("*.md") if p.name != "README.md"] - if len(doc_mds) > 5 and not (docs_dir / "README.md").exists(): - report.add( - "docs_advisory", - f"docs/ has {len(doc_mds)} documents but no docs/README.md index", - ) - for path in iter_files(root): - if not is_markdown(path): - continue - if path.name.endswith(".zh-CN.md"): - en_sibling = path.with_name(path.name[:-len(".zh-CN.md")] + ".md") - if not en_sibling.exists(): - rel = path.relative_to(root).as_posix() - report.add( - "docs_advisory", - f"{rel}: Chinese sibling has no English source file (section 4)", - ) - - -QUOTE_MARKS = "\"'\u201c\u201d\u2018\u2019" -QUOTED_TERM = re.compile(r"[\"\'\u201c\u2018][^\"\'\u201c\u201d\u2018\u2019]{1,40}[\"\'\u201d\u2019]") - - -def quoted_term_list(line: str, match: re.Match) -> bool: - """True when a banned word sits inside a list of quoted terms. - - Section 2 forbids marketing words in prose. A guide that writes - "Elevate", "Seamless", "Unleash" is naming the words it forbids, and - reporting it makes the rule unusable exactly where it is most needed -- - the same shape as the placeholder-subdomain and template-underscore - exemptions. - - The condition is two or more quoted terms on the line, so one quoted word - used as a claim ("our 'seamless' onboarding") is still reported. That - keeps the exemption about lists, not about quotation marks. - """ - before = line[match.start() - 1] if match.start() else "" - after = line[match.end()] if match.end() < len(line) else "" - if before not in QUOTE_MARKS or after not in QUOTE_MARKS: - return False - return len(QUOTED_TERM.findall(line)) >= 2 - - -def check_language(root: Path, report: Report) -> None: - marketing = re.compile( - r"\b(" + "|".join(re.escape(w) for w in MARKETING_WORDS) + r")\b", re.IGNORECASE - ) - filler = re.compile( - r"\b(" + "|".join(re.escape(p) for p in FILLER_PHRASES) + r")\b", re.IGNORECASE - ) - for path in iter_files(root): - if not is_markdown(path): - continue - rel = path.relative_to(root).as_posix() - # A word inside a fenced block or a code span is being named, not used. - # The guide lists the words it forbids, and without this the guide is - # the largest offender in the organisation. - text = blank_code_spans(blank_fenced_blocks(path.read_text(encoding="utf-8", errors="ignore"))) - first_heading = True - for number, line in enumerate(text.splitlines(), 1): - for match in marketing.finditer(line): - if quoted_term_list(line, match): - continue - report.add("language", f"{rel}:{number}: marketing word '{match.group(0)}' (section 2)") - for match in filler.finditer(line): - report.add("language", f"{rel}:{number}: filler phrase '{match.group(0)}' (section 2)") - if line.startswith("#") and has_emoji(line): - report.add("language", f"{rel}:{number}: emoji in heading (section 2)") - if line.startswith("#"): - # Section 5 is about the title a reader sees in a list of - # documents. A dated section inside a research log is a - # chronology, not a title. - if first_heading and DATED_TITLE.search(line): - report.add("language", f"{rel}:{number}: dated title in a reader-facing doc (section 5)") - first_heading = False - - -CODE_SPAN = re.compile(r"`[^`\n]+`") - - -def blank_code_spans(text: str) -> str: - """Replace inline code with spaces, keeping offsets. - - A word in backticks is a mention: the style guide writes `robust` when it - means the word, and a research note writes a commit id as a value. Neither - is prose, and both were reported as prose. - """ - return CODE_SPAN.sub(lambda m: " " * len(m.group(0)), text) - - -def blank_fenced_blocks(text: str) -> str: - """Replace fenced blocks with spaces, keeping every offset in place. - - `hex_is_prose` needs to know whether a match sits in a contract example or - in running text, and it needs the character offsets to stay usable for the - line and backtick lookups that follow. - """ - def blank(match: re.Match) -> str: - return re.sub(r"[^\n]", " ", match.group(0)) - - return FENCED_BLOCK.sub(blank, text) - - -def check_leaks(root: Path, report: Report) -> None: - for path in iter_files(root): - if not is_markdown(path): - continue - rel = path.relative_to(root).as_posix() - text = path.read_text(encoding="utf-8", errors="ignore") - prose = blank_fenced_blocks(text) - ctx = FileContext( - url_spans=[(m.start(), m.end()) for m in URL_TOKEN.finditer(text)], - prose=prose, - masked=blank_code_spans(prose), - ) - for pattern, category, label, predicate in LEAK_PATTERNS: - for match in pattern.finditer(text): - # A pattern without a predicate describes prose, and a fenced - # block is not prose. The style guide shows each of these - # shapes as an example of what to avoid. - if predicate is None and not ctx.prose[match.start():match.end()].strip(): - continue - if predicate is not None and not predicate(text, match, ctx): - continue - report.add(category, f"{rel}: {label}: {match.group(0)[:40]}") - - -def check_internal_paths(root: Path, report: Report) -> None: - gitignore = root / ".gitignore" - text = gitignore.read_text(encoding="utf-8", errors="ignore") if gitignore.exists() else "" - missing = [pattern for pattern in INTERNAL_NOTE_GITIGNORE if pattern not in text] - if missing: - # Blocking in both phases. This is the first of the three layers that - # keep working notes out of a public repository, and it is the only one - # that acts before the note is committed at all. A repository that - # loses it stops preventing the mistake and starts detecting it, which - # is a different and much later guarantee. - report.add( - "hygiene", - ".gitignore: internal note paths not ignored — " + ", ".join(missing), - ) - if "!.github/engineering.md" in text: - report.add( - "hygiene", - ".gitignore: remove obsolete !.github/engineering.md unignore rule", - ) - if (root / "docs" / "acceptance-policy.md").exists() and not is_untracked_gitignored( - root, "docs/acceptance-policy.md" - ): - report.add( - "publish", - "docs/acceptance-policy.md: internal acceptance policy stays out of public repositories (section 5)", - ) - - for path in iter_files(root): - if not is_markdown(path): - continue - rel = path.relative_to(root).as_posix() - if INTERNAL_NOTE_DIRS.search(rel): - report.add("content", f"{rel}: plan/handoff directory in a public repo (section 5)") - if re.search(r"(^|/)(out|output)/[^/]+/", rel): - report.add("content", f"{rel}: committed output directory (section 5)") - - -def main(argv: list[str] | None = None) -> int: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--root", default=".", help="repository root (default: cwd)") - parser.add_argument( - "--phase", type=int, choices=(1, 2), default=1, - help="1 blocks leaks and broken links; 2 also blocks naming (default: 1)", - ) - parser.add_argument( - "--advisory", action="store_true", - help="report findings but always exit 0, for adopting a repository gradually", - ) - parser.add_argument( - "--visibility", choices=("public", "private"), default="public", - help="private demotes publication rules to notes; default treats the repository as public", - ) - args = parser.parse_args(argv) - - root = Path(args.root).resolve() - blocking_categories = set(PHASE_BLOCKING[args.phase]) - if args.visibility == "private": - blocking_categories -= PUBLICATION_CATEGORIES - report = Report(blocking_categories) - - check_naming(root, report) - check_readme(root, report) - check_links(root, report) - check_agent_files(root, report) - check_language(root, report) - check_docs_baseline(root, report, visibility=args.visibility) - check_doc_hygiene_advisory(root, report) - check_leaks(root, report) - check_internal_paths(root, report) - - blocking = report.blocking_findings() - advisory = report.advisory_findings() - - for category, message in blocking: - print(f"ERROR [{category}] {message}") - for category, message in advisory: - print(f"NOTE [{category}] {message}") - - print() - print( - f"phase {args.phase} ({args.visibility}): {len(blocking)} blocking, " - f"{len(advisory)} advisory. Advisory findings become blocking in phase 2." - ) - if args.visibility == "private": - print( - "private repository: publication rules report as notes; " - "they would block if this repository were published" - ) - if args.advisory: - # A repository adopts the rule set before it can satisfy it. Failing on - # the adoption pull request would make that pull request the first thing - # the check blocks, and the check would be removed instead of the - # findings fixed. The findings are still printed and still counted. - print("advisory run: exit status forced to 0") - return 0 - - return 1 if blocking else 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/scripts/test_style.py b/scripts/test_style.py deleted file mode 100644 index 4ac8572..0000000 --- a/scripts/test_style.py +++ /dev/null @@ -1,608 +0,0 @@ -"""Regression tests for the documentation checker. - -This checker runs against every repository that adopts the rule set, from one -copy or many, so a rule that silently stops working fails everywhere at once -and nobody notices. Each test here pins one decision that was easy to get -wrong: - - * the leak heuristics must not report the placeholder forms the guide asks - for, or the report fills with non-defects and gets ignored - * the .gitignore block must be required, not suggested, or the layer that - stops a note being committed disappears without a trace - * phase 1 must stay quiet about naming, or every open pull request goes red - the day the checker lands - -Run with `python scripts/test_style.py`. -""" - -from __future__ import annotations - -import importlib.util -import io -import subprocess -import tempfile -import unittest -from contextlib import redirect_stdout -from pathlib import Path - -_spec = importlib.util.spec_from_file_location( - "check_style", Path(__file__).with_name("check_style.py") -) -check_style = importlib.util.module_from_spec(_spec) -_spec.loader.exec_module(check_style) - -GITIGNORE_BLOCK = "\n".join(check_style.INTERNAL_NOTE_GITIGNORE) + "\n" -SHA = "8e8ec51884bf8abb0a7ca699fc2da39fad134f89" - - -class CheckerTestCase(unittest.TestCase): - def setUp(self) -> None: - self._temp = tempfile.TemporaryDirectory() - self.addCleanup(self._temp.cleanup) - self.root = Path(self._temp.name) - self.write("README.md", "# t\n") - - def write(self, rel: str, text: str) -> Path: - path = self.root / rel - path.parent.mkdir(parents=True, exist_ok=True) - path.write_text(text, encoding="utf-8") - return path - - def git(self, *args: str) -> None: - subprocess.run( - ["git", "-C", str(self.root), *args], check=True, capture_output=True - ) - - def report(self, phase: int = 2) -> check_style.Report: - return check_style.Report(check_style.PHASE_BLOCKING[phase]) - - def messages(self, report: check_style.Report, category: str) -> list[str]: - return [m for c, m in report.findings if c == category] - - def run_main(self, *extra: str) -> tuple[int, str]: - with redirect_stdout(io.StringIO()) as captured: - code = check_style.main(["--root", str(self.root), "--phase", "2", *extra]) - return code, captured.getvalue() - - -class NamingTest(CheckerTestCase): - def test_underscore_is_flagged(self): - self.write("docs/hk_equity_runtime.md", "# x\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertTrue(any("underscores" in m for m in self.messages(report, "naming"))) - - def test_github_templates_keep_their_underscore(self): - # GitHub only reads pull_request_template.md / issue_template.md with the - # underscore; renaming them to hyphens disables the template. - self.write(".github/pull_request_template.md", "## Summary\n") - self.write(".github/issue_template.md", "---\nname: bug\n---\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertEqual(self.messages(report, "naming"), []) - - def test_date_is_flagged_outside_record_paths(self): - self.write("docs/2026-10-30-release.md", "# x\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertTrue(any("dates belong" in m for m in self.messages(report, "naming"))) - - def test_date_is_allowed_in_record_paths(self): - self.write("docs/research/2026-10-30-revision.md", "# x\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertFalse(any("dates belong" in m for m in self.messages(report, "naming"))) - - def test_en_suffix_is_flagged(self): - self.write("docs/runtime-baseline.en.md", "# x\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertTrue(any(".en.md is not a suffix" in m for m in self.messages(report, "naming"))) - - def test_root_proposal_has_its_owner_requested_name(self): - self.write("PROPOSAL.md", "# Proposed governance change\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertEqual(self.messages(report, "naming"), []) - - def test_nested_proposal_still_requires_lowercase(self): - self.write("docs/PROPOSAL.md", "# x\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertTrue(any("lowercase" in m for m in self.messages(report, "naming"))) - - def test_uppercase_convention_is_allowed(self): - self.write("SECURITY.md", "# x\n") - report = self.report() - check_style.check_naming(self.root, report) - self.assertEqual(self.messages(report, "naming"), []) - - -class LeakTest(CheckerTestCase): - def findings(self, text: str) -> list[str]: - self.write("docs/page.md", text) - report = self.report() - check_style.check_leaks(self.root, report) - return [m for _, m in report.findings] - - def test_home_path_is_reported(self): - found = self.findings("Config lives in `/Users/someone/projects/app`.\n") - self.assertTrue(any("absolute home path" in m for m in found)) - - def test_placeholder_bucket_is_not_reported(self): - for text in ( - "Write to `gs:///strategy-artifacts/x.csv`.\n", - "The default is `gs://.../universe.csv`.\n", - "bucket 一般是 `gs://run-sources-${PROJECT_ID}-${REGION}`\n", - "Try `gs://your-bucket/path/to/signal.json`.\n", - "Try `gs://example-bucket/releases/current`.\n", - ): - with self.subTest(text=text): - self.assertEqual(self.findings(text), []) - - def test_real_bucket_is_reported(self): - found = self.findings("Default root `gs://acme-prod-inputs/research/v2/input/`.\n") - self.assertTrue(any("non-placeholder bucket" in m for m in found)) - - def test_placeholder_subdomain_is_not_reported(self): - # Section 13 asks for a placeholder where a deployed subdomain would - # appear. Reporting the guide's own recommended form is how a check - # teaches people to ignore it. - for text in ( - "Open https://YOUR_SUBDOMAIN.workers.dev/admin.\n", - "Set the host to https://your-subdomain.workers.dev.\n", - "The example uses https://xxx.workers.dev.\n", - "Deploy to https://.workers.dev.\n", - ): - with self.subTest(text=text): - self.assertEqual(self.findings(text), []) - - def test_real_subdomain_is_reported(self): - found = self.findings("Open https://pigbibi.workers.dev/admin.\n") - self.assertTrue(any("personal subdomain" in m for m in found)) - - def test_subdomain_in_a_fenced_block_is_an_example(self): - # The guide shows forbidden shapes inside fenced blocks. Installing a - # predicate on the rule must not start reporting those examples, or a - # narrowing fix silently widens what blocks. - text = ( - "Do not publish a real host:\n\n" - "```markdown\n" - "Do not publish https://demo.workers.dev as a real host.\n" - "```\n" - ) - self.assertEqual(self.findings(text), []) - - def test_sha_in_url_is_not_reported(self): - text = ( - "- [Store](https://github.com/QuantStrategyLab/QuantPlatformKit/blob/" - f"{SHA}/src/store.py)\n" - ) - self.assertEqual(self.findings(text), []) - - def test_sha_in_code_span_is_not_reported(self): - self.assertEqual(self.findings(f"Pinned at `{SHA}`.\n"), []) - - def test_sha_in_fenced_block_is_not_reported(self): - text = f'```json\n{{"config_revision": "{SHA}"}}\n```\n' - self.assertEqual(self.findings(text), []) - - def test_sha_in_link_label_is_not_reported(self): - text = f"[{SHA}](https://github.com/QuantStrategyLab/x/commit/{SHA})\n" - self.assertEqual(self.findings(text), []) - - def test_repeated_character_placeholder_is_not_reported(self): - self.assertEqual(self.findings(f"`{'0' * 40}`\n"), []) - - def test_bare_sha_in_prose_is_reported(self): - found = self.findings(f"Merged from PR #712 (head {SHA}, open).\n") - self.assertTrue(any("40-character hex string in prose" in m for m in found)) - - -class InternalNoteTest(CheckerTestCase): - def test_missing_gitignore_entries_are_hygiene(self): - report = self.report() - check_style.check_internal_paths(self.root, report) - found = self.messages(report, "hygiene") - self.assertEqual(len(found), 1) - self.assertIn("docs/audits/", found[0]) - - def test_complete_gitignore_passes(self): - self.write(".gitignore", GITIGNORE_BLOCK) - report = self.report() - check_style.check_internal_paths(self.root, report) - self.assertEqual(self.messages(report, "hygiene"), []) - - def test_hygiene_blocks_in_phase_one(self): - report = self.report(phase=1) - check_style.check_internal_paths(self.root, report) - self.assertEqual(len(report.blocking_findings()), 1) - - def test_audit_directory_is_reported(self): - self.write(".gitignore", GITIGNORE_BLOCK) - self.write("docs/audits/2026-10-30-review.md", "# x\n") - report = self.report() - check_style.check_internal_paths(self.root, report) - self.assertTrue(any("plan/handoff directory" in m for m in self.messages(report, "content"))) - - def test_changes_directory_and_obsolete_engineering_rule_are_reported(self): - self.write(".gitignore", GITIGNORE_BLOCK + "!.github/engineering.md\n") - self.write("docs/changes/task/change-card.md", "# x\n") - report = self.report() - check_style.check_internal_paths(self.root, report) - self.assertTrue(any("engineering.md" in m for m in self.messages(report, "hygiene"))) - self.assertTrue(any("plan/handoff directory" in m for m in self.messages(report, "content"))) - - def test_acceptance_policy_blocks_public_only(self): - self.write(".gitignore", GITIGNORE_BLOCK) - self.write("docs/acceptance-policy.md", "# policy\n") - pub = self.report(phase=2) - check_style.check_internal_paths(self.root, pub) - self.assertTrue(any(cat == "publish" for cat, _ in pub.blocking_findings())) - _, output = self.run_main("--visibility", "private") - self.assertTrue( - any(line.startswith("NOTE") and "acceptance-policy.md" in line for line in output.splitlines()) - ) - - -class AgentFileTest(CheckerTestCase): - def test_missing_agents_md_is_reported(self): - report = self.report() - check_style.check_agent_files(self.root, report) - self.assertTrue(any("AGENTS.md is missing" in m for m in self.messages(report, "agents"))) - - def test_untracked_agents_md_is_reported(self): - self.write("AGENTS.md", "# rules\n") - self.write("CLAUDE.md", "@AGENTS.md\n") - self.git("init", "--quiet") - report = self.report() - check_style.check_agent_files(self.root, report) - found = self.messages(report, "agents") - self.assertTrue(any("not tracked by Git" in m for m in found)) - - def test_tracked_agents_md_passes(self): - self.write("AGENTS.md", "# rules\n\n" + check_style.CONTENT_BOUNDARY + "\n") - self.write("CLAUDE.md", "@AGENTS.md\n") - self.git("init", "--quiet") - self.git("add", "AGENTS.md", "CLAUDE.md") - report = self.report() - check_style.check_agent_files(self.root, report) - self.assertEqual(self.messages(report, "agents"), []) - - def test_content_boundary_block_is_required(self): - # A rule that lives only in the guide is read by whoever opens the - # guide. The block is the copy that reaches an agent before it writes, - # so its absence is a finding rather than a preference. - self.write("AGENTS.md", "# rules\n") - self.write("CLAUDE.md", "@AGENTS.md\n") - self.git("init", "--quiet") - self.git("add", "AGENTS.md", "CLAUDE.md") - report = self.report() - check_style.check_agent_files(self.root, report) - self.assertTrue( - any("content-boundary" in m for m in self.messages(report, "agents")) - ) - - def test_claude_must_be_the_single_line(self): - self.write("AGENTS.md", "# rules\n") - self.write("CLAUDE.md", "# rules\n\ncopied body\n") - self.git("init", "--quiet") - self.git("add", "AGENTS.md", "CLAUDE.md") - report = self.report() - check_style.check_agent_files(self.root, report) - self.assertTrue(any("@AGENTS.md" in m for m in self.messages(report, "agents"))) - - def test_retired_engineering_doc_and_long_agents_are_reported(self): - long_body = "# rules\n" + "\n".join(f"line {i}" for i in range(125)) + "\n" + check_style.CONTENT_BOUNDARY + "\n" - self.write("AGENTS.md", long_body) - self.write("CLAUDE.md", "@AGENTS.md\n") - self.write(".github/engineering.md", "# legacy\n") - self.git("init", "--quiet") - self.git("add", "AGENTS.md", "CLAUDE.md") - report = self.report() - check_style.check_agent_files(self.root, report) - found = self.messages(report, "agents") - self.assertTrue(any("engineering.md is retired" in m for m in found)) - self.assertTrue(any("exceeds 120 lines" in m for m in found)) - - -class LanguageTest(CheckerTestCase): - """Section 2 — the word list reports usage, not mention.""" - - def test_marketing_word_in_prose_is_reported(self): - self.write("docs/note.md", "# Note\n\nA seamless handover.\n") - report = self.report() - check_style.check_language(self.root, report) - self.assertTrue(any("seamless" in m for m in self.messages(report, "language"))) - - def test_marketing_word_in_a_code_span_is_a_mention(self): - # The guide writes the words it forbids. Reporting them there makes the - # guide the largest offender in the organisation. - self.write("docs/note.md", "# Note\n\nAvoid `seamless` and `world-class`.\n") - report = self.report() - check_style.check_language(self.root, report) - self.assertEqual(self.messages(report, "language"), []) - - def test_marketing_word_in_a_fenced_block_is_a_mention(self): - self.write("docs/note.md", "# Note\n\n```markdown\nA seamless handover.\n```\n") - report = self.report() - check_style.check_language(self.root, report) - self.assertEqual(self.messages(report, "language"), []) - - def test_technical_terms_are_not_marketing(self): - # A robust estimator and statistical power are terms of art; the list - # that flagged them reported twelve non-defects in the research docs. - self.write( - "docs/note.md", - "# Note\n\nThe robust plateau holds, and the test is more powerful " - "than the Reality Check.\n", - ) - report = self.report() - check_style.check_language(self.root, report) - self.assertEqual(self.messages(report, "language"), []) - - def test_dated_section_heading_is_not_a_dated_title(self): - self.write( - "docs/note.md", - "# Contract acceptance\n\n## Bounded run acceptance (2026-10-05)\n", - ) - report = self.report() - check_style.check_language(self.root, report) - self.assertEqual(self.messages(report, "language"), []) - - def test_dated_document_title_is_reported(self): - self.write("docs/note.md", "# Console audit, 2026-10-15\n") - report = self.report() - check_style.check_language(self.root, report) - self.assertTrue(any("dated title" in m for m in self.messages(report, "language"))) - - -class CrossRepositoryTest(CheckerTestCase): - def test_cross_repository_reference_never_blocks(self): - # The scheduled link job resolves these against the sibling's default - # branch. A finding that says "not checked" cannot block a merge. - # Two levels up: one level stays inside the repository, and on macOS - # `/tmp` resolves through a symlink, so a one-level fixture passed here - # and failed on the Linux runner. - self.write("docs/note.md", "# Note\n\n[peer](../../Sibling/docs/x.md)\n") - report = self.report(phase=2) - check_style.check_links(self.root, report) - self.assertTrue(any(c == "xref" for c, _ in report.findings)) - self.assertEqual(report.blocking_findings(), []) - - -class DocsBaselineTest(CheckerTestCase): - def test_public_repository_needs_the_reader_file_set(self): - self.write("LICENSE", "MIT\n") - report = self.report() - check_style.check_docs_baseline(self.root, report, visibility="public") - found = self.messages(report, "docs") - self.assertTrue(any("CONTRIBUTING.md" in m for m in found)) - self.assertTrue(any("CODE_OF_CONDUCT.md" in m for m in found)) - - def test_public_repository_without_license_is_reported(self): - report = self.report() - check_style.check_docs_baseline(self.root, report, visibility="public") - found = self.messages(report, "docs") - self.assertTrue(any("LICENSE" in m for m in found)) - - def test_public_repository_with_the_file_set_passes(self): - self.write("LICENSE", "MIT\n") - for name in ("CONTRIBUTING.md", "SECURITY.md", "CODE_OF_CONDUCT.md"): - self.write(name, "# " + name + "\n") - report = self.report() - check_style.check_docs_baseline(self.root, report, visibility="public") - self.assertEqual(self.messages(report, "docs"), []) - - def test_private_repository_needs_only_the_credential_doc(self): - self.write("SECURITY.md", "# Credentials\n") - report = self.report() - check_style.check_docs_baseline(self.root, report, visibility="private") - self.assertEqual(self.messages(report, "docs"), []) - - def test_private_repository_without_the_credential_doc_is_reported(self): - report = self.report() - check_style.check_docs_baseline(self.root, report, visibility="private") - self.assertTrue(any("SECURITY.md" in m for m in self.messages(report, "docs"))) - - -class PhaseTest(CheckerTestCase): - def test_phase_one_does_not_block_naming(self): - self.write("docs/hk_equity_runtime.md", "# x\n") - report = self.report(phase=1) - check_style.check_naming(self.root, report) - self.assertTrue(report.findings) - self.assertEqual(report.blocking_findings(), []) - - def test_phase_two_blocks_naming(self): - self.write("docs/hk_equity_runtime.md", "# x\n") - report = self.report(phase=2) - check_style.check_naming(self.root, report) - self.assertTrue(report.blocking_findings()) - - -class AdvisoryModeTest(CheckerTestCase): - """Adoption has to be possible before compliance. - - A repository adds the check while it still has findings. If that pull - request goes red, the check is removed instead of the findings fixed, and - the repository ends up with neither. - """ - - def test_advisory_reports_the_findings_it_would_have_blocked_on(self): - self.write("docs/hk_equity_runtime.md", "# x\n") - code, output = self.run_main("--advisory") - self.assertEqual(code, 0) - self.assertIn("underscores", output) - self.assertIn("advisory run", output) - - def test_without_advisory_the_same_findings_still_fail(self): - self.write("docs/hk_equity_runtime.md", "# x\n") - code, _ = self.run_main() - self.assertEqual(code, 1) - - -class VisibilityTest(CheckerTestCase): - """The publication rules govern publishing, not existence. - - A private repository publishes nothing, and a deployment document may need - the real host name to be usable — an OAuth callback URL with a placeholder - in it cannot be configured. Blocking there would force a document to be - wrong in order to pass. - """ - - def deploy_doc(self) -> None: - self.write( - "docs/deployment.md", - "Callback: https://invoice.pigbibi.workers.dev/auth/github/callback\n" - "Data lives in /home/ubuntu/app/data.\n", - ) - - def test_public_repository_blocks_published_identifiers(self): - self.deploy_doc() - _, output = self.run_main("--visibility", "public") - self.assertTrue( - any(line.startswith("ERROR") and "personal subdomain" in line - for line in output.splitlines()) - ) - - def test_private_repository_reports_them_without_blocking(self): - self.deploy_doc() - _, output = self.run_main("--visibility", "private") - self.assertFalse( - any(line.startswith("ERROR") and "personal subdomain" in line - for line in output.splitlines()) - ) - self.assertTrue( - any(line.startswith("NOTE") and "personal subdomain" in line - for line in output.splitlines()) - ) - self.assertIn("private repository", output) - - def test_the_default_is_public(self): - # Forgetting the flag must not switch a leak rule off. - self.deploy_doc() - _, output = self.run_main() - self.assertIn("(public)", output) - self.assertTrue( - any(line.startswith("ERROR") and "personal subdomain" in line - for line in output.splitlines()) - ) - - def test_private_still_blocks_a_missing_security_policy(self): - # Visibility narrows the publication rules only; it is not a blanket - # exemption for everything the checker knows. - _, output = self.run_main("--visibility", "private") - self.assertTrue( - any(line.startswith("ERROR") and "SECURITY.md" in line - for line in output.splitlines()) - ) - - - - -class ReadmeHygieneTest(CheckerTestCase): - def test_readme_patch_prose_phase_heading_and_post_license_block(self): - self.write( - "README.md", - "# Demo\n\n## P0-P6 Runtime Authority\n\nIn this PR the gate remains unchanged.\n\n## License\n\nMIT\n\n## Weekly secret scan\n\nExtra\n", - ) - self.write( - "README.zh-CN.md", - "# 示例\n\n## 许可证\n\nMIT\n\n## 每周密钥扫描\n\nExtra\n", - ) - report = self.report(phase=2) - check_style.check_readme(self.root, report) - blocking = [m for c, m in report.blocking_findings() if c == "content"] - self.assertTrue(any("phase code heading" in m for m in blocking)) - self.assertTrue(any("In this PR" in m for m in blocking)) - self.assertTrue(any("after License" in m and "README.md" in m for m in blocking)) - self.assertTrue(any("after License" in m and "README.zh-CN.md" in m for m in blocking)) - - def test_clean_readme_and_fenced_examples_pass(self): - self.write( - "README.md", - "# Demo\n\nUses `remains unchanged` in code.\n\n```markdown\nIn this PR\n```\n\n## Weekly secret scan\n\nRuns weekly.\n\n## License\n\nMIT\n", - ) - report = self.report(phase=2) - check_style.check_readme(self.root, report) - self.assertEqual(report.blocking_findings(), []) - - -class ExampleMentionTest(CheckerTestCase): - """Rules must not fire on text that is showing what not to do. - - Both cases below came from real repositories: a reference page documenting - how to attach an image, and a design guide listing the filler words it - forbids. Each was blocked by the rule it was teaching the reader to obey. - """ - - def test_a_link_inside_inline_code_is_an_example(self): - self.write( - "docs/pr-attachments.md", - "# t\n\nReference it as `![alt](./settings-after.png)` to place it inline.\n", - ) - report = self.report() - # The temporary directory is not resolved; check_links resolves the - # target, so an unresolved root would look like a sibling checkout. - check_style.check_links(self.root.resolve(), report) - self.assertEqual(self.messages(report, "links"), []) - - def test_a_real_broken_link_is_still_reported(self): - self.write("docs/page.md", "# t\n\nSee [the settings](./settings-after.png).\n") - report = self.report() - check_style.check_links(self.root.resolve(), report) - self.assertEqual(len(self.messages(report, "links")), 1) - - def test_a_quoted_word_list_is_not_marketing_language(self): - self.write( - "docs/design.md", - '# t\n\n* **NO filler verbs.** "Elevate", "Seamless", "Unleash"' - " \u2192 concrete verbs only.\n", - ) - report = self.report() - check_style.check_language(self.root, report) - self.assertEqual(self.messages(report, "language"), []) - - def test_one_quoted_word_is_still_a_claim(self): - # The exemption is about lists. A single quoted word is a word used. - self.write("docs/page.md", "# t\n\nOur 'Seamless' onboarding flow.\n") - report = self.report() - check_style.check_language(self.root, report) - self.assertEqual(len(self.messages(report, "language")), 1) - - -class GitVisibilityTest(CheckerTestCase): - def test_gitignored_notes_do_not_block_while_staged_notes_do(self): - self.write(".gitignore", GITIGNORE_BLOCK) - self.write("AGENTS.md", "# rules\n\n" + check_style.CONTENT_BOUNDARY + "\n") - self.write("CLAUDE.md", "@AGENTS.md\n") - self.write("LICENSE", "MIT\n") - self.write("CONTRIBUTING.md", "# Contributing\n") - self.write("CODE_OF_CONDUCT.md", "# Code of Conduct\n") - self.write("SECURITY.md", "# Security\n") - self.git("init", "--quiet") - self.git("config", "user.name", "Test User") - self.git("config", "user.email", "test@example.com") - self.git( - "add", - ".gitignore", "AGENTS.md", "CLAUDE.md", - "LICENSE", "CONTRIBUTING.md", "CODE_OF_CONDUCT.md", "SECURITY.md", "README.md", - ) - - self.write("docs/changes/task/change-card.md", "# Card\n") - self.write("docs/plans/draft.md", "# Draft\n") - self.write("docs/acceptance-policy.md", "# Policy\n") - - code, output = self.run_main() - self.assertEqual(code, 0, msg=output) - - self.git("add", "-f", "docs/changes/task/change-card.md", "docs/acceptance-policy.md") - blocked_code, blocked_output = self.run_main() - self.assertEqual(blocked_code, 1, msg=blocked_output) - self.assertIn("docs/changes/task/change-card.md", blocked_output) - self.assertIn("docs/acceptance-policy.md", blocked_output) - - -if __name__ == "__main__": - unittest.main(verbosity=2)