Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Design

## Overview

This change introduces declarative dry-run classification for commands in devops-cli and enforces coverage using a ratchet test.

## Component Design

### 1. Decorator and Models (`src/devops_cli/dry_run/decorator.py`)

A decorator `@dry_run_class(classification)` sets the attribute `_dry_run_class` on the target callback. Valid classifications are:
- `request-plan`: The command declares `--dry-run` and returns planned requests without executing external actions.
- `no-external-request`: The command makes no network, cluster, model, or external process calls and executes normally in dry-run mode.

A helper function `get_dry_run_class(cmd_or_callback)` resolves the classification from either a Click command or a callback function, unwrapping `__wrapped__` chains when necessary.

### 2. Lazy Proxy Dry-Run Routing (`src/devops_cli/core/command_resolver.py`)

In `resolve_proxy_dry_run`:
- If the resolved leaf command has class `no-external-request`:
- If the command does not declare `--dry-run`, return `(True, list(args))` to execute normally without injecting `--dry-run`.
- If the command declares `--dry-run`, forward `--dry-run` normally.
- If the command has class `request-plan` and declares `--dry-run`, forward `--dry-run`.
- If the command lacks a classification and does not declare `--dry-run`, return `(False, None)` to print the generic delegated preview message.

### 3. Ratchet Invariant Test (`tests/test_dry_run_class_ratchet.py`)

The ratchet test iterates over all command paths reachable from `devops_cli.main._COMMAND_SPECS`:
1. Every path must either possess a dry-run classification or appear in `tests/fixtures/dry_run_gaps.json`.
2. A path present in `tests/fixtures/dry_run_gaps.json` that possesses a classification triggers a test failure instructing the author to remove it.
3. An entry in `tests/fixtures/dry_run_gaps.json` that does not match a reachable command triggers a test failure.
4. A command with class `request-plan` that fails to declare `--dry-run` triggers a test failure.
5. An MCP tool in `CONST_MCP_EXPOSURE_TABLE` wrapping a `no-external-request` command must set `readOnlyHint` (`read_only=True`).
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Proposal

## Why

Issue #2230: The owner dry-run rule states that a dry run makes no external request. External requests include GitHub, Kubernetes clusters, AI models, network endpoints, and child processes that access them. A dry run returns planned requests and the real result type. Commands without external requests can run normally in dry-run mode. Currently, no code records which commands follow this rule, and no invariant test prevents new commands from violating it.

## What Changes

- Add a `@dry_run_class` decorator in `src/devops_cli/dry_run/decorator.py` supporting `request-plan` and `no-external-request` classes.
- Update lazy proxy resolution in `src/devops_cli/core/command_resolver.py` to run `no-external-request` commands normally under `--dry-run`.
- Add `tests/fixtures/dry_run_gaps.json` containing the list of command paths lacking a dry-run class.
- Mark the 32 distinct commands that currently implement request plans with `@dry_run_class("request-plan")`.
- Add `tests/test_dry_run_class_ratchet.py` enforcing that every command reachable from `_COMMAND_SPECS` has a valid class or appears in `dry_run_gaps.json`.
- Add an MCP consistency check ensuring that tools in `CONST_MCP_EXPOSURE_TABLE` wrapping `no-external-request` commands specify `readOnlyHint: true`.
- Remove the obsolete substring test `test_invariants_all_dry_run_commands_consistent` in `tests/test_main_dry_run.py`.

## Capabilities

### New Capabilities

- `command-dry-run-class`: command-declares-dry-run-class, proxy-runs-no-external-request-commands, ratchet-prevents-unclassified-commands, mcp-read-only-hint-agrees-with-no-external-request

### Modified Capabilities

None.

## Impact

Reachable commands with `request-plan` declare `--dry-run`. Reachable commands with `no-external-request` execute normally when `--dry-run` is active. Unclassified commands continue to print the delegated preview message.

## Feasibility

