agentleFS
yerdaulet-damiryerdaulet-damir/vibe-coding-rules/.cursor/rules/decomposition.mdc

vibe-coding-rules / rules

Cursor rule10 starsChanged 7 months agoMITView on GitHub

File and folder decomposition rules — when and how to split

The file

We split this decomposition.mdc into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When routers/generate.py reaches 400 LOC because it handles image, video, audio:

    line 81
  2. Very specific:

    Not re-exported from __init__.py.

    line 171
  3. Distinctive:

    Not imported from outside the package.

    line 172

≈ “Common” is an estimate: at least 1 in 5 of the 7,324 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
decomposition.mdc
---
description: File and folder decomposition rules — when and how to split
globs: ["**/*.py"]
alwaysApply: true
---

# Decomposition Rules

Files grow. Without a rule, they grow until grep is the only navigation tool. These rules tell you exactly when to split, and how to split safely.

## File Size Limits

| LOC range  | State  | Action required                                                  |
| ---------- | ------ | ---------------------------------------------------------------- |
| 0–400      | Green  | None.                                                            |
| 400–600    | Yellow | Plan a split. Add `# TODO(decompose): A1 — split by ...` header. |
| 600+       | Red    | **Block the merge.** Decompose first.                            |

CI runs `python scripts/check_loc.py app/` and fails on any file > 600 LOC.

## When to Convert a File into a Package

Convert `<file>.py` into `<file>/` when **any** is true:

- Crosses 400 LOC and the next change would push it past 500.
- Contains 2+ disjoint sub-domains (image vs video, user vs admin).
- Mixes HTTP handlers with worker handlers.
- Mixes Pydantic schemas + auth deps + route handlers.
- Has 2+ callers each importing only one symbol — the split lines are obvious.

## How to Split Safely (Atomic PR Pattern)

1. **Create the package.** `<file>/__init__.py` is empty for now.
2. **Move disjoint pieces to sub-files** (`a.py`, `b.py`, `c.py`).
3. **Re-export old public names** from `__init__.py`:

   ```python
   # services/wallet/__init__.py
   from .user import WalletUserService
   from .admin import WalletAdminService

   # backwards-compat alias for old imports
   WalletService = WalletUserService

   __all__ = ["WalletUserService", "WalletAdminService", "WalletService"]
   ```

4. **Run tests** — they must pass without changes.
5. **Open a follow-up PR** to migrate callers off the legacy alias. Once it lands, delete the alias.

## Static Data Separation

Static data goes in `app/data/` (project-wide) or `<domain>/registry.py` (domain-local). **Never** inline:

```python
# ❌ BAD — pricing constants tangled into service file
class AIService:
    PRICING = {
        "gpt-4o":     Decimal("0.005"),
        "claude-3-5": Decimal("0.003"),
        # ... 80 more lines ...
    }
    async def generate(self, ...): ...
```

```python
# ✅ GOOD — static data in its own module
# app/data/model_pricing.py
from decimal import Decimal
PRICING: dict[str, Decimal] = {
    "gpt-4o":     Decimal("0.005"),
    "claude-3-5": Decimal("0.003"),
}

# app/services/ai/orchestrator.py
from app.data.model_pricing import PRICING
```

## Folder-per-Domain Examples

When `routers/generate.py` reaches 400 LOC because it handles image, video, audio:

```
# BEFORE
app/routers/generate.py        # 820 LOC

# AFTER
app/routers/generate/
    __init__.py                # combines and re-exports `router`
    image.py                   # ~150 LOC
    video.py                   # ~180 LOC
    audio.py                   # ~120 LOC
```

`__init__.py`:

```python
from fastapi import APIRouter
from .image import router as image_router
from .video import router as video_router
from .audio import router as audio_router

router = APIRouter(prefix="/generate", tags=["generate"])
router.include_router(image_router)
router.include_router(video_router)
router.include_router(audio_router)
```

`main.py` keeps `from app.routers.generate import router` — **zero caller changes**.

## Provider Decomposition

A vendor with multiple format APIs:

```
# BEFORE
providers/falai.py             # 640 LOC

# AFTER
providers/falai/
    __init__.py                # exports FalImageAdapter, FalVideoAdapter
    _client.py                 # HTTP plumbing, auth, retries (shared)
    image.py                   # FalImageAdapter
    video.py                   # FalVideoAdapter
```

`_client.py` is package-internal (underscore prefix) — never imported across packages.

## Service Decomposition

A `wallet_service.py` doing user + admin + history work:

```
# BEFORE
services/wallet_service.py     # 720 LOC

# AFTER
services/wallet/
    __init__.py                # re-exports
    user.py                    # WalletUserService — end-user methods
    admin.py                   # WalletAdminService — admin tools
    history.py                 # WalletHistoryService — read-only history
    _writer.py                 # apply_ledger_entry — single writer
    _repo.py                   # internal repo interface (if shared)
    exceptions.py              # InsufficientFundsError, etc.
```

Admin code cannot leak into user paths. The single-writer file (`_writer.py`) is the only place that mutates balance.

## Worker Handlers vs. Routers

```
# ❌ BAD — router contains worker logic
app/routers/tasks.py           # 800 LOC: HTTP enqueue + image processing + video processing

# ✅ GOOD
app/routers/tasks.py           # ~60 LOC: enqueue, list, cancel
app/services/task_handlers/
    __init__.py
    image.py                   # process_image_task(payload, deps)
    video.py                   # process_video_task(payload, deps)
    audio.py                   # process_audio_task(payload, deps)
```

The queue worker imports from `services/task_handlers`, not from routers.

## Naming for Internal Files

Files prefixed with `_` are **package-internal**:

- Not re-exported from `__init__.py`.
- Not imported from outside the package.
- Examples: `_client.py`, `_writer.py`, `_repo.py`.

## When NOT to Split

Resist premature decomposition:

- File is 280 LOC but holds a coherent single concept → leave it.
- File is 350 LOC of generated code (Pydantic v2 OpenAPI client output) → leave it; it's data-shaped.
- Splitting would create files of <60 LOC each that do nothing meaningful alone → leave it. Single concept = single file.

A 400 LOC file doing one thing is healthier than four 100 LOC files that nobody can locate without grep.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.