Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / docs / structure / README.md

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