Skip to content

Add --description option for state directory metadata#372

Merged
kkaarreell merged 1 commit into
mainfrom
ks_description
Jun 10, 2026
Merged

Add --description option for state directory metadata#372
kkaarreell merged 1 commit into
mainfrom
ks_description

Conversation

@kkaarreell

@kkaarreell kkaarreell commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Adds support for human-readable descriptions on state directories, stored in .newa-metadata.yaml files. This helps users distinguish between multiple state directories for the same erratum (e.g., original runs, debug sessions, retests).

Features:

  • New --description/-d option to set description on state directories
  • Automatic default descriptions for copied/extracted state directories
  • Displays descriptions in 'newa list' output
  • Tracks parent state directory for lineage tracking
  • Support for updating descriptions on existing state directories

Usage examples:

  • New run: newa -d "Full RC1 validation" event --erratum 12345
  • Copy: newa -D /path/run-123 --copy-state-dir -d "Debug session"
  • Extract: newa -E archive.tar.gz -d "From Jenkins job #456"
  • Update: newa -D /path/run-123 -d "Updated description"

Summary by Sourcery

Add support for human-readable metadata on state directories and surface it in CLI behavior and list output.

New Features:

  • Introduce a --description / -d CLI option to attach human-readable descriptions to state directories, stored in a .newa-metadata.yaml file.
  • Add a StateMetadata helper for reading and writing per-state-directory metadata, including descriptions and parent state directory information.
  • Display state directory descriptions alongside paths in the newa list command output.

Enhancements:

  • Automatically populate default descriptions and parent state directory metadata when copying or extracting state directories, and allow updating descriptions on existing state directories.

Tests:

  • Add unit tests covering StateMetadata behavior, including description handling, parent state directory tracking, persistence, YAML format, and special characters in descriptions.

@kkaarreell kkaarreell self-assigned this Jun 10, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a new --description/-d CLI option and underlying metadata support so state directories can store human-readable descriptions and parent lineage in .newa-metadata.yaml, and surfaces these descriptions in newa list output. The PR introduces a StateMetadata helper, wires description handling into new, copy, extract, and update flows, and adds unit tests and documentation.

File-Level Changes

Change Details Files
Introduce StateMetadata helper and .newa-metadata.yaml format for storing state directory metadata such as description and parent_state_dir.
  • Add newa/cli/metadata.py with StateMetadata class that reads/writes YAML metadata in .newa-metadata.yaml within a state directory
  • Support reading empty/nonexistent metadata files safely and updating arbitrary metadata fields via an update() method
  • Provide convenience setters/getters for description and parent_state_dir fields using yaml_utils.yaml_parser
newa/cli/metadata.py
tests/unit/test_metadata.py
Add --description/-d CLI option and propagate description into CLIContext for use in state-dir creation and updates.
  • Extend main() click entrypoint with --description/-d option defaulting to empty string
  • Pass description argument into CLIContext via the main() invocation
  • Add description attribute to CLIContext model with default empty string
newa/cli/main.py
newa/models/settings.py
Set or update descriptions and parent_state_dir for new, copied, extracted, and existing state directories via StateMetadata.
  • On state-dir initialization, set description for newly-created state dirs when ctx.description is provided and metadata file does not yet exist
  • When extracting a state dir, record parent_state_dir as the archive/URL and set an explicit or default description ("Extracted from ")
  • When copying a state dir, record parent_state_dir as the source directory and set an explicit or default description ("Copied from ")
  • Allow updating description on an existing state dir when -D or -P is used with --description but without copy/extract operations
newa/cli/utils.py
newa/cli/main.py
Display state directory descriptions in newa list output when metadata is present.
  • Instantiate StateMetadata for each listed state directory in list_cmd and read the description field
  • Render list output headers as " ():" when a description exists, preserving previous format otherwise
