# Project Overview
**OrgNote** is a comprehensive note-taking ecosystem based on the Zettelkasten approach, providing full compatibility with Emacs Org mode and Org-roam. The project consists of multiple interconnected repositories working together to deliver a seamless note-taking experience across web, mobile, desktop, and command-line interfaces.
# Tech Stack
## Development Tools
- Vitest - Testing framework
- ESLint - Code linting with TypeScript support
- TypeScript Compiler - With custom transformers for path resolution and ESM
- OpenAPI Generator - API code generation
- Bun - Package manager (1.1.22)
## Key Dependencies
- org-mode-ast - Org-mode abstract syntax tree parser
- openpgp - OpenPGP encryption implementation
- axios - HTTP client for API requests
- @capacitor/filesystem - Cross-platform file system access
- @codemirror/state & @codemirror/view - Code editor components
## Build & Deployment
- ES Modules - Native ESM output with import path fixing
- TypeScript transformers - Path resolution and ESM compatibility
- bun/yalc publishing - Multi-target publishing support
:ID: base-agents
### Document comprehension verification (READ THIS FIRST)
When you receive a task, BEFORE starting work:
1. Mandatory confirmation (brief handshake)
Confirm readiness with:
✓ Document read
✓ Conflicts identified: [none / list any conflicts]
✓ Plan: [3-5 bullet points of what you'll do next]
2. Failure mode detection
If you catch yourself:
- Skipping file reads → STOP, read completely
- Assuming APIs exist → STOP, verify with grep
- Writing long functions → STOP, split immediately
- Adding comments → STOP, rename for clarity
- Rushing to code → STOP, complete Pre-implementation checklist
Then immediately COURSE-CORRECT.
### Meta-cognitive framework
1. Thinking protocol (leverage sequential thinking tool)
- USE sequential thinking tool for complex multi-step tasks
- For simple tasks: brief analysis is sufficient
- Question assumptions and validate information critically
- Generate multiple solution hypotheses before choosing
- Verify each hypothesis against requirements
2. Plan-Act-Reflect cycle (ENFORCE on every task)
1. **PLAN** - Create detailed execution plan BEFORE coding:
- List all affected files and dependencies
- Identify potential risks and edge cases
- Define success criteria and validation steps
- Estimate token/time budget for complex operations
- Get explicit user confirmation if scope unclear
2. **ACT** - Execute with verification checkpoints:
- Work in small, testable increments
- Validate each change before proceeding
- Document decisions in-context (why, not what)
- Run linter/typecheck after EVERY change
- Commit small, logical units frequently
3. **REFLECT** - Mandatory post-action review:
- Verify ALL requirements were met (checklist)
- Check for unintended side effects
- Confirm code quality standards compliance
- Update relevant documentation
- Ask: "What could break? What did I miss?"
3. Instruction adherence enforcement
- Read this ENTIRE document before starting ANY task
- Maintain internal checklist of active requirements
- Explicitly state which requirements are being addressed
- Flag any conflicts or ambiguities BEFORE acting
- Never assume - always verify against explicit instructions
- When uncertain: ASK, don't guess
**USE XML TAGS for key checkpoints only:**
critique>Before finalization - verify solution qualitycritique>
Express certainty level when making inferences
coordination>Track progress on multi-part taskscoordination>
Note: For deep reasoning, use sequential thinking tool instead of verbose tags.
### Before start
1. Repository inspection (run once per session)
- Run a single tree scan (prefer `eza`, fallback to `tree`):
if command -v eza >/dev/null 2>&1; then
eza --tree ./ -I 'node_modules|dist|storybook-static|.git|*.lock|*.lockb|coverage|bun.lock' -L 10
else
tree -a -I 'node_modules|dist|storybook-static|.git|*.lock|*.lockb|coverage|bun.lock' -L 10
fi
- If `package.json` exists, scan its `scripts` with `jq`:
if [ -f package.json ]; then
jq -r '.scripts // {} | to_entries[] | "\(.key)\t\(.value)"' package.json
fi
### Common practices (IMMUTABLE RULES - zero exceptions)
1. Language and documentation
- **ENGLISH ONLY** for all code, identifiers, documentation, commit messages
- Code MUST be self-documenting through clear naming and structure
- **MINIMIZE COMMENTS** - prefer self-documenting code
Allowed: JSDoc/docstrings for public APIs, "why" comments for non-obvious business logic
- README/docs only when explicitly requested
2. Architectural principles (enforce ruthlessly)
- **SOLID principles** (non-negotiable):
- Single Responsibility: one function = one reason to change
- Open/Closed: extend via composition, never modify core
- Liskov Substitution: subtypes must be substitutable
- Interface Segregation: many specific interfaces > one general
- Dependency Inversion: depend on abstractions, not concretions
- **Functional-first approach:**
- Pure functions: no side effects, same input = same output
- Immutable data structures by default
- Composition over inheritance
- Higher-order functions over control structures
3. Code structure rules (enforce with linters where possible)
- **Function size:** Target ≤20 lines, MAX 30 (split if larger)
- **Cyclomatic complexity:** Target ≤5, MAX 10 (use linter to enforce)
- **Nesting depth:** Target ≤1, MAX 2 (extract nested logic to functions)
- **File length:** Target ≤200 lines, MAX 300 (split into modules)
- **Parameter count:** Target ≤3, MAX 4 (use objects for more)
4. Required constructs (use these patterns)
- **PREFER guard clauses and early returns** - handle edge cases first, main logic last
Exception: SCSS/CSS files may use else
- **PREFER array methods (map/filter/reduce) or recursion** - declarative over imperative
Exception: Allow imperative loops when significantly clearer or more performant
- **PREFER lookup objects, strategy pattern, or polymorphism** - data-driven dispatch over branching
- **EXTRACT constants with semantic names** - named values reveal intent and prevent errors
- **ISOLATE I/O at boundaries** - keep business logic pure, side effects at edges
- **EXTRACT nested logic to functions** - minimize nesting depth
- **CHOOSE domain-specific names** - every identifier communicates purpose clearly
**Justified exceptions:** When deviating from these patterns, document decision in commit message or Decision Log.
5. Quality assurance (NON-NEGOTIABLE checkpoints)
- **Automated enforcement (configure linters):**
- ESLint/TSLint: max function length, cyclomatic complexity, nesting depth
- Prettier/Black: consistent formatting
- Type checkers: strict mode, no implicit any
- Secret scanners: gitleaks, detect-secrets (pre-commit hooks)
- **Before ANY commit:**
1. Run linter → fix ALL warnings
2. Run type checker → resolve ALL errors
3. Run test suite → verify coverage targets met
4. Verify no console.logs, debugger statements, TODOs
5. Check git diff for unintended changes and secrets
6. ASK user for commit approval (NEVER auto-commit)
- **Test coverage requirements (tier-based):**
- Critical paths (auth, payments, data integrity): ≥90% coverage
- Business logic and services: ≥80% coverage
- Utilities and helpers: ≥70% coverage
- Required test types: positive cases, negative cases, edge cases, error paths
- Integration tests for critical user flows
- E2E tests for key user journeys
6. Error handling protocol
- **Explicit error types** - never generic Error
- **Fail fast** - validate inputs at boundaries immediately
- **Meaningful messages** - include context, expected vs actual
- **NO silent catches** - every catch must log or re-throw
- **Result types** - prefer Result over exceptions where feasible
7. Security baseline (enforce by default)
- **Input validation** - sanitize ALL external data
- **Output encoding** - escape before rendering
- **Least privilege** - minimal permissions/access
- **NO hardcoded secrets** - use environment variables
- **Safe defaults** - opt-in for dangerous operations
- **Dependency audit** - check for known vulnerabilities
8. Security & Privacy protocol (CRITICAL - NEVER violate)
1. Data protection in responses
- **NEVER output secrets:** API keys, passwords, private keys, tokens, credentials
- **NEVER show .env contents:** Mask environment variables (e.g., `API_KEY=*****`)
- **NEVER display PII:** Real emails, phones, addresses (use `user@***.com`, `555-***-1234`)
- **NEVER expose internal URLs:** Mask production domains, internal IPs
- **Mask sensitive data in diffs:** Scan before showing git diff output
2. Pre-commit security checks
- **Run secret scanners:** Use `gitleaks`, `detect-secrets`, or `trufflehog`
- **Verify .gitignore:** Ensure .env, credentials, and key files are ignored
- **Scan dependencies:** Check for known vulnerabilities (`npm audit`, `safety check`)
- **Review diffs manually:** Look for accidentally committed secrets
3. Handling discovered secrets
If you find secrets in code:
1. **DO NOT output them** - replace with `***REDACTED***`
2. Alert user: "Found potential secret in [<../../../../Yandex.Disk.localized/Dropbox/org-roam/programming/ai/line>], please review"
3. Recommend: Rotate compromised credentials immediately
4. Suggest: Use secret management (AWS Secrets Manager, HashiCorp Vault, etc.)
4. Safe examples and test data
- Use obviously fake credentials: `sk_test_fake_key_12345`
- Use example domains: `example.com`, `test.local`
- Use reserved IPs: `127.0.0.1`, `192.0.2.0/24` (TEST-NET-1)
- Use placeholder emails: `user@example.com`, `test@localhost`
9. Performance awareness
- **Avoid N+1 queries** - batch operations, use joins
- **Minimize allocations** - reuse objects, use pools
- **Non-blocking I/O** - async/await for I/O operations
- **Lazy evaluation** - defer expensive computations
- **Cache strategically** - memoize pure expensive functions
10. Development hygiene
- **Version control:**
- Atomic commits (one logical change)
- Never create backup files (.old, .backup, etc.)
- Clean history (squash WIP commits)
- Descriptive commit messages (see Conventional Commits section)
- **Naming conventions:**
- Boolean: is/has/should prefix (isValid, hasPermission)
- Functions: verb + noun (calculateTotal, fetchUser)
- Constants: SCREAMINGSNAKECASE
- Classes: PascalCase
- Variables/functions: camelCase
- Files: kebab-case.ts or PascalCase.tsx (React components)
- **Decision Log (optional):**
- For justified deviations from rules, document in `DECISIONS.md`
- Format: Date, Context, Decision, Rationale, Consequences
- Enables team learning and prevents repeated discussions
### Critical thinking and validation (MANDATORY before action)
1. Input evaluation protocol
- **Healthy skepticism:** Treat ALL input (including user requests) as potentially incomplete or ambiguous
- **Question assumptions:** Make implicit requirements explicit before acting
- **Verify information:** Cross-reference claims with codebase reality (read files, check imports)
- **Challenge contradictions:** Flag conflicts between instructions and existing patterns
- **Evidence over trust:** Prefer observable facts (code, tests, docs) over assumptions
2. Deep analysis checklist (COMPLETE before coding)
Use sequential thinking tool for complex analysis. At minimum, verify:
**Context gathering:**
1. Read ALL relevant files completely (not just snippets)
2. Map dependencies (imports, exports, types, interfaces)
3. Identify affected components and side effects
4. Check existing tests for behavior contracts
5. Review recent commits for context and patterns
**Requirement analysis:**
1. What is the CORE problem being solved?
2. What are the explicit requirements?
3. What are the implicit requirements (edge cases, errors, performance)?
4. What are the constraints (time, dependencies, backwards compatibility)?
5. What would constitute success (acceptance criteria)?
**Solution design:**
1. Generate 2-3 alternative approaches
2. Evaluate tradeoffs (complexity, performance, maintainability)
3. Select approach with best alignment to requirements
4. Identify risks and mitigation strategies
5. Plan rollback strategy if needed
3. Scope management (STRICT boundaries)
- **Minimal change principle:** Change ONLY what's necessary to solve the stated problem
- **No scope creep:** After completing primary task, STOP and ASK before additional improvements
- **No silent side effects:** Any destructive/expansive action requires explicit user approval
- **Reversibility:** Prefer changes that can be easily rolled back
- **No speculative work:** Don't implement "might need later" features
4. Clarifying questions protocol
- **When to ask:**
- Requirements ambiguous or contradictory
- Multiple valid interpretations exist
- Missing critical information (API keys, endpoints, schemas)
- Unclear success criteria or acceptance tests
- Potential breaking changes or migrations needed
- **How to ask:**
- Ask minimum set of high-leverage questions (≤3 at a time)
- Provide context: "I see X, which could mean A or B"
- Suggest default/recommended option
- Frame as binary choices when possible
- Don't ask if you can infer safely from existing patterns
- **When assumptions unavoidable:**
- State assumption explicitly: "Assuming X because Y"
- Choose reversible, low-risk assumption
- Document assumption in code/commit message
- Flag for user review
5. Decision documentation
When making significant architectural decisions, document briefly:
- **Problem:** What issue are we solving?
- **Alternatives:** What options were considered?
- **Choice:** What was selected and why?
- **Tradeoffs:** What are we sacrificing?
- **Risks:** What could go wrong?
Optional: Use `` tag for formal Decision Log entries.
### Code standards and patterns (ENFORCEMENT TIER 1 - CRITICAL)
1. Anti-pattern detection (REJECT code containing these)
- **Deep nesting** (>1 level): Extract functions, use early returns
- **Large functions** (>20 LOC): Split by responsibility
- **Duplicate code** (>3 lines repeated): Extract to function/constant
- **Magic values**: Replace with named constants
- **Ambiguous names** (x, tmp, data, info): Use domain terms
- **Side effects in pure logic**: Isolate I/O at boundaries
- **Mutable state without need**: Prefer immutable data structures
- **Implicit dependencies**: Use dependency injection
- **Error swallowing**: Never catch without handling
- **God objects/functions**: Split responsibilities
2. Refactoring patterns (PREFERRED solutions)
1. Guard clauses over else
if (!data || !data.isValid) return null;
return transform(data);
2. Lookup tables over switch/case
const DISCOUNTS = { premium: 0.2, regular: 0.1 } as const;
const getDiscount = (type: keyof typeof DISCOUNTS) => DISCOUNTS[type] ?? 0;
3. Strategy pattern over conditionals
const processors: Record = {
card: new CardProcessor(),
paypal: new PaypalProcessor(),
};
const processPayment = (type: string, amount: number) =>
processors[type]?.process(amount);
4. Composition over inheritance
const canMove = (state) => ({ move: () => state.position += 1 });
const canBark = () => ({ bark: () => console.log('Woof!') });
const createDog = () => Object.assign({}, canMove({ position: 0 }), canBark());
5. Pure functions for testability
const calculateTotal = (current: number, value: number) => current + value;
const addToTotal = async (current: number, value: number) => {
const newTotal = calculateTotal(current, value);
await saveToDatabase(newTotal);
return newTotal;
};
3. Type safety and contracts
- **Use TypeScript strict mode** (strict, noImplicitAny, strictNullChecks)
- **Avoid any type** - use unknown and narrow with type guards
- **Branded types** for primitives with semantic meaning
- **Discriminated unions** for variant types
- **Readonly by default** - use readonly, Readonly, as const
- **Generics with constraints** - bound type parameters appropriately
4. Module design principles
- **Single export per file** for main entity (except utils/types)
- **Barrel exports** (index.ts) for public API only
- **Colocation** - group related files by feature, not type
- **Encapsulation** - export minimal public surface
- **Dependency direction** - business logic never depends on infrastructure
### AI-specific quality enhancement techniques
1. Forced verification points (STOP and CHECK)
At these moments, you MUST pause and explicitly verify:
1. **Before first code change:**
- "Have I read ALL relevant files completely?"
- "Do I understand the FULL context and dependencies?"
- "Have I identified ALL edge cases and error scenarios?"
- "Is my solution the SIMPLEST that solves the problem?"
2. **After planning but before implementing:**
- "Does my plan address EVERY requirement?"
- "Am I changing the MINIMUM necessary?"
- "Have I considered backwards compatibility?"
- "What tests will prove this works?"
3. **Before submitting changes:**
- "Did I follow EVERY rule in this document?"
- "Are there ANY comments in the code?"
- "Did I run linter and typecheck?"
- "Does this introduce ANY complexity that could be avoided?"
2. Structured problem-solving
For complex problems:
- Use sequential thinking tool (if available) for deep multi-step reasoning
- For simpler tasks: brief analysis → plan → execute
- Always generate 2-3 solution approaches before choosing
- Document significant decisions with rationale
3. Self-questioning protocol
Before EVERY significant decision, ask yourself:
- "What am I assuming that might be wrong?"
- "What information am I missing?"
- "What could break if I do this?"
- "Is there a simpler way?"
- "Am I following the rules, or am I rationalizing exceptions?"
- "Would this pass code review by a senior engineer?"
4. Iterative refinement over big-bang
- **Never** implement entire features in one shot
- **Always** work in small, testable increments:
1. Types/interfaces first
2. Core logic (pure functions)
3. Integration (I/O, side effects)
4. Error handling
5. Tests for each layer
6. Documentation (if required)
5. Hallucination prevention
- **NO guessing:** If unsure, read the file or ask user
- **NO assuming:** Verify imports, types, APIs exist before using
- **NO inventing:** Don't create APIs or functions that don't exist
- **Cite sources:** Reference specific files/lines when discussing code
- **Validate assumptions:** Use grep/ripgrep to confirm patterns exist
6. Anti-cargo-cult programming
- **Understand WHY:** Don't copy patterns without understanding rationale
- **Question conventions:** Is this pattern actually needed here?
- **Avoid complexity theater:** Simple solution > clever solution
- **No premature optimization:** Solve the problem first, optimize later
- **No premature abstraction:** Wait for 3rd use case before abstracting
7. Rubber duck debugging (systematic problem diagnosis)
When stuck, work through problem systematically:
1. What am I trying to achieve? (goal)
2. What is actually happening? (current state)
3. What is the difference? (gap)
4. What have I tried? (attempts)
5. What assumptions am I making? (beliefs)
6. What if those assumptions are wrong? (alternatives)
7. What's the simplest possible test? (validation)
Use sequential thinking tool for complex debugging scenarios.
8. Meta-awareness triggers
If you notice yourself:
- Generating very long functions → STOP, refactor into smaller pieces
- Adding lots of conditions → STOP, use lookup/strategy pattern
- Copy-pasting code → STOP, extract common functionality
- Writing comments → STOP, rename variables/functions for clarity
- Feeling uncertain → STOP, ask user for clarification
- Rushing → STOP, re-read requirements carefully
9. Iterative refinement protocol (MANDATORY for complex tasks)
Do NOT implement everything at once. Follow this cycle:
1. **Minimal Working Version (MWV)**
- Implement simplest solution that demonstrates core functionality
- Ignore edge cases, optimization, polish
- Goal: prove concept works
- Test immediately
2. **Incremental Enhancement**
- Add ONE improvement at a time
- Test after each addition
- Validate against requirements
- Stop when requirements met (avoid over-engineering)
3. **Refinement Cycle**
- Implement → Test → Critique → Refine → Repeat
- Each iteration improves one aspect
- Keep changes small and reversible
Example progression:
- Iteration 1: Core logic only (happy path)
- Iteration 2: Input validation (edge cases)
- Iteration 3: Error handling (failure paths)
- Iteration 4: Performance optimization (if needed)
- Iteration 5: Accessibility/UX polish (if needed)
NEVER accept first solution as final - always critique and refine.
10. Confidence calibration and uncertainty expression
Express confidence level for non-trivial decisions:
**HIGH confidence (>80%):** State facts and proceed
Example: "The bug is on line 23 - missing await keyword. Fixing now."
**MEDIUM confidence (40-80%):** State assumptions and offer options
Example: "This appears to be N+1 query issue, but I haven't profiled. Options: (1) optimize now, (2) measure first. Recommend measuring first."
**LOW confidence (<40%):** Request information, do NOT guess
Example: "I don't have enough context about your payment provider. Which service are you using: Stripe, PayPal, or other?"
When uncertain:
- NEVER hallucinate or make up APIs/functions
- ALWAYS state what information is missing
- ALWAYS offer to investigate (read files, search codebase)
- PREFER asking over guessing
Uncertainty indicators to use:
- "Based on the code I've seen…" (limited context)
- "This likely indicates…" (inference, not fact)
- "I need to verify…" (requires confirmation)
- "I'm uncertain about X, let me check…" (honesty)
11. Self-critique protocol (REQUIRED before finalization)
Before submitting ANY solution, perform self-critique:
critique>
WHAT I IMPLEMENTED:
[Describe solution briefly]
VERIFICATION CHECKLIST:
1. Does it meet ALL stated requirements?
2. Are there edge cases I missed? (null, empty, boundary values)
3. Is error handling complete?
4. Could this break existing functionality?
5. Is this the SIMPLEST solution, or am I over-engineering?
6. Are there performance implications?
7. Is the code readable without comments?
8. Did I follow ALL rules from this document?
IDENTIFIED ISSUES:
[List any problems found - if none, state "None identified"]
REFINEMENTS NEEDED:
[List improvements to make - if none, state "Solution is ready"]
critique>
If issues found during self-critique:
1. Fix immediately if trivial
2. Ask user if uncertain about priority
3. Iterate until self-critique passes
NEVER skip self-critique - it catches 80% of bugs before they ship.
### Task decomposition for complex work
1. Single-focus vs multi-component tasks
Simple tasks: Execute directly with Plan-Act-Reflect
Complex tasks (multiple subsystems/domains): Decompose into focused sub-tasks
2. Decomposition strategy
When task involves 3+ distinct areas of concern:
1. **Identify boundaries**
- Separate concerns by domain (auth, payments, notifications)
- Separate by layer (database, API, UI)
- Separate by type (implementation, testing, documentation)
2. **Sequential execution**
- Complete one sub-task fully before next
- Each sub-task follows full Plan-Act-Reflect cycle
- Validate integration points between sub-tasks
3. **Coordination between sub-tasks**
coordination>
TASK: [Overall goal]
SUB-TASK 1: [First focus area]
- Dependencies: None
- Deliverables: [What gets created]
- Validation: [How to verify]
Status: ✓ Complete
SUB-TASK 2: [Second focus area]
- Dependencies: Sub-task 1 output
- Deliverables: [What gets created]
- Validation: [How to verify]
Status: In progress…
SUB-TASK 3: [Third focus area]
- Dependencies: Sub-tasks 1, 2
- Deliverables: [What gets created]
- Validation: [How to verify]
Status: Pending
coordination>
3. Example: E-commerce checkout flow
Instead of implementing everything at once:
**Sub-task 1 (Database):** Cart and order schemas, migrations
→ Test: Can create/read carts and orders
**Sub-task 2 (API):** Cart endpoints (add/remove/update items)
→ Test: API returns expected responses
**Sub-task 3 (Business Logic):** Order calculation (tax, shipping, total)
→ Test: Calculations correct for edge cases
**Sub-task 4 (Payment):** Payment provider integration
→ Test: Mock payment succeeds/fails appropriately
**Sub-task 5 (Integration):** Wire everything together
→ Test: Full checkout flow works end-to-end
4. Benefits of decomposition
- Each piece is simple enough to reason about fully
- Easier to test incrementally
- Can pause/resume between sub-tasks
- Clear progress tracking
- Failures are isolated and easy to debug
5. When NOT to decompose
- Task is naturally atomic (single file, single concern)
- Decomposition overhead > implementation time
- Strong coupling makes separation awkward
Use judgment: decompose when it clarifies, not when it complicates.
### Conventional commits protocol (STRICT adherence)
1. NEVER commit without explicit user approval
- **ALWAYS ask user:** "Ready to commit these changes?"
- **Show diff summary:** List changed files and key modifications
- **Wait for confirmation:** Do not proceed until user approves
2. Commit message format (ENFORCE)
`(): `
Optional body and footer:
():
[Optional body explaining WHAT changed and WHY]
[Optional footer: refs, breaking changes]
3. Type taxonomy (ONLY these allowed)
- **feat** - New feature or capability (user-facing)
- **fix** - Bug fix (corrects unexpected behavior)
- **perf** - Performance improvement (faster, less memory, etc.)
- **refactor** - Code restructure, no behavior change (no feat/fix)
- **style** - Formatting, naming, whitespace (no logic change)
- **test** - Add/modify tests, fixtures, test utilities
- **docs** - Documentation, README, comments (if required by project)
- **build** - Build system, dependencies, tooling
- **ci** - CI/CD pipeline configuration
- **chore** - Maintenance tasks, version bumps, trivial changes
- **revert** - Revert previous commit (reference original in body)
4. Scope guidelines (context-specific)
Choose scope that identifies affected subsystem:
- **Technical scopes:** api, cli, core, db, auth, cache, logger
- **Domain scopes:** user, order, payment, product, admin
- **Infra scopes:** docker, k8s, terraform, monitoring
- **Meta scopes:** deps, config, scripts, tools
Examples: `feat(auth): add OAuth2 support`, `fix(db): prevent connection leak`
5. Subject line rules (STRICT)
- **Imperative mood:** "add", "fix", "update" (not "added", "fixes", "updating")
- **Lowercase:** Start with lowercase letter (except proper nouns)
- **No period:** Don't end with period/punctuation
- **≤50 characters:** Be concise, details go in body
- **Descriptive:** Clearly state what changed, not why
1. Examples with analysis
- `feat(api): add rate limiting middleware` ✓ (imperative, scoped, concise)
- `fix(auth): handle expired tokens correctly` ✓ (clear what was fixed)
- `refactor(parser): extract validation logic` ✓ (describes transformation)
- `perf(db): add index on user_email column` ✓ (specific optimization)
2. Anti-patterns to avoid
- `feat(api): Added rate limiting.` → Use imperative mood, remove period
- `fix: bug fix` → Add scope, be specific about what was fixed
- `Updated stuff` → Use conventional format with type and scope
- `feat(api): This commit adds rate limiting to the API` → Remove filler words, be concise
6. Body guidelines (when needed)
Use body when:
- Change is non-obvious and needs context
- Explaining WHY (motivation) not WHAT (visible in diff)
- Documenting tradeoffs or alternatives considered
- Providing migration instructions
Format:
- Separate from subject with blank line
- Wrap at 72 characters
- Use bullet points for multiple points
- Focus on intent and rationale
Example:
refactor(parser): extract validation into separate module
- Validation logic was duplicated across 3 parsers
- New module enables easier testing and reuse
- No behavior change, pure code organization
7. Footer guidelines (special cases)
1. Breaking changes (MUST document)
feat(api): change response format to JSON:API spec
BREAKING CHANGE: All API responses now follow JSON:API format.
Clients must update to parse new structure.
Migration:
- Update response parsing to access data.attributes
- Error responses now in errors array
- Pagination metadata in meta object
2. Issue references
fix(auth): prevent session fixation attack
Refs: #123, #456
Closes: #789
3. Reverts
revert: feat(api): add rate limiting middleware
This reverts commit abc123def456.
Reason: Rate limiting caused 500 errors under high load.
Will re-implement with different strategy.
8. Commit composition rules
- **Atomic commits:** One logical change = one commit
- **Complete commits:** Each commit should leave codebase in working state
- **No WIP commits in main:** Squash feature branch before merge
- **Tests included:** If adding feature, include tests in same commit
- **Clean history:** Use interactive rebase to clean up before pushing
9. Pre-commit validation checklist
Before creating commit, YOU MUST verify ALL:
1. All tests pass
2. Linter shows no errors
3. Type checker passes
4. No console.log/debugger statements
5. No commented-out code
6. No TODO/FIXME comments (create issues instead)
7. Commit message follows format exactly
8. Changes match commit message description
10. Multi-file commit strategy
When changing multiple files:
- Group by logical change, not file type
- Prefer multiple small commits over one large commit
- Each commit should be independently revertable
Example sequence:
1. `feat(user): add email validation`
2. `test(user): add validation test cases`
3. `docs(api): update user creation endpoint docs`
Better than single: `feat(user): add email validation with tests and docs`
### Pre-implementation checklist (COMPLETE before writing code)
1. Context verification (MANDATORY - prevents missing requirements)
YOU MUST complete ALL of these before proceeding:
1. Read ALL relevant files completely (not grep snippets)
2. Map all dependencies and imports
3. Identify all affected components
4. Check existing tests for behavior contracts
5. Review recent git history for patterns
6. Understand error handling strategy
7. Verify no duplicate functionality exists
2. Requirements validation
VERIFY all requirements are clear:
1. Core problem clearly defined
2. Explicit requirements documented
3. Edge cases identified (empty, null, undefined, errors)
4. Performance constraints understood
5. Backwards compatibility requirements clear
6. Success criteria defined (how to test)
7. User approval obtained if scope ambiguous
3. Solution design
PLAN before implementing:
1. Generate 2+ alternative approaches
2. Evaluate tradeoffs (complexity vs value)
3. Select minimal viable solution
4. Identify rollback strategy
5. Plan incremental implementation
6. Determine test strategy
### Review protocol (SELF-REVIEW before submission)
1. Pre-review validation
- **Scope:** Review only uncommitted changes (`git diff HEAD --name-only`)
- **Standards:** Enforce ALL rules from this document with ZERO tolerance
- **Automation:** Run linter, type checker, tests BEFORE manual review
2. Review checklist (REJECT code failing ANY item)
1. Critical standards (ENFORCE strictly)
CODE MUST:
1. Express intent through names, minimize comments (allow JSDoc for public APIs, "why" comments for non-obvious logic)
2. Keep functions ≤30 LOC (target ≤20) with single responsibility
3. Maintain nesting depth ≤2 (target ≤1, extract nested logic)
4. Prefer guard clauses and early returns (except SCSS may use else)
5. Prefer array methods or recursion (allow imperative loops when clearer)
6. Prefer lookup objects or strategy pattern (allow simple conditionals when clearer)
7. Define semantic constants for all numeric/string literals
8. Contain only active, production code (clean dead code and debugger statements)
9. Import and use only necessary dependencies
10. Handle all errors explicitly with meaningful messages
11. Load secrets from environment, never hardcode
2. Architecture standards
CODE MUST:
1. Follow SOLID principles (single responsibility, open/closed, Liskov, interface segregation, dependency inversion)
2. Keep business logic pure (side effects isolated at boundaries)
3. Prefer immutable data structures with clear justification for mutation
4. Favor composition over deep inheritance
5. Maintain single, focused responsibility per module
6. Inject dependencies explicitly through constructors/parameters
7. Keep infrastructure concerns separate from business logic
3. Type safety standards
CODE MUST:
1. Use unknown type and narrow with type guards (avoid any)
2. Check for null/undefined before access
3. Handle promise rejections with catch or try/catch
4. Validate types before assertions (minimize as keyword usage)
5. Provide explicit types for all public interfaces
4. Security standards
CODE MUST:
1. Validate and sanitize all external input
2. Use parameterized queries or ORM to prevent SQL injection
3. Escape output and use Content Security Policy for XSS prevention
4. Avoid eval and innerHTML with untrusted data
5. Enforce authentication and authorization at all protected endpoints
6. Sanitize sensitive data before logging
5. Performance standards
CODE MUST:
1. Batch related queries (avoid N+1 patterns)
2. Optimize React rendering with memo/useMemo/useCallback where beneficial
3. Memoize expensive pure computations
4. Use async I/O on hot paths
5. Minimize allocations in loops and hot code
6. Testing standards
CODE MUST INCLUDE:
1. Tests achieving coverage targets (90% critical / 80% business logic / 70% utilities)
2. Required test types: positive cases, negative cases, edge cases, error paths
3. Unit tests for all public functions
4. Integration tests for critical user flows
5. Focus on meaningful assertions, not just coverage percentage
3. Review output format (STRUCTURED response)
1. 1. Executive summary
One paragraph: severity level (Critical/Major/Minor), main issues, recommended direction
2. 2. Critical issues (MUST fix - blocks merge)
For each issue:
- **<../../../../Yandex.Disk.localized/Dropbox/org-roam/programming/ai/line>** - Issue description
- **Why critical:** Impact explanation
- **Fix:** Specific refactoring (code sketch if needed)
3. 3. Major issues (SHOULD fix - degrades quality)
Same format as critical
4. 4. Minor issues (NICE to fix - style/consistency)
Bulleted list with <../../../../Yandex.Disk.localized/Dropbox/org-roam/programming/ai/line> references
5. 5. Security & robustness analysis
- Input validation coverage
- Error handling completeness
- Resource cleanup (connections, files, timers)
- Race condition risks
6. 6. Refactoring plan (if major issues found)
Ordered steps (≤5):
1. Step 1 - what to extract/rename/move
2. Step 2 - what to test
3. …
7. 7. Required tests
Concrete test cases:
- `testName`: what behavior it verifies, what assertions needed
8. 8. Code sketch (minimal pseudocode showing KEY changes)
// file: component.ts
- function bigFunction(lots, of, params) {
- if (condition) {
- // nested logic
- }
- }
+ function processValid(data: Data): Result {
+ if (!isValid(data)) return error;
+ return transform(data);
+ }
9. 9. Language adherence
MANDATORY: Response in same language as question (Russian/English/etc.)
### Context engineering and token management
1. Context window optimization (CRITICAL for quality)
- **Read strategically:** Load COMPLETE relevant files, not scattered snippets
- **Depth over breadth:** Better to fully understand 3 files than partially know 10
- **Dependency mapping:** Track imports/exports to identify related code
- **Incremental loading:** Start with core files, expand to dependencies as needed
- **Context retention:** Refer to previously read content rather than re-reading
2. Information prioritization
1. **Core business logic** - domain models, services, use cases
2. **Type definitions** - interfaces, types, contracts
3. **Entry points** - main files, routers, controllers
4. **Tests** - reveal expected behavior and edge cases
5. **Configuration** - environment, build, dependencies
6. **Documentation** - README, CHANGELOG only if needed
3. Token budget awareness
- **Expensive operations:** Full file reads, large diffs, comprehensive searches
- **Cheap operations:** Targeted grep, tree structure, git log –oneline
- **Batch strategically:** Group related reads, combine small operations
- **Avoid redundancy:** Don't re-read unchanged files
- **Summarize when possible:** Extract key points rather than full content
4. Codebase navigation strategy
1. First time in codebase (cold start)
1. Run tree/eza to understand structure (≤10 levels)
2. Read package.json/requirements.txt for tech stack
3. Read README for project overview (if exists)
4. Identify main entry points (main.ts, index.ts, app.py)
5. Map high-level architecture (dirs = domains)
2. Task-specific navigation (warm context)
1. Use ripgrep to locate relevant symbols/patterns
2. Read complete files containing those symbols
3. Follow imports to understand dependencies
4. Check tests for behavior validation
5. Review recent commits for context
5. Verification and validation hooks
1. Before making changes
- **Read target file completely** (not just function to change)
- **Check for similar patterns** in codebase (consistency)
- **Verify type definitions** are current and complete
- **Review tests** to understand contracts
2. After making changes
- **Re-read modified sections** to verify correctness
- **Check imports** still resolve correctly
- **Run linter/typecheck** to catch issues early
- **Validate against requirements** from initial analysis
### Available CLI tools
1. Essential tools (use these for efficiency)
- **eza** (or tree fallback) - Fast directory visualization
- **rg (ripgrep)** - Blazing fast code search (better than grep/ack/ag)
- **git** - Version control operations
- **gh** - GitHub CLI for issues, PRs, releases
- **jq** - JSON parsing and transformation
- **find** - File system search
- **fd** - Fast file finding (if available)
# Project-Specific Rules
## Error Handling Convention
- **PREFER the `to(...)` pattern** for recoverable sync/async operations instead of ad-hoc `try/catch` when the project already handles failures as `Result`-like values
- **Use meaningful error messages** in `to(...)` when the failure crosses module boundaries or would be hard to diagnose without context
const result = await to(fs.add), 'Optional error')(payload);
if (result.isErr()) {
if (isConstraintError(result.error)) return;
throw result.error;
}