@ivannikov-pro/ai-context-surgeon 1.0.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/LICENSE +22 -0
- package/README.md +372 -0
- package/bin/catalog.js +153 -0
- package/bin/cli.js +380 -0
- package/bin/installer.js +135 -0
- package/bin/prompts.js +371 -0
- package/checklists/phase-1-analysis.md +58 -0
- package/checklists/phase-2-planning.md +67 -0
- package/checklists/phase-3-restructuring.md +77 -0
- package/checklists/phase-4-documentation.md +111 -0
- package/checklists/phase-5-validation.md +91 -0
- package/examples/before-after/README.md +139 -0
- package/examples/ideal-monorepo/README.md +127 -0
- package/knowledge/agent-context-system/artifacts/guide.md +183 -0
- package/knowledge/agent-context-system/artifacts/knowledge.md +177 -0
- package/knowledge/agent-context-system/artifacts/skills.md +101 -0
- package/knowledge/agent-context-system/artifacts/workflows.md +143 -0
- package/knowledge/agent-context-system/metadata.json +26 -0
- package/knowledge/agent-context-system/timestamps.json +5 -0
- package/knowledge/agent-vulnerabilities/LICENSE +21 -0
- package/knowledge/agent-vulnerabilities/artifacts/stealth_injection.md +110 -0
- package/knowledge/agent-vulnerabilities/artifacts/vulnerabilities.md +232 -0
- package/knowledge/agent-vulnerabilities/metadata.json +14 -0
- package/knowledge/agent-vulnerabilities/timestamps.json +5 -0
- package/knowledge/power-words-dictionary/LICENSE +21 -0
- package/knowledge/power-words-dictionary/artifacts/dictionary.md +231 -0
- package/knowledge/power-words-dictionary/artifacts/prompt_amplifier.py +381 -0
- package/knowledge/power-words-dictionary/metadata.json +14 -0
- package/knowledge/power-words-dictionary/timestamps.json +5 -0
- package/package.json +77 -0
- package/roles/README.md +81 -0
- package/roles/architect.md +203 -0
- package/roles/inspector.md +232 -0
- package/roles/librarian.md +176 -0
- package/roles/scout.md +169 -0
- package/roles/surgeon.md +172 -0
- package/roles/tuner.md +204 -0
- package/skills/annotate-jsdoc/SKILL.md +262 -0
- package/skills/prompt-engineering/LICENSE +21 -0
- package/skills/prompt-engineering/SKILL.md +235 -0
- package/skills/prompt-engineering/scripts/extract_instructions.py +416 -0
- package/skills/prompt-engineering/scripts/prompt_amplifier.py +381 -0
- package/skills/prompt-engineering/scripts/prompt_diff_tracker.py +281 -0
- package/skills/prompt-engineering/scripts/prompt_dna_analyzer.py +692 -0
- package/skills/prompt-engineering/scripts/templates/code_review.md +47 -0
- package/skills/prompt-engineering/scripts/templates/dump_extraction.md +59 -0
- package/skills/prompt-engineering/scripts/templates/multi_agent_orchestration.md +100 -0
- package/skills/prompt-engineering/scripts/templates/prompt_audit.md +106 -0
- package/skills/prompt-engineering/scripts/templates/stealth_injection.md +110 -0
- package/skills/prompt-engineering/scripts/templates/task_automation.md +87 -0
- package/skills/prompt-engineering/workflows/amplify.md +36 -0
- package/skills/prompt-engineering/workflows/audit.md +55 -0
- package/skills/prompt-engineering/workflows/context-dump.md +90 -0
- package/skills/prompt-engineering/workflows/diff.md +44 -0
- package/strategy/bash-guide.md +134 -0
- package/strategy/context-exclusion.md +220 -0
- package/strategy/context-weight-theory.md +49 -0
- package/strategy/file-navigation-header.md +562 -0
- package/strategy/jsdoc-guide.md +596 -0
- package/strategy/monorepo_strategy.md +726 -0
- package/strategy/package-json-guide.md +541 -0
- package/templates/AGENTS.md.template +148 -0
- package/templates/antigravityignore.template +64 -0
- package/templates/cursorrules.template +7 -0
- package/templates/knowledge-item.template +44 -0
- package/templates/package-json-ideal.template +26 -0
- package/templates/package-readme.template +45 -0
- package/templates/publish-meta.template +34 -0
- package/templates/skill.template +50 -0
- package/templates/workflow.template +33 -0
- package/tools/analyze-package-json.sh +213 -0
- package/tools/analyze-structure.sh +101 -0
- package/tools/audit-jsdoc.sh +176 -0
- package/tools/check-fnh-freshness.sh +74 -0
- package/tools/detect-circular-deps.sh +147 -0
- package/tools/detect-god-files.sh +71 -0
- package/tools/enforce-god-files.sh +112 -0
- package/tools/enrich-package-json.js +311 -0
- package/tools/generate-jsdoc-headers.sh +109 -0
- package/tools/generate-source-map.sh +71 -0
- package/tools/lint-imports.sh +123 -0
- package/tools/measure-context-cost.sh +206 -0
- package/tools/scan-fnh.sh +174 -0
- package/tools/shared/config.sh +53 -0
- package/tools/validate-context-hygiene.sh +52 -0
- package/tools/validate-context-weight.sh +100 -0
- package/tools/validate-naming.sh +98 -0
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
<!-- FNH: Strategy — Monorepo management for AI agents (Architectural Guide) | SECTIONS: Context Management, Models, Tools | DEPS: none -->
|
|
2
|
+
|
|
3
|
+
# 🏗️ Strategy for working with AI Agents in a Large Monorepo
|
|
4
|
+
|
|
5
|
+
> Complete guide: architecture, models, techniques, antipatterns | SECTIONS: Context Management, Task Breakdown, Model Selection, Tool Usage | DEPS: none
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
1. [Philosophy: Why Context is Everything](#1-philosophy)
|
|
12
|
+
2. [Monorepo Architecture for AI](#2-monorepo-architecture-for-ai)
|
|
13
|
+
3. [File Navigation: The Agent's Roadmap](#3-file-navigation)
|
|
14
|
+
4. [Models: Which One for What](#4-models)
|
|
15
|
+
5. [AGENTS.md — The Law of the Monorepo](#5-agentsmd)
|
|
16
|
+
6. [Knowledge Items — Project Memory](#6-knowledge-items)
|
|
17
|
+
7. [Skills — Agent Abilities](#7-skills)
|
|
18
|
+
8. [Workflows — Routine Automation](#8-workflows)
|
|
19
|
+
9. [MCP Servers — External Tools](#9-mcp-servers)
|
|
20
|
+
10. [Context Saving Techniques](#10-context-saving-techniques)
|
|
21
|
+
11. [Antipatterns](#11-antipatterns)
|
|
22
|
+
12. [Checklist: Ideal Monorepo State](#12-checklist)
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 1. Philosophy: The AI AGENT FIRST Manifesto
|
|
27
|
+
|
|
28
|
+
> [!IMPORTANT]
|
|
29
|
+
> **We do not optimize for human readability.**
|
|
30
|
+
> Humans ideally will never look at this code. YOU (the AI Agent) write the code, and YOU read the code. Therefore, the codebase must be formatted exclusively to improve **your** "life". Everything — from FNH headers to file structures — exists solely to make parsing, navigating, and reasoning as easy as possible for a machine interpreter.
|
|
31
|
+
|
|
32
|
+
### The Problem with Context
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
Agent context ≈ 128K–200K tokens
|
|
36
|
+
System prompt ≈ 20K–35K tokens (already consumed)
|
|
37
|
+
Tools (MCP) ≈ 10K–20K tokens (already consumed)
|
|
38
|
+
Actual budget for your task ≈ 70K–150K tokens
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> [!CAUTION]
|
|
42
|
+
> Every unnecessary file loaded into context is a penalty to reasoning accuracy.
|
|
43
|
+
> AI does not "forget" — it loses **focus**. The more irrelevant noise there is, the worse the response quality.
|
|
44
|
+
|
|
45
|
+
### Four Pillars of Agentic Architecture
|
|
46
|
+
|
|
47
|
+
| # | Pillar | Essence |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| 1 | **Machine-Oriented** | Format for parsers, not people (e.g., single-line FNHs instead of bloated human comments). |
|
|
50
|
+
| 2 | **Modularity** | The agent must only see the package it is currently working with. |
|
|
51
|
+
| 3 | **Navigability** | The agent must find the needed file in 1–2 steps, without full directory scanning. |
|
|
52
|
+
| 4 | **Self-documenting** | The code and structure must explain themselves without external Notion/Confluence docs. |
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## 2. Monorepo Architecture for AI
|
|
57
|
+
|
|
58
|
+
### Ideal Structure
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
monorepo/
|
|
62
|
+
├── .agents/ # 🧠 Agent's brain
|
|
63
|
+
│ ├── AGENTS.md # Laws — HIGHEST PRIORITY
|
|
64
|
+
│ ├── knowledge/ # Project memory (Knowledge Items)
|
|
65
|
+
│ │ ├── api-conventions/
|
|
66
|
+
│ │ ├── database-schema/
|
|
67
|
+
│ │ └── deployment-rules/
|
|
68
|
+
│ ├── skills/ # Agent skills
|
|
69
|
+
│ │ ├── db-migration/
|
|
70
|
+
│ │ └── api-endpoint/
|
|
71
|
+
│ └── workflows/ # Automation
|
|
72
|
+
│ ├── deploy.md
|
|
73
|
+
│ ├── test.md
|
|
74
|
+
│ └── lint-fix.md
|
|
75
|
+
│
|
|
76
|
+
├── packages/ # 📦 Packages (each is an isolated world)
|
|
77
|
+
│ ├── core/
|
|
78
|
+
│ │ ├── README.md # ← Package roadmap
|
|
79
|
+
│ │ ├── package.json
|
|
80
|
+
│ │ ├── src/
|
|
81
|
+
│ │ │ ├── index.ts # ← Public API
|
|
82
|
+
│ │ │ ├── types.ts # ← Contracts
|
|
83
|
+
│ │ │ └── internal/ # ← Implementation details
|
|
84
|
+
│ │ └── tests/
|
|
85
|
+
│ ├── api/
|
|
86
|
+
│ ├── web/
|
|
87
|
+
│ └── shared/
|
|
88
|
+
│
|
|
89
|
+
├── apps/ # 🚀 Applications
|
|
90
|
+
│ ├── dashboard/
|
|
91
|
+
│ └── mobile-api/
|
|
92
|
+
│
|
|
93
|
+
├── tools/ # 🔧 Infrastructure utilities
|
|
94
|
+
│ ├── scripts/
|
|
95
|
+
│ └── generators/
|
|
96
|
+
│
|
|
97
|
+
├── README.md # Global project map
|
|
98
|
+
├── turbo.json / nx.json # Dependency graph
|
|
99
|
+
└── package.json # Root manifest
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Package Isolation Rule
|
|
103
|
+
|
|
104
|
+
> [!IMPORTANT]
|
|
105
|
+
> Every package in `packages/` must be a **completely autonomous context**.
|
|
106
|
+
> An agent working on `packages/api` should not need to read `packages/web`.
|
|
107
|
+
|
|
108
|
+
How to ensure this:
|
|
109
|
+
|
|
110
|
+
1. **Clear Interfaces** — every package exports only through `index.ts`
|
|
111
|
+
2. **Typed Contracts** — shared types live in `packages/shared/types/`
|
|
112
|
+
3. **README per package** — describes the API, dependencies, and ownership
|
|
113
|
+
4. **Separate `tsconfig.json`** — the package compiles independently
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 3. File Navigation: The Agent's Roadmap
|
|
118
|
+
|
|
119
|
+
> [!TIP]
|
|
120
|
+
> The agent is a blind navigator. It doesn't see the project as a whole, it relies on `list_dir` → `view_file` → `grep_search`.
|
|
121
|
+
> Every such call consumes context. Your job is to reduce the number of steps to a minimum.
|
|
122
|
+
|
|
123
|
+
### Level 1: Root README.md
|
|
124
|
+
|
|
125
|
+
```markdown
|
|
126
|
+
# Project Name
|
|
127
|
+
|
|
128
|
+
## Architecture
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
packages/
|
|
132
|
+
core/ ← Domain logic, entities, use cases
|
|
133
|
+
api/ ← REST API, Express handlers, middleware
|
|
134
|
+
web/ ← React SPA, pages, components
|
|
135
|
+
shared/ ← Shared types, utils, constants
|
|
136
|
+
notifications/ ← Email, push, SMS services
|
|
137
|
+
apps/
|
|
138
|
+
dashboard/ ← Admin panel (Next.js)
|
|
139
|
+
mobile-api/ ← Mobile BFF
|
|
140
|
+
tools/
|
|
141
|
+
scripts/ ← Build, deploy, migration scripts
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Key Decisions
|
|
146
|
+
- Monorepo manager: Turborepo
|
|
147
|
+
- DB: PostgreSQL + Prisma
|
|
148
|
+
- Auth: JWT + refresh tokens
|
|
149
|
+
- API style: REST, versioned (v1, v2)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Level 2: Package README
|
|
153
|
+
|
|
154
|
+
```markdown
|
|
155
|
+
# packages/api
|
|
156
|
+
|
|
157
|
+
REST API server. Express + Zod validation.
|
|
158
|
+
|
|
159
|
+
## Source Structure
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
src/
|
|
163
|
+
index.ts ← Entry point, app setup
|
|
164
|
+
routes/ ← Route definitions
|
|
165
|
+
v1/
|
|
166
|
+
users.ts ← CRUD /api/v1/users
|
|
167
|
+
orders.ts ← CRUD /api/v1/orders
|
|
168
|
+
auth.ts ← Login, register, refresh
|
|
169
|
+
middleware/
|
|
170
|
+
auth.ts ← JWT verification
|
|
171
|
+
validation.ts ← Zod schema middleware
|
|
172
|
+
error-handler.ts ← Global error handler
|
|
173
|
+
services/
|
|
174
|
+
user.service.ts ← Business logic for users
|
|
175
|
+
order.service.ts ← Business logic for orders
|
|
176
|
+
utils/
|
|
177
|
+
logger.ts ← Winston logger setup
|
|
178
|
+
response.ts ← Standardized API responses
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Dependencies
|
|
183
|
+
- `@monorepo/core` — domain entities
|
|
184
|
+
- `@monorepo/shared` — types, constants
|
|
185
|
+
|
|
186
|
+
## Environment
|
|
187
|
+
- PORT=3000
|
|
188
|
+
- DATABASE_URL=postgresql://...
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Level 3: JSDoc Navigation (20+ files in a directory)
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
/** Route: GET/POST /api/v1/users — User CRUD operations */
|
|
195
|
+
import { Router } from "express";
|
|
196
|
+
// ...
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
/** Middleware: JWT verification — validates Bearer token, sets req.user */
|
|
201
|
+
import { verify } from "jsonwebtoken";
|
|
202
|
+
// ...
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Threshold Rule
|
|
206
|
+
|
|
207
|
+
| Files in Directory | Action |
|
|
208
|
+
| --- | --- |
|
|
209
|
+
| ≤ 10 | Semantic names are sufficient (`user.service.ts`) |
|
|
210
|
+
| 10–20 | Add Source Structure in the package README |
|
|
211
|
+
| 20+ | Source Structure + JSDoc headers in every file |
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## 4. Models: Which One for What
|
|
216
|
+
|
|
217
|
+
### Selection Matrix
|
|
218
|
+
|
|
219
|
+
| Task | Model | Mode | Why |
|
|
220
|
+
| --- | --- | --- | --- |
|
|
221
|
+
| **Architectural decision**, refactoring, complex migration | **Claude Opus 4.6** | Planning | Best reasoning, deep analysis, thinking mode |
|
|
222
|
+
| **Code generation**, feature implementation | **Gemini 3.1 Pro (High)** | Planning | Strong code generation, massive context window |
|
|
223
|
+
| **Quick edits**, minor fixes, formatting | **Gemini 3 Flash** | Fast | Minimum latency, cost savings |
|
|
224
|
+
| **Bulk operations**, batch refactoring | **Gemini 3.1 Pro (Low)** | Fast | Balance of speed/quality at scale |
|
|
225
|
+
| **Code review**, PR descriptions, docs | **Claude Sonnet 4.6** | Planning | Good linguistic qualities, cheaper than Opus |
|
|
226
|
+
| **Prompt testing**, A/B comparisons | **GPT-OSS 120B** | Planning | Alternative viewpoint, cross-validation |
|
|
227
|
+
| **Automated pipeline tasks** (CI/Orchestrator) | **Gemini 3.1 Pro (Low)** or **GPT-OSS 120B** | Fast | Predictability, cost efficiency |
|
|
228
|
+
|
|
229
|
+
### The "Model Cascade" Strategy
|
|
230
|
+
|
|
231
|
+
```mermaid
|
|
232
|
+
graph TD
|
|
233
|
+
A[New Task] --> B{Complexity?}
|
|
234
|
+
B -->|Trivial: typo, format, rename| C[Gemini 3 Flash — Fast]
|
|
235
|
+
B -->|Standard: CRUD, endpoint, test| D[Gemini 3.1 Pro High — Fast]
|
|
236
|
+
B -->|Complex: architecture, migration| E[Claude Opus 4.6 — Planning]
|
|
237
|
+
|
|
238
|
+
E --> F[Opus creates plan]
|
|
239
|
+
F --> G[Gemini 3.1 Pro implements plan]
|
|
240
|
+
G --> H[Claude Sonnet reviews result]
|
|
241
|
+
|
|
242
|
+
style C fill:#4ade80
|
|
243
|
+
style D fill:#60a5fa
|
|
244
|
+
style E fill:#f472b6
|
|
245
|
+
style F fill:#f472b6
|
|
246
|
+
style G fill:#60a5fa
|
|
247
|
+
style H fill:#a78bfa
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
> [!TIP]
|
|
251
|
+
> **Golden Rule**: Use Opus for **thinking**, Gemini for **doing**, Flash for **small details**.
|
|
252
|
+
> Don't waste Opus on writing CRUD endpoints. Don't let Flash design architecture.
|
|
253
|
+
|
|
254
|
+
### Model Switching Rule
|
|
255
|
+
|
|
256
|
+
Switch the model **between tasks**, not in the middle of a task. Switching models resets conversation context.
|
|
257
|
+
|
|
258
|
+
If a task requires different models (planning + implementation), use:
|
|
259
|
+
|
|
260
|
+
1. Opus → creates `implementation_plan.md`
|
|
261
|
+
2. Gemini → new conversation, reads plan, implements
|
|
262
|
+
3. Sonnet → new conversation, reviews result
|
|
263
|
+
|
|
264
|
+
---
|
|
265
|
+
|
|
266
|
+
## 5. AGENTS.md — The Law of the Monorepo
|
|
267
|
+
|
|
268
|
+
> [!IMPORTANT]
|
|
269
|
+
> `AGENTS.md` is the **constitution** of the project. Everything written here has the highest priority.
|
|
270
|
+
> The agent is **obligated** to follow these rules, even if its instructions say otherwise.
|
|
271
|
+
|
|
272
|
+
### Structure of an Ideal AGENTS.md
|
|
273
|
+
|
|
274
|
+
```markdown
|
|
275
|
+
# Project Rules
|
|
276
|
+
|
|
277
|
+
## Language & Style
|
|
278
|
+
|
|
279
|
+
- All code comments and commit messages in English
|
|
280
|
+
- Respond to the user in Russian (or their language)
|
|
281
|
+
- Use TypeScript strict mode. No `any`.
|
|
282
|
+
|
|
283
|
+
## Architecture Rules
|
|
284
|
+
|
|
285
|
+
- Every package must have a README.md with Source Structure
|
|
286
|
+
- No cross-package imports except through index.ts
|
|
287
|
+
- Shared types go to packages/shared/types/
|
|
288
|
+
- Services never import from routes (dependency direction: routes → services → repositories)
|
|
289
|
+
|
|
290
|
+
## Code Conventions
|
|
291
|
+
|
|
292
|
+
- Use Zod for all input validation
|
|
293
|
+
- Use Result<T, E> pattern, no thrown exceptions in business logic
|
|
294
|
+
- Logging: use structured JSON via Winston
|
|
295
|
+
- Tests: Vitest, minimum 80% coverage per package
|
|
296
|
+
|
|
297
|
+
## File Operations
|
|
298
|
+
|
|
299
|
+
- Never modify files in packages/ you are not explicitly asked to work on
|
|
300
|
+
- Always run `turbo build --filter=<package>` after changes
|
|
301
|
+
- Run `turbo test --filter=<package>` before signaling done
|
|
302
|
+
|
|
303
|
+
## Context Rules
|
|
304
|
+
|
|
305
|
+
- Before modifying a package, ALWAYS read its README.md first
|
|
306
|
+
- Do not scan entire monorepo. Work within the target package only.
|
|
307
|
+
- If you need types from another package, read only its index.ts
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Why Each Rule Exists
|
|
311
|
+
|
|
312
|
+
| Rule | Context Savings |
|
|
313
|
+
| --- | --- |
|
|
314
|
+
| "Work within the target package only" | The agent avoids reading 200 files from neighboring packages |
|
|
315
|
+
| "Read README.md first" | 1 file instead of 20 `list_dir` + `view_file` |
|
|
316
|
+
| "No cross-package imports except through index.ts" | The agent needs only 1 file to understand a package's API |
|
|
317
|
+
| "Dependency direction" | The agent knows where to look and where not to |
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## 6. Knowledge Items — Project Memory
|
|
322
|
+
|
|
323
|
+
KI is the **encyclopedia** that the agent checks at the beginning of every conversation.
|
|
324
|
+
|
|
325
|
+
### What to put in KI in a Monorepo
|
|
326
|
+
|
|
327
|
+
| KI | Content | When needed |
|
|
328
|
+
| --- | --- | --- |
|
|
329
|
+
| `database-schema` | All tables, relations, indexes | When working with Prisma, migrations, queries |
|
|
330
|
+
| `api-conventions` | Response formatting, error codes, pagination | When creating/editing endpoints |
|
|
331
|
+
| `auth-flow` | JWT lifecycle, refresh logic, roles | When handling authentication |
|
|
332
|
+
| `deployment-pipeline` | How to deploy, staging vs prod, env vars | When dealing with CI/CD |
|
|
333
|
+
| `known-bugs` | Known bugs, workarounds | When debugging weird behavior |
|
|
334
|
+
| `package-ownership` | Who owns what, areas of responsibility | When routing tasks in orchestrator |
|
|
335
|
+
| `tech-debt-registry` | List of tech debt with priorities | When planning refactoring |
|
|
336
|
+
|
|
337
|
+
### Rules for Writing KI
|
|
338
|
+
|
|
339
|
+
```
|
|
340
|
+
✅ Good:
|
|
341
|
+
"API responses are always wrapped in { data: T, error: null } | { data: null, error: { code, message } }"
|
|
342
|
+
|
|
343
|
+
❌ Bad:
|
|
344
|
+
"We have a flexible and modern API architecture supporting many formats"
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
> [!TIP]
|
|
348
|
+
> **Atomicity Rule**: One KI = One Topic. Don't write "everything about the project" into one file.
|
|
349
|
+
> The agent only loads relevant KIs. The smaller the KI, the more accurate the selection.
|
|
350
|
+
|
|
351
|
+
---
|
|
352
|
+
|
|
353
|
+
## 7. Skills — Agent Abilities
|
|
354
|
+
|
|
355
|
+
Skills are **instructions** on how to execute a specific, repeatable task.
|
|
356
|
+
|
|
357
|
+
### Which Skills to Create in a Monorepo
|
|
358
|
+
|
|
359
|
+
| Skill | Purpose | Example |
|
|
360
|
+
| --- | --- | --- |
|
|
361
|
+
| `create-package` | Scaffold a new package | tsconfig, package.json, README, template files |
|
|
362
|
+
| `create-api-endpoint` | Scaffold a REST endpoint | route + service + validation + test |
|
|
363
|
+
| `db-migration` | Prisma migration | schema change → prisma migrate → seed |
|
|
364
|
+
| `add-feature-flag` | Setup a feature flag | config + check + rollout |
|
|
365
|
+
| `create-shared-type` | New shared type | where to put, how to export |
|
|
366
|
+
|
|
367
|
+
### Example SKILL.md
|
|
368
|
+
|
|
369
|
+
```markdown
|
|
370
|
+
---
|
|
371
|
+
name: create-api-endpoint
|
|
372
|
+
description: >-
|
|
373
|
+
Create a new REST API endpoint following project conventions.
|
|
374
|
+
Use when adding new routes to packages/api.
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## When to Use
|
|
378
|
+
|
|
379
|
+
- User asks to add a new API endpoint
|
|
380
|
+
- User asks to add a new route
|
|
381
|
+
|
|
382
|
+
## When NOT to Use
|
|
383
|
+
|
|
384
|
+
- Modifying existing endpoints (just edit the file)
|
|
385
|
+
- WebSocket endpoints (different pattern)
|
|
386
|
+
|
|
387
|
+
## Steps
|
|
388
|
+
|
|
389
|
+
1. Read packages/api/README.md for Source Structure
|
|
390
|
+
2. Create route file in src/routes/v1/{resource}.ts
|
|
391
|
+
3. Create service in src/services/{resource}.service.ts
|
|
392
|
+
4. Add Zod validation schemas
|
|
393
|
+
5. Register route in src/routes/v1/index.ts
|
|
394
|
+
6. Create test in tests/routes/{resource}.test.ts
|
|
395
|
+
7. Run: turbo test --filter=@monorepo/api
|
|
396
|
+
|
|
397
|
+
## Template
|
|
398
|
+
|
|
399
|
+
### Route (src/routes/v1/{resource}.ts)
|
|
400
|
+
|
|
401
|
+
\`\`\`typescript
|
|
402
|
+
/\*_ Route: GET/POST /api/v1/{resource} — {Resource} CRUD _/
|
|
403
|
+
import { Router } from 'express';
|
|
404
|
+
import { z } from 'zod';
|
|
405
|
+
import { validate } from '../../middleware/validation';
|
|
406
|
+
import { {Resource}Service } from '../../services/{resource}.service';
|
|
407
|
+
|
|
408
|
+
const router = Router();
|
|
409
|
+
|
|
410
|
+
const createSchema = z.object({
|
|
411
|
+
name: z.string().min(1).max(255),
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
router.get('/', async (req, res, next) => { /_ ... _/ });
|
|
415
|
+
router.post('/', validate(createSchema), async (req, res, next) => { /_ ... _/ });
|
|
416
|
+
|
|
417
|
+
export default router;
|
|
418
|
+
\`\`\`
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
---
|
|
422
|
+
|
|
423
|
+
## 8. Workflows — Routine Automation
|
|
424
|
+
|
|
425
|
+
Workflows are **step-by-step instructions** for frequently executed procedures.
|
|
426
|
+
|
|
427
|
+
### Key Workflows for a Monorepo
|
|
428
|
+
|
|
429
|
+
````markdown
|
|
430
|
+
## <!-- .agents/workflows/test.md -->
|
|
431
|
+
|
|
432
|
+
## description: Run tests for a specific package
|
|
433
|
+
|
|
434
|
+
// turbo-all
|
|
435
|
+
|
|
436
|
+
1. Determine the target package from user request
|
|
437
|
+
2. Run tests:
|
|
438
|
+
|
|
439
|
+
```bash
|
|
440
|
+
cd /path/to/monorepo && npx turbo test --filter=@monorepo/{package}
|
|
441
|
+
```
|
|
442
|
+
````
|
|
443
|
+
|
|
444
|
+
3. If tests fail, analyze the output and suggest fixes
|
|
445
|
+
|
|
446
|
+
````
|
|
447
|
+
|
|
448
|
+
```markdown
|
|
449
|
+
<!-- .agents/workflows/build-check.md -->
|
|
450
|
+
---
|
|
451
|
+
description: Verify build for affected packages
|
|
452
|
+
---
|
|
453
|
+
// turbo-all
|
|
454
|
+
|
|
455
|
+
1. Run type check:
|
|
456
|
+
```bash
|
|
457
|
+
cd /path/to/monorepo && npx turbo build --dry-run
|
|
458
|
+
````
|
|
459
|
+
|
|
460
|
+
2. Build only affected:
|
|
461
|
+
|
|
462
|
+
```bash
|
|
463
|
+
cd /path/to/monorepo && npx turbo build --filter=...[HEAD~1]
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
````
|
|
467
|
+
|
|
468
|
+
```markdown
|
|
469
|
+
<!-- .agents/workflows/new-package.md -->
|
|
470
|
+
---
|
|
471
|
+
description: Create a new package in the monorepo
|
|
472
|
+
---
|
|
473
|
+
1. Ask the user for: package name, description, dependencies
|
|
474
|
+
2. Create directory structure (see create-package skill)
|
|
475
|
+
3. Run: npm install
|
|
476
|
+
4. Run: turbo build --filter=@monorepo/{name}
|
|
477
|
+
5. Verify it builds successfully
|
|
478
|
+
````
|
|
479
|
+
|
|
480
|
+
> [!TIP]
|
|
481
|
+
> **`// turbo-all`** is a magic annotation. It tells the agent to auto-approve all commands in the workflow.
|
|
482
|
+
> Only use this for safe, read-only, or idempotent operations.
|
|
483
|
+
|
|
484
|
+
---
|
|
485
|
+
|
|
486
|
+
## 9. MCP Servers — External Tools
|
|
487
|
+
|
|
488
|
+
> [!WARNING]
|
|
489
|
+
> **Budget: ~100 tools** combined across all MCP servers, ~20K tokens for definitions.
|
|
490
|
+
> Every unnecessary tool deteriorates the tool choice model. One `search` tool with routing is better than 100 endpoint wrapper tools.
|
|
491
|
+
|
|
492
|
+
### Recommended MCPs for a Monorepo
|
|
493
|
+
|
|
494
|
+
| MCP Server | Purpose | Context Cost |
|
|
495
|
+
| --- | --- | --- |
|
|
496
|
+
| `github-mcp-server` | PRs, issues, code search | ~5K tokens (many tools) |
|
|
497
|
+
| `context7` | Framework documentation | ~2K tokens |
|
|
498
|
+
| `docs-cache` | Documentation cache + notes | ~3K tokens |
|
|
499
|
+
| Custom `project-search` | Semantic search over your repo | ~1K tokens |
|
|
500
|
+
|
|
501
|
+
### Advice: Custom MCP for Navigation
|
|
502
|
+
|
|
503
|
+
Create an MCP server that understands your monorepo structure:
|
|
504
|
+
|
|
505
|
+
```typescript
|
|
506
|
+
// tools:
|
|
507
|
+
// - find_package(name) → returns README.md content + file list
|
|
508
|
+
// - find_type(typeName) → returns file path + definition
|
|
509
|
+
// - find_service(domain) → returns service files for a domain
|
|
510
|
+
// - get_dependency_graph(packageName) → returns deps
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
This is **one MCP with 4 tools** instead of the agent doing 10+ `list_dir` + `grep_search`.
|
|
514
|
+
|
|
515
|
+
---
|
|
516
|
+
|
|
517
|
+
## 10. Context Saving Techniques
|
|
518
|
+
|
|
519
|
+
### Technique 1: "Lazy Loading"
|
|
520
|
+
|
|
521
|
+
```
|
|
522
|
+
❌ Agent: view_file on 15 files sequentially, then starts thinking
|
|
523
|
+
✅ Agent: reads README → understands structure → conditionally reads ONLY 2-3 files
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
Ensured by an AGENTS.md rule:
|
|
527
|
+
|
|
528
|
+
```
|
|
529
|
+
Before modifying a package, ALWAYS read its README.md first.
|
|
530
|
+
Do not read more than 3 files before starting work.
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
### Technique 2: "Targeted Grep over Full Read"
|
|
534
|
+
|
|
535
|
+
```
|
|
536
|
+
❌ view_file("src/services/user.service.ts") — 500 lines
|
|
537
|
+
✅ grep_search("createUser", "src/services/") → found line 42 → view_file(lines 35-65)
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
### Technique 3: "Types as Contracts"
|
|
541
|
+
|
|
542
|
+
Instead of reading implementation — read only the types:
|
|
543
|
+
|
|
544
|
+
```
|
|
545
|
+
❌ view_file("packages/core/src/services/payment.service.ts") — 300 lines of implementation
|
|
546
|
+
✅ view_file("packages/core/src/types.ts") — 50 lines of interfaces
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
This requires:
|
|
550
|
+
|
|
551
|
+
- All public types extracted to `types.ts` or `types/`
|
|
552
|
+
- `index.ts` re-exports only the public API
|
|
553
|
+
- Implementation is inside `internal/`
|
|
554
|
+
|
|
555
|
+
### Technique 4: "Context via Conventions"
|
|
556
|
+
|
|
557
|
+
The stricter the conventions, the less the agent has to read:
|
|
558
|
+
|
|
559
|
+
```
|
|
560
|
+
If the agent knows:
|
|
561
|
+
- "All services are in src/services/{domain}.service.ts"
|
|
562
|
+
- "All tests mirror src/ inside tests/"
|
|
563
|
+
- "Validation is via Zod in src/validation/{domain}.schema.ts"
|
|
564
|
+
|
|
565
|
+
Then it can predict file paths without running list_dir.
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
### Technique 5: "Package Barrier"
|
|
569
|
+
|
|
570
|
+
```
|
|
571
|
+
AGENTS.md:
|
|
572
|
+
"Never read files outside the target package unless explicitly asked.
|
|
573
|
+
If you need information from another package, read ONLY its:
|
|
574
|
+
1. README.md
|
|
575
|
+
2. src/index.ts (public API)
|
|
576
|
+
3. src/types.ts (type contracts)"
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
This is context restriction **by design**. The agent won't wander around 10 packages.
|
|
580
|
+
|
|
581
|
+
### Technique 6: "Cascading Task Decomposition"
|
|
582
|
+
|
|
583
|
+
For a complex task spanning 5+ packages:
|
|
584
|
+
|
|
585
|
+
```
|
|
586
|
+
Conversation 1 (Opus, Planning):
|
|
587
|
+
→ Analyzes the task, creates implementation_plan.md
|
|
588
|
+
→ Breaks it down into 5 subtasks, one per package
|
|
589
|
+
|
|
590
|
+
Conversations 2–6 (Gemini Pro, Fast):
|
|
591
|
+
→ Each conversation handles exactly one package
|
|
592
|
+
→ Reads plan + package README
|
|
593
|
+
→ Implements changes within one package only
|
|
594
|
+
|
|
595
|
+
Conversation 7 (Sonnet, Planning):
|
|
596
|
+
→ Integration testing
|
|
597
|
+
→ turbo build, turbo test
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### Technique 7: "Knowledge Item instead of Re-reading"
|
|
601
|
+
|
|
602
|
+
If the agent reads the exact same database schema 3 times on the same day — turn it into a KI:
|
|
603
|
+
|
|
604
|
+
```
|
|
605
|
+
.agents/knowledge/database-schema/artifacts/schema.md
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
The KI is injected automatically, saving the agent a `view_file` call for the Prisma schema.
|
|
609
|
+
|
|
610
|
+
### Technique 8: "Explicit Ownership in Pipeline"
|
|
611
|
+
|
|
612
|
+
```json
|
|
613
|
+
// pipeline.json — orchestrator knows who touches what
|
|
614
|
+
{
|
|
615
|
+
"tasks": [
|
|
616
|
+
{
|
|
617
|
+
"id": "api-users-crud",
|
|
618
|
+
"owns": ["packages/api/src/routes/v1/users.ts", "packages/api/src/services/user.*"],
|
|
619
|
+
"role": "back"
|
|
620
|
+
}
|
|
621
|
+
]
|
|
622
|
+
}
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
The `owns` pattern prevents the agent from deviating out of bounds.
|
|
626
|
+
|
|
627
|
+
---
|
|
628
|
+
|
|
629
|
+
## 11. Antipatterns
|
|
630
|
+
|
|
631
|
+
### ❌ Structural Antipatterns
|
|
632
|
+
|
|
633
|
+
| Antipattern | Consequence | Solution |
|
|
634
|
+
| --- | --- | --- |
|
|
635
|
+
| All files in one `src/` folder | Agent reads 50+ files | Separate into domains / packages |
|
|
636
|
+
| No README in packages | Agent wanders around wasting context | README + Source Structure |
|
|
637
|
+
| Circular dependencies | Agent cannot determine scope | Strict dependency direction |
|
|
638
|
+
| "God-file" (3000+ lines) | Agent loads massive amount of tokens | Split by responsibility |
|
|
639
|
+
| Inconsistent names | Agent cannot predict paths | Strict naming conventions |
|
|
640
|
+
|
|
641
|
+
### ❌ AI Antipatterns
|
|
642
|
+
|
|
643
|
+
| Antipattern | Consequence | Solution |
|
|
644
|
+
| --- | --- | --- |
|
|
645
|
+
| "Do everything in one conversation" | Context overflows, quality drops | Cascading decomposition |
|
|
646
|
+
| Opus for CRUD | Expensive, slow, overkill | Flash / Gemini Pro |
|
|
647
|
+
| Flash for Architecture | Bad decisions | Opus in Planning mode |
|
|
648
|
+
| No AGENTS.md | Agent invents conventions | Provide AGENTS.md |
|
|
649
|
+
| All docs in Notion | Agent can't see them | Move to .agents/knowledge/ |
|
|
650
|
+
| 50 MCP tools | Tool selection degrades | ≤20 tools, better routing |
|
|
651
|
+
|
|
652
|
+
### ❌ Context Antipatterns
|
|
653
|
+
|
|
654
|
+
| Antipattern | Consequence | Solution |
|
|
655
|
+
| --- | --- | --- |
|
|
656
|
+
| Opening 20 files in tabs | They pollute `ADDITIONAL_METADATA` | Close irrelevant files |
|
|
657
|
+
| Long conversation (30+ prompts) | Checkpoints truncate, specifics lost | New conversation + handoff |
|
|
658
|
+
| Copy-pasting raw trace | 500 lines of stack trace | Feed only core 10-15 lines |
|
|
659
|
+
| Framing the task vaguely | Agent wastes context on clarification | Provide exact file scopes |
|
|
660
|
+
|
|
661
|
+
---
|
|
662
|
+
|
|
663
|
+
## 12. Checklist: Ideal Monorepo State
|
|
664
|
+
|
|
665
|
+
### 🏗️ Structure
|
|
666
|
+
|
|
667
|
+
- [ ] Every package is isolated and has its own `package.json`, `tsconfig.json`
|
|
668
|
+
- [ ] Public API is exported via `index.ts`
|
|
669
|
+
- [ ] Types are extracted to `types.ts` or `types/`
|
|
670
|
+
- [ ] Implementation is in `internal/` (not imported directly)
|
|
671
|
+
- [ ] Root `README.md` includes an architectural map
|
|
672
|
+
- [ ] `README.md` in each package contains Source Structure
|
|
673
|
+
|
|
674
|
+
### 🧠 Agent
|
|
675
|
+
|
|
676
|
+
- [ ] `.agents/AGENTS.md` holds project rules
|
|
677
|
+
- [ ] Knowledge Items for persistent knowledge (DB schema, API conventions, auth)
|
|
678
|
+
- [ ] Skills for repeatable tasks (create endpoint, migration)
|
|
679
|
+
- [ ] Workflows for routine procedures (test, build, deploy)
|
|
680
|
+
- [ ] JSDoc headers in source files (requires when 20+ files in dir)
|
|
681
|
+
|
|
682
|
+
### 📊 Models
|
|
683
|
+
|
|
684
|
+
- [ ] Opus/Sonnet used for planning and reviewing
|
|
685
|
+
- [ ] Gemini Pro used for implementation
|
|
686
|
+
- [ ] Flash used for minor edits
|
|
687
|
+
- [ ] Cascading strategy utilized for complex tasks
|
|
688
|
+
|
|
689
|
+
### 🛡️ Context
|
|
690
|
+
|
|
691
|
+
- [ ] "Read README first" rule in AGENTS.md
|
|
692
|
+
- [ ] "Stay within package" rule in AGENTS.md
|
|
693
|
+
- [ ] Close irrelevant tabs before the task
|
|
694
|
+
- [ ] Decompose tasks into separated conversations (1 package = 1 convo)
|
|
695
|
+
- [ ] Use `owns` property in pipeline to restrict scope
|
|
696
|
+
|
|
697
|
+
---
|
|
698
|
+
|
|
699
|
+
## Final Formula
|
|
700
|
+
|
|
701
|
+
```
|
|
702
|
+
Ideal Context Consumption =
|
|
703
|
+
Package README (≈200 tokens)
|
|
704
|
+
+ 2–3 target files (≈2K tokens)
|
|
705
|
+
+ Thematic KI (≈500 tokens)
|
|
706
|
+
+ AGENTS.md rules (≈300 tokens)
|
|
707
|
+
────────────────────────────
|
|
708
|
+
≈ 3K tokens to grasp the task
|
|
709
|
+
|
|
710
|
+
vs.
|
|
711
|
+
|
|
712
|
+
Typical Context Unprepared =
|
|
713
|
+
15 × list_dir (≈3K)
|
|
714
|
+
+ 20 × view_file (≈30K)
|
|
715
|
+
+ 5 × grep_search (≈2K)
|
|
716
|
+
────────────────────────────
|
|
717
|
+
≈ 35K tokens wandering
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
> [!CAUTION]
|
|
721
|
+
> **10x difference** in context consumption. With a budget of 70K–150K tokens per task,
|
|
722
|
+
> this translates to the difference between "The agent solved the task flawlessly" and "The agent got confused by the context".
|
|
723
|
+
|
|
724
|
+
---
|
|
725
|
+
|
|
726
|
+
_Created: 2026-04-03 | Based on real-world experience using Antigravity IDE, 6 models, and AUTO_DEV pipelines._
|