agentleFS
FranciscoThiesenFranciscoThiesen/mirror_bridge/AGENTS.md

mirror_bridge

AGENTS.md89 starsChanged 11 months agoApache-2.0View on GitHub

mirror_bridge is a C++26 reflection-based binding generator: it inspects C++ structs/classes at compile time (via P2996 reflection) and emits zero-overhead Python, Lua, JavaScript and C# bindings — no hand-written binding code. The one critical constraint: it requires a C++26 reflection compiler — either stock GCC 16+ (g++ -std=c++26 -freflection) or Bloomberg's clang-p2996 (with libc++ and the <meta> header). A pre-reflection distro compiler will fail with "no member named 'meta'" or an unknown -freflection flag. Unless your host already has GCC…

  • 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 32,671 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 repository2 other files it gives its agents.

The file

We split this AGENTS.md into the rules it gives an agent

7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If a binary can't find libc++ at runtime, set LD_LIBRARY_PATH=/usr/local/lib (see failure modes).

    line 45
  2. Very specific:

    Read source in that JSON before anything else:

    line 87
  3. Very specific:

    Read it rather than guessing at the surface: the signatures come from the same reflection the binding does, so the parameter names are the …

    line 106
  4. Very specific:

    Authorship: NEVER add yourself (the agent) as a commit author or Co-Authored-By trailer.

    line 235
  5. Distinctive:

    Commit cadence: commit every meaningful, self-contained set of changes.

    line 237
  6. Distinctive:

    Validate locally: test changes inside the container before relying on CI — keeps the loop tight.

    line 238
  7. Distinctive:

    Comments: do NOT add comments that restate self-documenting code or merely repeat a function name.

    line 239

≈ “Common” is an estimate: at least 1 in 5 of the 41,581 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

AGENTS.md

mirror_bridge is a C++26 reflection-based binding generator: it inspects C++ structs/classes at compile time (via P2996 reflection) and emits zero-overhead Python, Lua, JavaScript and C# bindings — no hand-written binding code. The one critical constraint: it requires a C++26 reflection compiler — either stock GCC 16+ (g++ -std=c++26 -freflection) or Bloomberg's clang-p2996 (with libc++ and the <meta> header). A pre-reflection distro compiler will fail with "no member named 'meta'" or an unknown -freflection flag. Unless your host already has GCC 16+ or clang-p2996, compile inside the project's Docker container (or a GitHub Codespace). The CLI auto-detects whichever compiler is present; override with MB_CXX. Editing, searching, and reading code on the host is always fine.

Note: P3394 field annotations ([[=exclude{}]], [[=readonly{}]]) work under both compilers, in the Python backend. The Lua and JavaScript backends do not read them yet: they enumerate every named member, so an annotated field is still bound there.

Environment setup

The reflection compiler lives in the Docker image. Prefer pulling the pre-built image over building from source (~45–90 min).

# Pull + tag the pre-built image (interactive helper, choose option 1)
./start_dev_container.sh

# Or pull directly
docker pull ghcr.io/franciscothiesen/mirror_bridge:latest
docker tag  ghcr.io/franciscothiesen/mirror_bridge:latest mirror_bridge:latest

start_dev_container.sh creates a persistent container named mirror_bridge_dev with the repo mounted at /workspace (host edits are immediately visible inside, and vice versa).

To run a command inside the container non-interactively (scriptable for an agent):

# One-off, throwaway container with the repo mounted:
docker run --rm -v "$(pwd):/workspace" -w /workspace mirror_bridge:latest \
    bash -lc './tests/run_all_tests.sh'

# Or exec into the long-lived dev container if it's already running:
docker exec -w /workspace mirror_bridge_dev bash -lc 'tools/mirror_bridge version'

If a binary can't find libc++ at runtime, set LD_LIBRARY_PATH=/usr/local/lib (see failure modes).

Common tasks

The CLI is the bash script tools/mirror_bridge (run it as tools/mirror_bridge or, from inside an example dir, ../../tools/mirror_bridge). All commands below assume you are inside the container.

Generate bindings from headers (auto-discovery):

# Single language
tools/mirror_bridge generate src/ --module my_module --lang python

# Choices for --lang: python | lua | js | csharp | all
# (csharp is NOT in `all`: it writes a .cs source file plus the shared library
#  that file loads, not a module the CLI can import and verify)
#
# For csharp, read <module>.cs the way you would read a .pyi: it IS the typed
# surface, it carries the real C++ parameter names, and anything that could not
# cross is a comment at the point the member would have been -- so a missing
# member can be told from an absent one without rerunning anything.
tools/mirror_bridge generate src/ --module my_module --lang all

