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.
Files changed (118) hide show
  1. package/README.md +125 -37
  2. package/package.json +1 -1
  3. package/src/cli.mjs +7 -4
  4. package/templates/bundle-ai-agents/skills/agent-orchestration/SKILL.md +107 -41
  5. package/templates/bundle-ai-agents/skills/agent-orchestration/references/graph-patterns.md +50 -0
  6. package/templates/bundle-ai-agents/skills/agent-orchestration/references/routing-strategies.md +47 -0
  7. package/templates/bundle-ai-agents/skills/api-design/SKILL.md +125 -16
  8. package/templates/bundle-ai-agents/skills/api-design/references/pydantic-patterns.md +72 -0
  9. package/templates/bundle-ai-agents/skills/api-design/references/rest-conventions.md +51 -0
  10. package/templates/bundle-ai-agents/skills/clean-architecture/SKILL.md +113 -21
  11. package/templates/bundle-ai-agents/skills/clean-architecture/references/dependency-injection.md +60 -0
  12. package/templates/bundle-ai-agents/skills/clean-architecture/references/layer-rules.md +56 -0
  13. package/templates/bundle-ai-agents/skills/context-engineering/SKILL.md +104 -36
  14. package/templates/bundle-ai-agents/skills/context-engineering/references/compression-techniques.md +76 -0
  15. package/templates/bundle-ai-agents/skills/context-engineering/references/context-budget-calculator.md +45 -0
  16. package/templates/bundle-ai-agents/skills/database-modeling/SKILL.md +146 -19
  17. package/templates/bundle-ai-agents/skills/database-modeling/references/index-strategies.md +48 -0
  18. package/templates/bundle-ai-agents/skills/database-modeling/references/naming-conventions.md +27 -0
  19. package/templates/bundle-ai-agents/skills/docker-containerization/SKILL.md +124 -15
  20. package/templates/bundle-ai-agents/skills/docker-containerization/references/compose-patterns.md +97 -0
  21. package/templates/bundle-ai-agents/skills/docker-containerization/references/dockerfile-checklist.md +37 -0
  22. package/templates/bundle-ai-agents/skills/eval-testing/SKILL.md +113 -25
  23. package/templates/bundle-ai-agents/skills/eval-testing/references/eval-types.md +52 -0
  24. package/templates/bundle-ai-agents/skills/eval-testing/references/golden-dataset-template.md +59 -0
  25. package/templates/bundle-ai-agents/skills/memory-management/SKILL.md +112 -28
  26. package/templates/bundle-ai-agents/skills/memory-management/references/memory-tiers.md +41 -0
  27. package/templates/bundle-ai-agents/skills/memory-management/references/namespace-conventions.md +41 -0
  28. package/templates/bundle-ai-agents/skills/prompt-engineering/SKILL.md +139 -47
  29. package/templates/bundle-ai-agents/skills/prompt-engineering/references/anti-patterns.md +59 -0
  30. package/templates/bundle-ai-agents/skills/prompt-engineering/references/prompt-templates.md +75 -0
  31. package/templates/bundle-ai-agents/skills/rag-pipeline/SKILL.md +104 -27
  32. package/templates/bundle-ai-agents/skills/rag-pipeline/references/chunking-strategies.md +27 -0
  33. package/templates/bundle-ai-agents/skills/rag-pipeline/references/embedding-models.md +31 -0
  34. package/templates/bundle-ai-agents/skills/rag-pipeline/references/rag-evaluation.md +39 -0
  35. package/templates/bundle-ai-agents/skills/testing-strategy/SKILL.md +127 -18
  36. package/templates/bundle-ai-agents/skills/testing-strategy/references/fixture-patterns.md +81 -0
  37. package/templates/bundle-ai-agents/skills/testing-strategy/references/naming-conventions.md +69 -0
  38. package/templates/bundle-base/skills/branch-strategy/SKILL.md +134 -21
  39. package/templates/bundle-base/skills/branch-strategy/references/branch-rules.md +40 -0
  40. package/templates/bundle-base/skills/code-review/SKILL.md +123 -38
  41. package/templates/bundle-base/skills/code-review/references/review-checklist.md +45 -0
  42. package/templates/bundle-base/skills/commit-pattern/SKILL.md +98 -39
  43. package/templates/bundle-base/skills/commit-pattern/references/conventional-commits.md +40 -0
  44. package/templates/bundle-data-pipeline/skills/data-preprocessing/SKILL.md +110 -19
  45. package/templates/bundle-data-pipeline/skills/data-preprocessing/references/pandas-cheatsheet.md +63 -0
  46. package/templates/bundle-data-pipeline/skills/data-preprocessing/references/pandera-schemas.md +44 -0
  47. package/templates/bundle-data-pipeline/skills/docker-containerization/SKILL.md +132 -16
  48. package/templates/bundle-data-pipeline/skills/docker-containerization/references/compose-patterns.md +82 -0
  49. package/templates/bundle-data-pipeline/skills/docker-containerization/references/dockerfile-best-practices.md +57 -0
  50. package/templates/bundle-data-pipeline/skills/feature-engineering/SKILL.md +143 -45
  51. package/templates/bundle-data-pipeline/skills/feature-engineering/references/encoding-guide.md +41 -0
  52. package/templates/bundle-data-pipeline/skills/feature-engineering/references/scaling-guide.md +38 -0
  53. package/templates/bundle-data-pipeline/skills/mlops-pipeline/SKILL.md +156 -37
  54. package/templates/bundle-data-pipeline/skills/mlops-pipeline/references/mlflow-commands.md +69 -0
  55. package/templates/bundle-data-pipeline/skills/model-training/SKILL.md +152 -33
  56. package/templates/bundle-data-pipeline/skills/model-training/references/evaluation-metrics.md +52 -0
  57. package/templates/bundle-data-pipeline/skills/model-training/references/model-selection-guide.md +41 -0
  58. package/templates/bundle-data-pipeline/skills/rag-pipeline/SKILL.md +127 -39
  59. package/templates/bundle-data-pipeline/skills/rag-pipeline/references/chunking-strategies.md +51 -0
  60. package/templates/bundle-data-pipeline/skills/rag-pipeline/references/embedding-models.md +49 -0
  61. package/templates/bundle-frontend-spa/skills/authentication/SKILL.md +196 -13
  62. package/templates/bundle-frontend-spa/skills/authentication/references/jwt-security.md +41 -0
  63. package/templates/bundle-frontend-spa/skills/component-design/SKILL.md +191 -41
  64. package/templates/bundle-frontend-spa/skills/component-design/references/accessibility-checklist.md +41 -0
  65. package/templates/bundle-frontend-spa/skills/component-design/references/tailwind-patterns.md +65 -0
  66. package/templates/bundle-frontend-spa/skills/e2e-testing/SKILL.md +241 -79
  67. package/templates/bundle-frontend-spa/skills/e2e-testing/references/playwright-selectors.md +66 -0
  68. package/templates/bundle-frontend-spa/skills/e2e-testing/references/test-patterns.md +82 -0
  69. package/templates/bundle-frontend-spa/skills/integration-api/SKILL.md +221 -31
  70. package/templates/bundle-frontend-spa/skills/integration-api/references/api-patterns.md +81 -0
  71. package/templates/bundle-frontend-spa/skills/react-patterns/SKILL.md +195 -70
  72. package/templates/bundle-frontend-spa/skills/react-patterns/references/component-checklist.md +22 -0
  73. package/templates/bundle-frontend-spa/skills/react-patterns/references/hook-patterns.md +63 -0
  74. package/templates/bundle-frontend-spa/skills/responsive-layout/SKILL.md +162 -22
  75. package/templates/bundle-frontend-spa/skills/responsive-layout/references/breakpoint-guide.md +63 -0
  76. package/templates/bundle-frontend-spa/skills/state-management/SKILL.md +158 -30
  77. package/templates/bundle-frontend-spa/skills/state-management/references/react-query-config.md +64 -0
  78. package/templates/bundle-frontend-spa/skills/state-management/references/state-patterns.md +78 -0
  79. package/templates/bundle-jhipster-microservices/skills/ci-cd-pipeline/SKILL.md +135 -45
  80. package/templates/bundle-jhipster-microservices/skills/ci-cd-pipeline/references/gitlab-ci-templates.md +93 -0
  81. package/templates/bundle-jhipster-microservices/skills/clean-architecture/SKILL.md +87 -21
  82. package/templates/bundle-jhipster-microservices/skills/clean-architecture/references/layer-rules.md +78 -0
  83. package/templates/bundle-jhipster-microservices/skills/ddd-tactical/SKILL.md +94 -25
  84. package/templates/bundle-jhipster-microservices/skills/ddd-tactical/references/ddd-patterns.md +48 -0
  85. package/templates/bundle-jhipster-microservices/skills/jhipster-angular/SKILL.md +63 -21
  86. package/templates/bundle-jhipster-microservices/skills/jhipster-angular/references/angular-microservices.md +40 -0
  87. package/templates/bundle-jhipster-microservices/skills/jhipster-angular/references/angular-structure.md +59 -0
  88. package/templates/bundle-jhipster-microservices/skills/jhipster-docker-k8s/SKILL.md +125 -91
  89. package/templates/bundle-jhipster-microservices/skills/jhipster-docker-k8s/references/docker-k8s-commands.md +68 -0
  90. package/templates/bundle-jhipster-microservices/skills/jhipster-entities/SKILL.md +72 -20
  91. package/templates/bundle-jhipster-microservices/skills/jhipster-entities/references/cross-service-entities.md +36 -0
  92. package/templates/bundle-jhipster-microservices/skills/jhipster-entities/references/jdl-types.md +56 -0
  93. package/templates/bundle-jhipster-microservices/skills/jhipster-gateway/SKILL.md +80 -8
  94. package/templates/bundle-jhipster-microservices/skills/jhipster-gateway/references/gateway-config.md +43 -0
  95. package/templates/bundle-jhipster-microservices/skills/jhipster-kafka/SKILL.md +115 -22
  96. package/templates/bundle-jhipster-microservices/skills/jhipster-kafka/references/kafka-events.md +39 -0
  97. package/templates/bundle-jhipster-microservices/skills/jhipster-registry/SKILL.md +92 -23
  98. package/templates/bundle-jhipster-microservices/skills/jhipster-registry/references/consul-config.md +61 -0
  99. package/templates/bundle-jhipster-microservices/skills/jhipster-service/SKILL.md +81 -18
  100. package/templates/bundle-jhipster-microservices/skills/jhipster-service/references/service-patterns.md +40 -0
  101. package/templates/bundle-jhipster-microservices/skills/testing-strategy/SKILL.md +101 -20
  102. package/templates/bundle-jhipster-microservices/skills/testing-strategy/references/test-naming.md +55 -0
  103. package/templates/bundle-jhipster-monorepo/skills/clean-architecture/SKILL.md +87 -21
  104. package/templates/bundle-jhipster-monorepo/skills/clean-architecture/references/layer-rules.md +78 -0
  105. package/templates/bundle-jhipster-monorepo/skills/ddd-tactical/SKILL.md +94 -25
  106. package/templates/bundle-jhipster-monorepo/skills/ddd-tactical/references/ddd-patterns.md +48 -0
  107. package/templates/bundle-jhipster-monorepo/skills/jhipster-angular/SKILL.md +99 -52
  108. package/templates/bundle-jhipster-monorepo/skills/jhipster-angular/references/angular-structure.md +59 -0
  109. package/templates/bundle-jhipster-monorepo/skills/jhipster-entities/SKILL.md +89 -36
  110. package/templates/bundle-jhipster-monorepo/skills/jhipster-entities/references/jdl-types.md +56 -0
  111. package/templates/bundle-jhipster-monorepo/skills/jhipster-liquibase/SKILL.md +123 -23
  112. package/templates/bundle-jhipster-monorepo/skills/jhipster-liquibase/references/liquibase-operations.md +95 -0
  113. package/templates/bundle-jhipster-monorepo/skills/jhipster-security/SKILL.md +106 -19
  114. package/templates/bundle-jhipster-monorepo/skills/jhipster-security/references/security-checklist.md +47 -0
  115. package/templates/bundle-jhipster-monorepo/skills/jhipster-spring/SKILL.md +84 -16
  116. package/templates/bundle-jhipster-monorepo/skills/jhipster-spring/references/spring-layers.md +41 -0
  117. package/templates/bundle-jhipster-monorepo/skills/testing-strategy/SKILL.md +101 -20
  118. 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: Implementar as 4 estratégias de context engineering (Write, Select, Compress, Isolate) para agentes. Use quando precisar gerenciar a janela de contexto, otimizar o que o agente recebe, ou reduzir custos de tokens.
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
- As 4 estratégias conforme Anthropic:
10
+ Apply the four context engineering strategies -- Write, Select, Compress, Isolate -- to maximize agent effectiveness while minimizing token costs.
9
11
 
