code-quality

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code Quality Check

代码质量检查

This skill runs code quality tools (ruff, mypy, pytest) and optionally simplifies code.
本技能可运行代码质量工具(ruff、mypy、pytest)并可选择简化代码。

Usage

使用方法

/code-quality [options]
/code-quality [选项]

Options

选项

  • --all
    or
    -a
    : Run on all synapse/ files (default: recently modified files only)
  • --no-simplify
    : Skip code-simplifier agent (by default, code-simplifier runs after checks pass)
  • --fix
    or
    -f
    : Auto-fix ruff issues with
    --fix
    flag
  • --test
    or
    -t
    : Run pytest after linting passes
  • --full
    : Run all checks including tests (equivalent to
    --fix --test
    )
  • --all
    -a
    : 在所有synapse/文件上运行(默认:仅最近修改的文件)
  • --no-simplify
    : 跳过代码简化Agent(默认情况下,检查通过后会运行代码简化工具)
  • --fix
    -f
    : 使用
    --fix
    标志自动修复ruff问题
  • --test
    -t
    : 代码检查通过后运行pytest
  • --full
    : 运行包括测试在内的所有检查(等效于
    --fix --test

Workflow

工作流程

Step 1: Identify Target Files

步骤1:确定目标文件

If
--all
flag is provided:
  • Target all files in
    synapse/
    directory
Otherwise:
  • Run
    git diff --name-only HEAD~1
    to get recently modified files
  • Filter to only
    .py
    files in
    synapse/
    or
    tests/
如果提供了
--all
标志:
  • 目标为
    synapse/
    目录下的所有文件
否则:
  • 运行
    git diff --name-only HEAD~1
    获取最近修改的文件
  • 筛选出
    synapse/
    tests/
    目录下的
    .py
    文件

Step 2: Run Ruff Linter

步骤2:运行Ruff代码检查器

bash
ruff check [files]
If
--fix
flag is provided:
bash
ruff check --fix [files]
Report any errors found. If errors remain after fix, stop and report.
bash
ruff check [files]
如果提供了
--fix
标志:
bash
ruff check --fix [files]
报告发现的所有错误。如果修复后仍有错误,停止操作并报告。

Step 3: Run Mypy Type Checker

步骤3:运行Mypy类型检查器

bash
uv run mypy [files]
Report any type errors found. If errors exist, stop and report.
bash
uv run mypy [files]
报告发现的所有类型错误。如果存在错误,停止操作并报告。

Step 4: Run Tests (if --test or --full)

步骤4:运行测试(若使用--test或--full)

If
--test
or
--full
flag is provided AND ruff/mypy passed:
bash
pytest
For running specific tests related to modified files:
bash
pytest tests/ -v
Report any test failures.
如果提供了
--test
--full
标志,且ruff/mypy检查通过:
bash
pytest
针对修改文件运行特定测试:
bash
pytest tests/ -v
报告所有测试失败情况。

Step 5: Run Code Simplifier (Default)

步骤5:运行代码简化工具(默认)

By default, run code-simplifier after all checks pass. Skip with
--no-simplify
flag.
Use the
code-simplifier:code-simplifier
agent to simplify the recently modified code:
Task tool with subagent_type: code-simplifier:code-simplifier
Prompt: Simplify and refine the recently modified code in [files].
Look for opportunities to reduce duplication, simplify conditionals,
and improve readability while maintaining all functionality.
默认情况下,所有检查通过后会运行代码简化工具。可使用
--no-simplify
标志跳过此步骤。
使用
code-simplifier:code-simplifier
Agent简化最近修改的代码:
Task tool with subagent_type: code-simplifier:code-simplifier
Prompt: 简化并优化[files]中最近修改的代码。
寻找减少代码重复、简化条件判断、
提升可读性同时保留所有功能的优化点。

Step 6: Report Results

步骤6:报告结果

Summarize:
  • Number of files checked
  • Ruff status (pass/fail, errors fixed if --fix)
  • Mypy status (pass/fail)
  • Pytest status (pass/fail, skipped if not run)
  • Code simplifier status (run by default, skipped if --no-simplify)
汇总内容:
  • 已检查的文件数量
  • Ruff状态(通过/失败,若使用--fix则包含已修复错误数)
  • Mypy状态(通过/失败)
  • Pytest状态(通过/失败,未运行则显示已跳过)
  • 代码简化工具状态(默认运行,若使用--no-simplify则显示已跳过)

Error Handling

错误处理

When errors are found:
  1. Ruff errors: If
    --fix
    is provided, attempt auto-fix first. Report remaining errors with file:line format.
  2. Mypy errors: Report type errors with file:line format. Suggest fixes based on error messages.
  3. Test failures: Report failed test names and assertion errors. Do NOT proceed to simplification if tests fail.
发现错误时:
  1. Ruff错误:如果提供了
    --fix
    ,先尝试自动修复。以文件:行号的格式报告剩余错误。
  2. Mypy错误:以文件:行号的格式报告类型错误。根据错误信息给出修复建议。
  3. 测试失败:报告失败的测试名称和断言错误。如果测试失败,不继续执行代码简化步骤。

Examples

示例

Basic check on recent changes

对最近修改内容进行基础检查

/code-quality
/code-quality

Check all files with auto-fix

对所有文件进行检查并自动修复

/code-quality --all --fix
/code-quality --all --fix

Full quality check with tests

包含测试的完整质量检查

/code-quality --full
/code-quality --full

Run with tests only

仅运行测试

/code-quality --test
/code-quality --test

Skip code simplification

跳过代码简化

/code-quality --no-simplify
/code-quality --no-simplify

Shorthand

简写命令

/lint          # Same as /code-quality (includes simplification)
/check         # Same as /code-quality
/lint -a -f    # All files with auto-fix
/lint -t       # With tests
/lint --full   # Full check (--fix --test)
/lint --no-simplify  # Skip simplification
/lint          # 与/code-quality功能相同(包含代码简化)
/check         # 与/code-quality功能相同
/lint -a -f    # 对所有文件进行检查并自动修复
/lint -t       # 附带测试
/lint --full   # 完整检查(--fix --test)
/lint --no-simplify  # 跳过代码简化

Tool Configuration

工具配置

This project uses the following configurations (from pyproject.toml):
本项目使用以下配置(来自pyproject.toml):

Ruff

Ruff

  • Target: Python 3.10
  • Line length: 88
  • Rules: E, F, I, UP, B, SIM
  • Ignored: E501, B008
  • Excluded: synapse/proto/a2a_pb2*.py
  • 目标版本:Python 3.10
  • 行长度:88
  • 规则:E、F、I、UP、B、SIM
  • 忽略规则:E501、B008
  • 排除文件:synapse/proto/a2a_pb2*.py

Mypy

Mypy

  • Strict mode with disallow_untyped_defs
  • Tests have relaxed rules (ignore_errors)
  • Proto files excluded
  • 严格模式,禁用无类型定义
  • 测试文件规则放宽(忽略错误)
  • 排除Proto文件

Pytest

Pytest

  • asyncio_mode: auto
  • Run with:
    pytest
    or
    pytest tests/ -v
  • asyncio_mode: auto
  • 运行命令:
    pytest
    pytest tests/ -v