ext-php-rs
AGENTS.md855 starsChanged 10 days agoApache-2.0View on GitHub
These rules come first, before every other rule here: 1. Fix the root cause. Find why the bug happens and fix that, not the symptom or the one input that showed it. If the same mistake exists elsewhere, fix it there too. 2. Think through every way the change could cause bugs, in edge cases and ordinary ones: other callers of the changed code, every PHP version from 8.1 to 8.6, ZTS and NTS, debug and release PHP, Linux, macOS…
- Commits and pushes
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 35,266 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository6 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- php-bindingsSkill
- pr-descriptionSkill
- proc-macrosSkill
- semver-breaking-changesSkill
- zend-memorySkill
The file
Show its rules and text
We split this AGENTS.md into the rules it gives an agent
28 rules. About 0 are common, 13 are distinctive, and 15 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈13
Distinctive · estimate
This author's own choices. Worth a look.
15
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 26
Never edit CHANGELOG.md or the crate versions in Cargo.toml by hand: release-plz owns them (its "chore: release" PR).
- Very specific: line 30
Read a failed job's full log with gh api repos/extphprs/ext-php-rs/actions/jobs/<id>/logs: gh run view --log can cut it.
- Very specific: line 33
Description: follow skill pr-description.
- Very specific: line 35
Breaking change: follow skill semver-breaking-changes (!
- Distinctive: line 40
Do fixes, reviews and PR work in the main session.
- Distinctive: line 44
Never use the Workflow tool unless explicitly asked for a workflow.
- Distinctive: line 51
Never hide an issue instead of fixing it:
- Very specific: line 57
no entries in .lsan-suppressions.txt for a real leak, no USE_ZEND_ALLOC/ASan options that make a report go away.
- Very specific: line 59
no overwriting an .expanded.rs, .stderr or insta .snap snapshot so it matches wrong output: read the diff first, and only accept output …
- Very specific: line 61
no hand edits to docsrs_bindings.rs, and no new extern "C" block or hard-coded struct offset to reach a Zend symbol that …
- Very specific: line 63
no disabling a mago rule, a typos word or a lefthook job, and no --no-verify.
- Very specific: line 64
no pinning a CI runner image (ubuntu-24.04) to dodge a break on ubuntu-latest: fix the step, and try it in docker run ubuntu:<version> …
- Distinctive: line 66
no changing fixtures or PHP test scripts to dodge behaviour.
- Distinctive: line 91
Tests must assert the change: Every test a PR adds or changes for the bug or feature itself must fail on the base branch and pass on the …
- Distinctive: line 148
don't restate the code in prose.
- Distinctive: line 149
don't mention approaches that were tried, considered, replaced or removed, or anything that only makes sense in the context of an agent's …
- Distinctive: line 151
prefer deleting an unnecessary comment over expanding or rewording it.
- Distinctive: line 152
document an idea once, where it naturally belongs.
- Very specific: line 153
Every unsafe block or unsafe fn says why it is sound (// SAFETY: / # Safety), in terms of the Zend invariant it relies on, with a php-src …
- Distinctive: line 163
Check each result on the branch before writing it.
- Distinctive: line 164
Write that prose in simple English: sentences of 25 words or fewer, active voice, simple tenses, one word for one meaning, no semicolons.
- Distinctive: line 170
If you update functionality, update the guide in the same PR.
- Very specific: line 189
Write commands that work in both zsh (macOS default) and bash (usual on Linux).
- Very specific: line 205
All worktrees of this repository share one stash list.
- Very specific: line 208
Never a reverse sed: it also rewrites other lines that match.
- Very specific: line 209
Put scratch worktrees and scratch crates under /tmp (on macOS it is /private/tmp) or the session scratchpad.
- Distinctive: line 218
When you get an issue, or someone else's PR, check main first:
- Very specific: line 223
Look for work in progress: gh pr list --repo extphprs/ext-php-rs --state all --search "<keywords>", and the PRs linked from the issue.
≈ “Common” is an estimate: at least 1 in 5 of the 45,344 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
ext-php-rs: how to work in this repository
Fixing bugs and writing features
These rules come first, before every other rule here:
- Fix the root cause. Find why the bug happens and fix that, not the symptom or the one input that showed it. If the same mistake exists elsewhere, fix it there too.
- Think through every way the change could cause bugs, in edge cases and ordinary
ones: other callers of the changed code, every PHP version from 8.1 to 8.6, ZTS and
NTS, debug and release PHP, Linux, macOS and Windows (
vectorcall), the cdylib and theembedpaths, module restarts, feature combinations (closure,anyhow,observer,enum, …), weak and strict mode, PHP subclasses of a Rust class, exceptions and zend bailouts in the middle of the call. Check each one with a test. - Add an explicit test case for each of those cases, not only for the reported bug.
Pull requests
- Push the branch to your fork, then
gh pr create --repo extphprs/ext-php-rs --base main. - PRs are squash-merged, and release-plz builds
CHANGELOG.mdand the version bump from the squashed commit. So the PR title is a conventional commit (fix(array): …,feat(class)!: …,perf(macro): …), with a scope from the existing history (git log --format=%s origin/main | head -50). - CI also runs
convco check origin/main..HEAD, so every commit on the branch must be a conventional commit too, fixups included. - Never edit
CHANGELOG.mdor the crate versions inCargo.tomlby hand: release-plz owns them (its "chore: release" PR). - Then poll
gh pr checks <n> --repo extphprs/ext-php-rs. Forgh run list,--commit <sha>is reliable;--branchsometimes returns stale runs. - Read a failed job's full log with
gh api repos/extphprs/ext-php-rs/actions/jobs/<id>/logs:gh run view --logcan cut it. - Description: follow skill
pr-description. The repository's PR template (Description + Checklist) stays. - Breaking change: follow skill
semver-breaking-changes(!in the title, a migration guide inguide/src/migration-guides/).
Agents and subagents
- Do fixes, reviews and PR work in the main session. Spawn a subagent only when asked to or for a genuinely independent, bounded task, and say so first.
- To review several PRs, use one subagent with an ordered target list (most urgent first, saying which to drop if time runs short), not one subagent per PR.
- Never use the Workflow tool unless explicitly asked for a workflow.
- Parallel agents building this workspace contend for CPU and for the
target/lock: onecargoper checkout at a time, and tell subagents the testing rules above explicitly.
No workarounds
Never hide an issue instead of fixing it:
- no
#[allow(...)]/#[expect(...)]to silence clippy pedantic, rustdoc ormissing_docsunless the lint is wrong for that code, and then with a comment that says why; - no
#[ignore],#[cfg]or feature gate to switch off a failing test, and no deleting an assertion that fails; - no entries in
.lsan-suppressions.txtfor a real leak, noUSE_ZEND_ALLOC/ASan options that make a report go away; - no overwriting an
.expanded.rs,.stderror insta.snapsnapshot so it matches wrong output: read the diff first, and only accept output that is correct; - no hand edits to
docsrs_bindings.rs, and no newextern "C"block or hard-coded struct offset to reach a Zend symbol thatallowed_bindings.rsshould list; - no disabling a mago rule, a typos word or a lefthook job, and no
--no-verify; - no pinning a CI runner image (
ubuntu-24.04) to dodge a break onubuntu-latest: fix the step, and try it indocker run ubuntu:<version>first; - no changing fixtures or PHP test scripts to dodge behaviour.
Fix the root cause in the code, in the bindings, or upstream. If a proper fix is out of reach, say so and ask. List any workaround explicitly in the report.
Building
- PHP is found through
$PHP(elsephponPATH) and$PHP_CONFIG. bindgen needs libclang (LIBCLANG_PATH).build.rsemits thephp8x,php_ztsandphp_debugcfgs from the PHP it finds (skillphp-bindings). - The PHP version is baked into the build.
build.rsreruns whenPHP,PHP_CONFIGorPATHchange, but not when the binary behind the same path changes (brew link php@8.3, a rebuilt PHP): then runcargo clean -p ext-php-rs, or the old bindings stay. - docs.rs builds without PHP from the checked-in
docsrs_bindings.rs. CI checks this path withDOCS_RS= cargo buildaftercargo clean. - Rust version: the crate uses edition 2024. Windows needs nightly
(
abi_vectorcall), see the#![cfg_attr(windows, feature(abi_vectorcall))]lines. - CI's clippy job uses the newest stable toolchain. A nix shell's clippy can be older
and miss new lints, and
rustup run stablein the shell still runs the shell'scargo-clippy. Put$(rustup run stable rustc --print sysroot)/binfirst onPATHand checkcargo clippy --version.
Tests
- Tests must assert the change. Every test a PR adds or changes for the bug or
feature itself must fail on the base branch and pass on the PR, because it checks
the exact behaviour being fixed or added. Prove it by running that single test on
both (see above). Edge-case tests that guard the change may pass on the base. Ways a
test silently asserts nothing:
- a
.phpscript that no#[test]runs: each integration module needs itspub modintests/src/integration/mod.rs, itsbuild_modulecall intests/src/lib.rs, and a#[test]in itsmod.rsthat callsrun_php("<dir>/<file>.php"); - a test behind
#[cfg(feature = "embed")],#[cfg(php84)]or another cfg that is off in the run you used as proof (check the test appears in therunning N testslist); run_phponly checks the exit status. A warning, a deprecation or a leak report on stderr does not fail it: userun_php_capturing_stderrand assert on the output (seetests/src/integration/panic/mod.rs);- an
assert()in PHP only fires withzend.assertions=1andassert.exception=1:run_phppasses both, a manualphpcommand must too; var_dump/echoin a test script asserts nothing: compare with===in anassert(), or check the exception class and message in atry/catch;- a Rust-side function the script never calls (a typo in the PHP name only fails at runtime, so the script must reach the call).
- a
- Integration tests run the extension in a real
phpprocess: one directory per area undertests/src/integration/with amod.rs(the Rust items and tests) and one or more.phpscripts._utils.phphas the shared helpers (assert_type_error,assert_exception_thrown, …). A few tests also have an_embedtwin that runs the same script throughEmbed(run_php_embed). - The harness loads the extension from
<repo>/target/{debug,release}, derived from the current directory. Don't setCARGO_TARGET_DIRfor integration tests: the build goes there andphploads a stale or missing library fromtarget/. - Class, enum and INI registration only run at MINIT, which never runs inside
Embed's request: test them through the cdylib (run_php), not only through embed. Embed::runreturnsR::default()when PHP bails out, and the rest of the closure doesn't run, so assertions inside it are skipped. Return the value out of the closure and assert outside it.Embed::evalcompilesreturn <code>;: a statement list is a parse error or stops after the first statement. Wrap statements in an IIFE ((function () { echo 'x'; header('X: 1'); })()), and load class declarations from a fixture withEmbed::run_script.- A lib unit test that creates or drops a
Zvalneeds#[cfg(feature = "embed")]: dropping aZvallinkszval_ptr_dtorand_efree, and withoutembedlibphp isn't linked (rust-lld fails on Linux and musl, macOS hides it). This is a link requirement, not a way to skip a test. Before pushing new lib tests, run CI'scargo test --release --workspace --features closure,anyhow,runtime,observer. - New macro behaviour gets an expansion test in
crates/macros/tests/expand/and, for every new error, a trybuild case intests/ui/(skillproc-macros). - Guide code blocks are compiled by skeptic (
guide_tests):rust,no_runblocks must compile,rust,ignoreblocks are not checked. - PHP test scripts are formatted and linted with mago (
mago.toml). Some rules are off on purpose there; read the reason before you write around one. Run it as CI does, on the whole tree and with no path arguments (a path argument skips files and hides warnings):mago format --check && mago lint --minimum-fail-level=warning, with the version pinned inflake.nixandbuild.yml(the nix shell'smago).
Code style
- Match the surrounding code: comment density, naming, idiom.
- Comments are concise and only explain what isn't obvious from the code:
- don't restate the code in prose;
- don't mention approaches that were tried, considered, replaced or removed, or anything that only makes sense in the context of an agent's iteration history;
- prefer deleting an unnecessary comment over expanding or rewording it;
- document an idea once, where it naturally belongs.
- Every
unsafeblock orunsafe fnsays why it is sound (// SAFETY:/# Safety), in terms of the Zend invariant it relies on, with a php-src link pinned to a commit when the invariant isn't obvious. - Public items need doc comments (
missing_docsis on). Rustdoc must build with-D warnings. rustfmt.tomlsetswrap_comments, socargo fmtcan rewrap comments in files you didn't edit. Checkgit diff --statand restore those files before committing.- Documentation (
guide/, rustdoc): more examples, less text. Show behaviour with a Rust declaration and the PHP that uses it, whose lines end in their result (// int(3),// TypeError,// no error), and keep the prose around it short. Check each result on the branch before writing it. Write that prose in simple English: sentences of 25 words or fewer, active voice, simple tenses, one word for one meaning, no semicolons. - The macro docs in
crates/macros/src/lib.rsare a copy ofguide/src/macros/*.mdbetween// BEGIN DOCS FROM/// END DOCS FROMmarkers. Edit the guide, then runtools/update_lib_docs.sh(needs the nightly toolchain for rustfmt). Never edit the copy. - If you update functionality, update the guide in the same PR.
Debugging pitfalls
- A crash in the extension shows up as a killed
php(the harness printsstatus: signal: 11 (SIGSEGV), or SIGABRT for a failed Zend assertion; a shell shows exit 139 or 134) with little output. Rerun the script withphp -n -dextension=<lib>so no host extension orphp.inigets involved, then underlldb -- php …(macOS) orgdb --args php …(Linux). - Release PHP builds compile Zend assertions out. A bug that "only" corrupts memory on
a release build often fails a Zend assertion on a debug build: reproduce on debug PHP
(CI's
setup-phpusesdebug: true; nix.#debug; skillzend-memory). - Zend's allocator hides use-after-free and leaks from ASan and valgrind. Run with
USE_ZEND_ALLOC=0(skillzend-memory). Embedandphpbehave differently: Embed shares one process across tests, so state leaks from one test into the next, and MINIT ran before the test started.
Shell, worktrees and git
- Write commands that work in both zsh (macOS default) and bash (usual on Linux); check
with
echo $0if unsure:- Quote globs meant for the tool, not the shell (
grep --include='*.rs'). In zsh an unmatched glob aborts the whole&&chain ("no matches found"); in bash it is passed on literally, so the command quietly acts on a path that doesn't exist. Either way the files you expect may not be there.lsrepro files before concluding anything from a missing result. - Brace variables next to a colon:
"${b}:branch","${r}:src/types/zval.rs". In zsh$b:l,$r:t, … are history modifiers and mangle refspecs; braces are harmless in bash. - Word-splitting a variable of args: zsh needs
${=args}, bash splits unquoted$args. Prefer an array (args=(a b); cmd "${args[@]}"), which behaves the same in both.
- Quote globs meant for the tool, not the shell (
- The working directory resets between Bash calls. Start every command that edits,
commits, builds or tests in a worktree with
cd <worktree> &&(or use absolute paths), and checkgit statusin the intended worktree before committing. - All worktrees of this repository share one stash list. Never use
git stashthere, and never pop or drop entries you didn't create. To compare with and without a change, usegit diff > f.patch+git apply -R, or a second worktree. Never a reversesed: it also rewrites other lines that match. - Put scratch worktrees and scratch crates under
/tmp(on macOS it is/private/tmp) or the session scratchpad./tmpmay be wiped on reboot, so don't keep anything there that must survive. - Each worktree has its own
target/, so its first build compiles everything again (bindgen included). Don't sharetarget/between worktrees throughCARGO_TARGET_DIRwhen you run integration tests (see Tests).
Issues
When you get an issue, or someone else's PR, check main first:
- Reproduce it on an up-to-date
main(git fetch origin, then a worktree oforigin/main). For a PR, run its new tests there: if they pass onmain, the bug is already fixed. - Look for work in progress:
gh pr list --repo extphprs/ext-php-rs --state all --search "<keywords>", and the PRs linked from the issue.
Report what you find before you write any code.
Avoid opening GitHub issues that do not provide a linked fix: always open a linked PR with a fix for the issue, unless it's an issue related to a big architectural question or any other major change requiring further discussion before implementation, or any other non-directly-actionable issue.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

