From cfd195b5144222585d96de8ad7710f0f3516baf0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 30 Jul 2026 18:10:31 -0700 Subject: [PATCH] docs: update AGENTS.md guidelines for AI models - Expand top-level AGENTS.md with environment execution rules and AI guidelines. - Add app/AGENTS.md for app subproject architecture and maintenance mode rules. - Add native/AGENTS.md for native C/C++/Rust build target requirements. Assisted-by: Gemini 3.6 Flash --- AGENTS.md | 36 ++++++++++++++++++++++++++++-------- app/AGENTS.md | 37 +++++++++++++++++++++++++++++++++++++ native/AGENTS.md | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 app/AGENTS.md create mode 100644 native/AGENTS.md diff --git a/AGENTS.md b/AGENTS.md index ee906336b..2286a8c01 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,12 +1,32 @@ -## AGENTS.md for Magisk +# AGENTS.md -### General +Guidelines and instructions for AI models and automated agents operating in the Magisk repository. -- Prefix every shell command with `scripts/env.py` to ensure that commands are executed with the correct environment. +## 1. Environment & Execution Setup -### App +- **`build.py` Invocations:** `./build.py` imports `scripts/env.py` internally and configures the environment automatically. It does **not** need to be prefixed with `scripts/env.py` (e.g., `./build.py all` or `./build.py native`). +- **Direct Tool Executions:** Standalone commands outside of `build.py` (such as `./gradlew`, `cargo`, `rustc`, `ndk-build`, etc.) **MUST** be prefixed with `scripts/env.py` (e.g., `scripts/env.py ./gradlew assembleDebug`). +- **NDK Toolchain Setup:** If NDK toolchain dependencies are missing or outdated, run `./build.py ndk`. -- All application related source code lives under `app` directory. When working on the application codebase, use `app` as the working directory. -- The `app` directory is itself a Gradle project. Use `./gradlew` with corresponding tasks to build the app. -- The Magisk app is written in Kotlin and Java. Prefer Kotlin for all new code. -- After doing changes in `app`, make sure to build the relevant modules to ensure they build successfully. +## 2. Codebase Structure & Architecture + +- **`app/` (Android Application):** Multi-module Gradle project for the Android app. Refer to [`app/AGENTS.md`](app/AGENTS.md) for app architecture, submodules, Kotlin conventions, and Gradle workflows. +- **`native/` (Native Core Binaries):** C, C++, and Rust codebase for building native components (`magisk`, `magiskinit`, `magiskboot`, `magiskpolicy`, `resetprop`). Refer to [`native/AGENTS.md`](native/AGENTS.md) for native architecture, submodules, FFI compilation, and Rust conventions. +- **`build.py` (Primary Build Orchestrator):** Python script managing native builds, app packaging, Clippy runs, Cargo invocations, and cleanup. +- **`tools/` & `scripts/`:** Build scripts, helper utilities (e.g., `elf-cleaner`), and environment setup tools. + +## 3. Build & Verification Workflows + +- **Build Complete Project (APK + Native):** `./build.py all` +- **Build Native Components:** `./build.py native [target...]` +- **Build Application Targets:** `./build.py app`, `./build.py stub`, `./build.py test` +- **Rust Verification & Linting:** `./build.py clippy`, `./build.py cargo ` +- **Clean Build Artifacts:** `./build.py clean [native|cpp|rust|java|app]` + +## 4. Guidelines for AI Models + +1. **Git / Commit Control:** NEVER commit changes or amend an existing git commit without the user's explicit request or approval. When explicitly requested to commit changes, follow the 50/72 rule for commit messages (subject line <= 50 characters, blank line before body, wrap body lines at 72 characters) and include an `Assisted-by: ` trailer in the commit message body (using a pretty name, e.g., `Assisted-by: Gemini 3.6 Flash`). +2. **Build Invocation:** Execute `./build.py ` directly without `scripts/env.py`. Prefix standalone tool executions (like `./gradlew` or raw `cargo`) with `scripts/env.py`. +3. **Pre-build Native Code:** Before modifying code in `native/`, build native binaries at least once with `./build.py native` to generate required bindings and header files. +4. **App Subproject Context:** Refer to [`app/AGENTS.md`](app/AGENTS.md) when working inside the `app/` subproject. +5. **Verification Loop:** After making changes, verify compilation and run linting/clippy checks for affected modules before concluding tasks. diff --git a/app/AGENTS.md b/app/AGENTS.md new file mode 100644 index 000000000..e38909770 --- /dev/null +++ b/app/AGENTS.md @@ -0,0 +1,37 @@ +# AGENTS.md (app subproject) + +Guidelines for AI models operating inside the `app/` subproject. + +## 1. Environment & Gradle Setup + +- **Working Directory:** Set working directory to `app/` when working on app code. +- **Environment Wrapper:** Standalone `./gradlew` commands MUST be prefixed with `../scripts/env.py` (e.g., `../scripts/env.py ./gradlew assembleDebug`), or run `./build.py app` from root. + +## 2. Architecture & Submodules + +Multi-module Gradle project structure: +- **`:apk`** (`apk/`): Legacy app APK. **Maintenance mode:** No new features should be added here. +- **`:apk-ng`** (`apk-ng/`): Next-gen app variant. Primary target for new UI/app 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. + +## 3. Development Guidelines + +- **Feature Development:** `:apk` is in maintenance mode. All new development MUST occur in `:core` and `:apk-ng`. +- **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/`) + +Prefix commands with `../scripts/env.py`: +- **Build Main APK (Debug):** `../scripts/env.py ./gradlew :apk:assembleDebug` +- **Build All Variants:** `../scripts/env.py ./gradlew assembleDebug` +- **Build Stub APK:** `../scripts/env.py ./gradlew :stub:assembleDebug` +- **Run Lint:** `../scripts/env.py ./gradlew lint` +- **Run Unit Tests:** `../scripts/env.py ./gradlew test` +- **Clean Artifacts:** `../scripts/env.py ./gradlew clean` diff --git a/native/AGENTS.md b/native/AGENTS.md new file mode 100644 index 000000000..c5f3b5d7f --- /dev/null +++ b/native/AGENTS.md @@ -0,0 +1,32 @@ +# AGENTS.md (native subproject) + +Guidelines for AI models operating inside the `native/` subproject. + +## 1. Environment & Build Requirements + +- **Working Directory:** Execute commands from repo root via `./build.py`. +- **Pre-build Requirement:** ALWAYS run `./build.py native` at least once before editing native sources to generate FFI bindings and headers (`flags.h`, `flags.rs`, generated headers/sources). + +## 2. Component Architecture + +Native C, C++, and Rust source modules under `native/src/`: +- **`base/`**: System wrappers, logging, and common utilities (C++/Rust). +- **`boot/`**: Boot image parsing, unpacking, and patching logic (Rust). +- **`core/`**: Magisk daemon, Zygisk engine, `su`, applets, system properties (C++/Rust). +- **`init/`**: Early boot `magiskinit`, ramdisk patching, SELinux pre-init (C/C++/Rust). +- **`sepolicy/`**: SELinux policy engine and `magiskpolicy` CLI (C++/Rust). +- **`external/`**: Embedded dependencies (`cxx-rs`, `selinux`, `crt0`, `system_properties`, `lz4-sys`). + +## 3. Build Targets & Commands (from Root) + +- **Build All 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 IDE Database:** `./build.py gen` +- **Clean Native Artifacts:** `./build.py clean native` + +## 4. Rust & C++ Conventions + +- **Clippy Rules:** `unwrap_used = "deny"` in workspace. **Do NOT use `.unwrap()` in Rust code.** Use `?`, `unwrap_or`, or pattern matching. +- **FFI & Toolchain:** C++/Rust FFI uses `cxx` (auto-generated headers). Rust Edition 2024. Configured with `panic = "immediate-abort"`.