10
- ## 1. Write Context — Memória Persistente
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
- O que o agente "sabe" antes de começar.
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 Padrões do projeto, arquitetura, decisões
16
- agents.md Comportamento específico do agente
17
- skills/SKILL.md Capacidades on-demand
18
- memory/ Aprendizados de execuções anteriores
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
- **Regra:** CLAUDE.md deve ter no máximo 2000 tokens. Se precisar de mais, mover para skills que são carregadas on-demand.
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
- ## 2. Select Context Retrieval Inteligente
47
+ ### Step 2: Select Context -- Retrieval for the Current Task
24
48
 
25
- Injetar apenas o contexto relevante para a task atual.
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
- # Buscar skills relevantes para a task
53
+ # Retrieve skills relevant to the task
30
54
  relevant_skills = retriever.invoke(task.description)
31
55
 
32
- # Buscar código relacionado no repo
56
+ # Search for related code in the repository
33
57
  related_code = code_search(task.description, worktree_path)
34
58
 
35
- # Buscar decisões anteriores similares
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
- **Regra:** Nunca injetar mais de 30% da janela de contexto com contexto selecionado. Deixar espaço para o agente raciocinar.
65
+ **Rule**: Never inject more than 30% of the context window with selected context. Leave space for the agent to reason.
42
66
 
