ratchet-size 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,213 @@
1
+ Metadata-Version: 2.4
2
+ Name: ratchet-size
3
+ Version: 0.1.0
4
+ Summary: A code-size budget that only goes down: keeps coding agents from bloating your repo.
5
+ Author: Krishan Subudhi
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/krishansubudhi/ratchet
8
+ Project-URL: Repository, https://github.com/krishansubudhi/ratchet
9
+ Project-URL: Issues, https://github.com/krishansubudhi/ratchet/issues
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Software Development :: Quality Assurance
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Provides-Extra: test
16
+ Requires-Dist: pytest; extra == "test"
17
+ Dynamic: license-file
18
+
19
+ # ratchet
20
+
21
+ [![CI](https://github.com/krishansubudhi/ratchet/actions/workflows/ci.yml/badge.svg)](https://github.com/krishansubudhi/ratchet/actions/workflows/ci.yml)
22
+
23
+ **A code-size budget that only goes down.** Coding agents are good at adding
24
+ code and bad at deciding not to. `ratchet` gives the repo a line budget:
25
+ growth past the ceiling is refused, in plain words the agent can act on, and
26
+ only a human can raise it.
27
+
28
+ ![ratchet refusing a duplicated-code change on pallets/click, then passing once the duplicate is removed](docs/demo.gif)
29
+
30
+ Real run, on [pallets/click](https://github.com/pallets/click): `ratchet init`
31
+ records today's size; an agent asked for a feature pastes a near-duplicate of
32
+ existing code instead of reusing it; `ratchet check` refuses the growth and
33
+ says exactly why; the duplicate goes, and the check passes again. Real
34
+ commands and output: [docs/examples.md](docs/examples.md). (The bloat step
35
+ is a scripted patch standing in for the agent, honestly -- see the caption
36
+ there.)
37
+
38
+ ## Quickstart
39
+
40
+ 1. **Install:** `pipx install ratchet-size` (or `pip install ratchet-size`,
41
+ or `pip install -e .` from a clone).
42
+ 2. **Record today's size:** `ratchet init` -- writes `.ratchet.json`; commit it.
43
+ 3. **Wire up your agent:** paste this sentence to it: *"Set up ratchet in
44
+ /path/to/repo by following
45
+ https://github.com/krishansubudhi/ratchet/blob/main/SETUP-FOR-AGENTS.md"*
46
+
47
+ That's it. Everything past this point is detail you can come back to: what
48
+ counts as a line, the four rules, grants, config, and per-harness
49
+ integrations.
50
+
51
+ ## Claude Code plugin
52
+
53
+ Prefer a plugin over the manual setup above? Ratchet ships as a Claude Code
54
+ plugin, with this repo doubling as its own marketplace:
55
+
56
+ ```text
57
+ /plugin marketplace add krishansubudhi/ratchet
58
+ /plugin install ratchet@ratchet
59
+ ```
60
+
61
+ That installs a **Stop** hook (`ratchet check --hook`, falling back to `uvx`
62
+ if `ratchet` isn't on `PATH` yet) so Claude Code can't end a turn with the
63
+ repo over budget, plus a `/ratchet:setup` command that runs the
64
+ [agent setup](SETUP-FOR-AGENTS.md) -- install, `ratchet init`, detect your
65
+ harness, `ratchet check` -- in the repo you're in. See
66
+ [`.claude-plugin/`](.claude-plugin) and [`hooks/hooks.json`](hooks/hooks.json)
67
+ for the manifests, or `claude plugin validate .` to check them yourself.
68
+
69
+ ## What it counts
70
+
71
+ **Scope:** the one git repository you run it in (`git rev-parse
72
+ --show-toplevel`). Other repos on the machine are never touched, and each
73
+ repo gets its own `.ratchet.json`.
74
+
75
+ **Which files:** git-tracked files, plus untracked files that aren't
76
+ gitignored, whose extension is one of the code extensions present at
77
+ `init` (`.py`, `.js`, `.ts`, `.go`, `.rs`, `.java`, `.rb`, `.c`, `.sh`, and
78
+ more -- see `CODE_EXTS` in `ratchet/measure.py`). Blank lines don't count.
79
+
80
+ **Source vs tests:** a file matching a test glob (`test_*`, `*_test.*`,
81
+ `*_tests.*`, `*.test.*`, `*.spec.*`, or living under `tests/`, `test/`,
82
+ `__tests__/`, `spec/`) is `tests`; everything else is `source`. Both groups
83
+ are plain config in `.ratchet.json` -- edit `include`/`exclude`/`extensions`
84
+ to carve out a different split, e.g. `"exclude": ["vendor/**", "**/*.gen.*"]`
85
+ to stop counting vendored or generated code.
86
+
87
+ **Existing repo:** `ratchet init` measures today's totals and sets the
88
+ ceilings there (plus optional `--slack`). A file already over
89
+ `max_file_lines` is frozen at its current size, so nothing is refused on
90
+ day one -- you start where you are. From then on a ceiling may only shrink,
91
+ unless a human runs `ratchet grant`.
92
+
93
+ ## What the agent sees when refused
94
+
95
+ ```
96
+ ratchet: REFUSED -- 2 problems (source 10,412/10,300, tests 4,980/5,000)
97
+
98
+ 1. source is 10,412 lines, ceiling 10,300: 112 over
99
+ grew vs HEAD: app/api.py +80, app/store.py +40
100
+ fix: remove at least 112 lines of source: delete dead code, reuse what
101
+ exists instead of adding a parallel version, simplify.
102
+ 2. app/api.py is 431 lines, limit 400
103
+ seams near the middle: line 198 `def upload`, line 230 `class Session`
104
+ fix: split it at a seam: move a cohesive group of definitions into a new
105
+ file, so each piece is under 400 lines.
106
+
107
+ Do not edit .ratchet.json or .ratchet-grants.jsonl to get past this.
108
+ If the growth is truly needed, stop and ask a human to run:
109
+ ratchet grant +112 --group source --reason "<why>" --by <name>
110
+ ratchet grant +31 --file app/api.py --reason "<why>" --by <name>
111
+ ```
112
+
113
+ Every refusal says what grew, by how much, and which fix applies: shrink,
114
+ split, or ask a human. For a harness, `ratchet check --json` gives the same
115
+ result as data.
116
+
117
+ ## The four rules
118
+
119
+ | Rule | What it does |
120
+ |------|--------------|
121
+ | **Budget** | Each group (`source`, `tests` by default) has a ceiling. A total over it is refused. |
122
+ | **Seams** | A file over `max_file_lines` (default 400) is refused, with the top-level definitions nearest the middle offered as places to cut. A file already over the limit at `init` is recorded at its size: it may shrink, never grow. |
123
+ | **Grants** | Only `ratchet grant` raises a limit, and every grant is appended to `.ratchet-grants.jsonl` with who and why. |
124
+ | **Tighten** | `ratchet tighten` lowers every ceiling to the measured size (plus optional `slack`). Nothing ever raises it automatically. |
125
+
126
+ Lines are counted without blanks. Docs, data, and fixtures don't count: only
127
+ files whose extensions you list in `.ratchet.json` do.
128
+
129
+ ## How grants work
130
+
131
+ Sometimes the growth is the point: a real feature, a new integration. Then a
132
+ human runs:
133
+
134
+ ```sh
135
+ ratchet grant +300 --group source --reason "CSV export (issue 41)" --by alice
136
+ ratchet grant +60 --file app/api.py --reason "until the router split lands" --by alice
137
+ ```
138
+
139
+ That raises the ceiling in `.ratchet.json` and appends a line to
140
+ `.ratchet-grants.jsonl`:
141
+
142
+ ```json
143
+ {"after": 10600, "before": 10300, "by": "alice", "lines": 300, "reason": "CSV export (issue 41)", "target": "source", "ts": "2026-01-15T09:30:00Z"}
144
+ ```
145
+
146
+ Commit both files. Here is what keeps grants honest:
147
+
148
+ - **In CI, run `ratchet check --base <target branch>`.** It refuses any ceiling
149
+ that is higher than the base's unless grants appended since the base pay for
150
+ it. An agent that edits `.ratchet.json` by hand passes locally and fails
151
+ review. The check also refuses a grants log that was rewritten rather than
152
+ appended to.
153
+ - Grants are one-line diffs in the pull request, so a reviewer sees them.
154
+ - Tell your agent never to run `ratchet grant`. The Claude Code integration
155
+ also puts it on the permission deny list.
156
+
157
+ Run `ratchet tighten` after a cleanup and commit the result. The room you
158
+ freed is now the new ceiling.
159
+
160
+ ## Running it by hand
161
+
162
+ `ratchet check` exits `0` when the repo is within budget, `1` when refused,
163
+ `2` on a setup error (bad config, not a git repo when `--base` is used, and
164
+ so on). `python -m ratchet` works the same as the `ratchet` entry point, if
165
+ you don't have it on `PATH`.
166
+
167
+ ## Integrations
168
+
169
+ | Harness | How |
170
+ |---------|-----|
171
+ | Claude Code | Stop hook `ratchet check --hook`: [integrations/claude-code](integrations/claude-code/README.md) |
172
+ | Cursor, Codex CLI, aider | instruction file + git hook / test-cmd: [integrations/other-agents.md](integrations/other-agents.md) |
173
+ | pre-commit | framework hook, or a plain git hook: [integrations/pre-commit](integrations/pre-commit/README.md) |
174
+ | GitHub Actions | `uses: krishansubudhi/ratchet@v0`: [integrations/github](integrations/github/README.md) |
175
+ | Anything else | prompt snippet: [integrations/any-agent.md](integrations/any-agent.md) |
176
+
177
+ ## Configuration
178
+
179
+ `ratchet init` writes `.ratchet.json`. It is JSON because ratchet must write
180
+ the file, and Python's standard library reads TOML but cannot write it.
181
+
182
+ ```json
183
+ {
184
+ "version": 1,
185
+ "max_file_lines": 400,
186
+ "slack": 0,
187
+ "groups": {
188
+ "source": {"include": ["**"], "exclude": ["**/tests/**", "**/test_*"], "extensions": [".py", ".ts"]},
189
+ "tests": {"include": ["**/tests/**", "**/test_*"], "extensions": [".py", ".ts"]}
190
+ },
191
+ "ceilings": {"source": 10300, "tests": 5000},
192
+ "file_ceilings": {"app/legacy.py": 1210}
193
+ }
194
+ ```
195
+
196
+ - Globs work like gitignore: `**/` means any number of directories.
197
+ - Each file belongs to the first group that matches it.
198
+ - `slack` keeps some headroom above the measured size after `init` and
199
+ `tighten`, if you would rather not require a delete for every add.
200
+ - Files are listed with `git ls-files`, so ignored files never count.
201
+ Outside git, ratchet walks the tree and skips the usual build and vendor
202
+ directories.
203
+
204
+ `ratchet init --max-file-lines 300 --slack 50` sets both at creation.
205
+
206
+ ## Requirements
207
+
208
+ Python 3.10+, git (optional, needed for `--base` and the "grew vs" lines). No
209
+ dependencies. Tests: `pip install pytest && python -m pytest -q`.
210
+
211
+ ## License
212
+
213
+ Apache-2.0. Copyright 2026 Krishan Subudhi.
@@ -0,0 +1,195 @@
1
+ # ratchet
2
+
3
+ [![CI](https://github.com/krishansubudhi/ratchet/actions/workflows/ci.yml/badge.svg)](https://github.com/krishansubudhi/ratchet/actions/workflows/ci.yml)
4
+
5
+ **A code-size budget that only goes down.** Coding agents are good at adding
6
+ code and bad at deciding not to. `ratchet` gives the repo a line budget:
7
+ growth past the ceiling is refused, in plain words the agent can act on, and
8
+ only a human can raise it.
9
+
10
+ ![ratchet refusing a duplicated-code change on pallets/click, then passing once the duplicate is removed](docs/demo.gif)
11
+
12
+ Real run, on [pallets/click](https://github.com/pallets/click): `ratchet init`
13
+ records today's size; an agent asked for a feature pastes a near-duplicate of
14
+ existing code instead of reusing it; `ratchet check` refuses the growth and
15
+ says exactly why; the duplicate goes, and the check passes again. Real
16
+ commands and output: [docs/examples.md](docs/examples.md). (The bloat step
17
+ is a scripted patch standing in for the agent, honestly -- see the caption
18
+ there.)
19
+
20
+ ## Quickstart
21
+
22
+ 1. **Install:** `pipx install ratchet-size` (or `pip install ratchet-size`,
23
+ or `pip install -e .` from a clone).
24
+ 2. **Record today's size:** `ratchet init` -- writes `.ratchet.json`; commit it.
25
+ 3. **Wire up your agent:** paste this sentence to it: *"Set up ratchet in
26
+ /path/to/repo by following
27
+ https://github.com/krishansubudhi/ratchet/blob/main/SETUP-FOR-AGENTS.md"*
28
+
29
+ That's it. Everything past this point is detail you can come back to: what
30
+ counts as a line, the four rules, grants, config, and per-harness
31
+ integrations.
32
+
33
+ ## Claude Code plugin
34
+
35
+ Prefer a plugin over the manual setup above? Ratchet ships as a Claude Code
36
+ plugin, with this repo doubling as its own marketplace:
37
+
38
+ ```text
39
+ /plugin marketplace add krishansubudhi/ratchet
40
+ /plugin install ratchet@ratchet
41
+ ```
42
+
43
+ That installs a **Stop** hook (`ratchet check --hook`, falling back to `uvx`
44
+ if `ratchet` isn't on `PATH` yet) so Claude Code can't end a turn with the
45
+ repo over budget, plus a `/ratchet:setup` command that runs the
46
+ [agent setup](SETUP-FOR-AGENTS.md) -- install, `ratchet init`, detect your
47
+ harness, `ratchet check` -- in the repo you're in. See
48
+ [`.claude-plugin/`](.claude-plugin) and [`hooks/hooks.json`](hooks/hooks.json)
49
+ for the manifests, or `claude plugin validate .` to check them yourself.
50
+
51
+ ## What it counts
52
+
53
+ **Scope:** the one git repository you run it in (`git rev-parse
54
+ --show-toplevel`). Other repos on the machine are never touched, and each
55
+ repo gets its own `.ratchet.json`.
56
+
57
+ **Which files:** git-tracked files, plus untracked files that aren't
58
+ gitignored, whose extension is one of the code extensions present at
59
+ `init` (`.py`, `.js`, `.ts`, `.go`, `.rs`, `.java`, `.rb`, `.c`, `.sh`, and
60
+ more -- see `CODE_EXTS` in `ratchet/measure.py`). Blank lines don't count.
61
+
62
+ **Source vs tests:** a file matching a test glob (`test_*`, `*_test.*`,
63
+ `*_tests.*`, `*.test.*`, `*.spec.*`, or living under `tests/`, `test/`,
64
+ `__tests__/`, `spec/`) is `tests`; everything else is `source`. Both groups
65
+ are plain config in `.ratchet.json` -- edit `include`/`exclude`/`extensions`
66
+ to carve out a different split, e.g. `"exclude": ["vendor/**", "**/*.gen.*"]`
67
+ to stop counting vendored or generated code.
68
+
69
+ **Existing repo:** `ratchet init` measures today's totals and sets the
70
+ ceilings there (plus optional `--slack`). A file already over
71
+ `max_file_lines` is frozen at its current size, so nothing is refused on
72
+ day one -- you start where you are. From then on a ceiling may only shrink,
73
+ unless a human runs `ratchet grant`.
74
+
75
+ ## What the agent sees when refused
76
+
77
+ ```
78
+ ratchet: REFUSED -- 2 problems (source 10,412/10,300, tests 4,980/5,000)
79
+
80
+ 1. source is 10,412 lines, ceiling 10,300: 112 over
81
+ grew vs HEAD: app/api.py +80, app/store.py +40
82
+ fix: remove at least 112 lines of source: delete dead code, reuse what
83
+ exists instead of adding a parallel version, simplify.
84
+ 2. app/api.py is 431 lines, limit 400
85
+ seams near the middle: line 198 `def upload`, line 230 `class Session`
86
+ fix: split it at a seam: move a cohesive group of definitions into a new
87
+ file, so each piece is under 400 lines.
88
+
89
+ Do not edit .ratchet.json or .ratchet-grants.jsonl to get past this.
90
+ If the growth is truly needed, stop and ask a human to run:
91
+ ratchet grant +112 --group source --reason "<why>" --by <name>
92
+ ratchet grant +31 --file app/api.py --reason "<why>" --by <name>
93
+ ```
94
+
95
+ Every refusal says what grew, by how much, and which fix applies: shrink,
96
+ split, or ask a human. For a harness, `ratchet check --json` gives the same
97
+ result as data.
98
+
99
+ ## The four rules
100
+
101
+ | Rule | What it does |
102
+ |------|--------------|
103
+ | **Budget** | Each group (`source`, `tests` by default) has a ceiling. A total over it is refused. |
104
+ | **Seams** | A file over `max_file_lines` (default 400) is refused, with the top-level definitions nearest the middle offered as places to cut. A file already over the limit at `init` is recorded at its size: it may shrink, never grow. |
105
+ | **Grants** | Only `ratchet grant` raises a limit, and every grant is appended to `.ratchet-grants.jsonl` with who and why. |
106
+ | **Tighten** | `ratchet tighten` lowers every ceiling to the measured size (plus optional `slack`). Nothing ever raises it automatically. |
107
+
108
+ Lines are counted without blanks. Docs, data, and fixtures don't count: only
109
+ files whose extensions you list in `.ratchet.json` do.
110
+
111
+ ## How grants work
112
+
113
+ Sometimes the growth is the point: a real feature, a new integration. Then a
114
+ human runs:
115
+
116
+ ```sh
117
+ ratchet grant +300 --group source --reason "CSV export (issue 41)" --by alice
118
+ ratchet grant +60 --file app/api.py --reason "until the router split lands" --by alice
119
+ ```
120
+
121
+ That raises the ceiling in `.ratchet.json` and appends a line to
122
+ `.ratchet-grants.jsonl`:
123
+
124
+ ```json
125
+ {"after": 10600, "before": 10300, "by": "alice", "lines": 300, "reason": "CSV export (issue 41)", "target": "source", "ts": "2026-01-15T09:30:00Z"}
126
+ ```
127
+
128
+ Commit both files. Here is what keeps grants honest:
129
+
130
+ - **In CI, run `ratchet check --base <target branch>`.** It refuses any ceiling
131
+ that is higher than the base's unless grants appended since the base pay for
132
+ it. An agent that edits `.ratchet.json` by hand passes locally and fails
133
+ review. The check also refuses a grants log that was rewritten rather than
134
+ appended to.
135
+ - Grants are one-line diffs in the pull request, so a reviewer sees them.
136
+ - Tell your agent never to run `ratchet grant`. The Claude Code integration
137
+ also puts it on the permission deny list.
138
+
139
+ Run `ratchet tighten` after a cleanup and commit the result. The room you
140
+ freed is now the new ceiling.
141
+
142
+ ## Running it by hand
143
+
144
+ `ratchet check` exits `0` when the repo is within budget, `1` when refused,
145
+ `2` on a setup error (bad config, not a git repo when `--base` is used, and
146
+ so on). `python -m ratchet` works the same as the `ratchet` entry point, if
147
+ you don't have it on `PATH`.
148
+
149
+ ## Integrations
150
+
151
+ | Harness | How |
152
+ |---------|-----|
153
+ | Claude Code | Stop hook `ratchet check --hook`: [integrations/claude-code](integrations/claude-code/README.md) |
154
+ | Cursor, Codex CLI, aider | instruction file + git hook / test-cmd: [integrations/other-agents.md](integrations/other-agents.md) |
155
+ | pre-commit | framework hook, or a plain git hook: [integrations/pre-commit](integrations/pre-commit/README.md) |
156
+ | GitHub Actions | `uses: krishansubudhi/ratchet@v0`: [integrations/github](integrations/github/README.md) |
157
+ | Anything else | prompt snippet: [integrations/any-agent.md](integrations/any-agent.md) |
158
+
159
+ ## Configuration
160
+
161
+ `ratchet init` writes `.ratchet.json`. It is JSON because ratchet must write
162
+ the file, and Python's standard library reads TOML but cannot write it.
163
+
164
+ ```json
165
+ {
166
+ "version": 1,
167
+ "max_file_lines": 400,
168
+ "slack": 0,
169
+ "groups": {
170
+ "source": {"include": ["**"], "exclude": ["**/tests/**", "**/test_*"], "extensions": [".py", ".ts"]},
171
+ "tests": {"include": ["**/tests/**", "**/test_*"], "extensions": [".py", ".ts"]}
172
+ },
173
+ "ceilings": {"source": 10300, "tests": 5000},
174
+ "file_ceilings": {"app/legacy.py": 1210}
175
+ }
176
+ ```
177
+
178
+ - Globs work like gitignore: `**/` means any number of directories.
179
+ - Each file belongs to the first group that matches it.
180
+ - `slack` keeps some headroom above the measured size after `init` and
181
+ `tighten`, if you would rather not require a delete for every add.
182
+ - Files are listed with `git ls-files`, so ignored files never count.
183
+ Outside git, ratchet walks the tree and skips the usual build and vendor
184
+ directories.
185
+
186
+ `ratchet init --max-file-lines 300 --slack 50` sets both at creation.
187
+
188
+ ## Requirements
189
+
190
+ Python 3.10+, git (optional, needed for `--base` and the "grew vs" lines). No
191
+ dependencies. Tests: `pip install pytest && python -m pytest -q`.
192
+
193
+ ## License
194
+
195
+ Apache-2.0. Copyright 2026 Krishan Subudhi.
@@ -0,0 +1,34 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "ratchet-size"
7
+ version = "0.1.0"
8
+ description = "A code-size budget that only goes down: keeps coding agents from bloating your repo."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "Apache-2.0"
12
+ authors = [{name = "Krishan Subudhi"}]
13
+ dependencies = []
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Topic :: Software Development :: Quality Assurance",
17
+ ]
18
+
19
+ [project.urls]
20
+ Homepage = "https://github.com/krishansubudhi/ratchet"
21
+ Repository = "https://github.com/krishansubudhi/ratchet"
22
+ Issues = "https://github.com/krishansubudhi/ratchet/issues"
23
+
24
+ [project.optional-dependencies]
25
+ test = ["pytest"]
26
+
27
+ [project.scripts]
28
+ ratchet = "ratchet.cli:main"
29
+
30
+ [tool.setuptools]
31
+ packages = ["ratchet"]
32
+
33
+ [tool.pytest.ini_options]
34
+ testpaths = ["tests"]
@@ -0,0 +1,5 @@
1
+ # Copyright 2026 Krishan Subudhi
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """ratchet: a code-size budget for repositories that coding agents work in."""
4
+
5
+ __version__ = "0.1.0"
@@ -0,0 +1,7 @@
1
+ # Copyright 2026 Krishan Subudhi
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ import sys
4
+
5
+ from .cli import main
6
+
7
+ sys.exit(main())