AgentSkillsCN

quality-gate

将计划模式下的分析结果转化为正式的规格文档,为每个文档赋予恰当的ID、元数据,并采用模板化的内容填充。

SKILL.md
--- frontmatter
name: quality-gate
description: Runs lint, typecheck, and test quality checks before marking a task as completed, updating state and regenerating progress reports

Quality Gate

Purpose

Enforce quality standards before task completion by running lint, typecheck, and test checks. Records results, blocks completion on failures, and regenerates progress reports after updates.

Patterns

You are the quality gate enforcement engine for the Task Master plugin. Your job is to run quality checks on completed work, record the results, and only mark tasks as completed when all required checks pass.

Inputs

You will receive:

  1. Task ID - The task to run quality checks on (e.g., "T-003")
  2. State file path - Path to the state.json file containing the task

Process

Step 1: Read Task State

Read the state.json file at the provided path. Find the task matching the given Task ID.

Validate:

  • The task exists in the state file
  • The task status is in-progress or pending (not already completed or cancelled)
  • If the task is blocked, report which tasks must complete first and exit

Step 2: Determine Checks to Run

Look for configuration in this order:

Priority 1: Project Config File

Check for .claude/task-master.config.json in the project root:

json
{
  "qualityGate": {
    "lint": { "command": "pnpm lint", "required": true },
    "typecheck": { "command": "pnpm typecheck", "required": true },
    "tests": { "command": "pnpm test", "required": true },
    "coverage": { "threshold": 90, "required": false }
  }
}

If this file exists, use its configuration.

Priority 2: Auto-Detection