43
- ## 3. Compress Context Resumo Eficiente
67
+ ### Step 3: Compress Context -- Reduce Without Losing Essentials
44
68
 
45
- Reduzir informação sem perder o essencial.
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
- Resuma este código mantendo:
54
- - Assinaturas de funções/classes
55
- - Tipos de entrada/saída
56
- - Lógica principal (sem detalhes de implementação)
57
- - Imports relevantes
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
- Código:
83
+ Code:
60
84
  {code}
61
85
  """)
62
86
  return summary.content
63
87
  ```
64
88
 
65
- **Regra:** Comprimir apenas quando necessário. Código que o agente vai modificar deve estar completo, não comprimido.
89
+ **Rule**: Never compress code the agent is about to modify. That code must remain complete and uncompressed.
66
90
 
67
- ## 4. Isolate Context Escopo por Agente
91
+ ### Step 4: Isolate Context -- Scope Per Agent
68
92
 
69
- Cada agente apenas o que precisa.
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
- **Regra:** Agentes nunca compartilham janela de contexto. Comunicação via mensagens estruturadas, não via contexto compartilhado.
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
- ## Budget de contexto
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
- Para um modelo com 200k tokens:
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
- | Componente | % | Tokens |
93
- |---|---|---|
94
- | System prompt + CLAUDE.md | 5% | 10k |
95
- | Skills carregadas | 10% | 20k |
96
- | Código relevante (Select) | 25% | 50k |
97
- | Histórico de conversa | 15% | 30k |
98
- | **Espaço para raciocínio** | **45%** | **90k** |
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
@@ -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: Modelar banco de dados PostgreSQL com migrations, índices, e pgvector. Use quando for criar tabelas, definir schema, criar migrations, ou otimizar queries.
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
- # Modelagem de Banco — PostgreSQL
8
+ # Database Modeling
7
9
 
8
- ## Convenções
10
+ Design PostgreSQL schemas with proper conventions, Alembic migrations, performant indexes, pgvector for semantic search, and full-text search capabilities.
9
11
 
10
- - Nomes de tabelas: `snake_case`, plural (`demands`, `tasks`, `agents`)
11
- - PKs: `id UUID DEFAULT gen_random_uuid()`
12
- - FKs: `<tabela_singular>_id` (ex: `demand_id`)
13
- - Timestamps: `created_at`, `updated_at` com default `NOW()`
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
- ## Migration com Alembic
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
- ## Índices
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
- -- Queries frequentes devem ter índice
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
- -- pgvector para busca semântica
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
- -- Full-text search
123
+ ```sql
124
+ -- Add generated tsvector column
48
125
  ALTER TABLE bundles ADD COLUMN search_vector tsvector
49
- GENERATED ALWAYS AS (to_tsvector('portuguese', name || ' ' || description)) STORED;
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
- ## Anti-patterns a evitar
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
- - Não usar CASCADE DELETE sem pensar nas consequências
56
- - Não criar índice em toda coluna (custo de escrita)
57
- - Não fazer SELECT * em produção
58
- - Não ignorar EXPLAIN ANALYZE para queries lentas
59
- - Não alterar schema sem migration
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`