Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: spec-driven
created: 2026-11-23
skip_specs: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Design

## Decisions

1. **The generator imports every module of devops-cli, and no list of modules exists.** `DocGenerator.catalog_module_names` takes the `.py` files of the directory of the `devops_cli` package with `Path.rglob`. It gives each file its dotted name with `module_name` of `devops_cli.core.import_graph`. `DocGenerator._import_all_exception_modules` imports each name with `importlib.import_module`. Then `DevOpsCLIError.__subclasses__` holds each error class, through any chain of bases and any import name. Reason: the hand list is the cause of #6815. A file list has no entry that a person can forget.
2. **The names come from the files, not from `pkgutil.walk_packages`.** Reason: `walk_packages` skips a directory with no `__init__.py`. The probe of the proposal shows that it skips the 6 modules of `devops_cli.docker`. An error class there would then be missing from the catalog, and no check would fail. The file list holds all 964 modules.
3. **The cost is about 2.3 s for each `devops docs generate`.** When the catalog runs, 565 modules are in memory, and the other 399 are new imports. The median of six runs of the command is 5.95 s on the base and 8.24 s with the change, on one workstation. The output files are the same. No module fails to import, and the imports happen in the function, so the `startup-imports` row of `devops ci` does not change. An import that fails still stops the docs stage with the name of the module. The catalog tests do the same imports: in a fresh worker, `tests/docs/test_exceptions.py` takes about 2.3 s, against 0.3 s on the base. `tests/module_durations.json` keeps 0.2 s for the module until `devops ci durations` writes the record again from the runs of `main`. A value that a person writes is not a measurement. The difference of 2 s is 0.3 % of the 660 s that the record gives each shard.
4. **The guard test runs the import step in the test process and checks `sys.modules`.** The test reads the `.py` files of `src/devops_cli` and calculates their dotted names with no help from the generator. Then it runs the import step of the catalog and expects each name in `sys.modules`. A module that an earlier test of the worker loaded can only hide a missing module. It cannot add a false failure. A check of the error classes alone passes on ec5b16bdc, because the list names each module with an error class today. On the base, the import step loads only the listed modules, so the test fails with the names of the others. The test starts no process.
5. **A new capability, `docs-error-catalog`, holds the rule.** No base spec holds a rule of `docs/ERRORS.md`. `docs-cli-reference` holds the rules of `docs/CLI_REFERENCE.md`, and `command-error-exit` holds the rules of how a command reports an error.

## Rejected alternatives

- **Keep `CONST_DOMAIN_EXCEPTION_MODULES` and add a test that fails when a module with an error class is not in it.** Rejected: the test must import every module too, so it costs the same time in the test run. A person still edits the list for each new module, and the list is the cause of #6815.
- **Find the error classes with an `ast` scan for `class X(DevOpsCLIError)`.** Rejected: a class whose base is another error class, from another module or under another name, needs name resolution that the scan must build again. That is a parser of our own, and the import of the modules gives the answer with the standard library.
- **Import the modules that `pkgutil.walk_packages` gives.** Rejected: it skips the namespace package `devops_cli.docker` (decision 2).
- **Run the import step in a fresh interpreter, and check `sys.modules` after it.** Rejected: the child imports each module of the package again and took 10 s to 15 s under the load of a test run. The import step in the test process takes about 2 s (decision 3).
- **Patch `importlib.import_module` and record the names that the import step asks for.** Rejected: `docs/TESTING.md` lets a test double only an edge, and the import system is not an edge. A function that gives the names needs no double.
- **Move every error class into `devops_cli.exceptions`.** Rejected: 21 classes of 16 modules would move, with the imports of their callers. That is a refactor of its own, and a new module could still define an error class elsewhere with no failed check.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Proposal: #6815

## Why