- Premise confirmed: `src/devops_cli/dry_run/decorator.py` exists and wraps command callbacks.
- `_COMMAND_SPECS` in `src/devops_cli/main.py` provides 512 total command paths.
- The 32 existing request-plan callbacks declare `--dry-run` and handle request planning.
- `CONST_MCP_EXPOSURE_TABLE` in `src/devops_cli/config/constants.py` contains 142 tool specifications.
- All new tests avoid sockets, network calls, and subprocess execution, satisfying the test floor.

## Existing Tool Considered

Click and Typer offer command inspection and callback metadata attachment via function attributes. Reusing Python function attributes copied by `functools.wraps` satisfies the requirement without new libraries or overhead.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## ADDED Requirements

### Requirement: command-declares-dry-run-class
Every command callback in devops-cli SHALL support explicit dry-run classification via the `@dry_run_class` decorator with values `request-plan` or `no-external-request`. A command marked with `request-plan` SHALL declare the `--dry-run` option.

#### Scenario: command declares request-plan class
- **WHEN** a command callback is decorated with `@dry_run_class("request-plan")`
- **THEN** inspecting the callback or Click command reveals the classification attribute `request-plan` and the command exposes `--dry-run`

### Requirement: proxy-runs-no-external-request-commands
When the user executes `devops --dry-run <command>` for a command with the `no-external-request` classification, the lazy proxy SHALL execute the command normally without `--dry-run` if the command does not declare `--dry-run`.

#### Scenario: dry run executes no-external-request command
- **WHEN** the user executes `devops --dry-run <command>` on a command marked `no-external-request`
- **THEN** the command executes without error and prints its normal output

### Requirement: ratchet-prevents-unclassified-commands
Every command path reachable from `_COMMAND_SPECS` SHALL either carry a dry-run classification or be listed in `tests/fixtures/dry_run_gaps.json`. An unclassified command missing from the gap list, or a classified command remaining in the gap list, SHALL fail the ratchet test.

#### Scenario: unclassified command not in gap list fails
- **WHEN** a new command path is added without a dry-run class and without an entry in `dry_run_gaps.json`
- **THEN** the ratchet test fails and identifies the missing command

### Requirement: mcp-read-only-hint-agrees-with-no-external-request
An MCP tool entry in `CONST_MCP_EXPOSURE_TABLE` that delegates to a command marked `no-external-request` SHALL specify `readOnlyHint` as true.

#### Scenario: mcp tool wraps no-external-request command
- **WHEN** an MCP tool definition in `CONST_MCP_EXPOSURE_TABLE` maps to a command path with `no-external-request`
- **THEN** the third element of the table entry is `True`
15 changes: 15 additions & 0 deletions openspec/changes/archive/2026-10-31-2230-dry-run-class/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Tasks

## 1. Dry-run class infrastructure and ratchet test

Reads: `src/devops_cli/dry_run/decorator.py`, `src/devops_cli/core/command_resolver.py`, `src/devops_cli/config/constants.py`.. Changes: `src/devops_cli/dry_run/decorator.py`, `src/devops_cli/dry_run/__init__.py`, `src/devops_cli/core/command_resolver.py`, `tests/test_dry_run_class_ratchet.py`, `tests/fixtures/dry_run_gaps.json`, `tests/test_main_dry_run.py`..

