copilot-spend 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,221 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+ # Temporary file for partial code execution
204
+ tempCodeRunnerFile.py
205
+
206
+ # Ruff stuff:
207
+ .ruff_cache/
208
+
209
+ # PyPI configuration file
210
+ .pypirc
211
+
212
+ # Marimo
213
+ marimo/_static/
214
+ marimo/_lsp/
215
+ __marimo__/
216
+
217
+ # Streamlit
218
+ .streamlit/secrets.toml
219
+
220
+ # Project-internal planning docs (brainstorms, plans) — never ship to PyPI or git
221
+ docs/
@@ -0,0 +1,44 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-05-17
11
+
12
+ ### Added
13
+
14
+ - `copilot-spend` bare command: reads the current-period Copilot quota and
15
+ prints used PRUs, included allowance, billable overage (at $0.04/PRU), and
16
+ the period reset date.
17
+ - `copilot-spend login` subcommand: GitHub OAuth device flow against the
18
+ well-known VS Code Copilot GitHub App client ID, with re-auth detection,
19
+ `slow_down` handling, defensive timeout, SSRF host validation, and
20
+ post-login verification against `/copilot_internal/user` before any token
21
+ is persisted.
22
+ - `copilot-spend logout` subcommand: removes stored credentials
23
+ idempotently and cleans up any legacy `session.json` from earlier builds.
24
+ - Multi-source auth resolution: prefers the native
25
+ `~/.config/copilot-spend/auth.json`, falls back to an existing
26
+ `~/.local/share/opencode/auth.json` so users already authenticated to
27
+ opencode work without re-running login.
28
+ - GitHub Enterprise host support: device flow and quota fetch both target
29
+ `https://<ghe-host>/api/v3/...` when an enterprise host is configured.
30
+ - Single-hop bearer path: the `ghu_` or `gho_` user token is sent directly
31
+ as `Bearer` to `/copilot_internal/user` — no separate session-token
32
+ exchange, no `session.json` cache.
33
+ - Hardened on-disk secrets: `auth.json` written atomically with `0o600`
34
+ inside a `0o700` config directory; refuses to use a config directory
35
+ owned by a different uid or group/world writable.
36
+ - Token redaction in all user-facing error paths, including post-login
37
+ verification failures.
38
+ - Documented exit codes (`0` success, `1` unexpected, `2` auth, `3` API,
39
+ `4` no Copilot quota).
40
+ - Mermaid flowchart in the README covering the full login, logout, and
41
+ bare-invocation flows.
42
+
43
+ [Unreleased]: https://github.com/nkootstra/copilot-spend/compare/v0.1.0...HEAD
44
+ [0.1.0]: https://github.com/nkootstra/copilot-spend/releases/tag/v0.1.0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Niels
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,263 @@
1
+ Metadata-Version: 2.4
2
+ Name: copilot-spend
3
+ Version: 0.1.0
4
+ Summary: Print your current-period GitHub Copilot spend and reset date.
5
+ Project-URL: Homepage, https://github.com/nkootstra/copilot-spend
6
+ Project-URL: Source, https://github.com/nkootstra/copilot-spend
7
+ Project-URL: Issues, https://github.com/nkootstra/copilot-spend/issues
8
+ Project-URL: Changelog, https://github.com/nkootstra/copilot-spend/blob/main/CHANGELOG.md
9
+ Project-URL: Documentation, https://github.com/nkootstra/copilot-spend#readme
10
+ Author-email: Niels Kootstra <niels.kootstra@gmail.com>
11
+ License: MIT License
12
+
13
+ Copyright (c) 2026 Niels
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ SOFTWARE.
32
+ License-File: LICENSE
33
+ Keywords: cli,copilot,github,quota,spend
34
+ Classifier: Development Status :: 3 - Alpha
35
+ Classifier: Environment :: Console
36
+ Classifier: Intended Audience :: Developers
37
+ Classifier: License :: OSI Approved :: MIT License
38
+ Classifier: Operating System :: MacOS
39
+ Classifier: Operating System :: POSIX :: Linux
40
+ Classifier: Programming Language :: Python :: 3
41
+ Classifier: Programming Language :: Python :: 3.10
42
+ Classifier: Programming Language :: Python :: 3.11
43
+ Classifier: Programming Language :: Python :: 3.12
44
+ Classifier: Programming Language :: Python :: 3.13
45
+ Classifier: Topic :: Software Development
46
+ Classifier: Topic :: Utilities
47
+ Requires-Python: >=3.10
48
+ Provides-Extra: dev
49
+ Requires-Dist: pytest>=7; extra == 'dev'
50
+ Description-Content-Type: text/markdown
51
+
52
+ # copilot-spend
53
+
54
+ Find out what your Copilot habit actually costs.
55
+
56
+ A small Python CLI that reads your GitHub Copilot quota and prints
57
+ your current-period spend in dollars and PRUs, plus when the period resets.
58
+ Works against both `github.com` and GitHub Enterprise hosts.
59
+
60
+ ## How it works
61
+
62
+ ```mermaid
63
+ flowchart TD
64
+ Start([copilot-spend ...]) --> Cmd{subcommand?}
65
+
66
+ Cmd -- login --> Host[prompt: github.com or GHE host]
67
+ Host --> DeviceCode[POST /login/device/code]
68
+ DeviceCode --> ShowCode[show user code + verification URL]
69
+ ShowCode --> Poll[poll /login/oauth/access_token]
70
+ Poll -- access_denied / expired --> LoginFail[/exit 2: login failed/]
71
+ Poll -- ghu_ token --> Verify[verify token works<br/>GET /copilot_internal/user]
72
+ Verify -- fail --> LoginFail
73
+ Verify -- ok --> WriteAuth[write auth.json 0o600]
74
+ WriteAuth --> LoginDone[/exit 0/]
75
+
76
+ Cmd -- logout --> DeleteFiles[delete auth.json]
77
+ DeleteFiles --> LogoutDone[/exit 0/]
78
+
79
+ Cmd -- bare --> Native{native<br/>~/.config/copilot-spend/auth.json?}
80
+ Native -- yes --> NativeBearer[bearer = ghu_ token]
81
+ Native -- no --> Opencode{opencode<br/>~/.local/share/opencode/auth.json?}
82
+ Opencode -- yes --> OpencodeBearer[bearer = gho_ token]
83
+ Opencode -- no --> NoCreds[/exit 2: run copilot-spend login/]
84
+ NativeBearer --> Quota[GET /copilot_internal/user with Bearer]
85
+ OpencodeBearer --> Quota
86
+ Quota -- 200 --> Print[/print spend + reset date<br/>exit 0/]
87
+ Quota -- 401 / 403 --> AuthErr[/exit 2: re-authenticate/]
88
+ Quota -- 404 --> NoSub[/exit 4: no Copilot quota/]
89
+ Quota -- 5xx / timeout --> ApiErr[/exit 3: API error/]
90
+ ```
91
+
92
+ The bare `copilot-spend` invocation, expanded as numbered steps:
93
+
94
+ 1. Resolves a GitHub Copilot token from the first source that exists, in
95
+ this order:
96
+ 1. Native: `~/.config/copilot-spend/auth.json`, created by running
97
+ `copilot-spend login`.
98
+ 2. Opencode fallback: `~/.local/share/opencode/auth.json` (keys
99
+ `github-copilot.access` and `github-copilot.enterpriseUrl`), if
100
+ opencode is installed.
101
+ 2. Uses the resolved token directly as the `Bearer` for the next step.
102
+ Both source kinds work the same way: native gives a `ghu_…` GitHub App
103
+ user-to-server token, opencode gives a `gho_…` OAuth App token, and
104
+ `/copilot_internal/user` accepts either one directly.
105
+ 3. Calls `GET /api/v3/copilot_internal/user` on your GHE host, or
106
+ `GET https://api.github.com/copilot_internal/user` if no enterprise
107
+ host is configured.
108
+ 4. Computes the billable overage:
109
+ `billable_PRUs = max(0, consumed - entitlement)`, then
110
+ `dollars_owed = billable_PRUs × $0.04`.
111
+ The first `entitlement` PRUs each period are included with your plan
112
+ and cost nothing.
113
+ 5. Prints a plain-text summary on stdout.
114
+
115
+ No background daemon. No history. No session-token cache. One HTTP
116
+ request per run.
117
+
118
+ ## Requirements
119
+
120
+ - Python 3.10 or newer
121
+ - macOS or Linux
122
+ - A GitHub Copilot token, obtained by either:
123
+ - running `copilot-spend login`, or
124
+ - having opencode installed and authenticated already
125
+
126
+ ## Install
127
+
128
+ ```sh
129
+ # Option A: pipx (persistent, isolated)
130
+ pipx install copilot-spend
131
+
132
+ # Option B: pip (into your active environment or --user)
133
+ pip install copilot-spend
134
+
135
+ # Option C: uv tool (persistent, isolated)
136
+ uv tool install copilot-spend
137
+
138
+ # Option D: one-off run, no install
139
+ uvx copilot-spend
140
+ pipx run copilot-spend
141
+ ```
142
+
143
+ ### Install from source
144
+
145
+ ```sh
146
+ git clone https://github.com/nkootstra/copilot-spend.git
147
+ cd copilot-spend
148
+
149
+ pipx install .
150
+ # or:
151
+ uv tool install --from . copilot-spend
152
+ # or one-off:
153
+ uvx --from . copilot-spend
154
+ ```
155
+
156
+ ## Usage
157
+
158
+ ```sh
159
+ copilot-spend # print current-period quota
160
+ copilot-spend login # authenticate via GitHub OAuth device flow
161
+ copilot-spend logout # remove copilot-spend's stored credentials
162
+ ```
163
+
164
+ `copilot-spend login` prompts for github.com or a GHE host, shows a
165
+ device code with a URL to visit, then polls until you complete the
166
+ flow in your browser. The new token is verified against
167
+ `/copilot_internal/user` before anything gets persisted. Credentials
168
+ land in `~/.config/copilot-spend/auth.json` (mode `0o600`). If you
169
+ already have opencode authenticated, the bare `copilot-spend` command
170
+ continues to work without login.
171
+
172
+ Example output (under your allowance):
173
+
174
+ ```
175
+ GitHub Copilot - your-login (business)
176
+ Used: 221 PRUs
177
+ Allowance: $12.00 (300 PRUs included)
178
+ Remaining: $3.16 (79 PRUs of free allowance left)
179
+ Resets: May 31, 2026 (in 15 days)
180
+ ```
181
+
182
+ Example output (over your allowance — billable overage):
183
+
184
+ ```
185
+ GitHub Copilot - your-login (business)
186
+ Used: 4073 PRUs
187
+ Allowance: $12.00 (300 PRUs included)
188
+ Billable: $150.92 (3773 PRUs over allowance at $0.04/PRU)
189
+ Resets: Jun 01, 2026 (in 16 days)
190
+ ```
191
+
192
+ Flags: `--help`, `--version`. Subcommands: `login`, `logout`.
193
+
194
+ ## Exit codes
195
+
196
+ | Code | Meaning |
197
+ |------|---------|
198
+ | 0 | Success |
199
+ | 1 | Unexpected error |
200
+ | 2 | Auth error (missing/invalid `auth.json` or token) |
201
+ | 3 | API error (network, timeout, 4xx/5xx) |
202
+ | 4 | No Copilot quota on the account |
203
+
204
+ ## Switch to your own GitHub App
205
+
206
+ `copilot-spend login` runs the GitHub OAuth device flow against
207
+ Microsoft's well-known VS Code Copilot GitHub App
208
+ (`Iv1.b507a08c87ecfe98`). This is the same client ID used by every
209
+ working third-party Copilot tool — copilot.vim, avante.nvim, LiteLLM,
210
+ and others — because GitHub's session-token exchange endpoint
211
+ (`/copilot_internal/v2/token`) only accepts tokens issued by a GitHub
212
+ App, not by an OAuth App.
213
+
214
+ The trade-off: the GitHub consent screen during login says "GitHub for
215
+ VS Code" rather than "copilot-spend", and you depend on Microsoft not
216
+ rotating that app. To remove both, register your own GitHub App and
217
+ swap the constant:
218
+
219
+ 1. Visit https://github.com/settings/apps and click **New GitHub App**.
220
+ This must be a *GitHub App*, not an *OAuth App* — OAuth Apps issue
221
+ `gho_…` tokens that the Copilot exchange endpoint rejects with 404.
222
+ 2. Set Homepage URL and Callback URL to anything (the device flow does
223
+ not use them).
224
+ 3. Enable **Device flow** under "Identifying and authorizing users".
225
+ 4. Account permissions: none required beyond user identification.
226
+ The `read:user` OAuth scope is enough.
227
+ 5. Note the resulting **Client ID** (starts with `Iv23` or `Iv1.`).
228
+ 6. Replace the `CLIENT_ID` constant in
229
+ `src/copilot_spend/login.py` with your new client ID.
230
+ 7. Rebuild/reinstall (`pipx install --force .` or
231
+ `uv tool install --force --from . copilot-spend`).
232
+
233
+ After the swap, the consent screen shows your app's name and your
234
+ copilot-spend install no longer breaks if Microsoft rotates
235
+ `Iv1.b507a08c87ecfe98`.
236
+
237
+ ## Caveats
238
+
239
+ - The PRU price is hardcoded at $0.04 (correct as of 2026-05). Update the
240
+ constant in `src/copilot_spend/quota.py` if GitHub changes it.
241
+ - v1 ships with VS Code's GitHub App ID `Iv1.b507a08c87ecfe98` for the
242
+ device flow. See "Switch to your own GitHub App" above to remove the
243
+ dependency.
244
+ - The billing model assumed: the first `entitlement` PRUs each period are
245
+ included with your plan, and anything beyond that is billable at $0.04
246
+ per PRU. This matches observed behavior on a business plan. Org-level
247
+ caps or contracts may change what you actually pay — treat the
248
+ `Billable` figure as a personal estimate, not an invoice.
249
+ - The reset-date field name in the Copilot API response is best-effort:
250
+ `copilot-spend` tries the field names observed on a real response, plus
251
+ a few defensive fallbacks, and prints `next reset: unknown` if none
252
+ match. Adjust `RESET_FIELD_CANDIDATES` in `quota.py` if your response
253
+ uses a different name.
254
+ - The `/copilot_internal/user` endpoint is not a public, documented API.
255
+ GitHub may change its shape at any time.
256
+
257
+ ## Development
258
+
259
+ ```sh
260
+ python -m venv .venv
261
+ .venv/bin/pip install -e ".[dev]"
262
+ .venv/bin/pytest
263
+ ```