newa/cli/commands/list_cmd.py
Document the new --description option and its behavior in README, including defaults and list examples.
  • Add README section describing --description/-d usage with event, copy, extract, and update workflows
  • Document default descriptions for copy/extract operations and show sample newa list output with descriptions
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The description-handling logic is currently spread across main, initialize_state_dir, and the copy/extract branches with slightly different code paths; consider centralizing this into a small helper (e.g., in metadata or a dedicated utility) to reduce duplication and keep the behavior consistent for all state-dir operations.
  • In list_cmd, StateMetadata.get_description() reparses the YAML file on each call; if there are many state directories this could become a bottleneck, so consider reading once per directory (or caching inside StateMetadata) instead of calling read() on every get_description().
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The description-handling logic is currently spread across `main`, `initialize_state_dir`, and the copy/extract branches with slightly different code paths; consider centralizing this into a small helper (e.g., in `metadata` or a dedicated utility) to reduce duplication and keep the behavior consistent for all state-dir operations.
- In `list_cmd`, `StateMetadata.get_description()` reparses the YAML file on each call; if there are many state directories this could become a bottleneck, so consider reading once per directory (or caching inside `StateMetadata`) instead of calling `read()` on every `get_description()`.

## Individual Comments

### Comment 1
<location path="newa/cli/metadata.py" line_range="19-23" />
<code_context>
+        self.state_dir = state_dir
+        self.metadata_file = state_dir / METADATA_FILENAME
+
+    def read(self) -> dict[str, Any]:
+        """Read metadata from the state directory."""
+        if not self.metadata_file.exists():
+            return {}
+        return yaml_parser().load(self.metadata_file.read_text()) or {}
+
+    def write(self, data: dict[str, Any]) -> None:
</code_context>
<issue_to_address>
**suggestion:** Handle malformed metadata files more defensively

If `.newa-metadata.yaml` is corrupted or partially written, `yaml_parser().load(...)` may raise and prevent commands like `list` from running. Please catch YAML parsing errors here and return `{}` (and optionally log a warning) so a bad metadata file doesn’t break the CLI for that state dir.

Suggested implementation:

```python
    def read(self) -> dict[str, Any]:
        """Read metadata from the state directory."""
        if not self.metadata_file.exists():
            return {}

        yaml = yaml_parser()
        try:
            data = yaml.load(self.metadata_file.read_text()) or {}
        except Exception as exc:  # be defensive against malformed / partial YAML
            logging.getLogger(__name__).warning(
                "Failed to parse metadata file %s: %s",
                self.metadata_file,
                exc,
            )
            return {}

        # Ensure we always return a mapping for callers expecting dict[str, Any]
        if not isinstance(data, dict):
            logging.getLogger(__name__).warning(
                "Metadata file %s did not contain a mapping; ignoring its contents",
                self.metadata_file,
            )
            return {}

        return data

```

To compile and run correctly, this change also requires:
1. Adding `import logging` at the top of `newa/cli/metadata.py` if it is not already imported.
2. If your project has a preferred logging pattern (e.g. a module-level `logger = logging.getLogger(__name__)`), you can create and reuse that instead of calling `logging.getLogger(__name__)` inline in the `read` method.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread newa/cli/metadata.py
Adds support for human-readable descriptions on state directories,
stored in .newa-metadata.yaml files. This helps users distinguish
between multiple state directories for the same erratum (e.g., original
runs, debug sessions, retests).

Features:
- New --description/-d option to set description on state directories
- Automatic default descriptions for copied/extracted state directories
- Displays descriptions in 'newa list' output
- Tracks parent state directory for lineage tracking
- Support for updating descriptions on existing state directories

Usage examples:
- New run: newa -d "Full RC1 validation" event --erratum 12345
- Copy: newa -D /path/run-123 --copy-state-dir -d "Debug session"
- Extract: newa -E archive.tar.gz -d "From Jenkins job #456"
- Update: newa -D /path/run-123 -d "Updated description"

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@kkaarreell

Copy link
Copy Markdown
Collaborator Author
  • The description-handling logic is currently spread across main, initialize_state_dir, and the copy/extract branches with slightly different code paths; consider centralizing this into a small helper (e.g., in metadata or a dedicated utility) to reduce duplication and keep the behavior consistent for all state-dir operations.

fixed

@kkaarreell kkaarreell merged commit 62cf767 into main Jun 10, 2026
16 checks passed
@kkaarreell kkaarreell deleted the ks_description branch June 10, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant