From meaningfy-ws-agent-skills
Clean Architecture and Cosmic Python guidance for well-tested, layered Python systems. Use for designing Python projects with layered architecture (models, adapters, services, entrypoints), enforcing Clean Code and SOLID principles, testing strategies (unit tests, BDD, Gherkin), CI/CD setup (pytest, tox, importlinter), and architectural decision-making (ADRs). Applicable to systems requiring strict boundary enforcement, clean separation of concerns, and comprehensive test coverage.
npx claudepluginhub joshuarweaver/cascade-ai-ml-agents-misc-1 --plugin meaningfy-ws-agent-skillsThis skill uses the workspace's default tool permissions.
**Cosmic Python is NOT about system architecture** (service boundaries, deployment topology, C4 models—that's the separate **architecture** skill).
references/MEANINGFY_PROMPT.mdreferences/example-adapters-layer.mdreferences/example-advanced-patterns.mdreferences/example-entrypoints-layer.mdreferences/example-models-layer.mdreferences/example-project-quality-ci.mdreferences/example-services-layer.mdreferences/example-testing-patterns.mdreferences/phase-1-strategic-blueprint-checklist.mdreferences/phase-2-clarity-gate-checklist.mdreferences/stream-coding-methodology.mdCreates isolated Git worktrees for feature branches with prioritized directory selection, gitignore safety checks, auto project setup for Node/Python/Rust/Go, and baseline verification.
Executes implementation plans in current session by dispatching fresh subagents per independent task, with two-stage reviews: spec compliance then code quality.
Dispatches parallel agents to independently tackle 2+ tasks like separate test failures or subsystems without shared state or dependencies.
Cosmic Python is NOT about system architecture (service boundaries, deployment topology, C4 models—that's the separate architecture skill).
Cosmic Python IS about code structure within a Python module or service: how to organize classes, functions, tests, and dependencies so that code is clean, testable, and maintainable.
When to use Cosmic Python:
This skill pairs with:
Our goal is clean code that passes code review quickly while keeping developers productive and safe.
This is achieved through three non-negotiable commitments:
Within each Python module or service, separate code into four tightly-bounded layers:
models/ – Domain logic (business rules, entities, value objects)
adapters/ – Infrastructure and integration (databases, APIs, file systems)
models only; never on services or entrypointsservices/ – Use-case orchestration (application logic, workflows)
models and adaptersmodels and adapters, never on entrypointsentrypoints/ – Request/response boundaries (API, CLI, schedulers, workers)
servicesThe Law:
entrypoints → services → models
↘
adapters → models
Never the reverse. High-level policy (models + services) must never depend on low-level details (adapters). Low-level details depend on abstractions (interfaces/protocols), not the other way around.
When you violate this:
How to enforce:
importlinter in CI/CD to block forbidden imports (see references)"If you can't test it, you can't understand it. If you can't understand it, you can't maintain it."
calculate_customer_tier() not calc_tier()services and entrypoints, not deep in modelsFor each function/class, ask:
If you can't answer these clearly, the architecture is drifting. See the references for detailed refactoring paths.
When you have clear specs from Stream Coding Phase 2 (doc is 9+/10 Clarity Gate):
models/ – Pure domain logic, no I/O, no frameworksadapters/ – Repositories, gateways, clients; mock external services in testsservices/ – Orchestrate models and adapters; test with mocked adaptersentrypoints/ – CLI, API, schedulers; minimal logic, mostly delegationmake check-architecture to validate import contractsBefore approving a pull request, ask:
Layering:
models/ import from services, adapters, or entrypoints? ❌ Should notadapters/ import from services or entrypoints? ❌ Should notservices/ import from entrypoints? ❌ Should notservices/ or models/, not scattered in entrypoints/? ✅ Should beClean Code:
Testing:
coverage reportObservability:
services/ and entrypoints/, not deep in models/? ✅ Correct placementFor each layer, write tests that match its responsibility:
| Layer | What to Test | How | Example |
|---|---|---|---|
models/ | Domain rules, invariants, transformations | Unit tests, no I/O, fast | test_user_cannot_have_negative_balance() |
adapters/ | Integration with external systems (mocked) | Unit tests with mocks, or integration with real test DBs | test_postgres_repository_insert_user() |
services/ | Use-case orchestration and business workflows | Unit tests with mocked adapters | test_user_signup_flow_sends_verification_email() |
entrypoints/ | Request parsing, response formatting, status codes | Unit tests, check contracts | test_api_endpoint_returns_201_on_create() |
Target: 80%+ coverage overall, with focus on each layer's responsibility (not mixing concerns).
Common anti-patterns and how to fix them:
| Anti-Pattern | How It Looks | Why It's Wrong | Fix |
|---|---|---|---|
| I/O in models | import requests in models/user.py | Models can't be tested in isolation | Move HTTP call to adapters/, inject via DIP |
| Business rules in entrypoints | API handler validates and transforms data | Logic is scattered, untestable | Extract to services/, call from handler |
| Circular imports | services/ → adapters/ → services/ | Can't import cleanly, hard to test | Restructure: adapters/ → models/; services/ → adapters/ + models/ |
| Magic strings everywhere | if user.role == "admin" in 5 files | Refactoring is fragile; intent hidden | Define ROLE_ADMIN = "admin" constant once, import everywhere |
| No tests for branching | services/ has 5 branches but only happy path tested | Edge cases crash production | Add parametrized tests for each branch |
| Clever one-liners | [x for x in y if x.z and (a or b)] | Unreadable; maintenance nightmare | Expand to 3-4 readable lines with intermediate variables |
make install # Set up environment (poetry install)
make test # Run unit tests with coverage
make test-bdd # Run BDD feature tests
make check-architecture # Validate import contracts (importlinter)
make lint # Run style checks (pylint, flake8)
make ci # Full pipeline (all above)
Before merging to main:
importlinter to block dependency violationsbilling/models/, billing/services/, not models/billing/import concrete implementations directly in servicesservices/ and entrypoints/, not deep in models/This approach requires team discipline. One developer ignoring layers breaks the architecture for everyone. All developers must respect boundaries and code review rigorously.
Stream Coding (documentation-first planning) and Cosmic Python (clean code structure) work together:
| Phase | Methodology | Focus | Output |
|---|---|---|---|
| 1 | Stream Coding | Strategic: WHAT to build, WHY | Strategic Blueprint + ADRs |
| 2 | Stream Coding | Specifications: HOW to build (AI-ready) | Implementation Specs (9+/10 Clarity Gate) |
| 3 | Cosmic Python | Code: Implement following layers/SOLID | Production code (80%+ tested) |
| 4 | Cosmic Python | Quality: Prevent drift, maintain specs | CI/CD gates, spec-first fixes |