|
| 1 | +# Code Standards — Mega Brain |
| 2 | + |
| 3 | +> **Version:** 1.0.0 | **Date:** 2026-03-05 |
| 4 | +
|
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Overview |
| 8 | + |
| 9 | +This document defines code conventions for the Mega Brain project. It covers two languages (JavaScript, Python) across three codebases (`bin/`, `core/intelligence/`, `.claude/hooks/`). |
| 10 | + |
| 11 | +**Enforcement:** `ruff` (Python), `biome` (JS). Config in `pyproject.toml` and `biome.json`. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 2. JavaScript Standards (bin/) |
| 16 | + |
| 17 | +### Runtime & Module System |
| 18 | +- Node.js >= 18.0.0, ESM only (`"type": "module"`) |
| 19 | +- All files use `import`/`export`. No `require()`. |
| 20 | + |
| 21 | +### Style (enforced by Biome) |
| 22 | +- Semicolons: yes |
| 23 | +- Quotes: single |
| 24 | +- Indentation: 2 spaces |
| 25 | +- Trailing commas: yes (multi-line) |
| 26 | +- Max line length: 100 |
| 27 | + |
| 28 | +### Naming |
| 29 | +- Files: `kebab-case.js` |
| 30 | +- Functions/variables: `camelCase` |
| 31 | +- Constants: `SCREAMING_SNAKE` |
| 32 | + |
| 33 | +### Imports |
| 34 | +```js |
| 35 | +// 1. Node.js builtins |
| 36 | +import { readFileSync } from 'fs'; |
| 37 | +import { resolve, dirname } from 'path'; |
| 38 | + |
| 39 | +// 2. Third-party (npm) |
| 40 | +import chalk from 'chalk'; |
| 41 | + |
| 42 | +// 3. Local modules |
| 43 | +import { readLicense } from './lib/license.js'; |
| 44 | +``` |
| 45 | + |
| 46 | +### Error Handling |
| 47 | +- Top-level: `main().catch(err => { console.error(err.message); process.exit(1); })` |
| 48 | +- Expected failures: bare `catch {}` when error is not needed |
| 49 | +- Logged failures: `catch (err)` with meaningful message |
| 50 | + |
| 51 | +### Documentation |
| 52 | +- JSDoc `/** */` on all exported functions |
| 53 | +- At minimum: description, `@param`, `@returns` |
| 54 | + |
| 55 | +### Type Safety |
| 56 | +- No TypeScript (project decision) |
| 57 | +- Use JSDoc type annotations + `jsconfig.json` with `checkJs: true` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 3. Python Standards — Core (core/intelligence/) |
| 62 | + |
| 63 | +### Runtime |
| 64 | +- Python >= 3.11 (pinned in `.python-version`) |
| 65 | +- Config centralized in `pyproject.toml` |
| 66 | + |
| 67 | +### Style (enforced by Ruff) |
| 68 | +- PEP 8 compliant |
| 69 | +- Quotes: double |
| 70 | +- Indentation: 4 spaces |
| 71 | +- Max line length: 100 |
| 72 | + |
| 73 | +### Naming |
| 74 | +- Files: `snake_case.py` |
| 75 | +- Functions/variables: `snake_case` |
| 76 | +- Classes: `PascalCase` |
| 77 | +- Private: leading underscore `_` |
| 78 | +- Constants: `SCREAMING_SNAKE` |
| 79 | + |
| 80 | +### Imports (enforced by Ruff isort) |
| 81 | +```python |
| 82 | +# 1. Stdlib (alphabetical) |
| 83 | +import json |
| 84 | +import re |
| 85 | +import sys |
| 86 | + |
| 87 | +# 2. Third-party |
| 88 | +import yaml |
| 89 | + |
| 90 | +# 3. Local |
| 91 | +from core.paths import ROOT, KNOWLEDGE_EXTERNAL, ROUTING |
| 92 | +``` |
| 93 | + |
| 94 | +### Paths — CRITICAL RULE |
| 95 | +**All files MUST import from `core.paths`.** Never compute `BASE_DIR` locally. |
| 96 | + |
| 97 | +```python |
| 98 | +# CORRECT |
| 99 | +from core.paths import ROOT, KNOWLEDGE_EXTERNAL |
| 100 | + |
| 101 | +# WRONG — never do this |
| 102 | +BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent |
| 103 | +``` |
| 104 | + |
| 105 | +### Type Hints |
| 106 | +- Required on all public function signatures |
| 107 | +- Use `typing` module for 3.11 compatibility (`Optional`, `Dict`, `List`) |
| 108 | +- Enforced by `pyright` in basic mode on `core/` |
| 109 | + |
| 110 | +### Docstrings |
| 111 | +- Google style (Args, Returns, Raises) |
| 112 | +- Required on modules and public functions |
| 113 | +- Optional on private helpers |
| 114 | + |
| 115 | +### Error Handling |
| 116 | +- Use specific exception tuples: `except (YAMLError, OSError):` |
| 117 | +- Never use bare `except:` |
| 118 | +- Add `logging.warning()` in catch blocks for traceability |
| 119 | + |
| 120 | +### Logging |
| 121 | +- All intelligence scripts: `import logging; logger = logging.getLogger(__name__)` |
| 122 | +- Use `logger.warning()` in error paths |
| 123 | +- Use `print()` only in `if __name__ == "__main__"` CLI entry points |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## 4. Python Standards — Hooks (.claude/hooks/) |
| 128 | + |
| 129 | +### Dependency Constraint |
| 130 | +**stdlib + PyYAML ONLY.** This is a distribution constraint — hooks ship with the npm package. No third-party imports beyond PyYAML. |
| 131 | + |
| 132 | +### Exit Codes |
| 133 | +- `0` = success (hook passed) |
| 134 | +- `1` = warning (continue with notification) |
| 135 | +- `2` = error/block (stop execution) |
| 136 | + |
| 137 | +### Communication |
| 138 | +- Output: JSON to stdout |
| 139 | +- Input: JSON from stdin (hook event payload) |
| 140 | +- Timeout: 30 seconds max |
| 141 | + |
| 142 | +### Prohibited Patterns |
| 143 | +- `2>/dev/null || true` — never suppress errors |
| 144 | +- `subprocess.run(shell=True)` — security risk |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## 5. File & Directory Conventions |
| 149 | + |
| 150 | +- Directories: lowercase (`inbox`, `system`) |
| 151 | +- Config files: SCREAMING-CASE (`STATE.json`, `MEMORY.md`) |
| 152 | +- Skills: kebab-case directories (`knowledge-extraction/`) |
| 153 | +- New output paths: MUST be added to `core/paths.py` ROUTING dict |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 6. Testing Standards |
| 158 | + |
| 159 | +### Frameworks |
| 160 | +- Python: `pytest` with `pytest-cov` |
| 161 | +- JS: Node built-in test runner (`node:test`) |
| 162 | + |
| 163 | +### Test Location |
| 164 | +``` |
| 165 | +tests/ |
| 166 | + python/ |
| 167 | + test_validation/ # Layer classification tests |
| 168 | + test_rag/ # RAG chunker, indexer, query tests |
| 169 | + test_hooks/ # Hook behavior tests |
| 170 | + js/ # CLI tests |
| 171 | + integration/ # Cross-language tests |
| 172 | +``` |
| 173 | + |
| 174 | +### Naming |
| 175 | +- Python: `test_*.py`, functions `test_*` |
| 176 | +- JS: `test_*.js` |
| 177 | + |
| 178 | +### Coverage Targets |
| 179 | +- Phase 1: 40% on `core/intelligence/rag/` and `core/intelligence/validation/` |
| 180 | +- Phase 2: 50% on `core/intelligence/` overall |
| 181 | +- Steady state: 70% on new code |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## 7. Git Workflow |
| 186 | + |
| 187 | +- Branch naming: `type/issue-XX-description` |
| 188 | +- Commit messages: Conventional Commits (`feat`, `fix`, `chore`, `docs`, `refactor`) |
| 189 | +- No direct commits to `main` |
| 190 | +- `git push` blocked for non-devops agents (settings.json deny list) |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## 8. Security Standards |
| 195 | + |
| 196 | +- NEVER hardcode secrets (enforced by `.gitleaks.toml` + `trufflehog`) |
| 197 | +- `.env` is the ONLY credential source |
| 198 | +- `.mcp.json` uses `${ENV_VAR}` syntax, never plaintext tokens |
| 199 | +- All hooks: `"timeout": 30` (ANTHROPIC-STANDARDS) |
| 200 | +- `curl`/`wget` denied in bash hooks |
| 201 | + |
| 202 | +--- |
| 203 | + |
| 204 | +## 9. Dependency Management |
| 205 | + |
| 206 | +- Python: `pyproject.toml` (primary), scoped optional dependencies |
| 207 | +- JS: `package.json` + `package-lock.json` (committed) |
| 208 | +- Hooks: `requirements-hooks.txt` (PyYAML only) |
| 209 | +- New dependencies require justification in PR description |
0 commit comments