- [x] 1.1 Write unit and invariant tests in `tests/test_dry_run_class_ratchet.py` that check criteria 1-7 (ratchet enforcement, gap list validity, dry-run option requirement for request-plan commands, lazy proxy execution for no-external-request commands, and MCP exposure table consistency). Confirm the tests fail before implementation.
- [x] 1.2 Implement the `@dry_run_class` decorator and `get_dry_run_class` in `src/devops_cli/dry_run/decorator.py` and export them in `src/devops_cli/dry_run/__init__.py`. Update `resolve_proxy_dry_run` in `src/devops_cli/core/command_resolver.py` to route `no-external-request` commands without `--dry-run`.
- [x] 1.3 Apply `@dry_run_class("request-plan")` to the 32 distinct commands that implement request plans in `src/devops_cli/commands/ci.py`, `src/devops_cli/commands/roadmap.py`, `src/devops_cli/commands/pr.py`, `src/devops_cli/commands/gh.py`, `src/devops_cli/commands/k8s/diagnostics.py`, `src/devops_cli/commands/k8s/cluster_secret_push.py`, `src/devops_cli/commands/k8s/cluster_jobs.py`, `src/devops_cli/commands/k8s/stack_lifecycle.py`, `src/devops_cli/commands/k8s/volume.py`, and `src/devops_cli/commands/review.py`. Generate `tests/fixtures/dry_run_gaps.json` with the remaining unclassified command paths.
- [x] 1.4 Remove the obsolete test `test_invariants_all_dry_run_commands_consistent` and `_collect_command_dry_run_info` in `tests/test_main_dry_run.py`. Confirm `tests/test_dry_run_class_ratchet.py` and `tests/test_main_dry_run.py` pass.
- [x] 1.5 Regenerate documentation with `uv run devops docs generate --sync-readme`, and verify that `uv run devops ci` passes.

## Workflow follow-up

- Pending a person: review the diff in tuicr (tuicr -r <base>..HEAD).
34 changes: 34 additions & 0 deletions openspec/specs/command-dry-run-class/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# command-dry-run-class Specification

## Purpose
Devops-cli commands declare their dry-run behavior so that users and agents know command side effects and run read-only operations safely.

## Requirements

### Requirement: command-declares-dry-run-class
Every command callback in devops-cli SHALL support explicit dry-run classification via the `@dry_run_class` decorator with values `request-plan` or `no-external-request`. A command marked with `request-plan` SHALL declare the `--dry-run` option.

#### Scenario: command declares request-plan class
- **WHEN** a command callback is decorated with `@dry_run_class("request-plan")`
- **THEN** inspecting the callback or Click command reveals the classification attribute `request-plan` and the command exposes `--dry-run`

### Requirement: proxy-runs-no-external-request-commands
When the user executes `devops --dry-run <command>` for a command with the `no-external-request` classification, the lazy proxy SHALL execute the command normally without `--dry-run` if the command does not declare `--dry-run`.

#### Scenario: dry run executes no-external-request command
- **WHEN** the user executes `devops --dry-run <command>` on a command marked `no-external-request`
- **THEN** the command executes without error and prints its normal output

### Requirement: ratchet-prevents-unclassified-commands
Every command path reachable from `_COMMAND_SPECS` SHALL either carry a dry-run classification or be listed in `tests/fixtures/dry_run_gaps.json`. An unclassified command missing from the gap list, or a classified command remaining in the gap list, SHALL fail the ratchet test.

#### Scenario: unclassified command not in gap list fails
- **WHEN** a new command path is added without a dry-run class and without an entry in `dry_run_gaps.json`
- **THEN** the ratchet test fails and identifies the missing command

### Requirement: mcp-read-only-hint-agrees-with-no-external-request
An MCP tool entry in `CONST_MCP_EXPOSURE_TABLE` that delegates to a command marked `no-external-request` SHALL specify `readOnlyHint` as true.

