maestro-bundle 1.3.0 → 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/README.md +125 -37
- package/package.json +1 -1
- package/src/cli.mjs +7 -4
- 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,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
|
+
```
|
|
@@ -1,19 +1,55 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: database-modeling
|
|
3
|
-
description:
|
|
3
|
+
description: Model PostgreSQL databases with migrations, indexes, and pgvector. Use when creating tables, defining schemas, writing migrations, or optimizing queries.
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
author: Maestro
|
|
4
6
|
---
|
|
5
7
|
|
|
6
|
-
#
|
|
8
|
+
# Database Modeling
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
Design PostgreSQL schemas with proper conventions, Alembic migrations, performant indexes, pgvector for semantic search, and full-text search capabilities.
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
12
|
+
## When to Use
|
|
13
|
+
- Creating new database tables or modifying existing schemas
|
|
14
|
+
- Writing Alembic migrations
|
|
15
|
+
- Adding indexes to optimize slow queries
|
|
16
|
+
- Setting up pgvector for embedding storage
|
|
17
|
+
- Configuring full-text search
|
|
18
|
+
- Reviewing schema design for anti-patterns
|
|
19
|
+
|
|
20
|
+
## Available Operations
|
|
21
|
+
1. Design tables following naming conventions
|
|
22
|
+
2. Create Alembic migrations (upgrade and downgrade)
|
|
23
|
+
3. Add indexes for query optimization
|
|
24
|
+
4. Set up pgvector for semantic search
|
|
25
|
+
5. Configure full-text search with tsvector
|
|
26
|
+
6. Analyze and optimize slow queries with EXPLAIN
|
|
27
|
+
|
|
28
|
+
## Multi-Step Workflow
|
|
29
|
+
|
|
30
|
+
### Step 1: Design the Table Schema
|
|
31
|
+
|
|
32
|
+
Follow these naming conventions strictly:
|
|
33
|
+
|
|
34
|
+
- Table names: `snake_case`, plural (`demands`, `tasks`, `agents`)
|
|
35
|
+
- Primary keys: `id UUID DEFAULT gen_random_uuid()`
|
|
36
|
+
- Foreign keys: `<singular_table>_id` (e.g., `demand_id`)
|
|
37
|
+
- Timestamps: `created_at`, `updated_at` with default `NOW()`
|
|
14
38
|
- Soft delete: `deleted_at TIMESTAMP NULL`
|
|
15
39
|
|
|
16
|
-
|
|
40
|
+
### Step 2: Create the Alembic Migration
|
|
41
|
+
|
|
42
|
+
Generate and write the migration file.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
# Generate a new migration
|
|
46
|
+
alembic revision --autogenerate -m "create_demands_table"
|
|
47
|
+
|
|
48
|
+
# Or create manually
|
|
49
|
+
alembic revision -m "create_demands_table"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Write the migration:
|
|
17
53
|
|
|
18
54
|
```python
|
|
19
55
|
# alembic/versions/001_create_demands.py
|
|
@@ -32,28 +68,119 @@ def downgrade():
|
|
|
32
68
|
op.drop_table('demands')
|
|
33
69
|
```
|
|
34
70
|
|
|
35
|
-
|
|
71
|
+
Run the migration:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Apply migration
|
|
75
|
+
alembic upgrade head
|
|
76
|
+
|
|
77
|
+
# Verify the table was created
|
|
78
|
+
psql $DATABASE_URL -c "\d demands"
|
|
79
|
+
|
|
80
|
+
# Check migration history
|
|
81
|
+
alembic history
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Step 3: Add Performance Indexes
|
|
85
|
+
|
|
86
|
+
Index columns used in WHERE clauses, JOINs, and ORDER BY.
|
|
36
87
|
|
|
37
88
|
```sql
|
|
38
|
-
--
|
|
89
|
+
-- Partial index for active records (most queries filter by status)
|
|
39
90
|
CREATE INDEX idx_tasks_status ON tasks(status) WHERE status != 'completed';
|
|
91
|
+
|
|
92
|
+
-- Foreign key index for JOIN performance
|
|
40
93
|
CREATE INDEX idx_tasks_demand ON tasks(demand_id);
|
|
94
|
+
|
|
95
|
+
-- Composite index for common query patterns
|
|
41
96
|
CREATE INDEX idx_tracking_events_demand_agent ON tracking_events(demand_id, agent_id, created_at DESC);
|
|
97
|
+
```
|
|
42
98
|
|
|
43
|
-
|
|
99
|
+
### Step 4: Set Up pgvector for Semantic Search
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
# Enable pgvector extension
|
|
103
|
+
psql $DATABASE_URL -c "CREATE EXTENSION IF NOT EXISTS vector;"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```sql
|
|
107
|
+
-- Create embeddings table
|
|
108
|
+
CREATE TABLE bundle_embeddings (
|
|
109
|
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
110
|
+
content TEXT NOT NULL,
|
|
111
|
+
embedding vector(1536) NOT NULL,
|
|
112
|
+
metadata JSONB,
|
|
113
|
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
-- HNSW index for fast cosine similarity search
|
|
44
117
|
CREATE INDEX idx_embeddings_vector ON bundle_embeddings
|
|
45
118
|
USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 200);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Step 5: Configure Full-Text Search
|
|
46
122
|
|
|
47
|
-
|
|
123
|
+
```sql
|
|
124
|
+
-- Add generated tsvector column
|
|
48
125
|
ALTER TABLE bundles ADD COLUMN search_vector tsvector
|
|
49
|
-
GENERATED ALWAYS AS (to_tsvector('
|
|
126
|
+
GENERATED ALWAYS AS (to_tsvector('english', name || ' ' || description)) STORED;
|
|
127
|
+
|
|
128
|
+
-- GIN index for full-text search
|
|
50
129
|
CREATE INDEX idx_bundles_search ON bundles USING GIN(search_vector);
|
|
51
130
|
```
|
|
52
131
|
|
|
53
|
-
|
|
132
|
+
### Step 6: Analyze and Optimize Queries
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
# Run EXPLAIN ANALYZE on slow queries
|
|
136
|
+
psql $DATABASE_URL -c "EXPLAIN ANALYZE SELECT * FROM tasks WHERE demand_id = 'abc-123' AND status = 'pending';"
|
|
137
|
+
|
|
138
|
+
# Check for missing indexes
|
|
139
|
+
psql $DATABASE_URL -c "SELECT schemaname, tablename, indexname FROM pg_indexes WHERE tablename = 'tasks';"
|
|
140
|
+
|
|
141
|
+
# Check table sizes
|
|
142
|
+
psql $DATABASE_URL -c "SELECT pg_size_pretty(pg_total_relation_size('tasks'));"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Resources
|
|
146
|
+
- `references/naming-conventions.md` - Complete naming conventions for PostgreSQL schemas
|
|
147
|
+
- `references/index-strategies.md` - When and how to create indexes for different query patterns
|
|
148
|
+
|
|
149
|
+
## Examples
|
|
150
|
+
|
|
151
|
+
### Example 1: Create a New Feature Table
|
|
152
|
+
User asks: "Create a tasks table with status tracking and assignment to agents."
|
|
153
|
+
Response approach:
|
|
154
|
+
1. Design columns: id (UUID), title, description, status, demand_id (FK), agent_id (FK), timestamps
|
|
155
|
+
2. Write Alembic migration with proper types and defaults
|
|
156
|
+
3. Add foreign key constraints with ON DELETE behavior
|
|
157
|
+
4. Create indexes on demand_id, agent_id, and status
|
|
158
|
+
5. Run migration: `alembic upgrade head`
|
|
159
|
+
6. Verify: `psql $DATABASE_URL -c "\d tasks"`
|
|
160
|
+
|
|
161
|
+
### Example 2: Optimize a Slow Query
|
|
162
|
+
User asks: "The demands list endpoint is slow when filtering by status."
|
|
163
|
+
Response approach:
|
|
164
|
+
1. Run `EXPLAIN ANALYZE` on the slow query
|
|
165
|
+
2. Check if an index exists on the status column
|
|
166
|
+
3. Create a partial index: `CREATE INDEX idx_demands_status ON demands(status) WHERE deleted_at IS NULL;`
|
|
167
|
+
4. Re-run `EXPLAIN ANALYZE` and compare execution times
|
|
168
|
+
5. Verify the index is being used (look for "Index Scan" in the plan)
|
|
169
|
+
|
|
170
|
+
### Example 3: Add Soft Delete
|
|
171
|
+
User asks: "Implement soft delete for the demands table."
|
|
172
|
+
Response approach:
|
|
173
|
+
1. Write migration to add `deleted_at TIMESTAMP NULL` column
|
|
174
|
+
2. Update partial indexes to exclude soft-deleted rows: `WHERE deleted_at IS NULL`
|
|
175
|
+
3. Update repository queries to filter `WHERE deleted_at IS NULL` by default
|
|
176
|
+
4. Create a `restore` method that sets `deleted_at = NULL`
|
|
177
|
+
5. Run: `alembic upgrade head`
|
|
54
178
|
|
|
55
|
-
|
|
56
|
-
-
|
|
57
|
-
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
179
|
+
## Notes
|
|
180
|
+
- Never alter schema without a migration -- even in development
|
|
181
|
+
- Always write both `upgrade()` and `downgrade()` functions
|
|
182
|
+
- Do not create indexes on every column (increases write cost)
|
|
183
|
+
- Do not use `SELECT *` in production code -- select only needed columns
|
|
184
|
+
- Always run `EXPLAIN ANALYZE` before and after adding indexes to verify improvement
|
|
185
|
+
- Use `CASCADE DELETE` with extreme caution -- prefer soft delete or application-level cleanup
|
|
186
|
+
- Use `TIMESTAMP WITH TIME ZONE` for all timestamp columns
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Index Strategies Reference
|
|
2
|
+
|
|
3
|
+
## When to Create Indexes
|
|
4
|
+
|
|
5
|
+
| Query Pattern | Index Type | Example |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| `WHERE column = value` | B-tree (default) | `CREATE INDEX idx_tasks_status ON tasks(status)` |
|
|
8
|
+
| `WHERE col1 = X AND col2 = Y` | Composite | `CREATE INDEX idx_tasks_demand_status ON tasks(demand_id, status)` |
|
|
9
|
+
| `WHERE status != 'completed'` | Partial | `CREATE INDEX idx_tasks_active ON tasks(status) WHERE status != 'completed'` |
|
|
10
|
+
| `WHERE deleted_at IS NULL` | Partial | `CREATE INDEX idx_demands_live ON demands(id) WHERE deleted_at IS NULL` |
|
|
11
|
+
| Full-text search | GIN on tsvector | `CREATE INDEX idx_search ON docs USING GIN(search_vector)` |
|
|
12
|
+
| Vector similarity | HNSW | `CREATE INDEX idx_vec ON embeddings USING hnsw(embedding vector_cosine_ops)` |
|
|
13
|
+
| JSONB queries | GIN | `CREATE INDEX idx_meta ON docs USING GIN(metadata)` |
|
|
14
|
+
|
|
15
|
+
## When NOT to Create Indexes
|
|
16
|
+
|
|
17
|
+
- Tables with fewer than 1,000 rows (sequential scan is faster)
|
|
18
|
+
- Columns with very low cardinality (e.g., boolean with 50/50 distribution)
|
|
19
|
+
- Columns that are rarely used in WHERE, JOIN, or ORDER BY
|
|
20
|
+
- Write-heavy tables where index maintenance cost exceeds read benefit
|
|
21
|
+
|
|
22
|
+
## Composite Index Column Order
|
|
23
|
+
|
|
24
|
+
Put the most selective column first (the one that filters out the most rows).
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
-- Good: demand_id is more selective than status
|
|
28
|
+
CREATE INDEX idx_tasks_demand_status ON tasks(demand_id, status);
|
|
29
|
+
|
|
30
|
+
-- Bad: status has few distinct values, less selective
|
|
31
|
+
CREATE INDEX idx_tasks_status_demand ON tasks(status, demand_id);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Monitoring Index Usage
|
|
35
|
+
|
|
36
|
+
```sql
|
|
37
|
+
-- Find unused indexes
|
|
38
|
+
SELECT indexrelname, idx_scan, idx_tup_read
|
|
39
|
+
FROM pg_stat_user_indexes
|
|
40
|
+
WHERE idx_scan = 0
|
|
41
|
+
ORDER BY pg_relation_size(indexrelid) DESC;
|
|
42
|
+
|
|
43
|
+
-- Find missing indexes (sequential scans on large tables)
|
|
44
|
+
SELECT relname, seq_scan, idx_scan, n_live_tup
|
|
45
|
+
FROM pg_stat_user_tables
|
|
46
|
+
WHERE seq_scan > idx_scan AND n_live_tup > 10000
|
|
47
|
+
ORDER BY seq_scan - idx_scan DESC;
|
|
48
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Naming Conventions Reference
|
|
2
|
+
|
|
3
|
+
## Tables
|
|
4
|
+
- `snake_case`, plural: `demands`, `tasks`, `tracking_events`
|
|
5
|
+
- Junction tables: `<table1>_<table2>` alphabetically: `agents_tasks`
|
|
6
|
+
|
|
7
|
+
## Columns
|
|
8
|
+
- Primary key: `id` (always UUID with `gen_random_uuid()`)
|
|
9
|
+
- Foreign key: `<singular_table>_id`: `demand_id`, `agent_id`
|
|
10
|
+
- Boolean: `is_<adjective>`: `is_active`, `is_verified`
|
|
11
|
+
- Timestamps: `created_at`, `updated_at`, `deleted_at`
|
|
12
|
+
- Status: `status VARCHAR(20)` with constrained values
|
|
13
|
+
|
|
14
|
+
## Indexes
|
|
15
|
+
- Format: `idx_<table>_<columns>`: `idx_tasks_status`, `idx_tasks_demand_id`
|
|
16
|
+
- Unique: `uq_<table>_<columns>`: `uq_users_email`
|
|
17
|
+
- Partial: suffix with purpose: `idx_tasks_status_active`
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
- Primary key: `pk_<table>`: `pk_demands`
|
|
21
|
+
- Foreign key: `fk_<table>_<referenced>`: `fk_tasks_demands`
|
|
22
|
+
- Unique: `uq_<table>_<columns>`: `uq_users_email`
|
|
23
|
+
- Check: `ck_<table>_<rule>`: `ck_demands_status_valid`
|
|
24
|
+
|
|
25
|
+
## Migrations
|
|
26
|
+
- Format: `NNN_<action>_<table>.py`: `001_create_demands.py`, `002_add_tasks_status_index.py`
|
|
27
|
+
- Actions: `create`, `add`, `alter`, `drop`, `rename`
|