Convert subproject AGENTS.md into skills

This commit is contained in:
topjohnwu
2026-08-26 05:42:55 +00:00
parent b910c7d915
commit 4623f546e3
5 changed files with 115 additions and 67 deletions
+50
View File
@@ -0,0 +1,50 @@
---
name: magisk-app
description: Guidelines, architecture, development conventions, and build workflows for the Magisk Android application (app/ subproject). Use when working on, modifying, building, or testing code in app/ or any of its submodules (:core, :apk-ng, :shared, :stub, :stub-res, :test, :build-logic, :apk).
---
# Magisk App Subproject Guidelines
Guidelines and workflows for developing and building the Android application in the `app/` subproject.
## 1. Environment & Gradle Setup
- **General Guidelines:** Always follow the top-level [`AGENTS.md`](../../../AGENTS.md) and [`magisk-git`](../magisk-git/SKILL.md) for repository rules, environment execution setup, and commit control policies.
- **Working Directory:** Set working directory to `app/` when running standalone Gradle commands, or execute build commands via `./build.py` from the repository root.
- **Environment Wrapper:** Standalone `./gradlew` commands MUST be prefixed with `../scripts/env.py` (e.g., `../scripts/env.py ./gradlew :apk-ng:assembleDebug`). Alternatively, run `./build.py app-ng` from the repository root (which configures the environment automatically and builds `:apk-ng`).
## 2. Architecture & Submodules
Multi-module Gradle project structure:
- **`:apk-ng`** (`apk-ng/`): Next-gen app variant. Primary target for UI and application features.
- **`:core`** (`core/`): Core domain logic, resources, Room DB, services. Primary target for core feature development.
- **`:shared`** (`shared/`): Shared utilities and common data structures.
- **`:stub`** (`stub/`): Lightweight stub app loader for hidden installs.
- **`:stub-res`** (`stub-res/`): Stub-specific Android resources.
- **`:test`** (`test/`): Application testing target.
- **`:build-logic`** (`build-logic/`): Custom Gradle plugins and build logic.
- **`:apk`** (`apk/`): Legacy app APK. **Maintenance mode:** Do NOT modify unless explicitly requested.
## 3. Development Guidelines
- **Target Modules:** All active app development occurs in `:core` and `:apk-ng`. Do NOT make changes to `:apk` unless explicitly requested.
- **Legacy Compatibility:** When modifying `:core` and `:shared`, make sure the `:apk` module is still buildable.
- **Language & UI:** Written in Kotlin/Java. **Prefer Kotlin for all new code.** Uses Jetpack Compose for UI (prefer over View XML).
- **String Resources:** Default strings in `core/src/main/res/values/strings.xml` and `stub-res/src/main/res/values/strings.xml`. Translations go in `values-[lang]/strings.xml`.
- **Data Stack:** Room, KSP, Wire (Protocol Buffers), Moshi.
## 4. Workflows & Verification
### From `app/` directory (prefixed with `../scripts/env.py`):
- **Build App (`:apk-ng` Debug):** `../scripts/env.py ./gradlew :apk-ng:assembleDebug`
- **Build Stub APK:** `../scripts/env.py ./gradlew :stub:assembleDebug`
- **Build Legacy APK (`:apk` Debug):** `../scripts/env.py ./gradlew :apk:assembleDebug`
- **Run Lint:** `../scripts/env.py ./gradlew lint` (or `../scripts/env.py ./gradlew :apk-ng:lintDebug`)
- **Run Unit Tests:** `../scripts/env.py ./gradlew test`
- **Clean Artifacts:** `../scripts/env.py ./gradlew clean`
### From repository root (via `build.py`):
- **Build App (`:apk-ng`):** `./build.py app-ng`
- **Build Stub App:** `./build.py stub`
- **Build Legacy App (`:apk`):** `./build.py app`
- **Run App Tests:** `./build.py test`
+38
View File
@@ -0,0 +1,38 @@
---
name: magisk-git
description: Guidelines and instructions for Git operations, commit policies, and commit message formatting in the Magisk repository. Use when committing changes, amending commits, or formatting commit messages.
---
# Magisk Git & Commit Guidelines
Guidelines and rules for Git operations and commit management in the Magisk repository.
## 1. Commit Control & Approval
- **NEVER** commit changes or amend an existing git commit without the user's explicit request or approval.
- Do not stage or commit files automatically as part of automated steps or task conclusions unless explicitly instructed by the user.
## 2. Commit Message Formatting
When explicitly requested to commit changes, format commit messages according to the following rules:
1. **50/72 Rule:**
- **Subject line:** At most 50 characters. Keep it concise, descriptive, and written in the imperative mood (e.g., `Fix build failure on Android 14`, `Add support for custom mounts`).
- **Blank line:** Always include a blank line between the subject line and the commit body.
- **Body text:** Wrap body lines at 72 characters maximum. Explain the *what* and *why* behind the change.
2. **Assisted-by Trailer:**
- Include an `Assisted-by: <Friendly Name of Current Model>` trailer in the commit message body (e.g., `Assisted-by: Gemini 3.7 Flash`, `Assisted-by: GPT-4o`, `Assisted-by: Claude 3.5 Sonnet`).
- **Note:** Always specify the underlying LLM model name, NOT the agent framework, runner, or harness name (such as Antigravity, AGY, etc.).
## 3. Example Commit Message
```text
Short summary of changes in 50 characters or less
Detailed explanation of why this change was made and what problems
it solves. Wrap all lines in the message body to at most 72
characters.
Assisted-by: <Friendly Name of Current Model>
```
+98
View File
@@ -0,0 +1,98 @@
---
name: magisk-native
description: Guidelines, architecture, C++/Rust FFI bindings, development conventions, and build workflows for the Magisk native codebase (native/ subproject). Use when working on, modifying, building, debugging, or testing native C, C++, and Rust code in native/ (including magisk, magiskinit, magiskboot, magiskpolicy, resetprop, zygisk, and native crates/modules).
---
# Magisk Native Subproject Guidelines
Guidelines and workflows for developing and building native C, C++, and Rust components in the `native/` subproject.
## 1. Environment & Build Requirements
- **General Guidelines:** Always follow the top-level [`AGENTS.md`](../../../AGENTS.md) and [`magisk-git`](../magisk-git/SKILL.md) for general repository rules, environment execution setup, and commit control policies.
- **Working Directory:** Execute commands from repo root via `./build.py`. Standalone tool executions (e.g. `cargo`, `rustc`, `ndk-build`) MUST be prefixed with `scripts/env.py`.
- **Codegen Requirement:** ALWAYS run `./build.py gen` before editing native sources to generate FFI bindings, headers, and flags (`flags.h`, `flags.rs`, `*-rs.hpp`, `*-rs.cpp`, protobuf generated modules) without performing a full build.
## 2. Component Architecture
Native C, C++, and Rust source modules under `native/src/`:
- **`base/`**: System wrappers, logging, custom string abstractions (`Utf8CStr`), mount helpers, and common utilities (C++/Rust).
- **`boot/`**: Boot image parsing, unpacking, repacking, and ramdisk patching logic (`magiskboot`) (Rust/C++).
- **`core/`**: Magisk daemon (`magiskd`), Zygisk engine, `su` implementation, applets, and system properties (`resetprop`) (C++/Rust).
- **`init/`**: Early boot `magiskinit`, ramdisk patching, SELinux pre-init (C/C++/Rust).
- **`sepolicy/`**: SELinux policy engine (`libpolicy`) and `magiskpolicy` CLI (C++/Rust).
- **`external/`**: Embedded dependencies (`cxx-rs`, `selinux`, `crt0`, `system_properties`, `lsplt`, `lz4-sys`, `xz-embedded`).
### Key Native Binary Output Targets
- `magisk`: Core daemon, Zygisk, `su`, and applets executable.
- `magiskinit`: Early init replacement executable (static).
- `magiskboot`: Boot image patcher executable (static).
- `magiskpolicy`: SELinux policy tool executable.
- `resetprop`: System property reader/writer executable.
## 3. Build System Orchestration
The build process follows a two-stage hybrid pipeline orchestrated by `build.py`:
```
1. dump_flags_native() --> Outputs flags.h & flags.rs to native/out/generated/
2. build_rust_src() --> Cargo build outputs lib<tgt>.a for each target ABI
Cargo build.rs runs cxx_gen to produce *-rs.hpp / *-rs.cpp
Cargo build.rs runs pb-rs to generate Protobuf bindings
Static libraries moved to native/out/<arch>/lib<tgt>-rs.a
3. build_cpp_src() --> ndk-build runs using Android.mk & Application.mk
Android-rs.mk imports lib<tgt>-rs.a as PREBUILT_STATIC_LIBRARY
Compiles C/C++ sources + *-rs.cpp bridge files + cxx.cc
4. clean_elf() --> tools/elf-cleaner strips incompatible ELF dynamic tags
```
## 4. FFI Architecture & Mechanics
- **Bridge Engine:** C++/Rust FFI uses `cxx` (`cxx-rs`) via `#[cxx::bridge]` modules declared in crate `lib.rs` files.
- **Header & Source Generation:**
- `codegen.rs` (`gen_cxx_binding()`) invokes `cxx_gen` in crate `build.rs` scripts.
- Automatically generates C++ bridge headers (`*-rs.hpp`) and source wrappers (`*-rs.cpp`) directly in each crate directory.
- Generates bindings for `base-rs`, `core-rs`, `init-rs`, `boot-rs`, and `policy-rs`.
- **Linking:** Generated `*-rs.cpp` bridge code and `cxx.cc` are compiled directly by `ndk-build` alongside native C++ source files, linking against the compiled Rust static library (`lib<tgt>-rs.a`).
## 5. Build Targets & Commands (from Root)
- **Build All Native Binaries:** `./build.py native`
- **Build Specific Target(s):** `./build.py native [magisk|magiskinit|magiskboot|magiskpolicy|resetprop]`
- **Rust Clippy Lint:** `./build.py clippy`
- **Cargo Commands:** `./build.py cargo check`, `./build.py cargo test`
- **Generate Bindings & IDE Files:** `./build.py gen`
- **Clean Native Artifacts:** `./build.py clean native`
## 6. Rust & C++ Conventions
- **Rust Edition & Profile:** Rust Edition 2024. Profile configured with `panic = "immediate-abort"` across dev and release profiles.
- **C++ Standard:** C++20 with `libc++` static linking.
- **Clippy Rules:** `unwrap_used = "deny"` in workspace configuration. **Do NOT use `.unwrap()` in Rust code.** Use `?`, `unwrap_or`, `unwrap_or_else`, or pattern matching.
- **Incremental Build Protection:** File writers (`write_if_diff`) skip rewriting identical generated files to preserve compilation timestamps and avoid unnecessary rebuilds.
## 7. Magisk Rust Patterns & Non-Standard Idioms
AI models modifying or writing Rust code in `native/src/` MUST follow these Magisk-specific idioms:
1. **Custom String Handling (`Utf8CStr` & `cstr!`):**
- Standard C/C++ APIs require null-terminated UTF-8 strings. Do NOT create intermediate `CString` allocations.
- Use `&Utf8CStr` for string references, `Utf8CString` for heap buffers, and `Utf8CStrBufArr<N>` for stack buffers (`base/cstr.rs`).
- Use `cstr!("literal")` for compile-time null-terminated static strings.
- Use `StringExt::nul_terminate` for in-place null byte termination using reserved string capacity.
2. **Instant Logging Error Model (`LoggedError`):**
- Application logic does NOT use standard error enums or `anyhow`.
- Errors are logged immediately at the failure point via `log_err!`, `.log()`, or `.log_with_msg()` and converted into zero-sized `LoggedError` / `LoggedResult<T>`.
- In debug builds, `#[track_caller]` logs the file and line number of error sites automatically. Use `.silent()` to silence expected failures.
3. **Libc System Call Conversions (`LibcReturn`):**
- When calling raw libc or C system APIs in Rust or `xwrap.rs`, use `LibcReturn::into_os_result` or `check_err` to automatically map negative integer returns or null pointers into `OsResult` or `LoggedResult`.
4. **Zero Allocation & Binary Size Rules:**
- Prefer custom `argh` derivation (`base/argh.rs`) for command-line parsing instead of adding standard argument dependencies.
- Avoid `serde` overhead; use procedural `Encodable`/`Decodable` binary traits over UNIX domain sockets for IPC.
5. **Nightly Synchronization & IPC:**
- Magisk uses nightly features (`unix_socket_ancillary_data`, `nonpoison_mutex`).
- Use non-poisoning mutexes/condvars for daemon threading and `send_fd`/`recv_fd` for raw file descriptor passing.