Skip to content

Commit 839afce

Browse files
thiagofinchclaude
andcommitted
feat(standards): add code standards, tech stack, and test infrastructure
- pyproject.toml: Python tooling config (ruff, pyright, pytest, scoped deps) - biome.json: JavaScript lint + format config - jsconfig.json: JS type checking via checkJs - .editorconfig: Cross-editor whitespace rules - .python-version / .nvmrc: Pin Python 3.12 and Node 18 - requirements-hooks.txt: Scoped hook deps (PyYAML only) - tests/python/: 57 test cases (44 layer classification + 13 RAG chunker) - reference/CODE-STANDARDS.md: Code conventions for JS + Python - reference/TECH-STACK.md: Canonical technology choices + rationale - reference/SOURCE-TREE.md: Directory structure reference - package.json: Added lint, format, test, test:coverage scripts Ruff passes clean (0 errors). All 57 tests pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 736d845 commit 839afce

File tree

16 files changed

+867
-0
lines changed

16 files changed

+867
-0
lines changed

.editorconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.js]
10+
indent_style = space
11+
indent_size = 2
12+
max_line_length = 100
13+
14+
[*.py]
15+
indent_style = space
16+
indent_size = 4
17+
max_line_length = 100
18+
19+
[*.{json,yaml,yml}]
20+
indent_style = space
21+
indent_size = 2
22+
23+
[*.md]
24+
trim_trailing_whitespace = false
25+
26+
[Makefile]
27+
indent_style = tab

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

biome.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"linter": {
7+
"enabled": true,
8+
"rules": {
9+
"recommended": true,
10+
"complexity": {
11+
"noForEach": "off"
12+
},
13+
"suspicious": {
14+
"noConsole": "off"
15+
},
16+
"style": {
17+
"noParameterAssign": "off"
18+
}
19+
}
20+
},
21+
"formatter": {
22+
"enabled": true,
23+
"indentStyle": "space",
24+
"indentWidth": 2,
25+
"lineWidth": 100
26+
},
27+
"javascript": {
28+
"formatter": {
29+
"quoteStyle": "single",
30+
"semicolons": "always",
31+
"trailingCommas": "all"
32+
}
33+
},
34+
"files": {
35+
"include": ["bin/**/*.js"],
36+
"ignore": ["node_modules", "vendor", ".claude/aios"]
37+
}
38+
}

jsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"checkJs": true,
4+
"strict": false,
5+
"module": "es2022",
6+
"moduleResolution": "node",
7+
"target": "es2022",
8+
"baseUrl": ".",
9+
"paths": {
10+
"#lib/*": ["./bin/lib/*"],
11+
"#utils/*": ["./bin/utils/*"]
12+
}
13+
},
14+
"include": ["bin/**/*.js"],
15+
"exclude": ["node_modules", "vendor"]
16+
}

pyproject.toml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[project]
2+
name = "mega-brain"
3+
version = "1.3.0"
4+
requires-python = ">=3.11"
5+
dependencies = [
6+
"PyYAML>=6.0,<7.0",
7+
]
8+
9+
[project.optional-dependencies]
10+
pipeline = [
11+
"PyYAML>=6.0,<7.0",
12+
]
13+
speakers = [
14+
"pyannote.audio>=3.1.0",
15+
"torch>=2.0.0,<3.0.0",
16+
"torchaudio>=2.0.0,<3.0.0",
17+
"numpy>=1.24.0",
18+
"scipy>=1.10.0",
19+
"assemblyai>=0.28.0",
20+
]
21+
rag = [
22+
"voyageai>=0.3.0",
23+
"rank-bm25>=0.2.2",
24+
]
25+
dev = [
26+
"pytest>=8.0",
27+
"pytest-cov>=5.0",
28+
"ruff>=0.8",
29+
"pyright>=1.1",
30+
]
31+
32+
[tool.ruff]
33+
target-version = "py312"
34+
line-length = 100
35+
src = ["core", ".claude/hooks"]
36+
exclude = [
37+
"core/intelligence/pipeline/read_ai_*.py",
38+
]
39+
40+
[tool.ruff.lint]
41+
select = ["E", "F", "W", "I", "N", "UP", "B", "SIM", "RUF"]
42+
ignore = [
43+
"E501", # line too long — handled by formatter
44+
"UP007", # Use X | Y for union — keep Optional for 3.11 compat
45+
"RUF013", # implicit-optional — existing codebase pattern, fix gradually
46+
"SIM102", # collapsible-if — readability preference
47+
"SIM108", # if-else-block-instead-of-if-exp — readability preference
48+
"SIM110", # reimplemented-builtin — clarity over brevity
49+
"SIM103", # needless-bool — clarity preference
50+
"SIM105", # suppressible-exception — explicit try/except is clearer
51+
"SIM116", # if-else-block-instead-of-dict-lookup — readability
52+
]
53+
54+
[tool.ruff.lint.isort]
55+
known-first-party = ["core"]
56+
57+
[tool.ruff.lint.per-file-ignores]
58+
".claude/hooks/*" = ["E722", "F401", "RUF001"] # hooks: bare except, re-exports, unicode emoji
59+
"core/intelligence/speakers/*" = ["F401"] # conditional imports for optional GPU deps
60+
"core/intelligence/pipeline/read_ai_*" = ["E999"] # special syntax files
61+
"core/intelligence/entities/*" = ["RUF001"] # unicode chars in business domain strings
62+
"core/intelligence/pipeline/autonomous_processor.py" = ["N818"] # legacy exception name
63+
"core/intelligence/rag/evaluator.py" = ["RUF034"] # useless if-else in demo code
64+
65+
[tool.ruff.format]
66+
quote-style = "double"
67+
68+
[tool.pyright]
69+
pythonVersion = "3.11"
70+
typeCheckingMode = "basic"
71+
include = ["core"]
72+
exclude = [
73+
".claude/hooks",
74+
"**/__pycache__",
75+
"core/intelligence/pipeline/read_ai_*.py",
76+
]
77+
78+
[tool.pytest.ini_options]
79+
testpaths = ["tests/python"]
80+
python_files = "test_*.py"
81+
python_functions = "test_*"
82+
addopts = "-v --tb=short"
83+
84+
[tool.coverage.run]
85+
source = ["core/intelligence"]
86+
omit = ["*/__pycache__/*", "*/__init__.py"]
87+
88+
[tool.coverage.report]
89+
fail_under = 40
90+
show_missing = true

reference/CODE-STANDARDS.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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

Comments
 (0)