Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / docs / structure / README.md
| 1 | # Structure ratchets |
| 2 | |
| 3 | Cheap, build-free checks in `tools/structure_check.py` (Python 3 stdlib |
| 4 | only), wired into `./check` / `check.bat`. They run on every `./check` |
| 5 | invocation; `./check structure` runs only them. Each check passes on the |
| 6 | current tree and names every violation individually. |
| 7 | |
| 8 | ## Entry points |
| 9 | |
| 10 | ``` |
| 11 | python3 tools/structure_check.py all # all four checks |
| 12 | python3 tools/structure_check.py wear # protocol-pair drift only |
| 13 | python3 tools/structure_check.py generated # generated-source guard only |
| 14 | python3 tools/structure_check.py exceptions # exception-marker ratchet only |
| 15 | python3 tools/structure_check.py exceptions --regen # regenerate exceptions.tsv |
| 16 | python3 tools/structure_check.py lines # line-count ratchet + per-module totals |
| 17 | python3 tools/structure_check.py lines --report # per-module totals, no enforcement |
| 18 | python3 tools/structure_check.py lines --regen # bump line budget to today's total |
| 19 | ./check structure # same as `all`, via ./check |
| 20 | ./check <module> [...] # structure checks run first |
| 21 | ``` |
| 22 | |
| 23 | Manifests in this directory (tab-separated, `#` comment lines): |
| 24 | |
| 25 | | File | Format | |
| 26 | | :--- | :--- | |
| 27 | | `wear-protocol-pairs.tsv` | `pair_id<TAB>phone_path<TAB>wear_path` | |
| 28 | | `generated-sources.tsv` | `path<TAB>generator command` | |
| 29 | | `exceptions.tsv` | `type<TAB>file<TAB>reason<TAB>count` (generated; see below) | |
| 30 | | `line-budget.json` | JSON: `baseline_lines`, counted `extensions`, `excluded_path_segments`, `excluded_modules`, per-module snapshot (see §4) | |
| 31 | |
| 32 | ## 1. Protocol-pair drift (`wear`) |
| 33 | |
| 34 | Gradle cannot let one app module depend on another, so each Wear OS |
| 35 | companion carries a copy of the phone's `Wear*Protocol.kt` under its own |
| 36 | package. `wear-protocol-pairs.tsv` lists the still-duplicated pairs (12 after the |
| 37 | clock migration; `library/wearlink/WearLinkProtocol.kt` and the migrated |
| 38 | `library/wearlink` per-app protocols are shared libraries both sides |
| 39 | depend on, not copies, and are deliberately not listed). |
| 40 | |
| 41 | Normalization per file: strip all comments with a string-aware scanner |
| 42 | (Kotlin `//`, nested `/* */`, `"..."`, `"""..."""`, char literals), drop |
| 43 | `package`/`import` lines, then tokenize (`[A-Za-z_]\w*`, numbers, single |
| 44 | punctuation chars). The two token streams must be identical; a violation |
| 45 | names the pair, both token counts, and the first differing token with |
| 46 | context. Comment-only or package/import-only edits do not trip it; any |
| 47 | code or string-literal change on one side does. |
| 48 | |
| 49 | ## 2. Generated-source guard (`generated`) |
| 50 | |
| 51 | A file counts as generated when, within its first 40 lines, a line matches |
| 52 | one of: `BEGIN GENERATED`, `@generated`, `GENERATED` at the start of a |
| 53 | comment line (e.g. `//! GENERATED from ...`, ` * GENERATED — ...`), |
| 54 | `Code generated ... DO NOT EDIT`, `// Generated constants|constant tables`, |
| 55 | or `Do not hand-edit`. Scan scope: `*.kt`/`*.rs`, excluding the root |
| 56 | AGENTS.md "Never scan" dirs (`build/`, `target/`, `third_party/`, |
| 57 | `analysis/`, `personal/`, ...) plus `lint-rules/`, `tools/`, `docs/` |
| 58 | (which quote header strings as data). |
| 59 | |
| 60 | Every such file must be listed in `generated-sources.tsv` with the |
| 61 | command that regenerates it (a recorded generator with no script in the |
| 62 | repo is stated as such in the manifest), and every listed file must |
| 63 | still exist and still carry its header. Presence only: the check never |
| 64 | regenerates anything. |
| 65 | |
| 66 | Build-time generated sources are the other pattern: a Gradle task writes |
| 67 | Kotlin under the module's `build/` directory and registers it as a |
| 68 | generated source directory (e.g. the `IconXxx()` wrappers in |
| 69 | `:library:ui`, generated by `GenerateIconsTask` in build-logic from |
| 70 | `library/ui/icons.tsv`). Those files are outside this guard and outside |
| 71 | the line ratchet by the `build/` exclusion in both, carry a |
| 72 | `GENERATED … Do not hand-edit` header for readers, and are never listed |
| 73 | in `generated-sources.tsv` (that manifest is for checked-in generated |
| 74 | files only; listing a build output would go stale on a clean tree). |
| 75 | |
| 76 | ## 3. Exception-marker ratchet (`exceptions`) |
| 77 | |
| 78 | `exceptions.tsv` inventories every escape-hatch marker occurrence in |
| 79 | Kotlin sources (`*.kt`/`*.kts`, same exclusions as above): the canonical |
| 80 | types from root AGENTS.md §2 — `PACKAGE_JNI` (the verbatim |
| 81 | `// PACKAGE STRUCTURE EXCEPTION (JNI)` marker; reason column records |
| 82 | that it carries no free-text reason), `RAW_SCAFFOLD`, `TOAST`, |
| 83 | `RAW_ANIMATION`, `SCAFFOLD_LOADING`, `SCAFFOLD_SEARCH`, |
| 84 | `SCREENSHOT_COVERAGE`. The reason column is the verbatim text after the |
| 85 | marker colon in the source. |
| 86 | |
| 87 | `./check` fails when the TSV does not match the tree exactly (type, |
| 88 | file, reason, count). Line numbers are deliberately not recorded, so |
| 89 | moving a marker inside its file does not churn the inventory. To add or move a marker intentionally: edit the |
| 90 | source, run `python3 tools/structure_check.py exceptions --regen`, |
| 91 | review the `exceptions.tsv` diff (that review is the reason edit), and |
| 92 | include both in the change. Malformed markers (missing colon/reason) |
| 93 | are still caught by lint (`ExceptionMarkerFormatDetector`); this |
| 94 | ratchet catches growth and drift. |
| 95 | |
| 96 | ## 4. Line-count ratchet (`lines`) |
| 97 | |
| 98 | Counts "all source" for the LOC-cut programme: every `.kt`, `.java`, |
| 99 | `.rs` and `.swift` file, tests and screenshot tests included, enumerated |
| 100 | with `git ls-files --cached --others --exclude-standard` (tracked plus |
| 101 | untracked-but-not-ignored files; no full-tree walk, ~1s). Any path with |
| 102 | a `third_party/`, `build/` or `target/` segment is excluded, so |
| 103 | generated build outputs never count. Files are grouped by Gradle |
| 104 | module (parsed from `settings.gradle.kts`, including `projectDir` |
| 105 | overrides; longest project dir wins) and per-module totals are printed |
| 106 | on every run (`lines --report` prints without enforcing). |
| 107 | |
| 108 | `line-budget.json` is the checked-in baseline. The ratchet fails when |
| 109 | the counted total grows past `baseline_lines`; shrinking always |
| 110 | passes. The per-module figures in the file are a snapshot for |
| 111 | reporting, not per-module caps — only the total is enforced. |
| 112 | Deliberate growth requires bumping the baseline in the same change: |
| 113 | run `python3 tools/structure_check.py lines --regen`, review the |
| 114 | `line-budget.json` diff, and include it with the source change. |
| 115 | |
| 116 | The baseline is 1,730,008 lines, measured with the method above. |
| 117 | (`scripts/` is counted, per the plan's stated `third_party/`-only |
| 118 | exclusion, so the baseline is higher than a `scripts/`-excluded count.) |
| 119 | |
| 120 | Exclusions are data-driven in `line-budget.json` |
| 121 | (`excluded_modules`). The only exclusion is `communicate-dev`, the |
| 122 | Communicate dev module, pre-registered before it existed: the exact |
| 123 | name is `include(":communicate-dev")` with dir `communicate-dev/` |
| 124 | (`settings.gradle.kts` also has `:madeveloper`, a different app), and |
| 125 | only that exact module is excluded. |
| 126 | |
| 127 | Tests: `python3 tools/test_structure_check.py`. |