Code improvement proposal

From MoonLadderStudios/MoonMind · View interactive version

Code improvement proposal is an AI agent skill from the GitHub repository MoonLadderStudios/MoonMind. Review a code file or directory and produce an evidence-backed GitHub or Jira issue proposing bug fixes, performance improvements, modularity improvements, DRY refactors, file splitting, reusable helpers, and architecture-alignment work. Use when a user asks for a code review, technical-debt proposa

How to use this skill

Skill file

Code Improvement Proposal

Review a code file or directory and produce one or more prioritized, evidence-backed improvement proposals, then publish a well-structured GitHub or Jira issue only when there is enough confidence and enough value to justify a tracked ticket.

This is a proposal-generating review skill, not an auto-refactor skill. It does not modify code. It produces an issue (or a dry-run payload) with a clear implementation plan. A separate "implement issue" skill can later make the changes.

Non-goals

Inputs

Required:

Optional (defaults below match the recommended best-default behavior):

Best defaults:

mode: dry_run
issue_backend: auto
severity_threshold: medium
dedupe_existing_issues: true
max_findings_per_issue: 12
include_tests: true
include_generated_code: false
publish_requires_explicit_request: true

Prefer dry_run unless the user explicitly asked to publish.

Optional routing file

A repository may provide .code-improvement-proposal.yml at the repo root to set the default backend, ignore globs, architecture-doc globs, path-based routing rules, and thresholds. When present, honor it. Example shape:

default_backend: github

ignore:
  - node_modules/**
  - vendor/**
  - dist/**
  - build/**
  - coverage/**
  - .git/**
  - "**/*.min.js"
  - "**/*.generated.*"
  - "**/generated/**"

architecture_docs:
  - README.md
  - docs/architecture/**/*.md
  - docs/adr/**/*.md
  - rfcs/**/*.md
  - CONTRIBUTING.md

routes:
  - paths: ["services/payments/**"]
    backend: jira
    jira:
      project_key: PAY
      issue_type: Task
      components: ["Payments"]
  - paths: ["packages/frontend/**"]
    backend: github
    github:
      repository: acme/web-platform
      labels: ["frontend", "code-quality"]

thresholds:
  split_file_loc: 600
  high_complexity_function_loc: 80
  duplicate_block_min_lines: 12
  max_findings_per_issue: 12

Access model

Use trusted tool surfaces, never raw credentials in the shell.

For GitHub, prefer gh when available; use a GitHub connector only when gh

is unavailable or unauthenticated. See

the provider command catalog for the

selected-provider gh invocation shapes and the GitHub payload template; load

it only when publishing through GitHub.

For Jira, prefer MoonMind's trusted Jira MCP/tool surface or connector. Do not expect raw Jira credentials in the agent shell and do not ask for ATLASSIAN_* secrets. Fetch project create metadata before publishing so required fields and issue-type IDs are validated.

Never print raw environment variables. Use targeted checks such as test -n "$GITHUB_TOKEN" or trusted-tool health calls; do not run printenv, env, set, or equivalent commands that can expose secrets. Do not use bare heredocs (e.g. << 'EOF' > file); use cat << 'EOF' > file or the write_file tool.

Review principles

Prefer findings that are:

Avoid findings that are:

The most important rule is the evidence gate: never say "improve modularity" in the abstract. Say exactly where modularity is breaking down, why it matters, what to extract or split, and how the team can validate the change.

Workflow

1. Normalize the target

2. Exclude non-reviewable files

Ignore (in addition to any .code-improvement-proposal.yml ignore globs):

Honor include_tests when deciding whether to review test files.

3. Gather architecture and convention context