If no config file exists, auto-detect the project's tooling:

  1. Package manager detection:

    • If pnpm-lock.yaml exists -> use pnpm
    • If yarn.lock exists -> use yarn
    • If package-lock.json exists -> use npm
    • Default: npm
  2. Script detection (read package.json):

    • If scripts.lint exists -> lint command = {pm} run lint
    • If scripts.typecheck exists -> typecheck command = {pm} run typecheck
    • If scripts.test exists -> test command = {pm} run test
    • If scripts.test:coverage exists -> coverage command = {pm} run test:coverage
  3. Tool detection (if scripts don't exist):

    • Check for eslint.config.* or .eslintrc.* -> npx eslint .
    • Check for tsconfig.json -> npx tsc --noEmit
    • Check for vitest.config.* -> npx vitest run
    • Check for jest.config.* -> npx jest
  4. Monorepo detection:

    • If turbo.json exists, consider using turbo run lint/typecheck/test
    • If the task affects a specific package, scope the commands to that package
    • Example: If task files are in packages/core/, run cd packages/core && pnpm run test

Step 3: Run Quality Checks

Execute each check sequentially. For each check:

  1. Announce what is being run: "Running lint check..."
  2. Execute the command
  3. Capture the exit code and output
  4. Record the result:
    • status: "pass" (exit code 0) or "fail" (non-zero exit code)
    • timestamp: Current ISO 8601 timestamp
    • details: First 500 characters of output if failed, empty if passed
    • coverage: (only for test check) Extract coverage percentage if available

Run checks in this order:

  1. complexity-check - Verify task complexity ≤ 4 (pre-flight check)
  2. lint - Code style and quality
  3. typecheck - Type safety
  4. tests - Test suite execution
  5. test-existence - Verify new tests were written for this task

If a required check fails, continue running remaining checks (to give a complete picture) but the overall gate will fail.

Complexity Check (Pre-flight)

CRITICAL: This is the first check and acts as a safety net. Before running any other quality checks, verify that the task's complexity score is ≤ 4.

  • Read the task's complexity field from state.json
  • If complexity > 4: gate FAILS immediately with message:
    code
    FAIL: Task complexity {score} exceeds maximum 4.
    This task is too complex for atomic execution and must be decomposed first.
    Use /replan to split this task into smaller tasks with complexity ≤ 4.
    
  • If complexity ≤ 4: check PASSES, continue to lint check

This prevents any task that slipped through the multi-pass decomposition from being completed without proper splitting.

Test Existence Check

CRITICAL: No tests = not done. After running the test suite, verify that the task actually includes new or modified test files. A task that produces code but adds zero tests MUST NOT be marked as completed.

To verify test existence:

  1. Check the task's staged or modified files for test files (files matching *.test.ts, *.spec.ts, *.test.tsx, *.spec.tsx, or within a __tests__/ directory)
  2. If the task is in the core, integration, or setup phase and has NO test files, the quality gate FAILS
  3. The docs and cleanup phases are exempt from this check (docs do not require tests; cleanup tasks that only remove code may not need new tests)

Report test existence as part of the quality gate output:

  • tests-added: YES (3 new test files) — gate passes
  • tests-added: NO — gate fails with message: "No new tests found. Every code change must include tests. No tests = not done."

Step 4: Record Results

Update the task's qualityGate field in state.json:

json
{
  "qualityGate": {
    "lint": {
      "status": "pass",
      "timestamp": "2025-01-15T14:30:00.000Z"
    },
    "typecheck": {
      "status": "pass",
      "timestamp": "2025-01-15T14:30:15.000Z"
    },
    "tests": {
      "status": "pass",
      "timestamp": "2025-01-15T14:30:45.000Z",
      "coverage": 94.2
    }
  }
}

Or for failures:

json
{
  "qualityGate": {
    "lint": {
      "status": "fail",
      "timestamp": "2025-01-15T14:30:00.000Z",
      "details": "Error: 3 lint errors found\n  src/models/user.ts:15 - no-unused-vars\n  src/models/user.ts:23 - prefer-const\n  src/services/auth.ts:8 - no-explicit-any"
    },
    "typecheck": {
      "status": "pass",
      "timestamp": "2025-01-15T14:30:15.000Z"
    },
    "tests": {
      "status": "fail",
      "timestamp": "2025-01-15T14:30:45.000Z",
      "details": "FAIL src/models/user.test.ts > User Model > should validate email format\n  Expected: true, Received: false"
    }
  }
}

Step 5: Evaluate Results

All Required Checks Pass

  1. Update the task's status to "completed"
  2. Set timestamps.completed to current ISO timestamp
  3. Update the summary object in state.json:
    • Decrement pending or inProgress (depending on previous status)
    • Increment completed
  4. Check if any tasks that were blocked can now be unblocked:
    • For each task with status blocked or pending:
      • Check if all tasks in its blockedBy array are now completed
      • If yes, the task is now ready (keep as pending but note it's unblocked)
  5. Proceed to Step 6

Any Required Check Fails

  1. Keep the task's status as in-progress
  2. Report failures with details and suggested fixes
  3. Do NOT proceed to Step 6 (TODOs regeneration)

Step 6: Regenerate TODOs.md

If the task was marked as completed, regenerate the TODOs.md file:

  1. Read the current state.json
  2. Recalculate progress: completed/total (percentage%)
  3. Update the markdown checklist:
    • Completed tasks: - [x] **T-001** (complexity: 2) - Task title [DONE]
    • Pending tasks: - [ ] **T-002** (complexity: 5) - Task title
    • Blocked tasks: - [ ] **T-003** (complexity: 4) - Task title [BLOCKED by T-002]
    • In-progress tasks: - [ ] **T-004** (complexity: 3) - Task title [IN PROGRESS]
  4. Update the progress header
  5. Write the updated TODOs.md

Step 7: Check Epic Completion

After updating the task:

  1. Check if ALL tasks in the state.json are completed
  2. If yes: a. The epic/spec is fully complete b. Read the spec's metadata.json c. Update metadata status to "completed" d. Set metadata completed timestamp e. Update .claude/specs/index.json entry status to "completed" f. Update .claude/tasks/index.json entry status to "completed" and progress g. Report epic completion

Output

All Checks Pass

code
Quality Gate Results for T-003
==============================

  complexity:   PASS (3/4 max)
  lint:         PASS
  typecheck:    PASS
  tests:        PASS (coverage: 94.2%)
  tests-added:  YES (2 new test files)

All quality checks passed!

Task T-003 marked as COMPLETED.
Progress: 3/8 tasks (37.5%)

Newly unblocked tasks:
  - T-005 (complexity: 4) - Create search API endpoint
  - T-006 (complexity: 3) - Add search page component

Suggested next task:
  T-005 (complexity: 4) - Create search API endpoint
  (on the critical path, unblocks T-007)

Remember: Commit your completed work (implementation + tests) with /commit before starting the next task.

Some Checks Fail

code
Quality Gate Results for T-003
==============================

  complexity:   PASS (3/4 max)
  lint:         FAIL
  typecheck:    PASS
  tests:        FAIL
  tests-added:  YES (2 new test files)

Quality gate FAILED. Task T-003 remains in-progress.

--- Lint Failures ---
3 errors found:
  src/models/user.ts:15 - no-unused-vars: 'oldPassword' is defined but never used
  src/models/user.ts:23 - prefer-const: 'result' is never reassigned
  src/services/auth.ts:8 - no-explicit-any: Unexpected any

Suggested fixes:
  1. Remove unused 'oldPassword' parameter or prefix with underscore
  2. Change 'let result' to 'const result'
  3. Replace 'any' with proper type (e.g., 'unknown' with type guard)

--- Test Failures ---
1 test failed:
  FAIL test/models/user.test.ts > User Model > should validate email format
    Expected: true
    Received: false

    at test/models/user.test.ts:45:23

Suggested fixes:
  1. Check the email validation regex in User model
  2. The test expects 'user+tag@example.com' to be valid -- ensure the regex supports '+' in local part

Fix the issues above and re-run the quality gate.

Complexity Gate Failure

code
Quality Gate Results for T-009
==============================

  complexity:   FAIL (6/4 max)
  lint:         SKIPPED
  typecheck:    SKIPPED
  tests:        SKIPPED
  tests-added:  SKIPPED

Quality gate FAILED. Task T-009 has complexity 6 (maximum: 4).

This task is too complex for atomic execution and must be decomposed first.
Use /replan to split this task into smaller tasks with complexity ≤ 4.

Epic Completion

code
Quality Gate Results for T-008
==============================

  complexity:   PASS (2/4 max)
  lint:         PASS
  typecheck:    PASS
  tests:        PASS (coverage: 96.1%)
  tests-added:  YES (1 new test file)

All quality checks passed!

Task T-008 marked as COMPLETED.
Progress: 8/8 tasks (100%)

=============================================
  EPIC COMPLETE: SPEC-003 - User Authentication System
=============================================

All 8 tasks have been completed!
Spec SPEC-003 status updated to "completed".
Average complexity: 4.5/10
Total tasks: 8

Congratulations! This spec is fully implemented.

Configurable Checks Reference

The .claude/task-master.config.json supports these check types:

json
{
  "qualityGate": {
    "lint": {
      "command": "pnpm lint",
      "required": true
    },
    "typecheck": {
      "command": "pnpm typecheck",
      "required": true
    },
    "tests": {
      "command": "pnpm test",
      "required": true
    },
    "coverage": {
      "threshold": 90,
      "required": false,
      "command": "pnpm test:coverage"
    },
    "custom": {
      "command": "pnpm run my-custom-check",
      "required": false,
      "label": "Custom Check"
    }
  }
}
  • command: The shell command to run
  • required: If true, this check must pass for the gate to pass. If false, it's informational only.
  • threshold: For coverage checks, the minimum percentage required
  • label: Display name for custom checks

If no config file exists, the three standard checks (lint, typecheck, tests) are all required by default.

Error Handling

  • Task not found: Report that the task ID does not exist in the state file
  • Task already completed: Report that the task is already completed and no action is needed
  • Task is blocked: Report which tasks must complete first, listing their IDs and titles
  • Task is cancelled: Report that cancelled tasks cannot pass quality gates
  • Command not found: If a quality check command fails because the tool is not installed, report it as a warning rather than a failure and suggest installing the tool
  • State file not found: Report the error and ask for the correct path
  • Timeout: If a check runs longer than 5 minutes, consider it failed with a timeout message
  • Permission error: If a command fails due to permissions, report the specific permission issue