@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,562 @@
|
|
|
1
|
+
<!-- FNH: Strategy — File Navigation Header standard | SECTIONS: First Line Rule, Format by Type, Vocabulary, Freshness, Checklist | DEPS: none -->
|
|
2
|
+
|
|
3
|
+
# 🧭 File Navigation Header (FNH)
|
|
4
|
+
|
|
5
|
+
> Guide: A universal header format for EVERY file, allowing the AI agent to gain a complete map of the contents in just 3–5 lines. | SECTIONS: First Line Rule, Format by Type, Vocabulary, Freshness, Checklist | DEPS: none
|
|
6
|
+
|
|
7
|
+
MANDATORY RULE: **FNH headers MUST be in English.** Non-Latin scripts (Cyrillic, CJK, Arabic) cost 1.5–2x more tokens. FNH is the most frequently read content by agents — even a 2x overhead here multiplies across every file in the repo.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Philosophy
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
Without FNH: With FNH:
|
|
15
|
+
view_file(1-800) → 1600 tokens view_file(1-5) → 30 tokens
|
|
16
|
+
"Ah, I need line 340..." "create() is at L45, going there"
|
|
17
|
+
view_file(330-370) → 80 tokens view_file(45-75) → 60 tokens
|
|
18
|
+
──────────────────────────────────────────────────────────────
|
|
19
|
+
Total: ~1680 tokens Total: ~90 tokens (95% savings)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The agent ALWAYS calls `view_file(lines=1-5)` first. If these 5 lines contain a **map of the file** — the agent DOES NOT read the entire file. It jumps straight to the needed location.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ⚡ The First Line Rule
|
|
27
|
+
|
|
28
|
+
> [!IMPORTANT]
|
|
29
|
+
> **Line 1 of every file MUST contain a machine-readable identity descriptor.**
|
|
30
|
+
> An agent must be able to call `view_file(lines=1-1)` on ANY file and immediately know what it is.
|
|
31
|
+
> This is the single most impactful rule in the entire FNH system.
|
|
32
|
+
|
|
33
|
+
### Universal Lookup Table
|
|
34
|
+
|
|
35
|
+
| File Type | Line 1 | FNH Location | Why |
|
|
36
|
+
| --- | --- | --- | --- |
|
|
37
|
+
| `.ts`, `.js`, `.mjs`, `.cjs` | `/** FNH comment */` | Line 1 | No obstacles — first line is free |
|
|
38
|
+
| `.py` | `# FNH comment` | Line 1 | Or line 2 if `# -*- coding: utf-8 -*-` is used |
|
|
39
|
+
| `.css`, `.scss`, `.less` | `/* FNH comment */` | Line 1 | CSS block comment |
|
|
40
|
+
| `.sh`, `.bash` | `#!/bin/bash` (shebang) | **Line 2** | Shebang on L1; FNH on L2. Agent MUST scan `view_file(1-2)` |
|
|
41
|
+
| `.md` | `<!-- FNH comment -->` | Line 1 | HTML comment — invisible in rendered markdown |
|
|
42
|
+
| `.yaml`, `.yml` | `# FNH comment` | Line 1 | YAML native comment |
|
|
43
|
+
| `.toml` | `# FNH comment` | Line 1 | TOML native comment |
|
|
44
|
+
| `.sql` | `-- FNH comment` | Line 1 | SQL native comment |
|
|
45
|
+
| `.sol` (Solidity) | `// SPDX-License...` | **Line 2** | SPDX requires Line 1, FNH is placed on Line 2 |
|
|
46
|
+
| `.dockerfile`, `Dockerfile` | `# FNH comment` | Line 1 | Docker native comment |
|
|
47
|
+
| `.env` | `# FNH comment` | Line 1 | Env file native comment |
|
|
48
|
+
| `.json` | ⛔ No comments | **N/A** | Use companion file or `description` field |
|
|
49
|
+
| `.html` | `<!-- FNH comment -->` | Line 1 | HTML comment — invisible in browser |
|
|
50
|
+
|
|
51
|
+
### The Shebang Exception
|
|
52
|
+
|
|
53
|
+
Executable scripts with a shebang (`#!/usr/bin/env node`, `#!/bin/bash`) are the **only** exception to the first-line rule for general files. The shebang MUST remain on line 1 for the OS to recognize the interpreter. In this case, the FNH goes on **line 2**.
|
|
54
|
+
|
|
55
|
+
> [!IMPORTANT]
|
|
56
|
+
> **Agent scan rule for shebang files:** Always use `view_file(lines=1-2)` instead of `view_file(lines=1-1)`.
|
|
57
|
+
> Line 1 of a shebang file is ALWAYS the interpreter directive (`#!/...`) and carries zero project context.
|
|
58
|
+
> The actual FNH identity is on **line 2**. Scanning only line 1 wastes a tool call.
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
#!/bin/bash
|
|
62
|
+
# Tool: analyze-structure — structural metrics for monorepo analysis | MODE: read-only
|
|
63
|
+
set -euo pipefail
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### The SPDX Exception (Solidity)
|
|
67
|
+
|
|
68
|
+
Solidity smart contracts (`.sol`) have a strict convention enforced by compilers and linters: Line 1 MUST be the `// SPDX-License-Identifier: ...` comment. Therefore, Solidity files share the same exception as Shebang scripts: the FNH goes on **line 2**.
|
|
69
|
+
|
|
70
|
+
> [!TIP]
|
|
71
|
+
> We use standard `//` for FNH in Solidity instead of NatSpec `///`. This is intentional: NatSpec creates public `userdoc`/`devdoc` JSON fields or Sphinx documentation on compilation, which is not the goal of an internal AI system header.
|
|
72
|
+
|
|
73
|
+
```solidity
|
|
74
|
+
// SPDX-License-Identifier: MIT
|
|
75
|
+
// Contract: StakingRewardPool — user staking & dynamic reward emission | INHERITS: Ownable
|
|
76
|
+
pragma solidity ^0.8.20;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
#!/usr/bin/env node
|
|
81
|
+
/** CLI: ai-context-surgeon — entry point for CLI commands | COMMANDS: analyze, restructure, validate */
|
|
82
|
+
const { program } = require("commander");
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### The JSON Exception
|
|
86
|
+
|
|
87
|
+
JSON does not support comments. Use these strategies:
|
|
88
|
+
|
|
89
|
+
| JSON File | Strategy |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `package.json` | Use the `"description"` field as the FNH equivalent |
|
|
92
|
+
| `tsconfig.json` | Actually JSONC — supports `//` comments on line 1 |
|
|
93
|
+
| `turbo.json` / `nx.json` | JSONC — supports `//` comments on line 1 |
|
|
94
|
+
| Other `.json` | Create a companion `<filename>.md` or use a `"//"` hack |
|
|
95
|
+
|
|
96
|
+
Example — `tsconfig.json` (JSONC format):
|
|
97
|
+
|
|
98
|
+
```jsonc
|
|
99
|
+
// Config: TypeScript strict — project-level compiler settings | TARGET: ES2022 | MODULE: NodeNext
|
|
100
|
+
{
|
|
101
|
+
"compilerOptions": {
|
|
102
|
+
"strict": true,
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Example — `package.json` (standard JSON):
|
|
108
|
+
|
|
109
|
+
```json
|
|
110
|
+
{
|
|
111
|
+
"name": "@project/api",
|
|
112
|
+
"description": "REST API server — Express + Zod + JWT auth | EXPORTS: routes, middleware",
|
|
113
|
+
"version": "1.0.0"
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### The Markdown First Line
|
|
118
|
+
|
|
119
|
+
For `.md` files, use an **HTML comment on line 1** before the heading. This is invisible in all rendered Markdown viewers but immediately available to `view_file(1-1)`:
|
|
120
|
+
|
|
121
|
+
```markdown
|
|
122
|
+
<!-- FNH: Guide — API conventions for the project | SECTIONS: Response Format, Errors, Pagination, Auth -->
|
|
123
|
+
|
|
124
|
+
# API Conventions
|
|
125
|
+
|
|
126
|
+
> Guide: REST API standards for the entire project...
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
> [!TIP]
|
|
130
|
+
> The HTML comment on line 1 + the blockquote FNH after H1 are **not redundant**. Line 1 serves the `view_file(1-1)` scan (machine identification). The blockquote serves the `view_file(1-5)` deeper read (detailed map). They work in tandem.
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## FNH Format for Code (.ts / .js)
|
|
135
|
+
|
|
136
|
+
### Compact (files under 100 lines)
|
|
137
|
+
|
|
138
|
+
One line is sufficient:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
/** Service: UserService — CRUD + auth | exports: create, getById, update, delete | deps: prisma, bcrypt */
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Standard (files 100–300 lines)
|
|
145
|
+
|
|
146
|
+
3–5 lines — a map with key functions and their characteristics:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
/**
|
|
150
|
+
* Service: UserService — CRUD + password hashing + email verification
|
|
151
|
+
* EXPORTS: create (@throws Conflict, sends email), getById (@throws NotFound, cached 60s),
|
|
152
|
+
* update (partial merge), delete (soft: sets deletedAt), verifyPassword (bcrypt timing-safe)
|
|
153
|
+
* DEPS: prisma, bcrypt, @project/shared
|
|
154
|
+
*/
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Extended (files 300+ lines — candidates for splitting)
|
|
158
|
+
|
|
159
|
+
Full map with sections:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
/**
|
|
163
|
+
* Route: /api/v1/users — full user management endpoints
|
|
164
|
+
* IMPORTS: express, zod, UserService, AuthGuard
|
|
165
|
+
* ROUTES:
|
|
166
|
+
* GET / → list (paginated, filterable)
|
|
167
|
+
* GET /:id → getById (@throws NotFound)
|
|
168
|
+
* POST / → create (@throws Conflict, validates body)
|
|
169
|
+
* PATCH /:id → update (@throws NotFound, partial)
|
|
170
|
+
* DELETE /:id → delete (@throws NotFound, soft delete, admin only)
|
|
171
|
+
* MIDDLEWARE: authGuard (all routes), rateLimit (POST only)
|
|
172
|
+
* VALIDATION: createUserSchema, updateUserSchema (Zod)
|
|
173
|
+
*/
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## FNH Format for Markdown (.md)
|
|
179
|
+
|
|
180
|
+
Every `.md` file MUST start with an HTML comment on **line 1**. This is the machine-readable identity. The blockquote after H1 provides the detailed map.
|
|
181
|
+
|
|
182
|
+
### Package README
|
|
183
|
+
|
|
184
|
+
```markdown
|
|
185
|
+
<!-- FNH: Package — @project/core domain logic | EXPORTS: User, Order, UserService | DEPS: @project/shared, prisma -->
|
|
186
|
+
|
|
187
|
+
# @project/core
|
|
188
|
+
|
|
189
|
+
> Package: domain logic — entities, services, repositories
|
|
190
|
+
> SECTIONS: Source Structure, Dependencies, Public API, Environment, Scripts
|
|
191
|
+
> KEY EXPORTS: User, Order, UserService, OrderService
|
|
192
|
+
> DEPS: @project/shared, prisma
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Documentation / Guide
|
|
196
|
+
|
|
197
|
+
```markdown
|
|
198
|
+
<!-- FNH: Guide — REST API conventions | SECTIONS: Response Format, Errors, Pagination, Auth, Rate Limits -->
|
|
199
|
+
|
|
200
|
+
# API Conventions
|
|
201
|
+
|
|
202
|
+
> Guide: REST API standards for the entire project
|
|
203
|
+
> SECTIONS: Response Format (#response), Error Codes (#errors), Pagination (#pagination),
|
|
204
|
+
> Authentication (#auth), Rate Limiting (#rate-limits), Versioning (#versioning)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Role Prompt
|
|
208
|
+
|
|
209
|
+
```markdown
|
|
210
|
+
<!-- FNH: Role — Scout read-only monorepo analysis | MODEL: Gemini 3 Flash | MODE: Fast -->
|
|
211
|
+
|
|
212
|
+
# 🔍 Scout — Reconnaissance
|
|
213
|
+
|
|
214
|
+
> Role: read-only monorepo analysis | Model: Gemini 3 Flash | Mode: Fast
|
|
215
|
+
> INPUT: target repo path → OUTPUT: scout-report.md
|
|
216
|
+
> SECTIONS: Prompt (#prompt), What to collect (#collection), Report format (#format), Limits (#limits)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Checklist
|
|
220
|
+
|
|
221
|
+
```markdown
|
|
222
|
+
<!-- FNH: Checklist — Phase 3 Restructuring (15 steps) | ROLE: Surgeon | MODEL: Gemini 3.1 Pro -->
|
|
223
|
+
|
|
224
|
+
# Phase 3: Restructuring
|
|
225
|
+
|
|
226
|
+
> Checklist: 15 restructuring steps with build-checkpoint after each
|
|
227
|
+
> ROLE: Surgeon | MODEL: Gemini 3.1 Pro | PHASES: Foundation, Core, Services, API, Apps
|
|
228
|
+
> PRE-REQ: architecture-plan.md approved
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## FNH Format for Configs
|
|
234
|
+
|
|
235
|
+
### YAML
|
|
236
|
+
|
|
237
|
+
```yaml
|
|
238
|
+
# Config: GitHub Actions CI — build + test + lint on push and PR
|
|
239
|
+
# TRIGGERS: push (main), pull_request (all) | JOBS: lint, test, build | NODE: 18, 20
|
|
240
|
+
name: CI
|
|
241
|
+
on: [push, pull_request]
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Shell
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
#!/bin/bash
|
|
248
|
+
# Tool: analyze-structure — structural metrics for monorepo analysis
|
|
249
|
+
# USAGE: bash tools/analyze-structure.sh /path/to/repo
|
|
250
|
+
# OUTPUT: stdout metrics (files, depth, god-files, packages)
|
|
251
|
+
# MODE: read-only, no modifications
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Dockerfile
|
|
255
|
+
|
|
256
|
+
```dockerfile
|
|
257
|
+
# Config: production Node.js API — multi-stage build
|
|
258
|
+
# STAGES: deps (install), build (tsc), run (distroless)
|
|
259
|
+
# BASE: node:18-alpine → gcr.io/distroless/nodejs18
|
|
260
|
+
# EXPOSES: 3000
|
|
261
|
+
FROM node:18-alpine AS deps
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### SQL
|
|
265
|
+
|
|
266
|
+
```sql
|
|
267
|
+
-- Migration: 003_add_phone_to_users — adds phone column, backfills from profiles
|
|
268
|
+
-- TABLES: users (ALTER), profiles (SELECT for backfill)
|
|
269
|
+
-- REVERSIBLE: yes (DROP COLUMN)
|
|
270
|
+
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## Anatomy of an FNH
|
|
276
|
+
|
|
277
|
+
Every FNH consists of **layers**. All layers are MANDATORY — the only difference is the level of detail.
|
|
278
|
+
|
|
279
|
+
MANDATORY RULE: A file gets an FNH **the moment it is created**, not when it "reaches a threshold." A single file in a folder without an FNH is a violation. No thresholds, no "we'll format it later."
|
|
280
|
+
|
|
281
|
+
```
|
|
282
|
+
LAYER 1 (MANDATORY): Category + Name + Purpose
|
|
283
|
+
→ "Service: UserService — CRUD + auth for users"
|
|
284
|
+
→ The agent knows in 10 tokens: IS THIS the file I need or not
|
|
285
|
+
|
|
286
|
+
LAYER 2 (MANDATORY): Exports / Public API
|
|
287
|
+
→ "EXPORTS: create, getById, update, delete"
|
|
288
|
+
→ The agent knows WHAT can be taken from this file
|
|
289
|
+
|
|
290
|
+
LAYER 3 (MANDATORY): Steps / Sections / Routes
|
|
291
|
+
→ "ROUTES: GET /, POST /, GET /:id, PATCH /:id, DELETE /:id"
|
|
292
|
+
→ The agent knows the STRUCTURE of the file without reading it
|
|
293
|
+
|
|
294
|
+
LAYER 4 (MANDATORY): Dependencies
|
|
295
|
+
→ "DEPS: prisma, bcrypt, @project/shared"
|
|
296
|
+
→ The agent knows the CONTEXT
|
|
297
|
+
|
|
298
|
+
LAYER 5 (MANDATORY): Gotchas / non-obvious behavior
|
|
299
|
+
→ "@throws Conflict | sends email | cached 60s"
|
|
300
|
+
→ The agent knows about SURPRISES before starting to edit
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Scaling Depth
|
|
304
|
+
|
|
305
|
+
All 5 layers are MANDATORY for every file. The difference is the **detail within the layers**:
|
|
306
|
+
|
|
307
|
+
| File Size | FNH Detail | FNH Lines |
|
|
308
|
+
| --- | --- | --- |
|
|
309
|
+
| Any size | All 5 layers | min 1 line |
|
|
310
|
+
| < 50 lines | Compact: 5 layers in 1 line | 1 line |
|
|
311
|
+
| 50–150 lines | Medium: 5 layers in 2–3 lines | 2–3 lines |
|
|
312
|
+
| 150–300 lines | Detailed: 5 layers in 3–5 lines | 3–5 lines |
|
|
313
|
+
| 300+ lines | Maximum + TIME TO SPLIT the file | 5–8 lines |
|
|
314
|
+
|
|
315
|
+
> [!IMPORTANT]
|
|
316
|
+
> A small file **does not** exempt you from an FNH. Even a single-line `index.ts` gets an FNH:
|
|
317
|
+
>
|
|
318
|
+
> ```typescript
|
|
319
|
+
> /** Barrel: @project/core public API | EXPORTS: User, UserService, CreateUserInput */
|
|
320
|
+
> export { User } from "./user";
|
|
321
|
+
> ```
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
## Full Examples
|
|
326
|
+
|
|
327
|
+
### TypeScript — Service
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
/**
|
|
331
|
+
* Service: OrderService — order lifecycle from cart to delivery
|
|
332
|
+
* EXPORTS: createOrder (@throws OutOfStock, sends confirmation), getOrder (@throws NotFound),
|
|
333
|
+
* cancelOrder (@throws AlreadyShipped, refunds payment), listOrders (paginated + filtered)
|
|
334
|
+
* DEPS: prisma, stripe, @project/shared, @project/notifications
|
|
335
|
+
*/
|
|
336
|
+
import { PrismaClient } from "@prisma/client";
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### TypeScript — Route
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
/**
|
|
343
|
+
* Route: /api/v1/products — product catalog CRUD + search
|
|
344
|
+
* ROUTES: GET / (search, paginated), GET /:id, POST / (admin), PATCH /:id (admin), DELETE /:id (admin)
|
|
345
|
+
* AUTH: GET public, POST/PATCH/DELETE require admin role
|
|
346
|
+
* VALIDATION: createProductSchema, updateProductSchema, searchQuerySchema (Zod)
|
|
347
|
+
*/
|
|
348
|
+
import { Router } from "express";
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### TypeScript — Middleware
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
/** Middleware: RateLimiter — sliding window 100req/min per IP | @mutates req.rateLimit | 429 on exceed */
|
|
355
|
+
import { RateLimiterMemory } from "rate-limiter-flexible";
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### TypeScript — Barrel / index.ts
|
|
359
|
+
|
|
360
|
+
```typescript
|
|
361
|
+
/**
|
|
362
|
+
* Barrel: @project/core public API
|
|
363
|
+
* ENTITIES: User, Order, Product
|
|
364
|
+
* SERVICES: UserService, OrderService, ProductService
|
|
365
|
+
* TYPES: CreateUserInput, OrderDTO, ProductFilter
|
|
366
|
+
*/
|
|
367
|
+
export { User } from "./entities/user";
|
|
368
|
+
export { Order } from "./entities/order";
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### TypeScript — Types
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
/**
|
|
375
|
+
* Types: User domain contracts
|
|
376
|
+
* INPUTS: CreateUserInput, UpdateUserInput, UserFilter
|
|
377
|
+
* OUTPUTS: UserDTO, UserListResponse
|
|
378
|
+
* INTERNAL: UserEntity (not exported)
|
|
379
|
+
*/
|
|
380
|
+
export interface CreateUserInput {
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Markdown — Package README
|
|
384
|
+
|
|
385
|
+
```markdown
|
|
386
|
+
<!-- FNH: Package — @project/api REST API server | EXPORTS: routes, middleware | DEPS: @project/core -->
|
|
387
|
+
|
|
388
|
+
# @project/api
|
|
389
|
+
|
|
390
|
+
> Package: REST API server — Express + Zod validation + JWT auth
|
|
391
|
+
> EXPORTS: routes (v1/users, v1/orders, v1/products), middleware (auth, rateLimit, errorHandler)
|
|
392
|
+
> DEPS: @project/core, @project/shared, express, zod, jsonwebtoken
|
|
393
|
+
> SCRIPTS: dev, build, test, lint
|
|
394
|
+
|
|
395
|
+
## Source Structure
|
|
396
|
+
|
|
397
|
+
...
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Markdown — Knowledge Item
|
|
401
|
+
|
|
402
|
+
```markdown
|
|
403
|
+
<!-- FNH: Knowledge — PostgreSQL schema (8 tables) | TABLES: users, orders, products -->
|
|
404
|
+
|
|
405
|
+
# Database Schema
|
|
406
|
+
|
|
407
|
+
> Knowledge: PostgreSQL schema — 8 tables, key relations, indexes, migration history
|
|
408
|
+
> TABLES: users, orders, order_items, products, categories, sessions, audit_log, settings
|
|
409
|
+
> RELATIONS: users 1→N orders, orders 1→N order_items, order_items N→1 products
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
---
|
|
413
|
+
|
|
414
|
+
## Key FNH Vocabulary (Standard Dictionary)
|
|
415
|
+
|
|
416
|
+
For predictability, use FIXED keywords:
|
|
417
|
+
|
|
418
|
+
| Keyword | Description | Used in |
|
|
419
|
+
| --- | --- | --- |
|
|
420
|
+
| `EXPORTS` | Public API / what it exports | .ts, .js, index.ts |
|
|
421
|
+
| `DEPS` | Key dependencies | all |
|
|
422
|
+
| `SECTIONS` | Document sections | .md |
|
|
423
|
+
| `IMPORTS` | Key imports | .ts, .js |
|
|
424
|
+
| `USAGE` | How to run | scripts, CLI |
|
|
425
|
+
| `OUTPUT` | What it produces | scripts, tools |
|
|
426
|
+
| `MODE` | Operating mode (read-only, etc.) | scripts, roles |
|
|
427
|
+
| `INPUT` | What it accepts | roles, scripts |
|
|
428
|
+
| `PRE-REQ` | What is needed before using | checklists, skills |
|
|
429
|
+
|
|
430
|
+
### Web & Backend (Node, Go, Python)
|
|
431
|
+
|
|
432
|
+
| Keyword | Description | Used in |
|
|
433
|
+
| --- | --- | --- |
|
|
434
|
+
| `ROUTES` | HTTP endpoints | route files |
|
|
435
|
+
| `MIDDLEWARE` | Used middleware | route files |
|
|
436
|
+
| `VALIDATION` | Validation schemas | route, schema files |
|
|
437
|
+
| `AUTH` | Authentication requirements | route files |
|
|
438
|
+
|
|
439
|
+
### Data & Infrastructure
|
|
440
|
+
|
|
441
|
+
| Keyword | Description | Used in |
|
|
442
|
+
| --- | --- | --- |
|
|
443
|
+
| `TYPES` | Exported types | types.ts |
|
|
444
|
+
| `TABLES` | DB Tables | .sql, knowledge |
|
|
445
|
+
| `RELATIONS` | Entity relationships | schema, knowledge |
|
|
446
|
+
| `TRIGGERS` | Launch conditions | CI/CD configs |
|
|
447
|
+
| `JOBS` | Pipeline jobs | CI/CD configs |
|
|
448
|
+
| `STAGES` | Build stages | Dockerfile |
|
|
449
|
+
|
|
450
|
+
### Web3 (Solidity, Fe, Vyper)
|
|
451
|
+
|
|
452
|
+
| Keyword | Description | Used in |
|
|
453
|
+
| --- | --- | --- |
|
|
454
|
+
| `INHERITS` | Base contracts | .sol |
|
|
455
|
+
| `STATE` | Key state variables | .sol |
|
|
456
|
+
| `EVENTS` | Emitted events | .sol |
|
|
457
|
+
|
|
458
|
+
---
|
|
459
|
+
|
|
460
|
+
## MANDATORY RULE: FNH Freshness
|
|
461
|
+
|
|
462
|
+
> [!CAUTION]
|
|
463
|
+
> **A stale FNH is worse than no FNH at all.** If the FNH says `EXPORTS: create, getById, delete`, but `delete` was removed and `archive` was added — the agent will look for a non-existent function and miss the new one. This **actively causes harm**.
|
|
464
|
+
|
|
465
|
+
### Rule for AGENTS.md
|
|
466
|
+
|
|
467
|
+
This rule MUST be included in every AGENTS.md:
|
|
468
|
+
|
|
469
|
+
```markdown
|
|
470
|
+
## FNH Freshness Rule
|
|
471
|
+
|
|
472
|
+
MANDATORY RULE: When you modify a file, you MUST update its File Navigation Header (FNH)
|
|
473
|
+
to reflect your changes. Specifically:
|
|
474
|
+
|
|
475
|
+
- If you ADD a new export/function/route — add it to the FNH
|
|
476
|
+
- If you REMOVE an export/function/route — remove it from the FNH
|
|
477
|
+
- If you RENAME anything — update it in the FNH
|
|
478
|
+
- If you change @throws, side effects, or deps — update the FNH
|
|
479
|
+
- If FNH does not exist — create one before starting your work
|
|
480
|
+
The FNH is the FIRST thing other agents read. A stale FNH misleads them. Keep it current.
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
### When to Update
|
|
484
|
+
|
|
485
|
+
| Change | Update FNH? |
|
|
486
|
+
| --- | :---------: |
|
|
487
|
+
| Added a new function/export | ✅ YES |
|
|
488
|
+
| Removed a function/export | ✅ YES |
|
|
489
|
+
| Renamed a function | ✅ YES |
|
|
490
|
+
| Added/removed a dependency | ✅ YES |
|
|
491
|
+
| Changed @throws / side effects | ✅ YES |
|
|
492
|
+
| Fixed a bug inside a function (API unchanged) | ❌ NO |
|
|
493
|
+
| Minor refactoring (variables, formatting) | ❌ NO |
|
|
494
|
+
| Added tests | ❌ NO |
|
|
495
|
+
|
|
496
|
+
### How It Works in Practice
|
|
497
|
+
|
|
498
|
+
```typescript
|
|
499
|
+
// BEFORE:
|
|
500
|
+
/**
|
|
501
|
+
* Service: UserService — CRUD + auth
|
|
502
|
+
* EXPORTS: create, getById, update, delete
|
|
503
|
+
* DEPS: prisma, bcrypt
|
|
504
|
+
*/
|
|
505
|
+
|
|
506
|
+
// Developer added archiveUser(), removed delete(), added redis for cache
|
|
507
|
+
|
|
508
|
+
// AFTER (updated FNH):
|
|
509
|
+
/**
|
|
510
|
+
* Service: UserService — CRUD + auth + archival
|
|
511
|
+
* EXPORTS: create, getById, update, archive (soft, sets archivedAt)
|
|
512
|
+
* DEPS: prisma, bcrypt, redis
|
|
513
|
+
*/
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### Automation: Validation Hook
|
|
517
|
+
|
|
518
|
+
In CI/CD, you can add a check: if a file has changed, but its first 5 lines (FNH) have not, show a warning. Script:
|
|
519
|
+
|
|
520
|
+
```bash
|
|
521
|
+
#!/bin/bash
|
|
522
|
+
# Tool: check-fnh-freshness — warns if file changed but FNH didn't
|
|
523
|
+
# USAGE: bash tools/check-fnh-freshness.sh (run in git repo)
|
|
524
|
+
for file in $(git diff --name-only HEAD~1 -- '*.ts' '*.js'); do
|
|
525
|
+
fnh_changed=$(git diff HEAD~1 -- "$file" | head -20 | grep -c '^[+-]\s*/\*\*\|^[+-]\s*\*')
|
|
526
|
+
if [ "$fnh_changed" -eq 0 ]; then
|
|
527
|
+
echo "⚠️ FNH may be stale: $file (content changed, header unchanged)"
|
|
528
|
+
fi
|
|
529
|
+
done
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
---
|
|
533
|
+
|
|
534
|
+
## Checklist: Add FNH to All Files
|
|
535
|
+
|
|
536
|
+
### First Line Rule
|
|
537
|
+
|
|
538
|
+
- [ ] Every `.ts`/`.js`/`.css` file has an FNH comment on **line 1**
|
|
539
|
+
- [ ] Every `.md` file has `<!-- FNH: ... -->` on **line 1** (before H1)
|
|
540
|
+
- [ ] Every `.md` file ALSO has a `>` blockquote FNH after H1 (detailed map)
|
|
541
|
+
- [ ] Every `.sh`/`.bash` script has FNH on **line 2** (after shebang)
|
|
542
|
+
- [ ] Every `.yml`/`.yaml` config has `# Config: ...` on **line 1**
|
|
543
|
+
- [ ] Every `.sql` file has `-- Migration/Query: ...` on **line 1**
|
|
544
|
+
- [ ] Every `Dockerfile` has `# Config: ...` on **line 1**
|
|
545
|
+
- [ ] Every `.env` file has `# Env: ...` on **line 1**
|
|
546
|
+
- [ ] Every `package.json` has a meaningful `"description"` field
|
|
547
|
+
- [ ] Every `tsconfig.json` has `// Config: ...` on **line 1** (JSONC)
|
|
548
|
+
|
|
549
|
+
### Content Quality
|
|
550
|
+
|
|
551
|
+
- [ ] FNH contains at least Layer 1 (category + name + purpose)
|
|
552
|
+
- [ ] Files > 100 lines have Layers 1–3 (exports + structure)
|
|
553
|
+
- [ ] `index.ts` barrel-files list all re-exports
|
|
554
|
+
- [ ] Route files list all endpoints with HTTP methods
|
|
555
|
+
- [ ] Standard vocabulary keywords are used (EXPORTS, DEPS, ROUTES, ...)
|
|
556
|
+
- [ ] No harmful tags present (@author, @since, @fileoverview)
|
|
557
|
+
- [ ] **FNH Freshness**: AGENTS.md includes the mandatory FNH update rule
|
|
558
|
+
- [ ] **CI hook**: `check-fnh-freshness.sh` is configured for warnings
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
_ai-context-surgeon / strategy | 2026-04-04_
|