Before judging the code, read available context: README.md, CONTRIBUTING.md, docs/architecture/**, docs/adr/**, docs/rfcs/**, CODEOWNERS/OWNERS, package/module READMEs, test-strategy docs, and lint/type/format configs.

Extract concrete documented constraints, for example:

architecture_claims:
  - "Domain logic belongs in services, not controllers"
  - "Payments must go through PaymentGateway interface"
  - "Database access should be isolated in repositories"
  - "Background jobs should be idempotent"

Only flag architecture misalignment when you can point to both the documented rule and the code location that appears to violate it.

4. Build a code inventory

For each reviewed file, collect lightweight metrics: path, language, approximate LOC, internal/external imports, public symbols, oversized functions/classes, repeated patterns, nearby tests, high-risk side effects, and architecture-relevant dependencies. Perfect metrics are not required — just enough structure to find where deeper review is worthwhile.

5. Analyze at three levels

File-level — local problems: off-by-one risks, unchecked null/None cases, resource leaks, unhandled async errors, N+1 queries, expensive loops, duplicated validation, oversized functions, unclear ownership of side effects, missing tests around branching behavior.

Module-level — cross-file problems: the same helper reimplemented in multiple places, business logic split inconsistently, controller/service/repository boundary leaks, circular dependencies, public-API duplication, inconsistent error handling, duplicated retry/cache/serialization code.

Architecture-level — compare code to docs: documented boundaries violated, a module depending on a forbidden layer, a feature bypassing a shared abstraction, new behavior lacking ADR coverage, a legacy pattern persisting despite a new architecture.

6. Create findings

Represent each finding using this schema:

id: CIP-001
category: bug | performance | modularity | dryness | architecture | testing | maintainability
severity: low | medium | high | critical
confidence: low | medium | high
effort: small | medium | large
impact: local | module | system
location:
  file: src/payments/charge_service.py
  start_line: 118
  end_line: 164
summary: Retry logic can double-charge after gateway timeout
evidence:
  - "retry_charge retries after ambiguous timeout without idempotency key"
  - "architecture docs require all payment jobs to be idempotent"
recommendation: Introduce an idempotent ChargeRequest helper and route retries through it
validation:
  - Add unit tests for timeout after gateway accepts charge
  - Add integration test with duplicate retry response

A finding must include an exact location, the observed problem, the impact, a proposed change, and a validation plan.

7. Prioritize and filter

Promote findings in this order:

  1. high-confidence bug risks,
  2. architecture violations with concrete impact,
  3. performance problems with likely user/system impact,
  4. duplication that creates correctness drift,
  5. modularity problems blocking maintainability or testing,
  6. large-file splitting when there is a clear boundary,
  7. general cleanup.

Only promote a finding into a published issue when it is specific, actionable, evidence-backed, worth tracking, not merely stylistic, above severity_threshold, and not already covered by an existing open issue. Do not exceed max_findings_per_issue.

8. Cluster findings into proposals

Default to one issue per coherent improvement theme, not one issue per tiny finding.

Good issue: "Refactor payments charge flow to centralize idempotency and retry handling"
Bad issue:  "Clean up src/payments"

Split into multiple issues when findings have different owners, modules, risk profiles, or implementation plans. For very large directories, create an umbrella issue with a checklist and recommend follow-up issues.

For each proposal, produce a title, summary, scope reviewed, findings table, proposed implementation plan, validation plan, risk/rollout notes, labels, target repository or Jira project, and the duplicate-detection result.

9. Route the issue

Resolve the backend and target in this order:

  1. explicit user-provided backend and target,
  2. .code-improvement-proposal.yml route by path glob,
  3. git remote origin for GitHub repo detection,
  4. CODEOWNERS/OWNERS or package ownership metadata,
  5. Jira project mapping from config,
  6. dry-run Markdown output with a needs-routing status.

Important distinction: GitHub routes to a repository; Jira routes to a project, issue type, component, and labels — not directly to a board. Boards are views over issues. Create the Jira issue in the right project and set the fields that cause it to appear on the intended board.

10. Publish (only when warranted)

For GitHub, before creating an issue:

  1. Search open issues for a similar title/path/theme.
  2. If a duplicate exists, return the existing issue and optionally propose a comment instead of opening a new one.
  3. If no duplicate exists and mode=publish, create the issue.
  4. If mode=dry_run, output the payload only.

GitHub payload shape and recommended labels live in

the provider command catalog; load it only

when publishing through GitHub.

Recommended GitHub labels: code-quality, technical-debt, refactor, bug, performance, architecture, testing, needs-triage.

For Jira, keep the internal issue body in Markdown first, then convert it to Atlassian Document Format (ADF) at the final publishing step. Fetch create metadata for the target project and issue type first, because Jira fields vary by project; map requested fields to Jira field IDs through metadata instead of hardcoding custom field IDs. Then create the issue through the trusted Jira tool surface (create operation with metadata-resolved fields, honoring the request's existing write intent, validation, and receipt semantics). A standalone adapter is permitted only when explicitly authorized, with equivalent write-intent, validation, and receipt semantics. Raw provider API shapes such as POST /rest/api/3/issue are not the primary path here; they belong only in the provider command catalog.

Before retrying after an uncertain network failure on either backend, search for a matching recently created issue (by title/path/theme for GitHub, by project/summary/marker for Jira) to avoid duplicates.

11. Output

Always return:

If published, include the created issue URL/key. If not published, include the exact payload that would be sent.

Final issue body template

Use this structure for GitHub Markdown, and convert it to ADF for Jira.

## Summary

This proposal identifies code-quality and maintainability improvements in `<target_path>`, focused on correctness, performance, modularity, DRYness, and alignment with documented architecture.

## Recommended outcome

Refactor `<module/theme>` to:

- centralize duplicated logic
- reduce coupling between `<A>` and `<B>`
- align implementation with `<architecture doc / ADR>`
- add tests around the highest-risk behavior

## Scope reviewed

- Target: `<target_path>`
- Files reviewed: `<N>`
- Primary languages/frameworks: `<detected stack>`
- Architecture docs considered:
  - `<doc 1>`
  - `<doc 2>`

## Priority

`<High | Medium | Low>`

Rationale: `<why this matters now>`

## Findings

| ID | Severity | Category | Location | Finding | Recommendation |
|---|---:|---|---|---|---|
| CIP-001 | High | Bug risk | `src/payments/charge_service.py:118-164` | Retry path can repeat a charge after ambiguous timeout | Add idempotency key helper and tests |
| CIP-002 | Medium | DRY/modularity | `src/payments/*.py` | Validation rules are duplicated across 4 files | Extract shared validator |
| CIP-003 | Medium | Architecture | `src/api/checkout_controller.py` | Controller performs payment orchestration that docs assign to the service layer | Move orchestration into the service |

## Proposed implementation plan

1. Add or identify a shared abstraction: `<helper/service/module>`.
2. Move duplicated logic into the abstraction.
3. Update call sites incrementally.
4. Add regression tests for the risky behavior.
5. Run existing lint, typecheck, and test commands.

## Suggested validation

- [ ] Unit tests for `<behavior>`
- [ ] Integration test for `<flow>`
- [ ] Existing test suite passes
- [ ] Linter/typechecker passes
- [ ] Targeted executable verification from the repository's own acceptance entrypoints (tests, checks, or documented validation commands; not a mandatory human smoke test) covering `<user-facing flow>`

## Architecture alignment

Relevant documented expectations:

- `<architecture rule from docs>`
- `<ADR or README constraint>`

Observed mismatch:

- `<specific code location and explanation>`

## Risks and rollout

Risk: `<low/medium/high>`

Mitigation:

- keep changes behind existing interfaces
- preserve public API behavior
- add regression coverage before moving call sites

## Notes

Generated by `code-improvement-proposal` from static review of `<target_path>`.
No code changes were made by this proposal.

Artifacts

Write local artifacts when possible so the review is auditable and reusable:

Use the issue for human planning and the optional SARIF for file/line-level static-analysis-style findings.

Security and quality constraints

Output statuses

A clean review may legitimately produce no findings: report no_findings with the reviewed scope and evidence instead of inventing a ticket to fill a quota.

Report exactly one terminal status: dry_run (payload only), published (issue created, include URL/key), duplicate (matched an existing open issue, no new issue created), needs_routing (no backend could be resolved), no_findings (review completed with evidence and nothing met the proposal bar), or blocked (a required tool, permission, or field is unavailable). Include sanitized blocker details when blocked.

Failure modes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.