The error catalog `docs/ERRORS.md` had no row for the four `DevOpsCLIError` subclasses of `devops_cli.github.onboard` (#6815). `DocGenerator._import_all_exception_modules` imports only the modules of `devops_cli.exceptions` and of `CONST_DOMAIN_EXCEPTION_MODULES`, a list that a person keeps. #6806 added the onboard modules and did not add them to the list, and no check failed. The catalog test saw the onboard errors only after an earlier test of its worker imported them, so its result changed with the shard layout. The change of #3244 then added the two modules to the list. The next module with an error class can miss the list again, and no check fails.

## What Changes

- `DocGenerator.catalog_module_names` gives the module of each `.py` file under the `devops_cli` package. The generator imports each one before it reads the `DevOpsCLIError` subclasses. So the catalog lists every error class, and a new module needs no list entry.
- `CONST_DOMAIN_EXCEPTION_MODULES` goes, with its import in `src/devops_cli/docs/generator.py`, its only reader.
- A new test runs the import step of the catalog and expects the module of each `.py` file of the package in `sys.modules`. It runs in the test process and starts no process of its own.
- `uv run devops docs generate` keeps the rows of `AnswerFlagError`, `BaselineUnreadableError`, `RouteRefusedError` and `RouteWriteError` in `docs/ERRORS.md`, and the file does not change.
- The content of `docs/ERRORS.md` is observable output of `devops docs generate`, so the change carries a spec delta.
- The change fits one pull request.

## Capabilities

### New Capabilities

- `docs-error-catalog`: The error catalog lists every error class of devops-cli

### Modified Capabilities

None.

## Impact

- Code: `src/devops_cli/docs/generator.py`, `src/devops_cli/config/constants.py`
- Documents: none. `docs/ERRORS.md` does not change.
- Tests: `tests/docs/test_exceptions.py`
- `devops docs generate` imports about 400 more modules and takes about 2.3 s more on a workstation. The catalog tests take about 2 s more in a fresh worker (design decision 3).

## Feasibility

- On 676e10721, `uv run pytest -n 0 tests/docs/test_exceptions.py::test_each_error_class_declares_the_codes_of_its_catalog_row` gives `1 passed`. With `tests/github/onboard/test_onboard_routes.py` first in the same process, it gives `1 failed, 35 passed` and `Left contains 4 more items`.
- A scratch script imports each module that `pkgutil.walk_packages` gives: 957 modules, with no import failure. It finds four error classes that the generator does not load: `AnswerFlagError` and `BaselineUnreadableError` of `devops_cli.github.onboard.baseline`, and `RouteRefusedError` and `RouteWriteError` of `devops_cli.github.onboard.routes`.
- The package holds 964 `.py` files. `pkgutil.walk_packages` does not give the 6 modules of `devops_cli.docker`, a directory with no `__init__.py` (a namespace package). The file list gives all 964, and each one imports with no failure.
- When the catalog runs in `devops docs generate`, 565 modules of devops-cli are in memory. An import of all 964 adds 2.4 s to 2.5 s in a scratch script (two runs).
- On 676e10721, the import step of the generator leaves 221 modules of the package unloaded in a fresh interpreter. Their import adds the four onboard classes. On ec5b16bdc, after #3244, it leaves 218 modules unloaded, and their import adds no error class.
- The rows of the four onboard errors are in `docs/ERRORS.md` on ec5b16bdc. The cause, the hand list with no check, is still there, so we can build the rest of the issue as written.

## Existing Tool Considered

The standard library covers the need: `pathlib.Path.rglob` gives the source files, `devops_cli.core.import_graph.module_name` gives the dotted name of each file, `importlib.import_module` loads it, and `type.__subclasses__` gives each error class. No third-party tool is necessary. The design rejects `pkgutil.walk_packages`. The change deletes `CONST_DOMAIN_EXCEPTION_MODULES` and the `pkgutil.iter_modules` loop of the generator. It builds about 15 lines of code and 10 lines of test: 20k tokens and 1 hour.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Purpose
The error catalog, `docs/ERRORS.md`, is the generated table of the `DevOpsCLIError` subclasses of devops-cli. Each row gives the error code, the exit status, the domain and the description of a subclass. `devops docs generate` writes it.

## ADDED Requirements

### Requirement: The error catalog lists every error class of devops-cli
`devops docs generate` SHALL import the module of each `.py` file under the `devops_cli` package before it reads the `DevOpsCLIError` subclasses, with no list of modules that a person keeps. `docs/ERRORS.md` SHALL hold one row for each subclass that a module of devops-cli defines, whatever modules the process imported before.

#### Scenario: The import step names the module of each source file
- **WHEN** the generator gives the module names of its import step
- **THEN** the names hold the module of each `.py` file of the package, such as `devops_cli.github.onboard.baseline` and the modules of `devops_cli.docker`

#### Scenario: The catalog after another test
- **WHEN** the catalog test compares the error classes with the rows of `docs/ERRORS.md` alone, and after the tests of `tests/github/onboard/test_onboard_routes.py` in one process
- **THEN** both runs pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tasks

## 1. The generator imports every module of devops-cli

Kind: behavior. Reads: `src/devops_cli/docs/generator.py`, `src/devops_cli/core/import_graph.py`, `src/devops_cli/config/constants.py`, `tests/docs/test_exceptions.py`, `tests/docs/test_docs.py`, `tests/ai/agents/test_pydantic_ai_exceptions.py`. Changes: `src/devops_cli/docs/generator.py`, `src/devops_cli/config/constants.py`, `tests/docs/test_exceptions.py`.

- [x] 1.1 Write `test_the_error_catalog_imports_the_module_of_each_source_file` in `tests/docs/test_exceptions.py`. It reads the `.py` files of `src/devops_cli` and calculates their dotted names. It runs the import step of the catalog and expects each name in `sys.modules`. On the base, the import step loads only the listed modules, so the test fails with the names of the other modules
- Verification: `$ uv run devops ci test tests/docs/test_exceptions.py` fails on the base
- [x] 1.2 In `src/devops_cli/docs/generator.py`, add `catalog_module_names`. It gives the module of each `.py` file under the directory of the `devops_cli` package, with `module_name` of `devops_cli.core.import_graph`. Make `_import_all_exception_modules` import each of these names, and remove the `pkgutil.iter_modules` loop. Remove `CONST_DOMAIN_EXCEPTION_MODULES` from `src/devops_cli/config/constants.py` and from the imports of the generator. Run `uv run devops docs generate`: `docs/ERRORS.md` keeps the four onboard rows and does not change
- Verification: `$ uv run devops ci test tests/docs/test_exceptions.py tests/docs/test_docs.py tests/ai/agents/test_pydantic_ai_exceptions.py` passes, and `$ uv run devops ci --check --only docs` passes

## Criteria

- Group 1: Test first: a check fails when a module of `src/devops_cli` defines a `DevOpsCLIError` subclass and the generator does not import that module. The check fails on the base, and `docs/ERRORS.md` keeps the rows of the four onboard errors. The catalog test passes alone and after `tests/github/onboard/test_onboard_routes.py` in one process.

## Workflow follow-up

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

## Purpose
The error catalog, `docs/ERRORS.md`, is the generated table of the `DevOpsCLIError` subclasses of devops-cli. Each row gives the error code, the exit status, the domain and the description of a subclass. `devops docs generate` writes it.

## Requirements

### Requirement: The error catalog lists every error class of devops-cli
`devops docs generate` SHALL import the module of each `.py` file under the `devops_cli` package before it reads the `DevOpsCLIError` subclasses, with no list of modules that a person keeps. `docs/ERRORS.md` SHALL hold one row for each subclass that a module of devops-cli defines, whatever modules the process imported before.

#### Scenario: The import step names the module of each source file
- **WHEN** the generator gives the module names of its import step
- **THEN** the names hold the module of each `.py` file of the package, such as `devops_cli.github.onboard.baseline` and the modules of `devops_cli.docker`

#### Scenario: The catalog after another test
- **WHEN** the catalog test compares the error classes with the rows of `docs/ERRORS.md` alone, and after the tests of `tests/github/onboard/test_onboard_routes.py` in one process
- **THEN** both runs pass
18 changes: 0 additions & 18 deletions src/devops_cli/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,6 @@
"bounds of a plan, a drain and a node restart.",
),
}
CONST_DOMAIN_EXCEPTION_MODULES: Final[tuple[str, ...]] = (
"devops_cli.ai.client.models",
"devops_cli.ai.gateway_tune",
"devops_cli.ai.pool_load",
"devops_cli.ai.rag.embeddings",
"devops_cli.ai.rag.qdrant",
"devops_cli.ai.run_store",
"devops_cli.core.templating",
"devops_cli.github.onboard.baseline",
"devops_cli.github.onboard.routes",
"devops_cli.github.ssh",
"devops_cli.http.client",
"devops_cli.k8s.node_port",
"devops_cli.k8s.service_proxy",
"devops_cli.review.state",
"devops_cli.telemetry.context",
"devops_cli.telemetry.memory_profiler",
)
CONST_PROJECT_CONFIG_ENV = "DEVOPS_CLI_CONFIG" # absolute path overrides CWD lookup
# A configuration file is only eligible for caching once it has been still for this
# long. The kernel stamps files from a coarse clock, so a file rewritten moments after
Expand Down
34 changes: 18 additions & 16 deletions src/devops_cli/docs/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
CONST_DOC_CREDENTIAL_NAME_WORDS,
CONST_DOC_CREDENTIAL_NAMING_WORD,
CONST_DOCS_DIR_NAME,
CONST_DOMAIN_EXCEPTION_MODULES,
CONST_MARKDOWN_HEADING_LEVEL,
CONST_PROJECT_CONFIG_CANDIDATES,
CONST_SETTINGS_SECTION_METADATA,
Expand Down Expand Up @@ -871,29 +870,32 @@ def generate_configuration_docs(self) -> str:

return "\n".join(lines)

@staticmethod
def catalog_module_names() -> list[str]:
"""Give the module of each `.py` file of devops-cli: the modules that the catalog imports (#6815).

Each module can define a `DevOpsCLIError` subclass, and `__subclasses__` gives only the
subclasses of loaded modules. So the names come from the files, and no list of modules
can leave a class out of the catalog. `pkgutil.walk_packages` skips a directory with no
`__init__.py`, such as `docker/`, so this method does not use it.
"""
import devops_cli
from devops_cli.core.import_graph import module_name

package_dir = Path(devops_cli.__file__).parent
return [module_name(path, package_dir.parent) for path in sorted(package_dir.rglob("*.py"))]

@staticmethod
def _import_all_exception_modules() -> None:
"""Import every module defining DevOpsCLIError subclasses, failing closed on error."""
"""Import each module of `catalog_module_names`, and stop on a failed import."""
import importlib
import pkgutil

import devops_cli.exceptions

module_names = [
*(
mod_name
for _, mod_name, _ in pkgutil.iter_modules(
devops_cli.exceptions.__path__, prefix="devops_cli.exceptions."
)
),
*CONST_DOMAIN_EXCEPTION_MODULES,
]
for mod_name in module_names:
for mod_name in DocGenerator.catalog_module_names():
try:
importlib.import_module(mod_name)
except Exception as exc:
raise RuntimeError(
f"Failed to import exception module '{mod_name}': {exc}. "
f"Failed to import module '{mod_name}' for the error catalog: {exc}. "
"A missing optional dependency now fails the docs stage, by design."
) from exc

Expand Down
20 changes: 20 additions & 0 deletions tests/docs/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ast
import inspect
import re
import sys
import textwrap

from devops_cli.docs.generator import DocGenerator
Expand Down Expand Up @@ -154,6 +155,25 @@ def _error_classes() -> list[type[DevOpsCLIError]]:
return found


def test_the_error_catalog_imports_the_module_of_each_source_file() -> None:
"""The catalog imports the module of each `.py` file, so a new error class needs no list entry (#6815).

A list of modules that a person kept left the four errors of `devops_cli.github.onboard` out
of docs/ERRORS.md, and no check failed. After the import step of the catalog, the module of
each file is loaded. A module that an earlier test of the worker loaded can only hide a
missing module, not add a false failure.
"""
src = REPO_ROOT / "src"
files = (
path.relative_to(src).with_suffix("").parts for path in (src / "devops_cli").rglob("*.py")
)
expected = {".".join(parts[:-1] if parts[-1] == "__init__" else parts) for parts in files}

DocGenerator._import_all_exception_modules()

assert sorted(expected - set(sys.modules)) == []


def _declared_codes(cls: type[DevOpsCLIError]) -> tuple[object, object]:
return getattr(cls, "error_code", None), getattr(cls, "exit_code", None)

Expand Down