|
| 1 | +# AGENTS Guide (`mpy-cli`) |
| 2 | + |
| 3 | +This guide is for coding agents working in this repository. |
| 4 | +Follow these commands and conventions unless the user explicitly asks otherwise. |
| 5 | + |
| 6 | +## Scope |
| 7 | +- Applies to the whole repo. |
| 8 | +- Main language: Python. |
| 9 | +- Main function: deploy local code to MicroPython devices via `mpremote`. |
| 10 | + |
| 11 | +## External Agent Rules |
| 12 | +- Cursor rules dir: `.cursor/rules/` -> not present. |
| 13 | +- Cursor single file rules: `.cursorrules` -> not present. |
| 14 | +- Copilot instructions: `.github/copilot-instructions.md` -> not present. |
| 15 | +- If any of these files are added, treat them as higher-priority local instructions. |
| 16 | + |
| 17 | +## Repository Facts |
| 18 | +- Python: `>=3.10` (3.11 recommended). |
| 19 | +- Packaging/config: `pyproject.toml`. |
| 20 | +- CLI entrypoint: `mpy_cli.cli:main`. |
| 21 | +- Console script: `mpy-cli`. |
| 22 | +- Tests: `pytest`, under `tests/`. |
| 23 | +- CI release workflow: `.github/workflows/release.yml`. |
| 24 | + |
| 25 | +## Setup Commands |
| 26 | +Run from repo root: |
| 27 | + |
| 28 | +```bash |
| 29 | +python3 -m venv .venv |
| 30 | +source .venv/bin/activate |
| 31 | +python3 -m pip install --upgrade pip |
| 32 | +python3 -m pip install -e ".[dev]" |
| 33 | +``` |
| 34 | + |
| 35 | +## Build / Lint / Test Commands |
| 36 | +No dedicated formatter/linter is configured in `pyproject.toml`. |
| 37 | +Testing is the primary quality gate. |
| 38 | + |
| 39 | +### Install / package actions |
| 40 | +```bash |
| 41 | +# editable install for development |
| 42 | +python3 -m pip install -e ".[dev]" |
| 43 | + |
| 44 | +# non-editable install |
| 45 | +python3 -m pip install . |
| 46 | +``` |
| 47 | + |
| 48 | +### Run all tests |
| 49 | +```bash |
| 50 | +python3 -m pytest -q |
| 51 | +``` |
| 52 | + |
| 53 | +### Run one test file |
| 54 | +```bash |
| 55 | +python3 -m pytest -q tests/test_cli.py |
| 56 | +``` |
| 57 | + |
| 58 | +### Run one test case (important) |
| 59 | +```bash |
| 60 | +python3 -m pytest -q tests/test_cli.py::test_init_command_creates_config_and_ignore |
| 61 | +``` |
| 62 | + |
| 63 | +### Run by keyword |
| 64 | +```bash |
| 65 | +python3 -m pytest -q -k config |
| 66 | +``` |
| 67 | + |
| 68 | +### Useful targeted suites |
| 69 | +```bash |
| 70 | +python3 -m pytest -q tests/test_docs_and_ci.py |
| 71 | +python3 -m pytest -q tests/test_mpremote_backend.py |
| 72 | +python3 -m pytest -q tests/test_executor.py |
| 73 | +``` |
| 74 | + |
| 75 | +### Optional syntax sanity check |
| 76 | +```bash |
| 77 | +python3 -m compileall mpy_cli |
| 78 | +``` |
| 79 | + |
| 80 | +## CLI Smoke Commands |
| 81 | +```bash |
| 82 | +mpy-cli init |
| 83 | +mpy-cli config |
| 84 | +mpy-cli plan --mode incremental --port /dev/ttyACM0 |
| 85 | +mpy-cli deploy --mode incremental --port /dev/ttyACM0 |
| 86 | +``` |
| 87 | + |
| 88 | +Non-interactive examples: |
| 89 | +```bash |
| 90 | +mpy-cli plan --mode full --no-interactive --port /dev/ttyACM0 |
| 91 | +mpy-cli deploy --mode full --no-interactive --yes --port /dev/ttyACM0 |
| 92 | +``` |
| 93 | + |
| 94 | +## Code Style Guidelines |
| 95 | + |
| 96 | +### Imports |
| 97 | +- Order imports: stdlib -> third-party -> local. |
| 98 | +- Prefer explicit imports; avoid wildcard imports. |
| 99 | + |
| 100 | +### Formatting |
| 101 | +- Use 4-space indentation. |
| 102 | +- Keep long calls split across lines for readability. |
| 103 | +- Keep diffs stable (use trailing commas in multiline literals/calls when helpful). |
| 104 | + |
| 105 | +### Types |
| 106 | +- Add type hints for public functions/methods. |
| 107 | +- Type dataclass fields explicitly. |
| 108 | +- Use `Literal` for constrained string values (`full`, `incremental`). |
| 109 | +- Use `Protocol` for pluggable interfaces (e.g., logger/scanner contracts). |
| 110 | + |
| 111 | +### Naming |
| 112 | +- `snake_case`: functions, vars, modules. |
| 113 | +- `PascalCase`: classes/dataclasses. |
| 114 | +- `UPPER_CASE`: constants. |
| 115 | +- Errors should end with `Error`. |
| 116 | + |
| 117 | +### Docstrings and Comments |
| 118 | +- Follow existing convention: Chinese Doxygen-style docstrings. |
| 119 | +- Public APIs should include `@brief`; add `@param`/`@return` when useful. |
| 120 | +- Add comments only for non-obvious logic. |
| 121 | + |
| 122 | +### Error Handling |
| 123 | +- Raise domain-specific exceptions in lower layers: |
| 124 | + - `ConfigError` |
| 125 | + - `GitDiffError` |
| 126 | + - `CommandExecutionError` |
| 127 | +- At CLI boundaries, convert expected failures to readable messages + exit codes. |
| 128 | +- Avoid exposing raw traceback to end users for expected errors. |
| 129 | + |
| 130 | +### Logging |
| 131 | +- Use `setup_logging()` from `mpy_cli/logging.py`. |
| 132 | +- Keep dual output: console + rotating file logs. |
| 133 | +- Preserve deploy progress logs (per operation / per file). |
| 134 | + |
| 135 | +### Paths and Persistence |
| 136 | +- Prefer `pathlib.Path`. |
| 137 | +- Keep runtime artifacts under project runtime dir (default `.mpy-cli/`). |
| 138 | +- Do not write runtime artifacts to system directories. |
| 139 | + |
| 140 | +### CLI Behavior Rules |
| 141 | +- Keep parser behavior, README examples, and tests synchronized. |
| 142 | +- If CLI args change, update all three: |
| 143 | + 1) `mpy_cli/cli.py` |
| 144 | + 2) `README.md` parameter section |
| 145 | + 3) `tests/test_docs_and_ci.py` |
| 146 | + |
| 147 | +### Deployment Semantics |
| 148 | +- `full`: wipe then upload all allowed files. |
| 149 | +- `incremental`: compute operations from git changes. |
| 150 | +- Nested uploads must ensure remote parent directories exist first. |
| 151 | + |
| 152 | +## Test Writing Conventions |
| 153 | +- Test files: `tests/test_*.py`. |
| 154 | +- Test names: `test_<behavior>`. |
| 155 | +- Keep tests behavior-focused and deterministic. |
| 156 | +- Add regression tests for every bug fix. |
| 157 | + |
| 158 | +## Documentation Conventions |
| 159 | +- Keep command examples in `README.md` current. |
| 160 | +- Keep parameter lists complete when CLI flags change. |
| 161 | +- Keep `docs/developer-guide.md` aligned with architecture changes. |
| 162 | + |
| 163 | +## Agent Completion Checklist |
| 164 | +1. Run targeted tests for changed modules. |
| 165 | +2. Run full suite: `python3 -m pytest -q`. |
| 166 | +3. Verify README command examples still match real CLI behavior. |
| 167 | +4. Ensure local artifacts are not accidentally staged. |
0 commit comments