# Useful flags: -o/--output DIR (default build/), -k/--keep-generated,
# -v/--verbose, -f/--force, -I DIR (extra include dir), --pch [PATH],
# --batch, --no-stubs, --link-args, --json
tools/mirror_bridge generate src/ --module my_module --lang python -I include/ -v

Before binding an unfamiliar or large codebase, do these two things first. They turn a long guessing loop into one pass, and they are the flags most worth knowing about:

# 1. Take the include paths and defines from the project's own build system
#    rather than rediscovering them as -I/-D by trial and error.
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON   # or: bear -- make
# 2. Ask what reflection can see, without generating or compiling anything.
tools/mirror_bridge generate include/mylib --module mylib --lang python \
    --compile-commands build/compile_commands.json --discover-only --json

Read source in that JSON before anything else:

  • "source": "reflection" -- the headers were read after the preprocessor and the class list is real. Proceed.
  • "source": "text scan" -- the reflection unit failed and every name came from reading header text, which cannot see class MYLIB_API Widget or resolve a typedef. Do not generate a module from this; it would bind the wrong names. The output names a planner log that says why it failed, and the usual cause is a missing include path or define, which is what --compile-commands is for.

Related flags: --compile-commands-for PATH scopes the database to one subtree (a monorepo's union of every subsystem's flags is self-contradictory), and --replace-prefix OLD=NEW maps a database written on another machine onto this one. MSVC and clang-cl spellings (/I, /D, /external:I, /FI) are translated, and the project's own -std= is dropped because reflection needs C++26.

A <module>.pyi is written beside the module unless you pass --no-stubs. Read it rather than guessing at the surface: the signatures come from the same reflection the binding does, so the parameter names are the real C++ ones and the types are what actually crosses -- a method returning a numeric vector gives an array.array, not a list, and the stub says so.

Reading a std::vector data member gives a live view of the C++ vector, not a copy, so obj.points.append(p) reaches C++ and reading the member is O(1) whatever its length. It is a collections.abc.MutableSequence; call list(...) on it when you need a snapshot or something that insists on a real list, such as json.dumps. A numeric one exports the buffer protocol, so numpy.asarray(obj.xs) is zero-copy.

--batch generates a <name>_batch sibling for every method taking and returning one arithmetic value, so a Python loop crosses the boundary once instead of once per element. Measured at 6.8x from a list and 12.9x from an array.array over 100,000 elements, and 1.0x when the method body is the expensive part -- it amortises the boundary, not the work. Off by default because each sibling costs compile time.

--json puts one object on stdout: the classes found, the outputs built, and on failure a per-language reason, suggestion and generated_binding -- the last being the generated .cpp the compiler's line numbers refer to, which is kept when a build fails.

generate scans every .hpp/.h under src_dir, binds all discovered struct/class definitions, emits a <module>_<lang>_binding.cpp, compiles it, and (unless -k) deletes the generated .cpp.

Build a precompiled header (3–6x faster builds):

tools/mirror_bridge pch --output build/ --type release   # or --type debug
tools/mirror_bridge generate src/ --module my_module --lang python --pch   # auto-detects build/*.gch

Build a hand-written binding file:

tools/mirror_bridge build my_bindings.cpp --lang python -I src/ -o build/

Run the full test suite:

./tests/run_all_tests.sh

It builds every *.cpp binding under tests/ and runs all test_*.py, test_*.js, test_*.lua, and test_*.sh. V8 tests are non-blocking. The script sets LD_LIBRARY_PATH, PYTHONPATH, and LUA_CPATH for you.

Run a single test (no per-test target exists; do it manually inside the container):

# 1. Build the binding the test imports (Python example):
clang++ -std=c++2c -freflection -freflection-latest -stdlib=libc++ \
    -I. -Itests/<dir> -fPIC -shared $(python3-config --includes --ldflags) \
    tests/<dir>/<module>_binding.cpp -o build/<module>.so
# 2. Run it:
LD_LIBRARY_PATH=/usr/local/lib PYTHONPATH=build python3 tests/<dir>/test_<name>.py

Regenerate after header changes / inspect the binding surface:

tools/mirror_bridge generate src/ --module my_module --lang python -f   # force rebuild
tools/mirror_bridge diff src/                 # show added/removed members vs. last snapshot
tools/mirror_bridge diff src/ --update        # update snapshot non-interactively (CI-friendly)
tools/mirror_bridge watch src/ --module my_module --lang python   # poll + auto-recompile

One-off compile of a binding (the canonical command the CLI runs under the hood):

