maestro-bundle 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/bundle-ai-agents/skills/agent-orchestration/SKILL.md +107 -41
- package/templates/bundle-ai-agents/skills/agent-orchestration/references/graph-patterns.md +50 -0
- package/templates/bundle-ai-agents/skills/agent-orchestration/references/routing-strategies.md +47 -0
- package/templates/bundle-ai-agents/skills/api-design/SKILL.md +125 -16
- package/templates/bundle-ai-agents/skills/api-design/references/pydantic-patterns.md +72 -0
- package/templates/bundle-ai-agents/skills/api-design/references/rest-conventions.md +51 -0
- package/templates/bundle-ai-agents/skills/clean-architecture/SKILL.md +113 -21
- package/templates/bundle-ai-agents/skills/clean-architecture/references/dependency-injection.md +60 -0
- package/templates/bundle-ai-agents/skills/clean-architecture/references/layer-rules.md +56 -0
- package/templates/bundle-ai-agents/skills/context-engineering/SKILL.md +104 -36
- package/templates/bundle-ai-agents/skills/context-engineering/references/compression-techniques.md +76 -0
- package/templates/bundle-ai-agents/skills/context-engineering/references/context-budget-calculator.md +45 -0
- package/templates/bundle-ai-agents/skills/database-modeling/SKILL.md +146 -19
- package/templates/bundle-ai-agents/skills/database-modeling/references/index-strategies.md +48 -0
- package/templates/bundle-ai-agents/skills/database-modeling/references/naming-conventions.md +27 -0
- package/templates/bundle-ai-agents/skills/docker-containerization/SKILL.md +124 -15
- package/templates/bundle-ai-agents/skills/docker-containerization/references/compose-patterns.md +97 -0
- package/templates/bundle-ai-agents/skills/docker-containerization/references/dockerfile-checklist.md +37 -0
- package/templates/bundle-ai-agents/skills/eval-testing/SKILL.md +113 -25
- package/templates/bundle-ai-agents/skills/eval-testing/references/eval-types.md +52 -0
- package/templates/bundle-ai-agents/skills/eval-testing/references/golden-dataset-template.md +59 -0
- package/templates/bundle-ai-agents/skills/memory-management/SKILL.md +112 -28
- package/templates/bundle-ai-agents/skills/memory-management/references/memory-tiers.md +41 -0
- package/templates/bundle-ai-agents/skills/memory-management/references/namespace-conventions.md +41 -0
- package/templates/bundle-ai-agents/skills/prompt-engineering/SKILL.md +139 -47
- package/templates/bundle-ai-agents/skills/prompt-engineering/references/anti-patterns.md +59 -0
- package/templates/bundle-ai-agents/skills/prompt-engineering/references/prompt-templates.md +75 -0
- package/templates/bundle-ai-agents/skills/rag-pipeline/SKILL.md +104 -27
- package/templates/bundle-ai-agents/skills/rag-pipeline/references/chunking-strategies.md +27 -0
- package/templates/bundle-ai-agents/skills/rag-pipeline/references/embedding-models.md +31 -0
- package/templates/bundle-ai-agents/skills/rag-pipeline/references/rag-evaluation.md +39 -0
- package/templates/bundle-ai-agents/skills/testing-strategy/SKILL.md +127 -18
- package/templates/bundle-ai-agents/skills/testing-strategy/references/fixture-patterns.md +81 -0
- package/templates/bundle-ai-agents/skills/testing-strategy/references/naming-conventions.md +69 -0
- package/templates/bundle-base/skills/branch-strategy/SKILL.md +134 -21
- package/templates/bundle-base/skills/branch-strategy/references/branch-rules.md +40 -0
- package/templates/bundle-base/skills/code-review/SKILL.md +123 -38
- package/templates/bundle-base/skills/code-review/references/review-checklist.md +45 -0
- package/templates/bundle-base/skills/commit-pattern/SKILL.md +98 -39
- package/templates/bundle-base/skills/commit-pattern/references/conventional-commits.md +40 -0
- package/templates/bundle-data-pipeline/skills/data-preprocessing/SKILL.md +110 -19
- package/templates/bundle-data-pipeline/skills/data-preprocessing/references/pandas-cheatsheet.md +63 -0
- package/templates/bundle-data-pipeline/skills/data-preprocessing/references/pandera-schemas.md +44 -0
- package/templates/bundle-data-pipeline/skills/docker-containerization/SKILL.md +132 -16
- package/templates/bundle-data-pipeline/skills/docker-containerization/references/compose-patterns.md +82 -0
- package/templates/bundle-data-pipeline/skills/docker-containerization/references/dockerfile-best-practices.md +57 -0
- package/templates/bundle-data-pipeline/skills/feature-engineering/SKILL.md +143 -45
- package/templates/bundle-data-pipeline/skills/feature-engineering/references/encoding-guide.md +41 -0
- package/templates/bundle-data-pipeline/skills/feature-engineering/references/scaling-guide.md +38 -0
- package/templates/bundle-data-pipeline/skills/mlops-pipeline/SKILL.md +156 -37
- package/templates/bundle-data-pipeline/skills/mlops-pipeline/references/mlflow-commands.md +69 -0
- package/templates/bundle-data-pipeline/skills/model-training/SKILL.md +152 -33
- package/templates/bundle-data-pipeline/skills/model-training/references/evaluation-metrics.md +52 -0
- package/templates/bundle-data-pipeline/skills/model-training/references/model-selection-guide.md +41 -0
- package/templates/bundle-data-pipeline/skills/rag-pipeline/SKILL.md +127 -39
- package/templates/bundle-data-pipeline/skills/rag-pipeline/references/chunking-strategies.md +51 -0
- package/templates/bundle-data-pipeline/skills/rag-pipeline/references/embedding-models.md +49 -0
- package/templates/bundle-frontend-spa/skills/authentication/SKILL.md +196 -13
- package/templates/bundle-frontend-spa/skills/authentication/references/jwt-security.md +41 -0
- package/templates/bundle-frontend-spa/skills/component-design/SKILL.md +191 -41
- package/templates/bundle-frontend-spa/skills/component-design/references/accessibility-checklist.md +41 -0
- package/templates/bundle-frontend-spa/skills/component-design/references/tailwind-patterns.md +65 -0
- package/templates/bundle-frontend-spa/skills/e2e-testing/SKILL.md +241 -79
- package/templates/bundle-frontend-spa/skills/e2e-testing/references/playwright-selectors.md +66 -0
- package/templates/bundle-frontend-spa/skills/e2e-testing/references/test-patterns.md +82 -0
- package/templates/bundle-frontend-spa/skills/integration-api/SKILL.md +221 -31
- package/templates/bundle-frontend-spa/skills/integration-api/references/api-patterns.md +81 -0
- package/templates/bundle-frontend-spa/skills/react-patterns/SKILL.md +195 -70
- package/templates/bundle-frontend-spa/skills/react-patterns/references/component-checklist.md +22 -0
- package/templates/bundle-frontend-spa/skills/react-patterns/references/hook-patterns.md +63 -0
- package/templates/bundle-frontend-spa/skills/responsive-layout/SKILL.md +162 -22
- package/templates/bundle-frontend-spa/skills/responsive-layout/references/breakpoint-guide.md +63 -0
- package/templates/bundle-frontend-spa/skills/state-management/SKILL.md +158 -30
- package/templates/bundle-frontend-spa/skills/state-management/references/react-query-config.md +64 -0
- package/templates/bundle-frontend-spa/skills/state-management/references/state-patterns.md +78 -0
- package/templates/bundle-jhipster-microservices/skills/ci-cd-pipeline/SKILL.md +135 -45
- package/templates/bundle-jhipster-microservices/skills/ci-cd-pipeline/references/gitlab-ci-templates.md +93 -0
- package/templates/bundle-jhipster-microservices/skills/clean-architecture/SKILL.md +87 -21
- package/templates/bundle-jhipster-microservices/skills/clean-architecture/references/layer-rules.md +78 -0
- package/templates/bundle-jhipster-microservices/skills/ddd-tactical/SKILL.md +94 -25
- package/templates/bundle-jhipster-microservices/skills/ddd-tactical/references/ddd-patterns.md +48 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-angular/SKILL.md +63 -21
- package/templates/bundle-jhipster-microservices/skills/jhipster-angular/references/angular-microservices.md +40 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-angular/references/angular-structure.md +59 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-docker-k8s/SKILL.md +125 -91
- package/templates/bundle-jhipster-microservices/skills/jhipster-docker-k8s/references/docker-k8s-commands.md +68 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-entities/SKILL.md +72 -20
- package/templates/bundle-jhipster-microservices/skills/jhipster-entities/references/cross-service-entities.md +36 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-entities/references/jdl-types.md +56 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-gateway/SKILL.md +80 -8
- package/templates/bundle-jhipster-microservices/skills/jhipster-gateway/references/gateway-config.md +43 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-kafka/SKILL.md +115 -22
- package/templates/bundle-jhipster-microservices/skills/jhipster-kafka/references/kafka-events.md +39 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-registry/SKILL.md +92 -23
- package/templates/bundle-jhipster-microservices/skills/jhipster-registry/references/consul-config.md +61 -0
- package/templates/bundle-jhipster-microservices/skills/jhipster-service/SKILL.md +81 -18
- package/templates/bundle-jhipster-microservices/skills/jhipster-service/references/service-patterns.md +40 -0
- package/templates/bundle-jhipster-microservices/skills/testing-strategy/SKILL.md +101 -20
- package/templates/bundle-jhipster-microservices/skills/testing-strategy/references/test-naming.md +55 -0
- package/templates/bundle-jhipster-monorepo/skills/clean-architecture/SKILL.md +87 -21
- package/templates/bundle-jhipster-monorepo/skills/clean-architecture/references/layer-rules.md +78 -0
- package/templates/bundle-jhipster-monorepo/skills/ddd-tactical/SKILL.md +94 -25
- package/templates/bundle-jhipster-monorepo/skills/ddd-tactical/references/ddd-patterns.md +48 -0
- package/templates/bundle-jhipster-monorepo/skills/jhipster-angular/SKILL.md +99 -52
- package/templates/bundle-jhipster-monorepo/skills/jhipster-angular/references/angular-structure.md +59 -0
- package/templates/bundle-jhipster-monorepo/skills/jhipster-entities/SKILL.md +89 -36
- package/templates/bundle-jhipster-monorepo/skills/jhipster-entities/references/jdl-types.md +56 -0
- package/templates/bundle-jhipster-monorepo/skills/jhipster-liquibase/SKILL.md +123 -23
- package/templates/bundle-jhipster-monorepo/skills/jhipster-liquibase/references/liquibase-operations.md +95 -0
- package/templates/bundle-jhipster-monorepo/skills/jhipster-security/SKILL.md +106 -19
- package/templates/bundle-jhipster-monorepo/skills/jhipster-security/references/security-checklist.md +47 -0
- package/templates/bundle-jhipster-monorepo/skills/jhipster-spring/SKILL.md +84 -16
- package/templates/bundle-jhipster-monorepo/skills/jhipster-spring/references/spring-layers.md +41 -0
- package/templates/bundle-jhipster-monorepo/skills/testing-strategy/SKILL.md +101 -20
- package/templates/bundle-jhipster-monorepo/skills/testing-strategy/references/test-naming.md +55 -0
|
@@ -1,28 +1,59 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: clean-architecture
|
|
3
|
-
description:
|
|
3
|
+
description: Implement Clean Architecture with domain, application, and infrastructure layers. Use when creating modules, organizing code in layers, separating business rules from infrastructure, or structuring a new project.
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
author: Maestro
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# Clean Architecture
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
Structure code into Domain, Application, and Infrastructure layers with strict dependency rules, ensuring business logic is isolated from frameworks and external services.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
- Starting a new module or microservice
|
|
14
|
+
- Refactoring code that mixes business logic with infrastructure
|
|
15
|
+
- Creating entities, value objects, use cases, or repositories
|
|
16
|
+
- Reviewing code for layer violations
|
|
17
|
+
- Setting up project structure for a new feature
|
|
18
|
+
|
|
19
|
+
## Available Operations
|
|
20
|
+
1. Define domain entities with business rules and events
|
|
21
|
+
2. Create application use cases that orchestrate domain logic
|
|
22
|
+
3. Implement infrastructure adapters (repositories, HTTP clients)
|
|
23
|
+
4. Set up dependency injection wiring
|
|
24
|
+
5. Validate layer dependencies (no inward violations)
|
|
25
|
+
|
|
26
|
+
## Multi-Step Workflow
|
|
27
|
+
|
|
28
|
+
### Step 1: Set Up the Layer Structure
|
|
29
|
+
|
|
30
|
+
Create the directory structure following the dependency rule: outer layers depend on inner layers, never the reverse.
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
mkdir -p src/domain/entities src/domain/repositories src/domain/value_objects src/domain/events
|
|
34
|
+
mkdir -p src/application/use_cases src/application/dtos
|
|
35
|
+
mkdir -p src/infrastructure/persistence src/infrastructure/http
|
|
36
|
+
mkdir -p src/api/controllers
|
|
37
|
+
```
|
|
9
38
|
|
|
10
39
|
```
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
Dependency Rule:
|
|
22
|
-
Domain
|
|
40
|
+
+------------------------------+
|
|
41
|
+
| API / CLI | <- Controllers, Routers
|
|
42
|
+
+------------------------------+
|
|
43
|
+
| APPLICATION | <- Use Cases, DTOs
|
|
44
|
+
+------------------------------+
|
|
45
|
+
| DOMAIN | <- Entities, VOs, Events, Repos (interface)
|
|
46
|
+
+------------------------------+
|
|
47
|
+
| INFRASTRUCTURE | <- DB, HTTP clients, Frameworks
|
|
48
|
+
+------------------------------+
|
|
49
|
+
|
|
50
|
+
Dependency Rule: arrows point INWARD (infra -> domain)
|
|
51
|
+
Domain NEVER imports from infrastructure
|
|
23
52
|
```
|
|
24
53
|
|
|
25
|
-
|
|
54
|
+
### Step 2: Build the Domain Layer
|
|
55
|
+
|
|
56
|
+
Domain contains entities with business rules, value objects, domain events, and repository interfaces (ports).
|
|
26
57
|
|
|
27
58
|
```python
|
|
28
59
|
# domain/entities/demand.py
|
|
@@ -45,7 +76,9 @@ class Demand:
|
|
|
45
76
|
def pending_events(self) -> list[DomainEvent]:
|
|
46
77
|
return list(self._events)
|
|
47
78
|
|
|
48
|
-
# domain/repositories/demand_repository.py (PORT
|
|
79
|
+
# domain/repositories/demand_repository.py (PORT -- interface only)
|
|
80
|
+
from abc import ABC, abstractmethod
|
|
81
|
+
|
|
49
82
|
class DemandRepository(ABC):
|
|
50
83
|
@abstractmethod
|
|
51
84
|
def find_by_id(self, id: DemandId) -> Demand: ...
|
|
@@ -53,7 +86,9 @@ class DemandRepository(ABC):
|
|
|
53
86
|
def save(self, demand: Demand) -> None: ...
|
|
54
87
|
```
|
|
55
88
|
|
|
56
|
-
|
|
89
|
+
### Step 3: Build the Application Layer
|
|
90
|
+
|
|
91
|
+
Application layer contains use cases that orchestrate domain entities. Use cases are the entry points for business operations.
|
|
57
92
|
|
|
58
93
|
```python
|
|
59
94
|
# application/use_cases/decompose_demand.py
|
|
@@ -72,7 +107,9 @@ class DecomposeDemand:
|
|
|
72
107
|
return DecomposeDemandOutput(tasks=[TaskDTO.from_entity(t) for t in tasks])
|
|
73
108
|
```
|
|
74
109
|
|
|
75
|
-
|
|
110
|
+
### Step 4: Build the Infrastructure Layer
|
|
111
|
+
|
|
112
|
+
Infrastructure implements the ports defined in the domain layer (adapters).
|
|
76
113
|
|
|
77
114
|
```python
|
|
78
115
|
# infrastructure/persistence/pg_demand_repository.py (ADAPTER)
|
|
@@ -92,8 +129,63 @@ class PgDemandRepository(DemandRepository):
|
|
|
92
129
|
self._session.commit()
|
|
93
130
|
```
|
|
94
131
|
|
|
95
|
-
|
|
132
|
+
### Step 5: Validate Layer Dependencies
|
|
133
|
+
|
|
134
|
+
Check that no layer violations exist. Domain must never import from infrastructure.
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
# Check for import violations (domain importing from infrastructure)
|
|
138
|
+
grep -r "from src.infrastructure" src/domain/ && echo "VIOLATION: Domain imports infrastructure!" || echo "OK: No violations"
|
|
139
|
+
grep -r "from src.api" src/domain/ && echo "VIOLATION: Domain imports API!" || echo "OK: No violations"
|
|
140
|
+
grep -r "from src.infrastructure" src/application/ && echo "VIOLATION: Application imports infrastructure!" || echo "OK: No violations"
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Use a linter rule to enforce this:
|
|
96
144
|
|
|
97
|
-
|
|
145
|
+
```bash
|
|
146
|
+
ruff check src/ --select I # Check import ordering
|
|
147
|
+
```
|
|
98
148
|
|
|
99
|
-
|
|
149
|
+
## Resources
|
|
150
|
+
- `references/layer-rules.md` - Detailed rules for what belongs in each layer
|
|
151
|
+
- `references/dependency-injection.md` - Patterns for wiring layers together
|
|
152
|
+
|
|
153
|
+
## Examples
|
|
154
|
+
|
|
155
|
+
### Example 1: Create a New Feature Module
|
|
156
|
+
User asks: "Create a user management module with CRUD operations."
|
|
157
|
+
Response approach:
|
|
158
|
+
1. Create domain entity `User` with validation rules and events
|
|
159
|
+
2. Create value objects: `UserId`, `Email`, `UserStatus`
|
|
160
|
+
3. Define `UserRepository` port (abstract class) in domain
|
|
161
|
+
4. Create use cases: `CreateUser`, `GetUser`, `ListUsers`, `UpdateUser`
|
|
162
|
+
5. Implement `PgUserRepository` adapter in infrastructure
|
|
163
|
+
6. Create `UserController` in API layer with FastAPI routes
|
|
164
|
+
7. Wire dependencies with dependency injection
|
|
165
|
+
|
|
166
|
+
### Example 2: Refactor a "Fat Controller"
|
|
167
|
+
User asks: "Our controller has all the business logic. Refactor it to Clean Architecture."
|
|
168
|
+
Response approach:
|
|
169
|
+
1. Identify business rules in the controller
|
|
170
|
+
2. Extract domain entity with those rules as methods
|
|
171
|
+
3. Create a use case that orchestrates the entity operations
|
|
172
|
+
4. Move database calls to a repository adapter
|
|
173
|
+
5. Controller should only: parse request -> call use case -> format response
|
|
174
|
+
6. Run import validation to confirm no layer violations
|
|
175
|
+
|
|
176
|
+
### Example 3: Add Domain Events
|
|
177
|
+
User asks: "When a demand is decomposed, we need to notify other services."
|
|
178
|
+
Response approach:
|
|
179
|
+
1. Create `DemandDecomposed` domain event class
|
|
180
|
+
2. Add event collection to the `Demand` entity
|
|
181
|
+
3. Emit the event in the `decompose()` method
|
|
182
|
+
4. Publish events in the use case after saving
|
|
183
|
+
5. Infrastructure layer handles event delivery (message queue, HTTP, etc.)
|
|
184
|
+
|
|
185
|
+
## Notes
|
|
186
|
+
- **Golden rule**: Use Case orchestrates -> Entity contains rules -> Repository persists
|
|
187
|
+
- Never put business logic in controllers, repositories, or generic "Service" classes
|
|
188
|
+
- Domain layer has zero external dependencies (no frameworks, no database drivers)
|
|
189
|
+
- Use cases should be small (one public method: `execute`)
|
|
190
|
+
- Value objects enforce validation at construction -- use them for all domain primitives
|
|
191
|
+
- Repository interfaces (ports) live in the domain; implementations (adapters) live in infrastructure
|
package/templates/bundle-ai-agents/skills/clean-architecture/references/dependency-injection.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Dependency Injection Reference
|
|
2
|
+
|
|
3
|
+
## FastAPI Dependency Injection
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
# infrastructure/dependencies.py
|
|
7
|
+
from fastapi import Depends
|
|
8
|
+
from sqlalchemy.orm import Session
|
|
9
|
+
|
|
10
|
+
def get_db_session() -> Session:
|
|
11
|
+
session = SessionLocal()
|
|
12
|
+
try:
|
|
13
|
+
yield session
|
|
14
|
+
finally:
|
|
15
|
+
session.close()
|
|
16
|
+
|
|
17
|
+
def get_demand_repository(session: Session = Depends(get_db_session)) -> DemandRepository:
|
|
18
|
+
return PgDemandRepository(session)
|
|
19
|
+
|
|
20
|
+
def get_decompose_demand(
|
|
21
|
+
repo: DemandRepository = Depends(get_demand_repository),
|
|
22
|
+
planner: TaskPlanner = Depends(get_task_planner),
|
|
23
|
+
event_bus: EventBus = Depends(get_event_bus),
|
|
24
|
+
) -> DecomposeDemand:
|
|
25
|
+
return DecomposeDemand(repo=repo, planner=planner, event_bus=event_bus)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage in Controller
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
@router.post("/demands/{demand_id}/decompose")
|
|
32
|
+
async def decompose_demand(
|
|
33
|
+
demand_id: str,
|
|
34
|
+
use_case: DecomposeDemand = Depends(get_decompose_demand),
|
|
35
|
+
):
|
|
36
|
+
return use_case.execute(demand_id)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Testing with Fakes
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# In tests, inject fakes instead of real implementations
|
|
43
|
+
def test_decompose_demand():
|
|
44
|
+
repo = FakeDemandRepository()
|
|
45
|
+
planner = FakePlanner(tasks=[Task(...)])
|
|
46
|
+
event_bus = FakeEventBus()
|
|
47
|
+
|
|
48
|
+
use_case = DecomposeDemand(repo=repo, planner=planner, event_bus=event_bus)
|
|
49
|
+
result = use_case.execute("demand-123")
|
|
50
|
+
|
|
51
|
+
assert len(result.tasks) == 1
|
|
52
|
+
assert len(event_bus.published_events) == 1
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Principle
|
|
56
|
+
|
|
57
|
+
- Controllers depend on use cases (via interface).
|
|
58
|
+
- Use cases depend on repository interfaces (ports).
|
|
59
|
+
- Infrastructure provides concrete implementations (adapters).
|
|
60
|
+
- Tests inject fakes that implement the same interfaces.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Layer Rules Reference
|
|
2
|
+
|
|
3
|
+
## Domain Layer
|
|
4
|
+
|
|
5
|
+
**Contains**: Entities, Value Objects, Domain Events, Repository Interfaces (Ports), Domain Services.
|
|
6
|
+
|
|
7
|
+
**Rules**:
|
|
8
|
+
- ZERO external dependencies (no frameworks, no database drivers, no HTTP clients)
|
|
9
|
+
- Contains all business rules and invariants
|
|
10
|
+
- Entities have identity and lifecycle
|
|
11
|
+
- Value Objects are immutable and compared by value
|
|
12
|
+
- Domain Events record things that happened
|
|
13
|
+
- Repository interfaces define WHAT, not HOW
|
|
14
|
+
|
|
15
|
+
**Allowed imports**: Only Python standard library and other domain classes.
|
|
16
|
+
|
|
17
|
+
**Forbidden imports**: SQLAlchemy, FastAPI, requests, any infrastructure package.
|
|
18
|
+
|
|
19
|
+
## Application Layer
|
|
20
|
+
|
|
21
|
+
**Contains**: Use Cases, DTOs (Data Transfer Objects), Application Services.
|
|
22
|
+
|
|
23
|
+
**Rules**:
|
|
24
|
+
- Orchestrates domain entities and services
|
|
25
|
+
- One use case = one business operation
|
|
26
|
+
- Each use case has a single public method (`execute`)
|
|
27
|
+
- Receives raw data, returns DTOs (never entities)
|
|
28
|
+
- Publishes domain events after persisting changes
|
|
29
|
+
|
|
30
|
+
**Allowed imports**: Domain layer classes.
|
|
31
|
+
|
|
32
|
+
**Forbidden imports**: FastAPI, SQLAlchemy, any infrastructure or API package.
|
|
33
|
+
|
|
34
|
+
## Infrastructure Layer
|
|
35
|
+
|
|
36
|
+
**Contains**: Repository Implementations (Adapters), ORM Models, HTTP Clients, Message Queue Clients, External Service Integrations.
|
|
37
|
+
|
|
38
|
+
**Rules**:
|
|
39
|
+
- Implements interfaces defined in the domain layer
|
|
40
|
+
- Converts between domain entities and persistence models
|
|
41
|
+
- Handles all framework-specific code
|
|
42
|
+
- Contains database migrations
|
|
43
|
+
|
|
44
|
+
**Allowed imports**: Domain layer, Application layer, third-party libraries.
|
|
45
|
+
|
|
46
|
+
## API Layer
|
|
47
|
+
|
|
48
|
+
**Contains**: Controllers/Routers, Request/Response Models, Middleware, Authentication.
|
|
49
|
+
|
|
50
|
+
**Rules**:
|
|
51
|
+
- Thin layer: parse request -> call use case -> format response
|
|
52
|
+
- No business logic
|
|
53
|
+
- Handles HTTP-specific concerns (status codes, headers, auth)
|
|
54
|
+
- Input validation at the boundary (Pydantic models)
|
|
55
|
+
|
|
56
|
+
**Allowed imports**: Application layer (use cases, DTOs). Never domain entities directly.
|
|
@@ -1,48 +1,72 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: context-engineering
|
|
3
|
-
description:
|
|
3
|
+
description: Implement the 4 context engineering strategies (Write, Select, Compress, Isolate) for AI agents. Use when managing context windows, optimizing what an agent receives, or reducing token costs.
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
author: Maestro
|
|
4
6
|
---
|
|
5
7
|
|
|
6
8
|
# Context Engineering
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
Apply the four context engineering strategies -- Write, Select, Compress, Isolate -- to maximize agent effectiveness while minimizing token costs.
|
|
9
11
|
|
|
10
|
-
##
|
|
12
|
+
## When to Use
|
|
13
|
+
- Designing what context an agent receives before execution
|
|
14
|
+
- Optimizing a system prompt that is too long or unfocused
|
|
15
|
+
- Reducing token costs on expensive LLM calls
|
|
16
|
+
- Setting up context isolation between agents in a multi-agent system
|
|
17
|
+
- Debugging an agent that "forgets" instructions or loses focus
|
|
11
18
|
|
|
12
|
-
|
|
19
|
+
## Available Operations
|
|
20
|
+
1. Write persistent context (CLAUDE.md, agents.md, skills)
|
|
21
|
+
2. Select relevant context via retrieval
|
|
22
|
+
3. Compress context to reduce token usage
|
|
23
|
+
4. Isolate context per agent scope
|
|
24
|
+
5. Budget context allocation across the window
|
|
25
|
+
|
|
26
|
+
## Multi-Step Workflow
|
|
27
|
+
|
|
28
|
+
### Step 1: Write Context -- Persistent Memory
|
|
29
|
+
|
|
30
|
+
Define what the agent "knows" before any task begins. This is your baseline context layer.
|
|
13
31
|
|
|
14
32
|
```
|
|
15
|
-
CLAUDE.md
|
|
16
|
-
agents.md
|
|
17
|
-
skills/SKILL.md
|
|
18
|
-
memory/
|
|
33
|
+
CLAUDE.md -> Project standards, architecture, decisions
|
|
34
|
+
agents.md -> Agent-specific behavior and role definition
|
|
35
|
+
skills/SKILL.md -> On-demand capabilities loaded when needed
|
|
36
|
+
memory/ -> Learnings from previous executions
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Check your CLAUDE.md token count:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
wc -w CLAUDE.md # Should be under ~1500 words (~2000 tokens)
|
|
19
43
|
```
|
|
20
44
|
|
|
21
|
-
**
|
|
45
|
+
**Rule**: CLAUDE.md must stay under 2000 tokens. If it grows beyond that, move details into skills that are loaded on-demand.
|
|
22
46
|
|
|
23
|
-
|
|
47
|
+
### Step 2: Select Context -- Retrieval for the Current Task
|
|
24
48
|
|
|
25
|
-
|
|
49
|
+
Inject only the context relevant to the current task. Never dump everything.
|
|
26
50
|
|
|
27
51
|
```python
|
|
28
52
|
def select_context(task: Task, retriever) -> str:
|
|
29
|
-
#
|
|
53
|
+
# Retrieve skills relevant to the task
|
|
30
54
|
relevant_skills = retriever.invoke(task.description)
|
|
31
55
|
|
|
32
|
-
#
|
|
56
|
+
# Search for related code in the repository
|
|
33
57
|
related_code = code_search(task.description, worktree_path)
|
|
34
58
|
|
|
35
|
-
#
|
|
59
|
+
# Find similar past decisions
|
|
36
60
|
past_decisions = memory_store.search(task.description, k=3)
|
|
37
61
|
|
|
38
62
|
return format_context(relevant_skills, related_code, past_decisions)
|
|
39
63
|
```
|
|
40
64
|
|
|
41
|
-
**
|
|
65
|
+
**Rule**: Never inject more than 30% of the context window with selected context. Leave space for the agent to reason.
|
|
42
66
|
|
|
43
|
-
|
|
67
|
+
### Step 3: Compress Context -- Reduce Without Losing Essentials
|
|
44
68
|
|
|
45
|
-
|
|
69
|
+
When context exceeds budget, compress it while preserving critical information.
|
|
46
70
|
|
|
47
71
|
```python
|
|
48
72
|
async def compress_code(code: str, max_tokens: int = 2000) -> str:
|
|
@@ -50,23 +74,23 @@ async def compress_code(code: str, max_tokens: int = 2000) -> str:
|
|
|
50
74
|
return code
|
|
51
75
|
|
|
52
76
|
summary = await llm.ainvoke(f"""
|
|
53
|
-
|
|
54
|
-
-
|
|
55
|
-
-
|
|
56
|
-
-
|
|
57
|
-
-
|
|
77
|
+
Summarize this code keeping:
|
|
78
|
+
- Function/class signatures
|
|
79
|
+
- Input/output types
|
|
80
|
+
- Main logic (without implementation details)
|
|
81
|
+
- Relevant imports
|
|
58
82
|
|
|
59
|
-
|
|
83
|
+
Code:
|
|
60
84
|
{code}
|
|
61
85
|
""")
|
|
62
86
|
return summary.content
|
|
63
87
|
```
|
|
64
88
|
|
|
65
|
-
**
|
|
89
|
+
**Rule**: Never compress code the agent is about to modify. That code must remain complete and uncompressed.
|
|
66
90
|
|
|
67
|
-
|
|
91
|
+
### Step 4: Isolate Context -- Scope Per Agent
|
|
68
92
|
|
|
69
|
-
|
|
93
|
+
Each agent should see only what it needs. No shared context windows.
|
|
70
94
|
|
|
71
95
|
```python
|
|
72
96
|
agent_contexts = {
|
|
@@ -83,16 +107,60 @@ agent_contexts = {
|
|
|
83
107
|
}
|
|
84
108
|
```
|
|
85
109
|
|
|
86
|
-
**
|
|
110
|
+
**Rule**: Agents never share a context window. Communication happens via structured messages, not shared context.
|
|
111
|
+
|
|
112
|
+
### Step 5: Budget Context Allocation
|
|
113
|
+
|
|
114
|
+
For a 200k token model, allocate the context window as follows:
|
|
87
115
|
|
|
88
|
-
|
|
116
|
+
| Component | % | Tokens | Description |
|
|
117
|
+
|---|---|---|---|
|
|
118
|
+
| System prompt + CLAUDE.md | 5% | 10k | Identity, rules, format |
|
|
119
|
+
| Loaded skills | 10% | 20k | On-demand capabilities |
|
|
120
|
+
| Retrieved code/docs (Select) | 25% | 50k | Task-relevant context |
|
|
121
|
+
| Conversation history | 15% | 30k | Previous messages |
|
|
122
|
+
| **Reasoning space** | **45%** | **90k** | Model's working memory |
|
|
89
123
|
|
|
90
|
-
|
|
124
|
+
Verify current usage:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
python -m context.budget --prompt system_prompt.md --skills skills/ --history conversation.json
|
|
128
|
+
```
|
|
91
129
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
130
|
+
## Resources
|
|
131
|
+
- `references/context-budget-calculator.md` - Formulas and guidelines for calculating context budgets
|
|
132
|
+
- `references/compression-techniques.md` - Techniques for compressing different content types
|
|
133
|
+
|
|
134
|
+
## Examples
|
|
135
|
+
|
|
136
|
+
### Example 1: Set Up Context for a New Agent
|
|
137
|
+
User asks: "Configure the context strategy for our new QA agent."
|
|
138
|
+
Response approach:
|
|
139
|
+
1. Write: Create agents/qa.md with QA agent identity, rules, and test standards
|
|
140
|
+
2. Select: Configure retriever to pull test files and coverage reports relevant to the task
|
|
141
|
+
3. Isolate: Limit visibility to `tests/`, `src/` (read-only), and CI config files
|
|
142
|
+
4. Budget: Allocate 5% system prompt, 20% test context, 20% source code, 55% reasoning
|
|
143
|
+
|
|
144
|
+
### Example 2: Fix an Agent That Loses Focus
|
|
145
|
+
User asks: "Our backend agent keeps forgetting to follow Clean Architecture halfway through long tasks."
|
|
146
|
+
Response approach:
|
|
147
|
+
1. Check CLAUDE.md size -- if over 2000 tokens, move details to skills
|
|
148
|
+
2. Move Clean Architecture rules into a skill that gets loaded per-task
|
|
149
|
+
3. Add a "checkpoint" mechanism: after every 5 tool calls, re-inject key rules
|
|
150
|
+
4. Reduce conversation history to last 10 messages to free reasoning space
|
|
151
|
+
|
|
152
|
+
### Example 3: Reduce Token Costs
|
|
153
|
+
User asks: "Our agent costs are too high. Optimize the context usage."
|
|
154
|
+
Response approach:
|
|
155
|
+
1. Audit current context window usage with the budget calculator
|
|
156
|
+
2. Compress code summaries for files the agent reads but does not modify
|
|
157
|
+
3. Reduce retrieved context from k=20 to k=5 with re-ranking
|
|
158
|
+
4. Shorten system prompt by moving examples to a skill file
|
|
159
|
+
5. Target: 40% reduction in input tokens per invocation
|
|
160
|
+
|
|
161
|
+
## Notes
|
|
162
|
+
- The 45% reasoning space is sacred -- never fill more than 55% of the window with context
|
|
163
|
+
- CLAUDE.md changes affect all agents -- be deliberate about what goes there
|
|
164
|
+
- Skills are the pressure release valve: move detailed instructions there for on-demand loading
|
|
165
|
+
- Monitor token usage per agent invocation to catch context bloat early
|
|
166
|
+
- Code the agent will modify must always be included uncompressed
|
package/templates/bundle-ai-agents/skills/context-engineering/references/compression-techniques.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Compression Techniques Reference
|
|
2
|
+
|
|
3
|
+
## Technique 1: Code Skeleton
|
|
4
|
+
|
|
5
|
+
Keep signatures and types, remove implementation details.
|
|
6
|
+
|
|
7
|
+
**Before** (450 tokens):
|
|
8
|
+
```python
|
|
9
|
+
class DemandRepository:
|
|
10
|
+
def __init__(self, session: Session):
|
|
11
|
+
self._session = session
|
|
12
|
+
|
|
13
|
+
def find_by_id(self, id: DemandId) -> Demand:
|
|
14
|
+
model = self._session.query(DemandModel).filter_by(id=str(id)).first()
|
|
15
|
+
if not model:
|
|
16
|
+
raise DemandNotFoundException(id)
|
|
17
|
+
return Demand(
|
|
18
|
+
id=DemandId(model.id),
|
|
19
|
+
description=model.description,
|
|
20
|
+
status=DemandStatus(model.status),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
def save(self, demand: Demand) -> None:
|
|
24
|
+
model = DemandModel(
|
|
25
|
+
id=str(demand.id),
|
|
26
|
+
description=demand.description,
|
|
27
|
+
status=demand.status.value,
|
|
28
|
+
)
|
|
29
|
+
self._session.merge(model)
|
|
30
|
+
self._session.commit()
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**After** (120 tokens):
|
|
34
|
+
```python
|
|
35
|
+
class DemandRepository:
|
|
36
|
+
def __init__(self, session: Session): ...
|
|
37
|
+
def find_by_id(self, id: DemandId) -> Demand: ... # raises DemandNotFoundException
|
|
38
|
+
def save(self, demand: Demand) -> None: ... # merges and commits
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Technique 2: Summary with Key Facts
|
|
42
|
+
|
|
43
|
+
For documentation, extract bullet points instead of full text.
|
|
44
|
+
|
|
45
|
+
**Before**: 500-word architecture document.
|
|
46
|
+
**After**: 5 bullet points with the critical decisions.
|
|
47
|
+
|
|
48
|
+
## Technique 3: History Trimming
|
|
49
|
+
|
|
50
|
+
Keep only the last N messages, or summarize older messages.
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
def trim_history(messages: list, max_messages: int = 10) -> list:
|
|
54
|
+
if len(messages) <= max_messages:
|
|
55
|
+
return messages
|
|
56
|
+
# Keep system message + last N messages
|
|
57
|
+
return [messages[0]] + messages[-max_messages:]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Technique 4: Selective File Loading
|
|
61
|
+
|
|
62
|
+
Load only the files the agent will interact with, not the entire directory.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
def select_files(task_description: str, file_index: dict) -> list[str]:
|
|
66
|
+
# Use embedding similarity to find relevant files
|
|
67
|
+
relevant = retriever.invoke(task_description)
|
|
68
|
+
return [f.metadata["path"] for f in relevant[:5]]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## When NOT to Compress
|
|
72
|
+
|
|
73
|
+
- Code the agent is about to modify (needs full context)
|
|
74
|
+
- Error messages and stack traces (details matter)
|
|
75
|
+
- Test files being debugged
|
|
76
|
+
- Configuration files being updated
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Context Budget Calculator Reference
|
|
2
|
+
|
|
3
|
+
## Budget Formula
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
total_available = model_context_window
|
|
7
|
+
reasoning_space = total_available * 0.45 # NEVER reduce below 40%
|
|
8
|
+
usable_context = total_available - reasoning_space
|
|
9
|
+
|
|
10
|
+
system_prompt_budget = usable_context * 0.09 # ~5% of total
|
|
11
|
+
skills_budget = usable_context * 0.18 # ~10% of total
|
|
12
|
+
retrieved_context_budget = usable_context * 0.45 # ~25% of total
|
|
13
|
+
history_budget = usable_context * 0.27 # ~15% of total
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Budget by Model
|
|
17
|
+
|
|
18
|
+
| Model | Context Window | System + Skills | Retrieved | History | Reasoning |
|
|
19
|
+
|---|---|---|---|---|---|
|
|
20
|
+
| Claude Sonnet (200k) | 200,000 | 30,000 | 50,000 | 30,000 | 90,000 |
|
|
21
|
+
| Claude Haiku (200k) | 200,000 | 30,000 | 50,000 | 30,000 | 90,000 |
|
|
22
|
+
| GPT-4 Turbo (128k) | 128,000 | 19,200 | 32,000 | 19,200 | 57,600 |
|
|
23
|
+
| GPT-4o (128k) | 128,000 | 19,200 | 32,000 | 19,200 | 57,600 |
|
|
24
|
+
|
|
25
|
+
## Warning Signs of Context Bloat
|
|
26
|
+
|
|
27
|
+
1. Agent starts ignoring rules mentioned in the system prompt -> system prompt too far back
|
|
28
|
+
2. Agent repeats itself -> conversation history too long, compress or trim
|
|
29
|
+
3. Agent hallucinates code structure -> retrieved context is stale or missing
|
|
30
|
+
4. Agent is slow and expensive -> too much context being sent per invocation
|
|
31
|
+
|
|
32
|
+
## Quick Check
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import tiktoken
|
|
36
|
+
|
|
37
|
+
def check_budget(system_prompt, skills, retrieved, history, model_limit=200000):
|
|
38
|
+
enc = tiktoken.encoding_for_model("gpt-4") # approximate
|
|
39
|
+
total = sum(len(enc.encode(t)) for t in [system_prompt, skills, retrieved, history])
|
|
40
|
+
usage_pct = total / model_limit * 100
|
|
41
|
+
reasoning_pct = (model_limit - total) / model_limit * 100
|
|
42
|
+
print(f"Context usage: {usage_pct:.1f}% | Reasoning space: {reasoning_pct:.1f}%")
|
|
43
|
+
if reasoning_pct < 40:
|
|
44
|
+
print("WARNING: Reasoning space below 40%. Reduce context.")
|
|
45
|
+
```
|