|
| 1 | +# Agents |
| 2 | + |
| 3 | +Instructions for AI coding agents working on erlang_python. Read this file |
| 4 | +before you change anything; it applies to every agent and every change. The |
| 5 | +same rules, written for people, are in [CONTRIBUTING.md](CONTRIBUTING.md). |
| 6 | + |
| 7 | +## Project Overview |
| 8 | + |
| 9 | +erlang_python embeds CPython in the Erlang VM through a NIF: Erlang processes |
| 10 | +call Python, and Python calls back into Erlang. Python runs in contexts: |
| 11 | +`worker` (a thread per context on the main interpreter), `owngil` (a thread |
| 12 | +and a subinterpreter with its own GIL, Python 3.14 and later) and `isolated` |
| 13 | +(a child OS process). Isolated sessions give each session a fresh child |
| 14 | +started from a prepared template. Requires Erlang/OTP 27 or later, Python |
| 15 | +3.12 or later with headers, CMake and a C compiler. |
| 16 | + |
| 17 | +## First principle: a NIF bug takes down the node |
| 18 | + |
| 19 | +The C code and the Erlang code around each NIF call run inside the VM. A |
| 20 | +crash, a leak or a deadlock there belongs to the whole node, not to one |
| 21 | +process. Every change to `c_src`, to `priv/_erlang_impl` or to the Erlang |
| 22 | +side of a NIF call must hold three things before it is called done, |
| 23 | +committed or opened as a pull request: |
| 24 | + |
| 25 | +1. **The whole defect class is fixed.** A finding, yours or a reviewer's, |
| 26 | + names examples. Grep for every other site of the same pattern (each |
| 27 | + `enif_binary_to_term`, each `Py_BuildValue` with an `O` argument, each |
| 28 | + raw resource cast) and fix or clear each one. Counts and line numbers |
| 29 | + quoted in a report drift: check them against the current tree. |
| 30 | +2. **The mechanism is right, not just plausible.** Say which thread owns |
| 31 | + the object, which locks are held and in what order, whether the call can |
| 32 | + block and whether it holds the GIL while it does, and whether escaping |
| 33 | + even applies at that position. The rules are in |
| 34 | + [c_src/README.md](c_src/README.md#rules-that-are-easy-to-break). |
| 35 | +3. **A test proves it.** The test fails on main without the change. A leak |
| 36 | + gets a test that measures memory; a race gets a test that forces the |
| 37 | + interleaving instead of waiting on timing. |
| 38 | + |
| 39 | +## Python code is not a security boundary |
| 40 | + |
| 41 | +Python code that erlang_python runs holds every authority of the user |
| 42 | +running the node. The audit-hook sandbox keeps Python from forking the VM, |
| 43 | +and isolated mode bounds what a child may consume and contains its crashes; |
| 44 | +neither confines what Python may reach (see |
| 45 | +[docs/security.md](docs/security.md) and the limits in |
| 46 | +[docs/isolated.md](docs/isolated.md#limits)). |
| 47 | + |
| 48 | +So anything Python code can do is a bug at most, never a vulnerability: |
| 49 | +getting past the audit hook, an isolated child or a session reading files, |
| 50 | +the network or the environment, a limit (rlimits, cgroups, `clear_env`) |
| 51 | +that was not applied, a C extension crashing the node, atom exhaustion from |
| 52 | +atoms Python sends. Treat it as a bug: |
| 53 | + |
| 54 | +- fix it in a public pull request, with a public issue if one is needed; |
| 55 | +- list it under **Fixed** in the CHANGELOG, in plain English; |
| 56 | +- do not open a GitHub Security Advisory, request a CVE, embargo the fix, |
| 57 | + give it a severity, or describe how to attack it; |
| 58 | +- when it arrived as an advisory, close the advisory as not a vulnerability |
| 59 | + and link the pull request that fixes it. |
| 60 | + |
| 61 | +Only a problem that gives something to someone who cannot already run code |
| 62 | +on the node goes through [SECURITY.md](SECURITY.md): memory corruption or |
| 63 | +code execution in the VM from data, not code, that passes through the |
| 64 | +library (terms, bytes in channels, buffers and shared memory), or the |
| 65 | +library loading or running code from a place the configuration did not |
| 66 | +name. If unsure, ask the maintainer before treating anything as a security |
| 67 | +issue. |
| 68 | + |
| 69 | +## Required Checks |
| 70 | + |
| 71 | +Every change must be formatted and pass all checks before committing. This |
| 72 | +list matches what CI gates on; see [CONTRIBUTING.md](CONTRIBUTING.md) for |
| 73 | +the rest of the workflow. |
| 74 | + |
| 75 | +```bash |
| 76 | +rebar3 fmt # Auto-format (always run first) |
| 77 | +rebar3 compile # Builds priv/py_nif.so through CMake |
| 78 | +rebar3 ct --readable=compact # Common Test suites |
| 79 | +rebar3 fmt --check # CI fails on unformatted code |
| 80 | +rebar3 lint # Elvis linter must pass |
| 81 | +rebar3 xref # Cross-reference analysis must pass |
| 82 | +rebar3 dialyzer # Type checking must pass |
| 83 | +make lint-docs # Snippets in README.md and docs/ must be valid |
| 84 | +make check-code-map # Every module and suite has its row |
| 85 | +rebar3 ex_doc # Docs must build |
| 86 | +``` |
| 87 | + |
| 88 | +`rebar3 compile` can succeed without building anything. CMake caches the |
| 89 | +interpreter and the build tool in `_build/cmake`; when either moves, |
| 90 | +compile prints a CMake error, exits 0 and keeps the old `py_nif.so`. After |
| 91 | +a toolchain or Python change, run `rm -rf _build/cmake` before compiling, |
| 92 | +pick the interpreter with `PYTHON_CONFIG=...`, and check what the NIF links |
| 93 | +against (`otool -L` on macOS, `ldd` on Linux). |
| 94 | + |
| 95 | +## Tests |
| 96 | + |
| 97 | +- Every behaviour change comes with a test in the same change that pins |
| 98 | + it, and the test fails on main. A manual smoke check is not a test. |
| 99 | +- Prefer Common Test cases that drive Python through `py:exec` and |
| 100 | + `py:eval` over ad hoc scripts. Gate a version-specific case on the |
| 101 | + runtime and name it after the gate. |
| 102 | +- A feature has three kinds of tests: unit cases, a profile (a bench |
| 103 | + escript in `examples/` with numbers before and after), and a stress |
| 104 | + suite gated by `STRESS=1`, run once under ASan. |
| 105 | +- A test fails for one reason. A case that measures time or memory prints |
| 106 | + its numbers with `ct:pal` and asserts only on invariants. |
| 107 | + |
| 108 | +## Plans |
| 109 | + |
| 110 | +Read the files a change touches before you change them. For a large |
| 111 | +refactor, or a change across many modules, propose a short plan and wait |
| 112 | +for the maintainer before you start. |
| 113 | + |
| 114 | +- Lead with what each piece gives the user, in plain English, and ASCII |
| 115 | + call paths (open, call, nested call, close, failure). Files and commits |
| 116 | + come after. |
| 117 | +- A plan that adds a primitive also covers the code that uses it (for |
| 118 | + example `py_buffer` on top of `py_shm`). Stage it inside the plan; do not |
| 119 | + leave it for a follow-up. |
| 120 | + |
| 121 | +## Pull Requests |
| 122 | + |
| 123 | +Work on one issue goes in one pull request. Give each change a reviewer has |
| 124 | +to look at its own commit, with its tests, rather than opening a pull |
| 125 | +request per change. Docs and CHANGELOG entries go in the same pull request. |
| 126 | +Pull requests are merged with a merge commit, so the commits stay. |
| 127 | + |
| 128 | +- Commit subject in the imperative, under about 60 characters, saying what |
| 129 | + the code now does. The body is one short paragraph on why and how; no |
| 130 | + list of files. |
| 131 | +- Descriptions state what was wrong and why it matters, the fix in a few |
| 132 | + lines, and breaking changes, as prose. Reference issues with `Fixes #N`. |
| 133 | + No test plan, no test counts, no file-by-file walkthrough, no labelled |
| 134 | + sections. |
| 135 | +- No `Co-Authored-By`, "Generated with" or similar trailers, in commits or |
| 136 | + pull requests. |
| 137 | +- Show the maintainer the full commit message and pull request text before |
| 138 | + you commit, push or open anything, and wait for approval. Ask before |
| 139 | + anything that publishes or cannot be undone: a push, a pull request, a |
| 140 | + merge, a tag, a release, closing an issue. |
| 141 | +- CHANGELOG: under the unreleased version, in `Added`, `Changed`, `Removed` |
| 142 | + or `Fixed`. Plain English, what was wrong or what is new and what it |
| 143 | + means for the user. Public API names are fine; code snippets, internal |
| 144 | + function names and implementation details are not. |
| 145 | +- Follow "Write for the person who reads it" in |
| 146 | + [CONTRIBUTING.md](CONTRIBUTING.md#write-for-the-person-who-reads-it): |
| 147 | + plain English, no inflated wording, never "comprehensive", no em dashes, |
| 148 | + and a person has read the change before it is opened. |
| 149 | + |
| 150 | +## Releases |
| 151 | + |
| 152 | +A release is one commit on `main` that bumps `vsn` in |
| 153 | +`src/erlang_python.app.src` and dates the CHANGELOG section, then an |
| 154 | +annotated tag `vX.Y.Z` on that commit and a GitHub release whose notes are |
| 155 | +that CHANGELOG section as is. The maintainer tags, releases and |
| 156 | +publishes to Hex; do not do any of it unless asked. |
| 157 | + |
| 158 | +Before a release, review the documentation and maintainability of main |
| 159 | +from a reader's point of view (the `docs-maintainability-review` skill, if |
| 160 | +your agent has it) and give the plan to the maintainer. The review changes |
| 161 | +no source code, its documentation work goes on its own branch, and the |
| 162 | +release waits until the maintainer has said which findings to fix first. |
| 163 | + |
| 164 | +## Where Things Are |
| 165 | + |
| 166 | +- [docs/architecture.md](docs/architecture.md): how the pieces fit. |
| 167 | +- [docs/code-map.md](docs/code-map.md): every module, C file and suite, |
| 168 | + with the guide that covers it. |
| 169 | +- [docs/protocols.md](docs/protocols.md) and |
| 170 | + [docs/state-machines.md](docs/state-machines.md): message and frame |
| 171 | + shapes, process states. |
| 172 | +- [docs/decisions/](docs/decisions/overview.md): decision records. A change |
| 173 | + that reverses or extends one adds a new record; the old one is not |
| 174 | + edited. |
| 175 | +- [c_src/README.md](c_src/README.md): the C files and their rules. |
| 176 | +- [test/coverage_audit.md](test/coverage_audit.md): which suite covers each |
| 177 | + documented API. |
| 178 | +- [CONTRIBUTING.md](CONTRIBUTING.md#recipes): what a new NIF, `erlang.*` |
| 179 | + function, context option, suite or guide must touch. |
| 180 | + |
| 181 | +## Formatting and Linting Notes |
| 182 | + |
| 183 | +erlfmt formats `src`, `test`, `examples`, `scripts` and `rebar.config`. |
| 184 | +The C and Python sources have no formatter; follow the style around the |
| 185 | +change. |
| 186 | + |
| 187 | +Elvis rules are configured in `elvis.config`, the only lint configuration. |
| 188 | +The modules that broke a rule when the rules were adopted are listed in |
| 189 | +that rule's `ignore`. A new module follows every rule. When you rework a |
| 190 | +module, take it off the lists it no longer needs; never add a module to a |
| 191 | +list to make a change pass. |
0 commit comments