@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,541 @@
|
|
|
1
|
+
<!-- FNH: Strategy — package.json standard for AI agents | SECTIONS: Fields Priority, Ordering, Lean Meta | DEPS: none -->
|
|
2
|
+
|
|
3
|
+
# 📦 package.json — Field Map for AI Agents
|
|
4
|
+
|
|
5
|
+
> Guide: Which package.json fields are important, which are noise, how to order them | SECTIONS: Fields Priority, Ordering, Lean Meta, Tools Integration | DEPS: none
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Philosophy
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
package.json of a typical project: ~80–200 lines, ~2K–5K tokens
|
|
13
|
+
package.json that an AI needs: ~15–40 lines, ~300–800 tokens
|
|
14
|
+
─────────────────────────────
|
|
15
|
+
~80% of content = noise for the agent
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
An AI agent reads package.json to answer 4 questions:
|
|
19
|
+
|
|
20
|
+
1. **What is this?** → `name`, `description`
|
|
21
|
+
2. **How to run it?** → `scripts`
|
|
22
|
+
3. **What does it depend on?** → `dependencies`
|
|
23
|
+
4. **What does it export?** → `main`, `exports`, `type`
|
|
24
|
+
|
|
25
|
+
**Everything else is noise** that fills the context window but doesn't help solve the task.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Priority Field Map
|
|
30
|
+
|
|
31
|
+
### 🔴 Tier 1 — Critical (Agent MUST read)
|
|
32
|
+
|
|
33
|
+
Without these fields, the agent won't understand the package. Maximum information density.
|
|
34
|
+
|
|
35
|
+
| Field | Why the agent needs it | Tokens | Example |
|
|
36
|
+
| --- | --- | --- | --- |
|
|
37
|
+
| `name` | Identity and scope | ~5 | `"@project/api"` |
|
|
38
|
+
| `description` | Understand purpose in 1 second | ~15 | `"REST API server with Express"` |
|
|
39
|
+
| `scripts` | Know how to build/test/run | ~30–60 | `{ "build": "tsc", "test": "vitest" }` |
|
|
40
|
+
| `dependencies` | See runtime dependencies | ~20–100 | List of packages |
|
|
41
|
+
| `main` / `module` / `exports` | Understand entry point and format | ~10–20 | `"./dist/index.js"` |
|
|
42
|
+
| `type` | `"module"` vs `"commonjs"` — affects import/require | ~3 | `"module"` |
|
|
43
|
+
|
|
44
|
+
**Tier 1 Cost: ~80–200 tokens** — this is all that's needed for 90% of tasks.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
### 🟡 Tier 2 — Useful (Read when necessary)
|
|
49
|
+
|
|
50
|
+
Helpful in the context of specific tasks, but not always needed.
|
|
51
|
+
|
|
52
|
+
| Field | When needed | Tokens | Recommendation |
|
|
53
|
+
| --- | --- | --- | --- |
|
|
54
|
+
| `devDependencies` | When setting up tooling, CI | ~50–300 | Keep it, but agent might skip it |
|
|
55
|
+
| `peerDependencies` | When publishing package, versioning | ~10–30 | Keep it |
|
|
56
|
+
| `version` | For bump, release, changelog | ~3 | Keep it |
|
|
57
|
+
| `private` | When deciding "whether to publish" | ~3 | Keep it |
|
|
58
|
+
| `engines` | For debugging "doesn't work on Node 18" | ~5 | Keep it |
|
|
59
|
+
| `workspaces` | For understanding monorepo structure | ~10–20 | Only in root package.json |
|
|
60
|
+
| `bin` | When creating CLI tools | ~5–15 | Keep if needed |
|
|
61
|
+
| `files` | When publishing (what goes to npm) | ~10 | Keep it |
|
|
62
|
+
|
|
63
|
+
**Tier 2 Cost: ~100–400 tokens**
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
### 🟢 Tier 3 — Metadata (Can be ignored)
|
|
68
|
+
|
|
69
|
+
They don't affect code and don't help the agent solve tasks.
|
|
70
|
+
|
|
71
|
+
| Field | Why it's noise | Tokens | Recommendation |
|
|
72
|
+
| --- | --- | --- | --- |
|
|
73
|
+
| `license` | Agent doesn't make legal decisions | ~3 | Keep it, but agent will skip |
|
|
74
|
+
| `author` | Doesn't affect code | ~5–10 | Keep it |
|
|
75
|
+
| `contributors` | Doesn't affect code | ~20–100 | Keep it |
|
|
76
|
+
| `repository` | Agent is already in the repo | ~10 | Keep it |
|
|
77
|
+
| `bugs` | Doesn't affect code | ~10 | Keep it |
|
|
78
|
+
| `homepage` | Doesn't affect code | ~5 | Keep it |
|
|
79
|
+
| `keywords` | Only for npm search | ~10–20 | Keep it |
|
|
80
|
+
| `funding` | Doesn't affect code | ~5–15 | Keep it |
|
|
81
|
+
|
|
82
|
+
**Tier 3 Cost: ~70–180 tokens** — harmless but useless noise.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
### ⚫ Tier 4 — Harmful (Actively impedes agent)
|
|
87
|
+
|
|
88
|
+
These fields **bloat the context** and create noise that disrupts focus.
|
|
89
|
+
|
|
90
|
+
| Field | Why harmful | Typical tokens | Recommendation |
|
|
91
|
+
| --- | --- | --- | --- |
|
|
92
|
+
| Inline `jest` config | 30–100 lines inside package.json | ~200–500 | ❌ **Extract** to `jest.config.ts` |
|
|
93
|
+
| Inline `eslint` config | Rules, extends, overrides | ~100–400 | ❌ **Extract** to `.eslintrc.js` |
|
|
94
|
+
| Inline `prettier` config | Formatting | ~30–80 | ❌ **Extract** to `.prettierrc` |
|
|
95
|
+
| Inline `babel` config | Transformations | ~50–200 | ❌ **Extract** to `babel.config.js` |
|
|
96
|
+
| Inline `browserslist` | Target browsers | ~20–30 | ❌ **Extract** to `.browserslistrc` |
|
|
97
|
+
| `resolutions` / `overrides` | Long lists of version overrides | ~100–500 | ⚠️ Keep only in root |
|
|
98
|
+
| `devDependencies` (50+ entries) | Huge tooling list | ~300–800 | ⚠️ Extract to root where possible |
|
|
99
|
+
| `husky` / `lint-staged` config | Git hooks | ~30–80 | ❌ **Extract** to `.husky/` + `.lintstagedrc` |
|
|
100
|
+
| `config` / custom fields | Arbitrary data | ~50–200 | ⚠️ Extract to separate `.config` file |
|
|
101
|
+
| Long `exports` maps | 20+ entry points | ~100–300 | ⚠️ Simplify or document |
|
|
102
|
+
|
|
103
|
+
**Tier 4 Cost: ~500–3000 tokens** — this is pure garbage for 99% of agent tasks.
|
|
104
|
+
|
|
105
|
+
> [!CAUTION]
|
|
106
|
+
> **One inline jest-config (40 lines) costs as much as all of Tier 1 (name + description + scripts + deps).**
|
|
107
|
+
> The agent spends context parsing jest rules instead of understanding business logic.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## Ideal Field Order
|
|
112
|
+
|
|
113
|
+
Place fields **from most important to least important**. The agent reads top to bottom.
|
|
114
|
+
If the context gets truncated, the less important stuff gets cut.
|
|
115
|
+
|
|
116
|
+
```jsonc
|
|
117
|
+
{
|
|
118
|
+
// ─── TIER 1: Identity ───
|
|
119
|
+
"name": "@project/api",
|
|
120
|
+
"version": "1.2.0",
|
|
121
|
+
"description": "REST API server — Express + Zod + Prisma",
|
|
122
|
+
"private": true,
|
|
123
|
+
"type": "module",
|
|
124
|
+
|
|
125
|
+
// ─── TIER 1: Entry points ───
|
|
126
|
+
"main": "./dist/index.js",
|
|
127
|
+
"types": "./dist/index.d.ts",
|
|
128
|
+
|
|
129
|
+
// ─── TIER 1: Commands ───
|
|
130
|
+
"scripts": {
|
|
131
|
+
"dev": "tsx watch src/index.ts",
|
|
132
|
+
"build": "tsc",
|
|
133
|
+
"test": "vitest run",
|
|
134
|
+
"lint": "eslint src/",
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
// ─── TIER 1: Runtime dependencies ───
|
|
138
|
+
"dependencies": {
|
|
139
|
+
"@project/core": "workspace:*",
|
|
140
|
+
"@project/shared": "workspace:*",
|
|
141
|
+
"express": "^4.18.0",
|
|
142
|
+
"zod": "^3.22.0",
|
|
143
|
+
"prisma": "^5.0.0",
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
// ─── TIER 2: Dev tooling ───
|
|
147
|
+
"devDependencies": {
|
|
148
|
+
"typescript": "^5.4.0",
|
|
149
|
+
"vitest": "^1.6.0",
|
|
150
|
+
"@types/express": "^4.17.0",
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
// ─── TIER 3: Metadata (EOF) ───
|
|
154
|
+
"license": "MIT",
|
|
155
|
+
"author": "team",
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Rule: "30 lines or less"
|
|
160
|
+
|
|
161
|
+
> [!IMPORTANT]
|
|
162
|
+
> The ideal package.json for a monorepo package is **≤30 lines**.
|
|
163
|
+
> If it's larger — look for things to extract into a separate config file.
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Monorepo Conventions
|
|
168
|
+
|
|
169
|
+
### Root package.json
|
|
170
|
+
|
|
171
|
+
```jsonc
|
|
172
|
+
{
|
|
173
|
+
"name": "monorepo-root",
|
|
174
|
+
"private": true,
|
|
175
|
+
"workspaces": ["packages/*", "apps/*"],
|
|
176
|
+
|
|
177
|
+
"scripts": {
|
|
178
|
+
"build": "turbo build",
|
|
179
|
+
"test": "turbo test",
|
|
180
|
+
"lint": "turbo lint",
|
|
181
|
+
"dev": "turbo dev",
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
// devDependencies here — shared tooling
|
|
185
|
+
"devDependencies": {
|
|
186
|
+
"turbo": "^2.0.0",
|
|
187
|
+
"typescript": "^5.4.0",
|
|
188
|
+
"vitest": "^1.6.0",
|
|
189
|
+
"eslint": "^9.0.0",
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
**Key point**: Hoist as many `devDependencies` to the **root** as possible. Packages inherit them.
|
|
195
|
+
|
|
196
|
+
### Package package.json
|
|
197
|
+
|
|
198
|
+
```jsonc
|
|
199
|
+
{
|
|
200
|
+
"name": "@project/core",
|
|
201
|
+
"version": "1.0.0",
|
|
202
|
+
"description": "Domain entities and business logic",
|
|
203
|
+
"private": true,
|
|
204
|
+
"type": "module",
|
|
205
|
+
"main": "./dist/index.js",
|
|
206
|
+
"types": "./dist/index.d.ts",
|
|
207
|
+
|
|
208
|
+
"scripts": {
|
|
209
|
+
"build": "tsc",
|
|
210
|
+
"test": "vitest run",
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
"dependencies": {
|
|
214
|
+
"@project/shared": "workspace:*",
|
|
215
|
+
},
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**~15 lines. ~200 tokens. Everything you need.**
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## What to Extract from package.json
|
|
224
|
+
|
|
225
|
+
| What | Where | Format |
|
|
226
|
+
| --- | --- | --- |
|
|
227
|
+
| Jest/Vitest config | `vitest.config.ts` / `jest.config.ts` | TypeScript |
|
|
228
|
+
| ESLint config | `eslint.config.js` / `.eslintrc.js` | JavaScript |
|
|
229
|
+
| Prettier config | `.prettierrc` | JSON |
|
|
230
|
+
| Babel config | `babel.config.js` | JavaScript |
|
|
231
|
+
| TypeScript config | `tsconfig.json` | JSON (already separate) |
|
|
232
|
+
| Browserslist | `.browserslistrc` | Plaintext |
|
|
233
|
+
| Lint-staged | `.lintstagedrc` | JSON |
|
|
234
|
+
| Husky hooks | `.husky/pre-commit` | Shell |
|
|
235
|
+
| Commitlint | `commitlint.config.js` | JavaScript |
|
|
236
|
+
| Stylelint | `.stylelintrc` | JSON |
|
|
237
|
+
| PostCSS | `postcss.config.js` | JavaScript |
|
|
238
|
+
|
|
239
|
+
### Rule: If config > 5 lines → extract it
|
|
240
|
+
|
|
241
|
+
```jsonc
|
|
242
|
+
// ❌ Bad: 40 lines of jest-config in package.json
|
|
243
|
+
{
|
|
244
|
+
"jest": {
|
|
245
|
+
"preset": "ts-jest",
|
|
246
|
+
"testEnvironment": "node",
|
|
247
|
+
"moduleNameMapper": { ... },
|
|
248
|
+
"coverageThreshold": { ... },
|
|
249
|
+
"transformIgnorePatterns": [ ... ],
|
|
250
|
+
// ... 30 more lines
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ✅ Good: separate file
|
|
255
|
+
// vitest.config.ts exists nicely alongside
|
|
256
|
+
// package.json contains no generic configs
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## scripts — order and conventions
|
|
262
|
+
|
|
263
|
+
### Standard Set (in this order)
|
|
264
|
+
|
|
265
|
+
```jsonc
|
|
266
|
+
"scripts": {
|
|
267
|
+
// 1. Development
|
|
268
|
+
"dev": "...", // Run for development
|
|
269
|
+
"start": "...", // Run for production
|
|
270
|
+
|
|
271
|
+
// 2. Build
|
|
272
|
+
"build": "...", // Build process
|
|
273
|
+
"clean": "...", // Clean artifacts
|
|
274
|
+
|
|
275
|
+
// 3. Quality
|
|
276
|
+
"test": "...", // Run tests
|
|
277
|
+
"test:watch": "...", // Tests in watch mode
|
|
278
|
+
"lint": "...", // Style check
|
|
279
|
+
"lint:fix": "...", // Auto-fix style
|
|
280
|
+
"typecheck": "...", // Type checker
|
|
281
|
+
|
|
282
|
+
// 4. Utility (if needed)
|
|
283
|
+
"db:migrate": "...", // DB migrations
|
|
284
|
+
"db:seed": "...", // DB seeder
|
|
285
|
+
"generate": "..." // Code generation
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Antipatterns in scripts
|
|
290
|
+
|
|
291
|
+
```jsonc
|
|
292
|
+
// ❌ Bad: unreadable one-liners
|
|
293
|
+
"scripts": {
|
|
294
|
+
"build": "rimraf dist && cross-env NODE_ENV=production webpack --config webpack.prod.js --progress --profile --bail && node scripts/post-build.js && echo 'Done'"
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ✅ Good: extract to script
|
|
298
|
+
"scripts": {
|
|
299
|
+
"build": "bash scripts/build.sh"
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
```jsonc
|
|
304
|
+
// ❌ Bad: 25 scripts
|
|
305
|
+
"scripts": {
|
|
306
|
+
"prebuild": "...",
|
|
307
|
+
"build": "...",
|
|
308
|
+
"postbuild": "...",
|
|
309
|
+
"pretest": "...",
|
|
310
|
+
"test": "...",
|
|
311
|
+
"test:unit": "...",
|
|
312
|
+
"test:integration": "...",
|
|
313
|
+
"test:e2e": "...",
|
|
314
|
+
"test:coverage": "...",
|
|
315
|
+
// ... 16 more scripts
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ✅ Good: 5–8 core scripts
|
|
319
|
+
"scripts": {
|
|
320
|
+
"dev": "...",
|
|
321
|
+
"build": "...",
|
|
322
|
+
"test": "vitest run",
|
|
323
|
+
"test:watch": "vitest",
|
|
324
|
+
"lint": "eslint src/",
|
|
325
|
+
"typecheck": "tsc --noEmit"
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
---
|
|
330
|
+
|
|
331
|
+
## dependencies — clean and organized
|
|
332
|
+
|
|
333
|
+
### Rules
|
|
334
|
+
|
|
335
|
+
1. **Separate runtime and dev**: `dependencies` = needed in production, `devDependencies` = tooling
|
|
336
|
+
2. **Internal deps use `workspace:*`**: `"@project/core": "workspace:*"`
|
|
337
|
+
3. **Minimum deps**: for every dependency ask "is this strictly needed right here?"
|
|
338
|
+
4. **Hoist shared devDeps to root**: TypeScript, ESLint, test frameworks — to root
|
|
339
|
+
|
|
340
|
+
### Token Cost of dependencies
|
|
341
|
+
|
|
342
|
+
| Dependencies count | Approx Tokens | Action |
|
|
343
|
+
| --- | --- | --- |
|
|
344
|
+
| 1–10 | ~30–60 | ✅ Normal |
|
|
345
|
+
| 11–25 | ~60–150 | ⚠️ Check — is everything needed? |
|
|
346
|
+
| 26–50 | ~150–300 | 🟡 A lot. Package might be doing too much |
|
|
347
|
+
| 50+ | ~300–600 | 🔴 Split the package into multiple |
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## exports — when simple is better
|
|
352
|
+
|
|
353
|
+
```jsonc
|
|
354
|
+
// ✅ Simple (1 entry point — ideal for monorepo packages)
|
|
355
|
+
{
|
|
356
|
+
"main": "./dist/index.js",
|
|
357
|
+
"types": "./dist/index.d.ts"
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 🟡 Medium (2-3 entry points — acceptable)
|
|
361
|
+
{
|
|
362
|
+
"exports": {
|
|
363
|
+
".": {
|
|
364
|
+
"types": "./dist/index.d.ts",
|
|
365
|
+
"import": "./dist/index.js"
|
|
366
|
+
},
|
|
367
|
+
"./utils": {
|
|
368
|
+
"types": "./dist/utils/index.d.ts",
|
|
369
|
+
"import": "./dist/utils/index.js"
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ❌ Complex (10+ entry points — the agent will get lost)
|
|
375
|
+
{
|
|
376
|
+
"exports": {
|
|
377
|
+
".": { ... },
|
|
378
|
+
"./components": { ... },
|
|
379
|
+
"./hooks": { ... },
|
|
380
|
+
"./utils": { ... },
|
|
381
|
+
"./types": { ... },
|
|
382
|
+
"./styles": { ... },
|
|
383
|
+
"./icons": { ... },
|
|
384
|
+
"./theme": { ... },
|
|
385
|
+
"./locales": { ... },
|
|
386
|
+
"./server": { ... }
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
// → If you need 10+ exports — split into smaller packages
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
## Checklist: Bring package.json to ideal state
|
|
395
|
+
|
|
396
|
+
### For every package in the monorepo:
|
|
397
|
+
|
|
398
|
+
- [ ] ≤30 lines
|
|
399
|
+
- [ ] Has `name` with scope (`@project/...`)
|
|
400
|
+
- [ ] Has `description` (one sentence)
|
|
401
|
+
- [ ] Has `type: "module"` (or `"commonjs"`)
|
|
402
|
+
- [ ] `scripts` contains only `dev`, `build`, `test`, `lint` (4–8 scripts)
|
|
403
|
+
- [ ] No inline configs (jest, eslint, prettier, babel)
|
|
404
|
+
- [ ] `dependencies` — only runtime (≤25 entries)
|
|
405
|
+
- [ ] Internal deps via `workspace:*`
|
|
406
|
+
- [ ] `devDependencies` — absolute minimum (shared tooling in root)
|
|
407
|
+
- [ ] No `resolutions` / `overrides` (only in root)
|
|
408
|
+
- [ ] `main` and `types` specified
|
|
409
|
+
- [ ] Fields are ordered logically (Tier 1 → Tier 3)
|
|
410
|
+
- [ ] Metadata (license, author, repository) — at the EOF
|
|
411
|
+
|
|
412
|
+
### For root package.json:
|
|
413
|
+
|
|
414
|
+
- [ ] `private: true`
|
|
415
|
+
- [ ] `workspaces` defined
|
|
416
|
+
- [ ] Shared `devDependencies` hoisted here
|
|
417
|
+
- [ ] `scripts` — turbo/nx commands
|
|
418
|
+
- [ ] `resolutions`/`overrides` — only here if needed
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## The "Lean + Enrich" Strategy — minimum for dev, full for publishing
|
|
423
|
+
|
|
424
|
+
> [!TIP]
|
|
425
|
+
> **Key Insight**: 90% of the time package.json is read by AI agents and developers.
|
|
426
|
+
> The npm registry only sees it at the moment of `npm publish`.
|
|
427
|
+
> Why keep metadata meant for npm, if it only makes the agent suffer?
|
|
428
|
+
|
|
429
|
+
### Concept
|
|
430
|
+
|
|
431
|
+
```
|
|
432
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
433
|
+
│ DEVELOPMENT (99% of time) │
|
|
434
|
+
│ │
|
|
435
|
+
│ package.json = LEAN (15–20 lines) │
|
|
436
|
+
│ Only Tier 1 + Tier 2 │
|
|
437
|
+
│ No: author, license, repository, keywords, bugs, funding │
|
|
438
|
+
│ │
|
|
439
|
+
│ Agent sees ~200 tokens instead of ~800 │
|
|
440
|
+
├─────────────────────────────────────────────────────────────┤
|
|
441
|
+
│ PUBLISHING (1% of time) │
|
|
442
|
+
│ │
|
|
443
|
+
│ node tools/enrich-package-json.js . --all │
|
|
444
|
+
│ package.json = FULL (40–50 lines) │
|
|
445
|
+
│ Added: author, license, repository, keywords, etc. │
|
|
446
|
+
│ │
|
|
447
|
+
│ npm publish │
|
|
448
|
+
│ │
|
|
449
|
+
│ node tools/enrich-package-json.js . --all --restore │
|
|
450
|
+
│ package.json = LEAN again │
|
|
451
|
+
└─────────────────────────────────────────────────────────────┘
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
### Configuration: `.publish-meta.json`
|
|
455
|
+
|
|
456
|
+
All metadata for publishing is stored in a **single file** at the monorepo root:
|
|
457
|
+
|
|
458
|
+
```jsonc
|
|
459
|
+
{
|
|
460
|
+
"shared": {
|
|
461
|
+
"author": "Your Name <you@example.com>",
|
|
462
|
+
"license": "MIT",
|
|
463
|
+
"engines": { "node": ">=18.0.0" },
|
|
464
|
+
},
|
|
465
|
+
"repositoryPattern": "git+https://github.com/org/repo.git",
|
|
466
|
+
"homepagePattern": "https://github.com/org/repo/tree/main/packages/{{package}}",
|
|
467
|
+
"bugsUrl": "https://github.com/org/repo/issues",
|
|
468
|
+
"defaultFiles": ["dist", "README.md", "LICENSE"],
|
|
469
|
+
"packages": {
|
|
470
|
+
"@scope/core": { "keywords": ["core", "domain"] },
|
|
471
|
+
"@scope/api": { "keywords": ["api", "rest"] },
|
|
472
|
+
},
|
|
473
|
+
}
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Commands
|
|
477
|
+
|
|
478
|
+
```bash
|
|
479
|
+
# Preview (changes nothing)
|
|
480
|
+
node tools/enrich-package-json.js . --all --dry-run
|
|
481
|
+
|
|
482
|
+
# Enrich before publishing
|
|
483
|
+
node tools/enrich-package-json.js . --all
|
|
484
|
+
|
|
485
|
+
# Publish
|
|
486
|
+
turbo publish
|
|
487
|
+
|
|
488
|
+
# Restore to lean version
|
|
489
|
+
node tools/enrich-package-json.js . --all --restore
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
### CI/CD Integration
|
|
493
|
+
|
|
494
|
+
```yaml
|
|
495
|
+
# GitHub Actions — publish workflow
|
|
496
|
+
- name: Enrich package.json
|
|
497
|
+
run: node tools/enrich-package-json.js . --all
|
|
498
|
+
|
|
499
|
+
- name: Publish
|
|
500
|
+
run: npx turbo publish
|
|
501
|
+
|
|
502
|
+
- name: Restore lean package.json
|
|
503
|
+
run: node tools/enrich-package-json.js . --all --restore
|
|
504
|
+
|
|
505
|
+
- name: Commit restore
|
|
506
|
+
run: |
|
|
507
|
+
git add packages/*/package.json
|
|
508
|
+
git commit -m "chore: restore lean package.json" || true
|
|
509
|
+
git push
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
### Benefits
|
|
513
|
+
|
|
514
|
+
| Metric | Without Lean+Enrich | With Lean+Enrich |
|
|
515
|
+
| --- | --- | --- |
|
|
516
|
+
| Lines in package.json | 40–80 | 15–20 |
|
|
517
|
+
| Tokens per file | ~600–1500 | ~200–300 |
|
|
518
|
+
| Metadata duplication | In every package | 1 file for entire monorepo |
|
|
519
|
+
| npm publish works | ✅ | ✅ (after enrich) |
|
|
520
|
+
| Agent works efficiently | ❌ noise | ✅ clean context |
|
|
521
|
+
|
|
522
|
+
---
|
|
523
|
+
|
|
524
|
+
## Context Cost Formula
|
|
525
|
+
|
|
526
|
+
```
|
|
527
|
+
package.json cost ≈
|
|
528
|
+
(tier1_fields × 1.0) // always read
|
|
529
|
+
+ (tier2_fields × 0.5) // sometimes read
|
|
530
|
+
+ (tier3_fields × 0.1) // skipped
|
|
531
|
+
+ (tier4_fields × 2.0) // HARMFUL — eats context AND breaks focus
|
|
532
|
+
|
|
533
|
+
Ideal: ≤300 tokens
|
|
534
|
+
Normal: ≤800 tokens
|
|
535
|
+
Bad: ≤2000 tokens
|
|
536
|
+
Harmful: >2000 tokens (inline configs, 50+ deps)
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
---
|
|
540
|
+
|
|
541
|
+
_ai-context-surgeon / strategy | 2026-04-04_
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
<!-- FNH: Template — AGENTS.md for new projects | SECTIONS: Rules, Structure, ai-context-surgeon | DEPS: none -->
|
|
2
|
+
# {{PROJECT_NAME}} — Agent Rules
|
|
3
|
+
|
|
4
|
+
> Config: AGENTS.md — highest priority rules for all AI agents in this repository | SECTIONS: Rules, Naming, FNH, Structure, ai-context-surgeon | DEPS: none
|
|
5
|
+
|
|
6
|
+
> This file has the HIGHEST PRIORITY. All agents MUST follow these rules without exception.
|
|
7
|
+
|
|
8
|
+
## Language & Communication
|
|
9
|
+
- Respond to the user in: {{USER_LANGUAGE}}
|
|
10
|
+
- All agent-facing content MUST be in English:
|
|
11
|
+
- Code comments, variable names, commit messages
|
|
12
|
+
- FNH headers (all file types)
|
|
13
|
+
- JSDoc annotations
|
|
14
|
+
- README Source Structure descriptions
|
|
15
|
+
- AGENTS.md rules
|
|
16
|
+
- Knowledge Items and Skills
|
|
17
|
+
- Rationale: English is 1.5–2x more token-efficient than non-Latin scripts and has stronger enforcement calibration in LLMs
|
|
18
|
+
|
|
19
|
+
## Architecture Rules
|
|
20
|
+
- Every package MUST have a `README.md` with Source Structure section
|
|
21
|
+
- No cross-package imports except through `src/index.ts`
|
|
22
|
+
- Shared types go to `packages/shared/types/`
|
|
23
|
+
- Dependency direction: `routes → services → repositories → entities`
|
|
24
|
+
- No circular dependencies between packages
|
|
25
|
+
|
|
26
|
+
## Navigation Rules (CRITICAL for context optimization)
|
|
27
|
+
- Before modifying ANY package, ALWAYS read its `README.md` first
|
|
28
|
+
- Do NOT scan the entire monorepo — work within the target package only
|
|
29
|
+
- If you need types from another package, read ONLY its `src/types.ts` or `src/index.ts`
|
|
30
|
+
- Do NOT read more than 5 files before starting work
|
|
31
|
+
- Use `grep_search` to find specific code instead of reading entire files
|
|
32
|
+
|
|
33
|
+
## File Navigation Headers (FNH)
|
|
34
|
+
|
|
35
|
+
MANDATORY RULE: Every file in this repository MUST have a navigation header on its first lines.
|
|
36
|
+
|
|
37
|
+
- `.ts`/`.js`: `/** Category: Name — description | deps: ... */`
|
|
38
|
+
- `.md`: `<!-- FNH comment -->` on line 1, AND `> Category: description` (blockquote after H1)
|
|
39
|
+
- `.sh`: `# Tool/Script: description` (after shebang)
|
|
40
|
+
- `.yml`/`.yaml`: `# Config: description` (first line)
|
|
41
|
+
- `.json`: `"description": "..."` (e.g., package.json)
|
|
42
|
+
- `.jsonc`/others: `// Config: description` (if comments supported)
|
|
43
|
+
|
|
44
|
+
**FNH Freshness Rule**: When you modify a file, you MUST update its FNH to reflect changes:
|
|
45
|
+
- ADD a new export/function/route → add it to the FNH
|
|
46
|
+
- REMOVE an export/function/route → remove it from the FNH
|
|
47
|
+
- RENAME anything → update it in the FNH
|
|
48
|
+
- Change deps, @throws, or side effects → update the FNH
|
|
49
|
+
- FNH does not exist → create one BEFORE starting your work
|
|
50
|
+
|
|
51
|
+
A stale FNH actively misleads other agents. Keeping it current is MANDATORY.
|
|
52
|
+
|
|
53
|
+
## Context Exclusion (.antigravityignore)
|
|
54
|
+
|
|
55
|
+
MANDATORY RULE: Agents MUST respect and maintain the `.antigravityignore` file to prevent context pollution.
|
|
56
|
+
- Hide lock files (`pnpm-lock.yaml`, `package-lock.json`)
|
|
57
|
+
- Hide build artifacts (`dist/`, `.next/`, `coverage/`)
|
|
58
|
+
- Hide cache directories (`.turbo/`, `.cache/`)
|
|
59
|
+
- NEVER hide source code, tests (`*.test.ts`), or `README.md`
|
|
60
|
+
|
|
61
|
+
## Code Conventions
|
|
62
|
+
- TypeScript strict mode — no `any`
|
|
63
|
+
- Validation: {{VALIDATION_LIB}} (e.g., Zod)
|
|
64
|
+
- Error handling: {{ERROR_PATTERN}} (e.g., Result<T, E> pattern)
|
|
65
|
+
- Logging: {{LOGGING_LIB}} (e.g., Winston structured JSON)
|
|
66
|
+
- Testing: {{TEST_FRAMEWORK}} (e.g., Vitest), minimum {{COVERAGE}}% coverage
|
|
67
|
+
|
|
68
|
+
## JSDoc Rules
|
|
69
|
+
- File headers: AI-optimized one-line format (see `npx @ivannikov-pro/ai-context-surgeon@latest strategy jsdoc`)
|
|
70
|
+
- Function JSDoc: ONLY for information invisible from TypeScript types
|
|
71
|
+
- ✅ @throws, side effects, @mutates, @deprecated, business rules
|
|
72
|
+
- ❌ @param {type}, @returns {type}, @author, @since, @fileoverview
|
|
73
|
+
- In JavaScript (non-TS) files: @param types ARE needed (replace missing TS)
|
|
74
|
+
|
|
75
|
+
## File Operations
|
|
76
|
+
- Never modify files in packages you are not explicitly asked to work on
|
|
77
|
+
- After changes, run: `{{BUILD_COMMAND}}`
|
|
78
|
+
- Before signaling done, run: `{{TEST_COMMAND}}`
|
|
79
|
+
|
|
80
|
+
## Naming Conventions
|
|
81
|
+
- Files: `kebab-case.ts`
|
|
82
|
+
- Directories: `kebab-case`
|
|
83
|
+
- Classes: `PascalCase`
|
|
84
|
+
- Functions/variables: `camelCase`
|
|
85
|
+
- Constants: `UPPER_SNAKE_CASE`
|
|
86
|
+
- Packages: `@{{SCOPE}}/kebab-case`
|
|
87
|
+
- Types/Interfaces: `PascalCase` (no `I` prefix)
|
|
88
|
+
|
|
89
|
+
## Package Ownership
|
|
90
|
+
{{PACKAGE_OWNERSHIP_TABLE}}
|
|
91
|
+
<!-- Example:
|
|
92
|
+
| Package | Owner | Description |
|
|
93
|
+
|----|----|----|
|
|
94
|
+
| packages/core | backend-team | Domain logic |
|
|
95
|
+
| packages/api | backend-team | REST API |
|
|
96
|
+
| packages/web | frontend-team | React SPA |
|
|
97
|
+
-->
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## 🔧 AI Context Surgeon — Tools & Knowledge API
|
|
102
|
+
|
|
103
|
+
This repository is optimized with [ai-context-surgeon](https://github.com/ivannikov-pro/ai-context-surgeon).
|
|
104
|
+
All tools, roles, strategies, and prompts are available on-demand — no file install needed.
|
|
105
|
+
|
|
106
|
+
### Quick Reference for AI Agents
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# Browse everything available
|
|
110
|
+
npx @ivannikov-pro/ai-context-surgeon@latest catalog
|
|
111
|
+
|
|
112
|
+
# Get condensed task instructions (saves ~95% tokens vs reading docs)
|
|
113
|
+
npx @ivannikov-pro/ai-context-surgeon@latest prompt fnh # How to create FNH headers
|
|
114
|
+
npx @ivannikov-pro/ai-context-surgeon@latest prompt jsdoc # How to write JSDoc
|
|
115
|
+
npx @ivannikov-pro/ai-context-surgeon@latest prompt god-file # How to split large files
|
|
116
|
+
|
|
117
|
+
# Run analysis tools
|
|
118
|
+
npx @ivannikov-pro/ai-context-surgeon@latest radar # FNH map of this repo
|
|
119
|
+
npx @ivannikov-pro/ai-context-surgeon@latest godfiles # Find files > 300 LOC
|
|
120
|
+
npx @ivannikov-pro/ai-context-surgeon@latest context-weight # Cognitive load analysis
|
|
121
|
+
npx @ivannikov-pro/ai-context-surgeon@latest circular # Circular dependency check
|
|
122
|
+
|
|
123
|
+
# Read full strategy docs when needed
|
|
124
|
+
npx @ivannikov-pro/ai-context-surgeon@latest strategy fnh
|
|
125
|
+
npx @ivannikov-pro/ai-context-surgeon@latest strategy monorepo
|
|
126
|
+
|
|
127
|
+
# Save any content to a file for other agents
|
|
128
|
+
npx @ivannikov-pro/ai-context-surgeon@latest role scout --output .agents/scout-prompt.md
|
|
129
|
+
npx @ivannikov-pro/ai-context-surgeon@latest checklist 1 --output .agents/phase-1.md
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### When to Use
|
|
133
|
+
|
|
134
|
+
| Situation | Command |
|
|
135
|
+
| --- | --- |
|
|
136
|
+
| Need to add FNH headers to files | `prompt fnh` |
|
|
137
|
+
| Need to write JSDoc annotations | `prompt jsdoc` |
|
|
138
|
+
| Need to split a god-file (>300 LOC) | `prompt god-file` |
|
|
139
|
+
| Need to understand repo structure | `radar` |
|
|
140
|
+
| Need to find heavy files | `context-weight` |
|
|
141
|
+
| Need to write a package README | `prompt readme` |
|
|
142
|
+
| Need full-pipeline optimization | `role scout` → `role architect` → `role surgeon` |
|
|
143
|
+
|
|
144
|
+
### Benefits
|
|
145
|
+
|
|
146
|
+
- **95% token savings**: `prompt fnh` = ~60 tokens (vs reading strategy doc = ~2,800 tokens)
|
|
147
|
+
- **Always up-to-date**: `@latest` ensures the newest version
|
|
148
|
+
- **Zero maintenance**: No files to keep in sync — tools run from npm cache
|