#### Scenario: mcp tool wraps no-external-request command
- **WHEN** an MCP tool definition in `CONST_MCP_EXPOSURE_TABLE` maps to a command path with `no-external-request`
- **THEN** the third element of the table entry is `True`
22 changes: 21 additions & 1 deletion src/devops_cli/commands/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
from devops_cli.core.cli import new_typer
from devops_cli.core.dependency_audit import AuditVerdict, audit_verdict
from devops_cli.core.process import prepare_tool_cmd, run_subprocess
from devops_cli.dry_run import is_dry_run, render_dry_run_result, set_dry_run
from devops_cli.dry_run import dry_run_class, is_dry_run, render_dry_run_result, set_dry_run
from devops_cli.dry_run.requests import PlannedRequest, render_request_plan
from devops_cli.lang import HELP, MESSAGES
from devops_cli.output import (
Expand Down Expand Up @@ -1406,6 +1406,7 @@ def _handle_ci_results(


@app.callback(invoke_without_command=True)
@dry_run_class("request-plan")
def all_checks(
ctx: typer.Context,
fix: Annotated[
Expand Down Expand Up @@ -1787,6 +1788,7 @@ def _run_shuffled_repeats(


@app.command()
@dry_run_class("request-plan")
def test(
paths: Annotated[list[Path] | None, typer.Argument(help=HELP.ci.test_paths)] = None,
verbose: Annotated[bool, typer.Option("--verbose", "-v", help=HELP.options.verbose)] = False,
Expand Down Expand Up @@ -1889,6 +1891,7 @@ def _handle_coverage_index_build(cmd: list[str]) -> None:


@app.command()
@dry_run_class("request-plan")
def coverage(
html: Annotated[bool, typer.Option("--html", help=HELP.ci.html_report)] = False,
build_index: Annotated[
Expand Down Expand Up @@ -1919,6 +1922,7 @@ def coverage(


@app.command()
@dry_run_class("request-plan")
def lint(
fix: Annotated[bool, typer.Option("--fix/--no-fix", help=HELP.ci.auto_fix)] = True,
check: Annotated[
Expand All @@ -1942,6 +1946,7 @@ def lint(


@app.command(name="format")
@dry_run_class("request-plan")
def fmt(
check: Annotated[
bool,
Expand All @@ -1965,6 +1970,7 @@ def fmt(


@app.command()
@dry_run_class("request-plan")
def typecheck(
dry_run: Annotated[
bool,
Expand All @@ -1982,6 +1988,7 @@ def typecheck(


@app.command()
@dry_run_class("request-plan")
def audit(
dry_run: Annotated[
bool,
Expand All @@ -1998,6 +2005,7 @@ def audit(


@app.command()
@dry_run_class("request-plan")
def security(
severity: Annotated[
str,
Expand All @@ -2019,6 +2027,7 @@ def security(


@app.command()
@dry_run_class("request-plan")
def actionlint(
dry_run: Annotated[
bool,
Expand All @@ -2043,6 +2052,7 @@ def actionlint(


@app.command()
@dry_run_class("request-plan")
def docs(
fix: Annotated[
bool,
Expand All @@ -2067,6 +2077,7 @@ def docs(


@app.command(name="uv-check")
@dry_run_class("request-plan")
def uv_check(
dry_run: Annotated[
bool,
Expand All @@ -2084,6 +2095,7 @@ def uv_check(


@app.command()
@dry_run_class("request-plan")
def lockfile(
dry_run: Annotated[
bool,
Expand All @@ -2101,6 +2113,7 @@ def lockfile(


@app.command()
@dry_run_class("request-plan")
def build(
dry_run: Annotated[
bool,
Expand All @@ -2118,6 +2131,7 @@ def build(


@app.command()
@dry_run_class("request-plan")
def outdated(
dry_run: Annotated[
bool,
Expand All @@ -2135,6 +2149,7 @@ def outdated(


@app.command()
@dry_run_class("request-plan")
def devcontainer(
dry_run: Annotated[
bool,
Expand All @@ -2152,6 +2167,7 @@ def devcontainer(


@app.command()
@dry_run_class("request-plan")
def deps(
dry_run: Annotated[
bool,
Expand All @@ -2169,6 +2185,7 @@ def deps(


@app.command()
@dry_run_class("request-plan")
def maintain(
fix: Annotated[
bool,
Expand Down Expand Up @@ -2218,6 +2235,7 @@ def maintain(


@app.command()
@dry_run_class("request-plan")
def run(
fix: Annotated[
bool,
Expand Down Expand Up @@ -2279,6 +2297,7 @@ def _report_mutation(results_text: str, targets: Sequence[MutationTarget]) -> No


@app.command(help=HELP.ci.mutate)
@dry_run_class("request-plan")
def mutate(
paths: Annotated[list[Path] | None, typer.Argument(help=HELP.ci.mutate_paths)] = None,
changed: Annotated[bool, typer.Option("--changed", help=HELP.ci.mutate_changed)] = False,
Expand Down Expand Up @@ -2324,6 +2343,7 @@ def _append_step_summary(text: str) -> None:


@app.command(name="job-time", help=HELP.ci.job_time)
@dry_run_class("request-plan")
def job_time(
repo: Annotated[str | None, typer.Option("--repo", "-R", help=HELP.ci.job_time_repo)] = None,
job: Annotated[str, typer.Option("--job", help=HELP.ci.job_time_job)] = CONST_CI_TEST_JOB_NAME,
Expand Down
3 changes: 2 additions & 1 deletion src/devops_cli/commands/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from devops_cli.core.cli import new_typer
from devops_cli.core.repo import find_repo_root, get_repo_origin_name
from devops_cli.dry_run import is_dry_run
from devops_cli.dry_run import dry_run_class, is_dry_run
from devops_cli.dry_run.requests import render_request_plan
from devops_cli.exceptions import DevOpsCLIError
from devops_cli.exceptions.git import GitHubOperationError
Expand Down Expand Up @@ -519,6 +519,7 @@ def _report_reconcile(result: ReconcileResult, repo: str) -> None:


@project_app.command("reconcile", help=HELP.gh.project_reconcile)
@dry_run_class("request-plan")
def reconcile_project_cmd(
project_number: Annotated[
int | None,
Expand Down
3 changes: 2 additions & 1 deletion src/devops_cli/commands/k8s/cluster_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from devops_cli.config.env import setting_variables
from devops_cli.config.settings import GitHubAppConfig
from devops_cli.core.process import run_subprocess
from devops_cli.dry_run import PlannedRequest, is_dry_run, render_request_plan
from devops_cli.dry_run import PlannedRequest, dry_run_class, is_dry_run, render_request_plan
from devops_cli.exceptions.k8s import ClusterJobError
from devops_cli.k8s.cluster_job import (
DEFAULT_TEMPLATE,
Expand Down Expand Up @@ -300,6 +300,7 @@ def _runner_env(template: TemplateName, repo: str | None) -> dict[str, str] | No
return {setting_variables()[CONST_GITHUB_APP_TOKEN_REPOSITORY_SETTING]: repo}


@dry_run_class("request-plan")
def run_job(
args: Annotated[list[str], typer.Argument(help=HELP.k8s.run_job_args, metavar="ARGS...")],
context: Annotated[
Expand Down
3 changes: 2 additions & 1 deletion src/devops_cli/commands/k8s/cluster_secret_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from devops_cli.commands.k8s.networking import VALID_STACKS
from devops_cli.config.constants import CONST_K8S_ALL_STACKS
from devops_cli.config.os_keyring import SecretStorageError, require_persistent_keyring
from devops_cli.dry_run import is_dry_run, render_request_plan
from devops_cli.dry_run import dry_run_class, is_dry_run, render_request_plan
from devops_cli.exceptions.k8s import ClusterSecretPushError, ClusterSecretWriteError
from devops_cli.k8s.cluster_secrets import (
BASE_STACK,
Expand Down Expand Up @@ -179,6 +179,7 @@ def push_for_stacks(stacks: Sequence[str], context: str | None) -> None:
run_push(secrets_for_stacks(stacks), PushOptions(context=context), mode=PushMode.PUSH)


@dry_run_class("request-plan")
def push_secrets(
stack: Annotated[
str, typer.Option("--stack", "-s", help=HELP.k8s.push_stack)
Expand Down
3 changes: 2 additions & 1 deletion src/devops_cli/commands/k8s/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
DEFAULT_SECURITY_STREAM_TAIL_LINES,
DEFAULT_TABLE_FORMAT,
)
from devops_cli.dry_run import is_dry_run, render_dry_run_result
from devops_cli.dry_run import dry_run_class, is_dry_run, render_dry_run_result
from devops_cli.lang import HELP
from devops_cli.output import (
TablePayload,
Expand Down Expand Up @@ -529,6 +529,7 @@ def _handle_doctor_output(report: Any, resolved_fmt: str) -> None:
_render_doctor_table(report)


@dry_run_class("request-plan")
def doctor_cmd(
namespace: Annotated[
str | None,
Expand Down
Loading
Loading