split-monolith
Skill28 starsChanged 7 months agoMITView on GitHub
Safe procedure for decomposing a god file (400+ LOC) into a sub-package without breaking any imports. Load when a file exceeds 400 lines or mixes multiple concerns. Implements vibecodex Principles A1 and A8 — folder-instead-of-file with backward-compatible re-exports.
In this repository14 other files it gives its agents.
- website/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- website/CLAUDE.mdCLAUDE.md
- .cursor/rules/architecture.mdcCursor rule
- .cursor/rules/decomposition.mdcCursor rule
- .cursor/rules/integrations.mdcCursor rule
- llms.txtllms.txt
- add-providerSkill
- debug-backendSkill
- debug-frontendSkill
- debug-goSkill
- new-feature-goSkill
- new-feature-nextjsSkill
- new-featureSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 30
Do NOT split by size. Split by type of responsibility.
- Distinctive: line 55
Write the target structure before touching any file.
- Distinctive: line 65
Do NOT move any code yet.
- Distinctive: line 122
All tests must be GREEN before deleting the original file.
- Distinctive: line 128
Only after Step 5 passes:
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill 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.
Files
split-monolith
A file split done wrong breaks every caller. Follow this procedure exactly — it is reversible at every step.
Step 0 — Confirm the file needs splitting
wc -l app/services/<file>.py| Lines | Action |
| < 400 | Do not split — you're solving a non-problem |
| 400–600 | Plan the split now, execute when convenient |
| > 600 | Split immediately (Principle A7 hard cap) |
Also ask: does this file mix multiple concerns? A file that is long but cohesive is better than a premature split.
Step 1 — Identify the domain splits
Do NOT split by size. Split by type of responsibility.
Good splits (by domain):
wallet_service.py (1200 LOC) →
wallet/user.py ← user-facing operations (charge, refund)
wallet/admin.py ← admin operations (top-up, override)
wallet/history.py ← read-only queriesGood splits (by layer):
generation_service.py (1000 LOC) →
generation/orchestrator.py ← coordinates the flow
generation/cost.py ← cost calculation logic
generation/storage.py ← result persistenceBad splits (by size only — don't do this):
big_service.py →
big_service_part1.py ← meaningless
big_service_part2.py ← meaninglessWrite the target structure before touching any file.
Step 2 — Create the package directory
mkdir app/services/<domain>/Do NOT move any code yet.
Step 3 — Create sub-files one at a time
For each sub-file, copy (not move) the relevant functions:
# Create the new file with the relevant subset
touch app/services/<domain>/user.py
# Copy relevant classes/functions from the originalEach sub-file must:
- Have its own imports (do not rely on
*imports) - Be under 200 LOC (you're splitting — keep it lean)
- Contain one cohesive responsibility
Step 4 — Create __init__.py with ALL old names re-exported
This is the most important step. Every name that existed in the original file must still be importable from the same path.
# app/services/<domain>/__init__.py
# Principle A8: re-export everything so callers don't change.
from app.services.<domain>.user import CreditsUserService
from app.services.<domain>.admin import CreditsAdminService
from app.services.<domain>.user import get_credits_user_service
# Backward-compat alias if the old class had a different name
CreditsService = CreditsUserService # old name → new class
__all__ = [
"CreditsUserService",
"CreditsAdminService",
"CreditsService", # backward compat
"get_credits_user_service",
]Step 5 — Verify no import breaks
# Check every file that imported from the old module still works
python3 -c "from app.services.<domain> import <OldClassName>"
python3 -c "from app.services.<domain> import <AnotherClass>"
# Run the full test suite
pytest tests/ -x -qAll tests must be GREEN before deleting the original file.
Step 6 — Delete the original file
Only after Step 5 passes:
rm app/services/<original_file>.pyRun tests again:
pytest tests/ -x -q
bash scripts/lint-architecture.shBoth must pass.
Common mistakes
| Mistake | Consequence | Prevention |
Split before writing __init__.py | Import errors everywhere | Always create __init__.py first |
| Split by size, not responsibility | Sub-files still coupled | Ask: "what is the single job of this file?" |
| Forget to re-export old names | Callers break silently | List every public name before splitting |
| Move code instead of copy+verify | Can't roll back | Copy first, delete only after tests pass |
| Split and refactor at same time | Impossible to debug | One PR = one split. No logic changes. |
Verification
The split was done correctly when:
- All sub-files are under 200 LOC
__init__.pyre-exports every name that existed beforepython3 -c "from app.services.<domain> import <OldName>"workspytest tests/ -x -qis greenbash scripts/lint-architecture.shexits 0- Original file is deleted
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
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.