# Python:
clang++ -std=c++2c -freflection -freflection-latest -stdlib=libc++ \
    -I. -I<src_dir> -I<project_root> -fPIC -shared -O3 -DNDEBUG \
    -Wno-deprecated-declarations $(python3-config --includes) \
    binding.cpp -o build/my_module.so $(python3-config --ldflags)

# Lua:  add  -I/usr/include/lua5.4 -llua5.4   and output .so
# JS:   add  -I/usr/include/node               and output .node

The four flags that must always be present: -std=c++2c -freflection -freflection-latest -stdlib=libc++. Include mirror_bridge.hpp (Python), lua/mirror_bridge_lua.hpp, or javascript/mirror_bridge_javascript.hpp from the binding source.

Controlling what gets bound

Auto-discovery binds every struct/class found in the scanned headers by default. Opt out via:

  • // MIRROR_BRIDGE_SKIP on the line immediately before a class → skips that class.
  • // MIRROR_BRIDGE_SKIP_FILE anywhere in a header → skips the whole file.
  • P3394 field annotations (clang-p2996 needs -freflection-latest; read by the Python backend only):
#include "python/mirror_bridge_annotations.hpp"
using mirror_bridge::exclude;
using mirror_bridge::readonly;

struct UserProfile {
    int user_id;                               // bound read/write
    [[=exclude{}]]  std::string password_hash; // not bound at all
    [[=readonly{}]] std::string created_at;    // bound read-only
};

Template classes are never auto-bound (the discovery parser skips them); instantiate them with a using alias first, then bind the alias.

When things fail

  • static_assert failed: "<T> contains members with types that mirror_bridge cannot convert" → a member has an unsupported type. Mark it [[=exclude{}]], or add a custom converter. Supported: arithmetic, std::string/string_view/const char*, containers, smart pointers, std::optional, std::expected, enums, nested bindable classes.
  • no member named 'meta' in namespace 'std' / unknown -freflection* → wrong compiler. You're on the host, not in the container. Use the Docker image.
  • libc++.so.1: cannot open shared object file → run with LD_LIBRARY_PATH=/usr/local/lib (the test runner also uses the arch-specific /usr/local/lib/<arch>-unknown-linux-gnu paths).
  • libstdc++ ABI symbols (std::__cxx11::*) unresolved when linking/loading against an external C++ library built with libstdc++ → preload it: LD_PRELOAD=libstdc++.so.6 python3 your_test.py. libc++ (std::__1::) and libstdc++ (std::__cxx11::) use distinct symbol namespaces and coexist in one process. See examples/open3d-runtime/build_and_test.sh.

Repo conventions

From CLAUDE.md — follow these exactly:

  • Authorship: NEVER add yourself (the agent) as a commit author or Co-Authored-By trailer. You are assisting, not authoring.
  • Commit cadence: commit every meaningful, self-contained set of changes.
  • Validate locally: test changes inside the container before relying on CI — keeps the loop tight.
  • Comments: do NOT add comments that restate self-documenting code or merely repeat a function name. DO add insightful comments for non-obvious / convoluted logic.
  • Style/philosophy: C++26, concept-driven design (see the Arithmetic/StringLike/Container/ SmartPointer/EnumType/Bindable/NestedBindable concepts in core/mirror_bridge_core.hpp), simplicity over cleverness, zero runtime overhead (all binding logic resolved at compile time).
  • License: Apache 2.0.

Repo map

PathContents
core/Language-agnostic reflection core (mirror_bridge_core.hpp): type concepts, validation, registry.
python/Python C-API binding headers: mirror_bridge_python.hpp, annotations, options, stubgen, buffer, eigen.
lua/Lua 5.4 C-API binding headers (mirror_bridge_lua.hpp, PCH header).
javascript/Node N-API + embedded V8 binding headers (mirror_bridge_javascript.hpp, mirror_bridge_v8.hpp).
rust/Rust codegen header (mirror_bridge_rust.hpp), beta.
single_header/Amalgamated single-header builds of each language backend.
tools/The mirror_bridge CLI (bash). The only user-facing entry point.
tests/Test suite; run_all_tests.sh is the entry point. Per-language subdirs + shell CLI tests.
examples/Progressive examples 01-hello-world … 11-expected, plus open3d-* real-world ports.
docs/Onboarding (getting-started/), guides/, reference/ (cli, type-conversion), internals/.
benchmarks/Performance + compile-time benchmarks vs. pybind11/nanobind/swig.
scripts/Python helpers: discover_symbols.py, generate_stubs.py.
mirror_bridge.hppTop-level umbrella header for Python bindings (included by generated code).
mirror_bridge_pch.hppHeader compiled into the precompiled header by mirror_bridge pch.
start_dev_container.shPull/build image, create/attach the persistent mirror_bridge_dev container.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.