agentcap-cli 0.4.1__tar.gz → 0.5.1__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.
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/PKG-INFO +91 -10
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/README.md +90 -9
- agentcap_cli-0.5.1/plugin/LICENSE +21 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/pyproject.toml +1 -1
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/__init__.py +1 -1
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/cli.py +1 -1
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/harness.py +11 -4
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/live_quota.py +19 -5
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/proxy.py +79 -22
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/quota.py +210 -37
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/conftest.py +7 -0
- agentcap_cli-0.5.1/tests/test_agent_set.py +265 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_cc_plugin.py +203 -8
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_cli.py +23 -0
- agentcap_cli-0.5.1/tests/test_codex_plugin.py +436 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_harness.py +16 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_live_quota.py +21 -2
- agentcap_cli-0.5.1/tests/test_proxy_cancel.py +164 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_proxy_core.py +56 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_quota.py +65 -8
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_self_approve.py +4 -0
- agentcap_cli-0.4.1/tests/test_proxy_cancel.py +0 -98
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/.gitignore +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/LICENSE +0 -0
- {agentcap_cli-0.4.1/plugin → agentcap_cli-0.5.1/codex-plugin}/LICENSE +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/landing/v2/README.md +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/_lifeline_exec.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/_lifeline_watch.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/adapters.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/alerts.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/analytics.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/budget.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/clamp.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/cli_history.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/data/pricing.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/demo.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/devtools/__init__.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/devtools/fake_upstream.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/estimate.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/eta.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/failsafe.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/history.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/ledger.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/license.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/lifeline.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/logschema.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/loop.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/plans.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/pricing.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/private.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/procstop.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/report.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/runner.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/upstreams.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/usage.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/src/agentcap/waste.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/agents/claude/projects/-home-dev-shop/sess-p/subagents/agent-a1b2c3.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/agents/claude/projects/-home-dev-shop/sess-p/subagents/agent-d4e5f6.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/agents/claude/projects/-home-dev-shop/sess-p.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/agents/codex/sessions/2026/10/29/rollout-2026-10-29T09-00-00-thr-parent.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/agents/codex/sessions/2026/10/29/rollout-2026-10-29T09-00-50-thr-child.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/agents/codex/sessions/2026/10/29/rollout-2026-10-29T09-01-20-thr-fork.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/agy-1.4.11-2026-10-30.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/claude-2.1.316-pretooluse.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/claude-2.1.316-statusline.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/codex-rollout-2026-10-29.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/opencode-2026-10-30.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/history/claude/.credentials.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/history/claude/projects/-home-dev-shop/1f0c-sess-a.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/history/codex/auth.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/history/codex/sessions/2026/10/29/rollout-2026-10-29T10-00-00-0199aaaa-bbbb-cccc-dddd-eeeeeeeeeeee.jsonl +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/upstream/README.md +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/upstream/anthropic_403_permission_error.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openai_403_region_blocked.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openrouter_401_user_not_found.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openrouter_403_moderation.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openrouter_429_rate_limited.json +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/license_support.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/proxy_support.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_adapters.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_agents.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_alerts.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_analytics.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_anthropic_proxy.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_anthropic_units.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_budget.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_child_status.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_clamp.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_claude_adapter_and_tiers.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_concurrent_forgive.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_e2e_claude.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_estimate.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_exit_codes.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_failsafe.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_history.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_ledger.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_license.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_lifeline.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_log_contract.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_log_contract_live.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_loop.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_plugin_package.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_pricing.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_private_dirs.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_procstop.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_proxy_auth.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_proxy_extras.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_proxy_streaming.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_release.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_report_cli.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_runaway.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_runner.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_stop_matrix.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_upstream_refusals.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_upstreams.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_usage.py +0 -0
- {agentcap_cli-0.4.1 → agentcap_cli-0.5.1}/tests/test_zombie_collector.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: agentcap-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: Local-first quota and cost guard for coding agents: per-job share of Claude Code/Codex/Gemini subscription quota, loop breaker, evidence receipt.
|
|
5
5
|
Project-URL: Homepage, https://agentcap.launchbench.studio
|
|
6
6
|
Project-URL: Repository, https://github.com/Blitz00X/agentcap
|
|
@@ -277,11 +277,30 @@ The easiest path on a Claude Max or Pro plan. Inside Claude Code:
|
|
|
277
277
|
/plugin marketplace add Blitz00X/agentcap
|
|
278
278
|
/plugin install agentcap@agentcap
|
|
279
279
|
/agentcap:setup # once: adds AgentCap's read-only statusLine (your own line keeps showing)
|
|
280
|
-
/agentcap:
|
|
281
|
-
/agentcap:
|
|
280
|
+
/agentcap:default 10% 5h # every new session (also `claude -p`) starts with this limit
|
|
281
|
+
/agentcap:quota 5% 5h # this session may use 5 more points of the 5-hour quota (`week` for weekly)
|
|
282
|
+
/agentcap:quota until 70% 5h # or: stop when the 5-hour quota reaches 70%
|
|
283
|
+
/agentcap:status # 5-hour quota at 32% now → stops at 37% (this session used 3.1 of 5 points)
|
|
282
284
|
/agentcap:off # clear it; /agentcap:unsetup restores your old statusLine
|
|
283
285
|
```
|
|
284
286
|
|
|
287
|
+
`/agentcap:setup` is optional: the quota is measured with `/usage`; setup only adds a live
|
|
288
|
+
statusline meter.
|
|
289
|
+
|
|
290
|
+
**Two ways to say the limit.** `5% 5h` means *5 more points* from where the 5-hour quota is when
|
|
291
|
+
you type it: at 20% it stops at 25%. `until 70% 5h` means *stop when the 5-hour quota reaches
|
|
292
|
+
70%*, wherever it is now. Every AgentCap message says both numbers, e.g. `5-hour quota at 20% now
|
|
293
|
+
→ stops at 25%`, so you never have to work it out. The same two forms work in Codex
|
|
294
|
+
(`agentcap 5% 5h`, `agentcap until 70% 5h`) and for defaults (`default until 70% 5h`).
|
|
295
|
+
|
|
296
|
+
**Or just ask the agent.** Say it in your own words, e.g. *"this session may use at most half
|
|
297
|
+
of the 5-hour quota"* or *"stop at 70% of the 5-hour quota"*. The agent turns it into
|
|
298
|
+
`echo agentcap set 50% 5h` (or `... set until 70% 5h`) and Claude Code asks you to approve it,
|
|
299
|
+
with the stop point in the prompt: one button. AgentCap sets the limit only after you approve.
|
|
300
|
+
The agent may only set or tighten a limit; raising it (`/agentcap:approve`, a higher
|
|
301
|
+
`/agentcap:quota`) and clearing it (`/agentcap:off`) stay yours. `approve` raises by 50% at most
|
|
302
|
+
twice; after that, set any new limit with `/agentcap:quota` or remove it with `/agentcap:off`.
|
|
303
|
+
|
|
285
304
|
No pip install: the plugin is stdlib Python (`python3` on PATH). The marketplace installs only
|
|
286
305
|
the self-contained `plugin/` folder (commands, hooks, the script and the few `agentcap` modules it
|
|
287
306
|
imports), not the whole repo. Contributors: `plugin/` is generated, edit the sources and run
|
|
@@ -309,19 +328,77 @@ How it works:
|
|
|
309
328
|
files are deleted after 7 days (on `SessionStart`).
|
|
310
329
|
- `/usage` as well: `/agentcap:quota` takes its baseline from Claude Code's own
|
|
311
330
|
`claude -p /usage` (a local command, no model call, no quota), and while a quota is set the
|
|
312
|
-
`PreToolUse` hook starts a background `/usage` read at most
|
|
331
|
+
`PreToolUse` hook starts a background `/usage` read at most every 30 s. The share is the
|
|
313
332
|
higher of the statusline and `/usage` (the statusline lagged `/usage` by 1-2 points in live
|
|
314
333
|
tests, and a stale first value once looked like 1% of use). Without `/usage` the baseline is
|
|
315
334
|
only this session's own statusline reading, never another session's.
|
|
316
335
|
- Read-only: no OAuth token, credentials file or API traffic is touched.
|
|
317
336
|
|
|
318
337
|
Honest limits: the % is account-level (parallel sessions and other devices count), the stop can
|
|
319
|
-
be one request late (a `/usage` check runs at most
|
|
338
|
+
be one request late (a `/usage` check runs at most every 30 s), and the share updates when the
|
|
320
339
|
statusLine refreshes. Verified live on a real Claude Pro account with Claude Code 2.1.323
|
|
321
340
|
(13 Nov 2026): `/agentcap:setup`, `/agentcap:quota 2% 5h` with its baseline from `/usage`,
|
|
322
341
|
the stop at the limit, `/agentcap:approve`, `/agentcap:unsetup`, and the self-guard denying
|
|
323
342
|
`env -u CLAUDECODE agentcap approve` from the model.
|
|
324
343
|
|
|
344
|
+
When the quota hook ends a headless `claude -p` turn, the command can exit 0 with no output.
|
|
345
|
+
The plugin writes a receipt on the first quota denial (verified live on 14 Nov 2026: a 2% default
|
|
346
|
+
stopped a `claude -p` job with a `quota_exceeded` receipt, matching `/usage`).
|
|
347
|
+
In an isolated Claude Code 2.1.325 test, `--output-format json` reported
|
|
348
|
+
`terminal_reason: "hook_stopped"` and the denied tool in `permission_denials`. A hook can stop
|
|
349
|
+
for other reasons too, so check the receipt's `reason` to identify a quota denial.
|
|
350
|
+
|
|
351
|
+
## Install as a Codex plugin (ChatGPT subscriptions)
|
|
352
|
+
|
|
353
|
+
Codex 0.167+ (verified on 0.167.1). Inside Codex:
|
|
354
|
+
|
|
355
|
+
```
|
|
356
|
+
/plugins # Add Marketplace → Blitz00X/agentcap → AgentCap → Install
|
|
357
|
+
# then trust AgentCap's 5 hooks when Codex asks
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Or from a shell:
|
|
361
|
+
|
|
362
|
+
```bash
|
|
363
|
+
codex plugin marketplace add Blitz00X/agentcap
|
|
364
|
+
codex plugin add agentcap@agentcap
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
No pip install: the hooks run `python3 -B -E -P` on the plugin's own script, which asks Codex's
|
|
368
|
+
`app-server` and reads the session rollout (read-only; no token or traffic is touched). Type
|
|
369
|
+
these as a prompt: a `UserPromptSubmit` hook handles them and they never reach the model, so no
|
|
370
|
+
quota is spent and only you can type them.
|
|
371
|
+
|
|
372
|
+
| Type in Codex | Effect |
|
|
373
|
+
| --- | --- |
|
|
374
|
+
| `agentcap 10% 5h` | this session may use 10 more points of the 5-hour quota (`week` for the weekly quota) |
|
|
375
|
+
| `agentcap until 70% 5h` | stop when the 5-hour quota reaches 70% |
|
|
376
|
+
| `agentcap status` | current share of the limit |
|
|
377
|
+
| `agentcap off` | clear this session's limit |
|
|
378
|
+
| `agentcap approve` | raise the limit by 50% after a stop (Pro, at most twice) |
|
|
379
|
+
| `agentcap default 10% 5h` | every new session, `codex exec` included, starts with this limit |
|
|
380
|
+
| `agentcap default off` | no default limit |
|
|
381
|
+
|
|
382
|
+
Or ask Codex in your own words (*"use at most half of the 5-hour quota"*): it runs
|
|
383
|
+
`echo agentcap set 50% 5h` with escalated permissions, Codex shows its approval prompt (*Would you
|
|
384
|
+
like to run the following command? … Yes, proceed*), and AgentCap sets the limit after you approve.
|
|
385
|
+
The agent may only set or tighten a limit; raising and clearing stay yours (`agentcap approve`,
|
|
386
|
+
`agentcap <N%>`, `agentcap off`).
|
|
387
|
+
|
|
388
|
+
`PreToolUse` denies a tool call at the limit, on the 4th identical call in a row and after 8
|
|
389
|
+
failing calls in a row; `sleep N` waits count for neither. Quota comes from `codex app-server`
|
|
390
|
+
(at set time, every 30 s and at turn end) plus the rollout.
|
|
391
|
+
|
|
392
|
+
Honest limits: Codex hooks need your trust, once per installed version: Codex fingerprints the
|
|
393
|
+
plugin, so **after every AgentCap update open Codex once and trust its hooks again**; until then
|
|
394
|
+
Codex shows `hook: ... Failed` and AgentCap is not active (seen live after 0.4.2 → 0.5.0).
|
|
395
|
+
A denial that says `agentcap guard (not a quota stop)` only refuses that one command (approving
|
|
396
|
+
your own limit, or writing to AgentCap's state or installed plugin); reading those files and
|
|
397
|
+
editing or testing your project are not blocked. Codex 0.167 cannot end a turn from a hook,
|
|
398
|
+
so every tool call at the limit is denied with the reason and the model is expected to stop and
|
|
399
|
+
tell you to type `agentcap approve` (it did in live tests). The % is account-wide, and one
|
|
400
|
+
response or one 30 s check can overshoot.
|
|
401
|
+
|
|
325
402
|
## Works with Claude Max / ChatGPT subscriptions (quota mode)
|
|
326
403
|
|
|
327
404
|
On a subscription there is no dollar bill, but there is a weekly and a 5-hour usage limit.
|
|
@@ -339,7 +416,9 @@ agentcap quota # read-only: current usage, asked
|
|
|
339
416
|
- **Claude Code:** for this run only, AgentCap adds a settings layer with `claude --settings <file>`
|
|
340
417
|
(your `~/.claude/settings.json` and project settings are never edited). It contains a
|
|
341
418
|
statusline command that records the official `rate_limits` (`five_hour` / `seven_day`
|
|
342
|
-
`used_percentage`, `resets_at`) and then runs **your own statusline command unchanged
|
|
419
|
+
`used_percentage`, `resets_at`) and then runs **your own statusline command unchanged**
|
|
420
|
+
(only the one in your user-level `~/.claude/settings.json`: a project's `statusLine` is never
|
|
421
|
+
run, since the project may be untrusted),
|
|
343
422
|
plus a `PreToolUse` hook that denies further tool calls with a clear reason once the job's
|
|
344
423
|
share reaches the limit and ends the turn (`continue: false`). An interactive session stays open
|
|
345
424
|
(run `agentcap approve` to continue, or quit); a session where the hook never fired gets
|
|
@@ -481,12 +560,14 @@ Models without a bundled price (for example OpenRouter or Ollama ids) are blocke
|
|
|
481
560
|
### Harness support matrix (real runs)
|
|
482
561
|
|
|
483
562
|
Legend: ✅ verified on a real account/endpoint · ⚠️ works with a limitation · ❌ not supported · **needs X** = untested, X blocks it.
|
|
563
|
+
Exit codes in the headings describe the wrapper. In-session plugins deny tool calls; as noted
|
|
564
|
+
above, a headless Claude turn can exit 0 after a denial.
|
|
484
565
|
|
|
485
|
-
| Harness + auth | Loop breaker (4x same call, exit 4) | Error breaker (8 failures, exit 4) | Quota % stop (exit 3) | $ / token cap |
|
|
566
|
+
| Harness + auth | Loop breaker (4x same call, wrapper exit 4) | Error breaker (8 failures, wrapper exit 4) | Quota % stop (wrapper exit 3) | $ / token cap |
|
|
486
567
|
| --- | --- | --- | --- | --- |
|
|
487
|
-
| Claude Code, Pro/Max sub, interactive | ✅ (deny keeps the session; resumes after approve) | ✅ | ✅ statusline + `/usage` every
|
|
488
|
-
| Claude Code, Pro/Max sub, headless `claude -p` | ✅ (exit 4) | ✅ (exit 4) | ✅ via `/usage` every 30 s (coarser; live: a 2% cap ended at 4%) | ❌ by design (refused) |
|
|
489
|
-
| Codex, ChatGPT sub | ✅ | ✅ | ✅ `exec` and interactive TUI (whole %; warns under 1%; one-response timeout, default 5m; terminal restored after a TUI stop; approve mid-run; live % from `codex app-server` at start, every 30 s and after exit). Live, Codex 0.167.1, Plus, 11-12 Nov 2026 | ❌ by design (no proxy on a subscription) |
|
|
568
|
+
| Claude Code, Pro/Max sub, interactive | ✅ (deny keeps the session; resumes after approve) | ✅ | ✅ statusline + `/usage` every 30 s (deny keeps the session); `agentcap approve` ✅; plugin. Live, Claude Code 2.1.322/323, Pro, 12-13 Nov 2026; in-session plugin, 14 Nov | ❌ by design (no proxy on a subscription login; AgentCap refuses) |
|
|
569
|
+
| Claude Code, Pro/Max sub, headless `claude -p` | ✅ (exit 4) | ✅ (exit 4) | ✅ via `/usage` every 30 s (coarser; live: a 2% cap ended at 4%); plugin, default limit for `claude -p` | ❌ by design (refused) |
|
|
570
|
+
| Codex, ChatGPT sub | ✅ | ✅ | ✅ `exec` and interactive TUI (whole %; warns under 1%; one-response timeout, default 5m; terminal restored after a TUI stop; approve mid-run; live % from `codex app-server` at start, every 30 s and after exit). Live, Codex 0.167.1, Plus, 11-12 Nov 2026; in-session plugin (TUI + exec), 14 Nov with test-local hooks; installed-plugin trust flow not yet verified | ❌ by design (no proxy on a subscription) |
|
|
490
571
|
| opencode Go | ✅ | ✅ | ❌ not yet: opencode 1.x's session log reports Go cost as 0 and leaves out subagents (live test, 11 Nov 2026), so a Go % cap cannot be measured; do not rely on `--quota` with opencode | ✅ `--tokens`; `--log-cap` uses opencode's own price table |
|
|
491
572
|
| agy, Google sub (plain `agentcap run -- agy` needs no key: breakers only) | ✅ | ✅ | ✅ (tested at 0.5%; ~10 s polling, can overshoot) | ❌ (sub); $ only via Gemini API |
|
|
492
573
|
| opencode + OpenRouter (free model) | ✅ | ✅ | n/a | ✅ $ cap |
|
|
@@ -247,11 +247,30 @@ The easiest path on a Claude Max or Pro plan. Inside Claude Code:
|
|
|
247
247
|
/plugin marketplace add Blitz00X/agentcap
|
|
248
248
|
/plugin install agentcap@agentcap
|
|
249
249
|
/agentcap:setup # once: adds AgentCap's read-only statusLine (your own line keeps showing)
|
|
250
|
-
/agentcap:
|
|
251
|
-
/agentcap:
|
|
250
|
+
/agentcap:default 10% 5h # every new session (also `claude -p`) starts with this limit
|
|
251
|
+
/agentcap:quota 5% 5h # this session may use 5 more points of the 5-hour quota (`week` for weekly)
|
|
252
|
+
/agentcap:quota until 70% 5h # or: stop when the 5-hour quota reaches 70%
|
|
253
|
+
/agentcap:status # 5-hour quota at 32% now → stops at 37% (this session used 3.1 of 5 points)
|
|
252
254
|
/agentcap:off # clear it; /agentcap:unsetup restores your old statusLine
|
|
253
255
|
```
|
|
254
256
|
|
|
257
|
+
`/agentcap:setup` is optional: the quota is measured with `/usage`; setup only adds a live
|
|
258
|
+
statusline meter.
|
|
259
|
+
|
|
260
|
+
**Two ways to say the limit.** `5% 5h` means *5 more points* from where the 5-hour quota is when
|
|
261
|
+
you type it: at 20% it stops at 25%. `until 70% 5h` means *stop when the 5-hour quota reaches
|
|
262
|
+
70%*, wherever it is now. Every AgentCap message says both numbers, e.g. `5-hour quota at 20% now
|
|
263
|
+
→ stops at 25%`, so you never have to work it out. The same two forms work in Codex
|
|
264
|
+
(`agentcap 5% 5h`, `agentcap until 70% 5h`) and for defaults (`default until 70% 5h`).
|
|
265
|
+
|
|
266
|
+
**Or just ask the agent.** Say it in your own words, e.g. *"this session may use at most half
|
|
267
|
+
of the 5-hour quota"* or *"stop at 70% of the 5-hour quota"*. The agent turns it into
|
|
268
|
+
`echo agentcap set 50% 5h` (or `... set until 70% 5h`) and Claude Code asks you to approve it,
|
|
269
|
+
with the stop point in the prompt: one button. AgentCap sets the limit only after you approve.
|
|
270
|
+
The agent may only set or tighten a limit; raising it (`/agentcap:approve`, a higher
|
|
271
|
+
`/agentcap:quota`) and clearing it (`/agentcap:off`) stay yours. `approve` raises by 50% at most
|
|
272
|
+
twice; after that, set any new limit with `/agentcap:quota` or remove it with `/agentcap:off`.
|
|
273
|
+
|
|
255
274
|
No pip install: the plugin is stdlib Python (`python3` on PATH). The marketplace installs only
|
|
256
275
|
the self-contained `plugin/` folder (commands, hooks, the script and the few `agentcap` modules it
|
|
257
276
|
imports), not the whole repo. Contributors: `plugin/` is generated, edit the sources and run
|
|
@@ -279,19 +298,77 @@ How it works:
|
|
|
279
298
|
files are deleted after 7 days (on `SessionStart`).
|
|
280
299
|
- `/usage` as well: `/agentcap:quota` takes its baseline from Claude Code's own
|
|
281
300
|
`claude -p /usage` (a local command, no model call, no quota), and while a quota is set the
|
|
282
|
-
`PreToolUse` hook starts a background `/usage` read at most
|
|
301
|
+
`PreToolUse` hook starts a background `/usage` read at most every 30 s. The share is the
|
|
283
302
|
higher of the statusline and `/usage` (the statusline lagged `/usage` by 1-2 points in live
|
|
284
303
|
tests, and a stale first value once looked like 1% of use). Without `/usage` the baseline is
|
|
285
304
|
only this session's own statusline reading, never another session's.
|
|
286
305
|
- Read-only: no OAuth token, credentials file or API traffic is touched.
|
|
287
306
|
|
|
288
307
|
Honest limits: the % is account-level (parallel sessions and other devices count), the stop can
|
|
289
|
-
be one request late (a `/usage` check runs at most
|
|
308
|
+
be one request late (a `/usage` check runs at most every 30 s), and the share updates when the
|
|
290
309
|
statusLine refreshes. Verified live on a real Claude Pro account with Claude Code 2.1.323
|
|
291
310
|
(13 Nov 2026): `/agentcap:setup`, `/agentcap:quota 2% 5h` with its baseline from `/usage`,
|
|
292
311
|
the stop at the limit, `/agentcap:approve`, `/agentcap:unsetup`, and the self-guard denying
|
|
293
312
|
`env -u CLAUDECODE agentcap approve` from the model.
|
|
294
313
|
|
|
314
|
+
When the quota hook ends a headless `claude -p` turn, the command can exit 0 with no output.
|
|
315
|
+
The plugin writes a receipt on the first quota denial (verified live on 14 Nov 2026: a 2% default
|
|
316
|
+
stopped a `claude -p` job with a `quota_exceeded` receipt, matching `/usage`).
|
|
317
|
+
In an isolated Claude Code 2.1.325 test, `--output-format json` reported
|
|
318
|
+
`terminal_reason: "hook_stopped"` and the denied tool in `permission_denials`. A hook can stop
|
|
319
|
+
for other reasons too, so check the receipt's `reason` to identify a quota denial.
|
|
320
|
+
|
|
321
|
+
## Install as a Codex plugin (ChatGPT subscriptions)
|
|
322
|
+
|
|
323
|
+
Codex 0.167+ (verified on 0.167.1). Inside Codex:
|
|
324
|
+
|
|
325
|
+
```
|
|
326
|
+
/plugins # Add Marketplace → Blitz00X/agentcap → AgentCap → Install
|
|
327
|
+
# then trust AgentCap's 5 hooks when Codex asks
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Or from a shell:
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
codex plugin marketplace add Blitz00X/agentcap
|
|
334
|
+
codex plugin add agentcap@agentcap
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
No pip install: the hooks run `python3 -B -E -P` on the plugin's own script, which asks Codex's
|
|
338
|
+
`app-server` and reads the session rollout (read-only; no token or traffic is touched). Type
|
|
339
|
+
these as a prompt: a `UserPromptSubmit` hook handles them and they never reach the model, so no
|
|
340
|
+
quota is spent and only you can type them.
|
|
341
|
+
|
|
342
|
+
| Type in Codex | Effect |
|
|
343
|
+
| --- | --- |
|
|
344
|
+
| `agentcap 10% 5h` | this session may use 10 more points of the 5-hour quota (`week` for the weekly quota) |
|
|
345
|
+
| `agentcap until 70% 5h` | stop when the 5-hour quota reaches 70% |
|
|
346
|
+
| `agentcap status` | current share of the limit |
|
|
347
|
+
| `agentcap off` | clear this session's limit |
|
|
348
|
+
| `agentcap approve` | raise the limit by 50% after a stop (Pro, at most twice) |
|
|
349
|
+
| `agentcap default 10% 5h` | every new session, `codex exec` included, starts with this limit |
|
|
350
|
+
| `agentcap default off` | no default limit |
|
|
351
|
+
|
|
352
|
+
Or ask Codex in your own words (*"use at most half of the 5-hour quota"*): it runs
|
|
353
|
+
`echo agentcap set 50% 5h` with escalated permissions, Codex shows its approval prompt (*Would you
|
|
354
|
+
like to run the following command? … Yes, proceed*), and AgentCap sets the limit after you approve.
|
|
355
|
+
The agent may only set or tighten a limit; raising and clearing stay yours (`agentcap approve`,
|
|
356
|
+
`agentcap <N%>`, `agentcap off`).
|
|
357
|
+
|
|
358
|
+
`PreToolUse` denies a tool call at the limit, on the 4th identical call in a row and after 8
|
|
359
|
+
failing calls in a row; `sleep N` waits count for neither. Quota comes from `codex app-server`
|
|
360
|
+
(at set time, every 30 s and at turn end) plus the rollout.
|
|
361
|
+
|
|
362
|
+
Honest limits: Codex hooks need your trust, once per installed version: Codex fingerprints the
|
|
363
|
+
plugin, so **after every AgentCap update open Codex once and trust its hooks again**; until then
|
|
364
|
+
Codex shows `hook: ... Failed` and AgentCap is not active (seen live after 0.4.2 → 0.5.0).
|
|
365
|
+
A denial that says `agentcap guard (not a quota stop)` only refuses that one command (approving
|
|
366
|
+
your own limit, or writing to AgentCap's state or installed plugin); reading those files and
|
|
367
|
+
editing or testing your project are not blocked. Codex 0.167 cannot end a turn from a hook,
|
|
368
|
+
so every tool call at the limit is denied with the reason and the model is expected to stop and
|
|
369
|
+
tell you to type `agentcap approve` (it did in live tests). The % is account-wide, and one
|
|
370
|
+
response or one 30 s check can overshoot.
|
|
371
|
+
|
|
295
372
|
## Works with Claude Max / ChatGPT subscriptions (quota mode)
|
|
296
373
|
|
|
297
374
|
On a subscription there is no dollar bill, but there is a weekly and a 5-hour usage limit.
|
|
@@ -309,7 +386,9 @@ agentcap quota # read-only: current usage, asked
|
|
|
309
386
|
- **Claude Code:** for this run only, AgentCap adds a settings layer with `claude --settings <file>`
|
|
310
387
|
(your `~/.claude/settings.json` and project settings are never edited). It contains a
|
|
311
388
|
statusline command that records the official `rate_limits` (`five_hour` / `seven_day`
|
|
312
|
-
`used_percentage`, `resets_at`) and then runs **your own statusline command unchanged
|
|
389
|
+
`used_percentage`, `resets_at`) and then runs **your own statusline command unchanged**
|
|
390
|
+
(only the one in your user-level `~/.claude/settings.json`: a project's `statusLine` is never
|
|
391
|
+
run, since the project may be untrusted),
|
|
313
392
|
plus a `PreToolUse` hook that denies further tool calls with a clear reason once the job's
|
|
314
393
|
share reaches the limit and ends the turn (`continue: false`). An interactive session stays open
|
|
315
394
|
(run `agentcap approve` to continue, or quit); a session where the hook never fired gets
|
|
@@ -451,12 +530,14 @@ Models without a bundled price (for example OpenRouter or Ollama ids) are blocke
|
|
|
451
530
|
### Harness support matrix (real runs)
|
|
452
531
|
|
|
453
532
|
Legend: ✅ verified on a real account/endpoint · ⚠️ works with a limitation · ❌ not supported · **needs X** = untested, X blocks it.
|
|
533
|
+
Exit codes in the headings describe the wrapper. In-session plugins deny tool calls; as noted
|
|
534
|
+
above, a headless Claude turn can exit 0 after a denial.
|
|
454
535
|
|
|
455
|
-
| Harness + auth | Loop breaker (4x same call, exit 4) | Error breaker (8 failures, exit 4) | Quota % stop (exit 3) | $ / token cap |
|
|
536
|
+
| Harness + auth | Loop breaker (4x same call, wrapper exit 4) | Error breaker (8 failures, wrapper exit 4) | Quota % stop (wrapper exit 3) | $ / token cap |
|
|
456
537
|
| --- | --- | --- | --- | --- |
|
|
457
|
-
| Claude Code, Pro/Max sub, interactive | ✅ (deny keeps the session; resumes after approve) | ✅ | ✅ statusline + `/usage` every
|
|
458
|
-
| Claude Code, Pro/Max sub, headless `claude -p` | ✅ (exit 4) | ✅ (exit 4) | ✅ via `/usage` every 30 s (coarser; live: a 2% cap ended at 4%) | ❌ by design (refused) |
|
|
459
|
-
| Codex, ChatGPT sub | ✅ | ✅ | ✅ `exec` and interactive TUI (whole %; warns under 1%; one-response timeout, default 5m; terminal restored after a TUI stop; approve mid-run; live % from `codex app-server` at start, every 30 s and after exit). Live, Codex 0.167.1, Plus, 11-12 Nov 2026 | ❌ by design (no proxy on a subscription) |
|
|
538
|
+
| Claude Code, Pro/Max sub, interactive | ✅ (deny keeps the session; resumes after approve) | ✅ | ✅ statusline + `/usage` every 30 s (deny keeps the session); `agentcap approve` ✅; plugin. Live, Claude Code 2.1.322/323, Pro, 12-13 Nov 2026; in-session plugin, 14 Nov | ❌ by design (no proxy on a subscription login; AgentCap refuses) |
|
|
539
|
+
| Claude Code, Pro/Max sub, headless `claude -p` | ✅ (exit 4) | ✅ (exit 4) | ✅ via `/usage` every 30 s (coarser; live: a 2% cap ended at 4%); plugin, default limit for `claude -p` | ❌ by design (refused) |
|
|
540
|
+
| Codex, ChatGPT sub | ✅ | ✅ | ✅ `exec` and interactive TUI (whole %; warns under 1%; one-response timeout, default 5m; terminal restored after a TUI stop; approve mid-run; live % from `codex app-server` at start, every 30 s and after exit). Live, Codex 0.167.1, Plus, 11-12 Nov 2026; in-session plugin (TUI + exec), 14 Nov with test-local hooks; installed-plugin trust flow not yet verified | ❌ by design (no proxy on a subscription) |
|
|
460
541
|
| opencode Go | ✅ | ✅ | ❌ not yet: opencode 1.x's session log reports Go cost as 0 and leaves out subagents (live test, 11 Nov 2026), so a Go % cap cannot be measured; do not rely on `--quota` with opencode | ✅ `--tokens`; `--log-cap` uses opencode's own price table |
|
|
461
542
|
| agy, Google sub (plain `agentcap run -- agy` needs no key: breakers only) | ✅ | ✅ | ✅ (tested at 0.5%; ~10 s polling, can overshoot) | ❌ (sub); $ only via Gemini API |
|
|
462
543
|
| opencode + OpenRouter (free model) | ✅ | ✅ | n/a | ✅ $ cap |
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kutay Karahan
|
|
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.
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "agentcap-cli"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.5.1"
|
|
8
8
|
description = "Local-first quota and cost guard for coding agents: per-job share of Claude Code/Codex/Gemini subscription quota, loop breaker, evidence receipt."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -817,7 +817,7 @@ def _serve(args: argparse.Namespace) -> int:
|
|
|
817
817
|
print(f"export ANTHROPIC_BASE_URL={base}/anthropic")
|
|
818
818
|
print(f"# run id: {run_id}, cap: ${cap:.2f}")
|
|
819
819
|
sys.stdout.flush()
|
|
820
|
-
uvicorn.run(app, host="127.0.0.1", port=args.port, log_level="info")
|
|
820
|
+
uvicorn.run(app, host="127.0.0.1", port=args.port, log_level="info", access_log=False)
|
|
821
821
|
return 0
|
|
822
822
|
|
|
823
823
|
|
|
@@ -22,6 +22,7 @@ Adapters:
|
|
|
22
22
|
from __future__ import annotations
|
|
23
23
|
|
|
24
24
|
import json
|
|
25
|
+
import math
|
|
25
26
|
import os
|
|
26
27
|
import re
|
|
27
28
|
import signal
|
|
@@ -77,9 +78,11 @@ def detect_harness(argv: Sequence[str]) -> str | None:
|
|
|
77
78
|
|
|
78
79
|
|
|
79
80
|
def _num(v: Any) -> int:
|
|
81
|
+
if isinstance(v, float) and not math.isfinite(v):
|
|
82
|
+
return 0
|
|
80
83
|
try:
|
|
81
84
|
return int(v or 0)
|
|
82
|
-
except (TypeError, ValueError):
|
|
85
|
+
except (TypeError, ValueError, OverflowError):
|
|
83
86
|
return 0
|
|
84
87
|
|
|
85
88
|
|
|
@@ -105,8 +108,8 @@ def opencode_go_limits(env: Mapping[str, str] | None = None) -> dict[str, float]
|
|
|
105
108
|
usd = float(val)
|
|
106
109
|
except ValueError:
|
|
107
110
|
raise ValueError(f"AGENTCAP_OPENCODE_GO_LIMITS: {key} is not a number: {val!r}") from None
|
|
108
|
-
if usd <= 0:
|
|
109
|
-
raise ValueError(f"AGENTCAP_OPENCODE_GO_LIMITS: {key} must be positive")
|
|
111
|
+
if not math.isfinite(usd) or usd <= 0:
|
|
112
|
+
raise ValueError(f"AGENTCAP_OPENCODE_GO_LIMITS: {key} must be a finite positive number")
|
|
110
113
|
limits[key] = usd
|
|
111
114
|
return limits
|
|
112
115
|
|
|
@@ -157,7 +160,11 @@ class OpenCodeAdapter:
|
|
|
157
160
|
tokens = (_num(t.get("input")) + _num(t.get("output")) + _num(t.get("reasoning"))
|
|
158
161
|
+ _num(cache.get("read")) + _num(cache.get("write")))
|
|
159
162
|
cost = part.get("cost")
|
|
160
|
-
|
|
163
|
+
if isinstance(cost, (int, float)) and math.isfinite(float(cost)):
|
|
164
|
+
cost_value = float(cost)
|
|
165
|
+
else:
|
|
166
|
+
cost_value = None
|
|
167
|
+
return [Usage(tokens, cost_value)]
|
|
161
168
|
if kind == "error":
|
|
162
169
|
err = obj.get("error") if isinstance(obj.get("error"), dict) else {}
|
|
163
170
|
return [ApiError(str(err.get("type") or "error"))]
|
|
@@ -233,16 +233,30 @@ def claude_live(*, binary: str | None = None, timeout: float = CLAUDE_TIMEOUT_S,
|
|
|
233
233
|
LiveReader = Callable[[str], "dict[str, Reading] | None"]
|
|
234
234
|
|
|
235
235
|
|
|
236
|
-
def live_readings(
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
236
|
+
def live_readings(
|
|
237
|
+
agent: str,
|
|
238
|
+
*,
|
|
239
|
+
enabled: bool | None = None,
|
|
240
|
+
state_dir: Path | None = None,
|
|
241
|
+
) -> dict[str, Reading] | None:
|
|
242
|
+
"""Current account readings for *agent* ('claude' / 'codex'), or None.
|
|
243
|
+
|
|
244
|
+
``enabled`` lets a caller that must ignore the ambient
|
|
245
|
+
``AGENTCAP_LIVE_QUOTA`` override force live reads on. ``state_dir`` keeps
|
|
246
|
+
the probe directory under the caller's pinned state root.
|
|
247
|
+
"""
|
|
248
|
+
|
|
249
|
+
if enabled is None:
|
|
250
|
+
enabled = live_enabled()
|
|
251
|
+
if not enabled:
|
|
240
252
|
return None
|
|
241
253
|
if agent == "codex":
|
|
242
254
|
return codex_live()
|
|
243
255
|
if agent == "claude":
|
|
244
256
|
from .quota import state_root
|
|
245
|
-
|
|
257
|
+
|
|
258
|
+
root = state_dir if state_dir is not None else state_root()
|
|
259
|
+
probe = root / "probe"
|
|
246
260
|
try:
|
|
247
261
|
probe.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
248
262
|
except OSError:
|
|
@@ -84,6 +84,9 @@ AUTH_REJECTED_STATUSES = frozenset({401, 402, 403})
|
|
|
84
84
|
_GEMINI_KEY_INVALID = "API_KEY_INVALID"
|
|
85
85
|
_ERROR_BODY_MAX = 65536
|
|
86
86
|
_ERROR_MESSAGE_MAX = 120
|
|
87
|
+
#: Upper bound for one request body. A loopback proxy has no client auth
|
|
88
|
+
#: (yet), so an unbounded body is an easy memory DoS; reject before reserving.
|
|
89
|
+
MAX_REQUEST_BODY = 32 * 1024 * 1024
|
|
87
90
|
_KEY_SHAPED = (
|
|
88
91
|
re.compile(r"(?i)\bbearer\s+\S+"),
|
|
89
92
|
re.compile(r"(?i)\b(?:sk|pk|rk)-[A-Za-z0-9_\-*.]{4,}"),
|
|
@@ -118,7 +121,23 @@ _GEMINI_METERED = re.compile(
|
|
|
118
121
|
r"(?:^|/)models/(?P<model>[^/:]+):(?P<action>generateContent|streamGenerateContent)$"
|
|
119
122
|
)
|
|
120
123
|
|
|
121
|
-
|
|
124
|
+
#: Hop-by-hop headers must not be forwarded to the upstream connection.
|
|
125
|
+
_REQUEST_HEADER_DROP = frozenset(
|
|
126
|
+
{
|
|
127
|
+
"host",
|
|
128
|
+
"content-length",
|
|
129
|
+
"accept-encoding",
|
|
130
|
+
"connection",
|
|
131
|
+
"keep-alive",
|
|
132
|
+
"proxy-authenticate",
|
|
133
|
+
"proxy-authorization",
|
|
134
|
+
"proxy-connection",
|
|
135
|
+
"te",
|
|
136
|
+
"trailer",
|
|
137
|
+
"transfer-encoding",
|
|
138
|
+
"upgrade",
|
|
139
|
+
}
|
|
140
|
+
)
|
|
122
141
|
_RESPONSE_HEADER_DROP = frozenset(
|
|
123
142
|
{"content-length", "transfer-encoding", "content-encoding"}
|
|
124
143
|
)
|
|
@@ -581,6 +600,23 @@ class _Proxy:
|
|
|
581
600
|
async def _handle_anthropic(self, request: Request) -> Response:
|
|
582
601
|
return await self._handle(request, "anthropic")
|
|
583
602
|
|
|
603
|
+
async def _read_body(self, request: Request) -> bytes | None:
|
|
604
|
+
"""Read a bounded request body; ``None`` means it is too large."""
|
|
605
|
+
|
|
606
|
+
raw_length = request.headers.get("content-length")
|
|
607
|
+
if raw_length is not None:
|
|
608
|
+
try:
|
|
609
|
+
if int(raw_length) > MAX_REQUEST_BODY:
|
|
610
|
+
return None
|
|
611
|
+
except ValueError:
|
|
612
|
+
pass
|
|
613
|
+
body = bytearray()
|
|
614
|
+
async for chunk in request.stream():
|
|
615
|
+
if len(body) + len(chunk) > MAX_REQUEST_BODY:
|
|
616
|
+
return None
|
|
617
|
+
body.extend(chunk)
|
|
618
|
+
return bytes(body)
|
|
619
|
+
|
|
584
620
|
async def _handle(self, request: Request, provider: str) -> Response:
|
|
585
621
|
path = request.path_params["path"]
|
|
586
622
|
# Codex can fall back to a websocket transport. The proxy cannot meter
|
|
@@ -599,7 +635,17 @@ class _Proxy:
|
|
|
599
635
|
headers={"Allow": "POST"},
|
|
600
636
|
media_type="text/plain",
|
|
601
637
|
)
|
|
602
|
-
body = await
|
|
638
|
+
body = await self._read_body(request)
|
|
639
|
+
if body is None:
|
|
640
|
+
self._count_blocked()
|
|
641
|
+
return _agentcap_error(
|
|
642
|
+
413,
|
|
643
|
+
"agentcap: request body too large "
|
|
644
|
+
f"(limit {MAX_REQUEST_BODY} bytes); request NOT sent.",
|
|
645
|
+
"agentcap_request_too_large",
|
|
646
|
+
code="agentcap_request_too_large",
|
|
647
|
+
headers={"x-agentcap-blocked": "size"},
|
|
648
|
+
)
|
|
603
649
|
decision = self._classify(provider, path, request.method)
|
|
604
650
|
if decision == "blocked":
|
|
605
651
|
self._observe()
|
|
@@ -1455,10 +1501,11 @@ class _Proxy:
|
|
|
1455
1501
|
else:
|
|
1456
1502
|
parser = OpenAIStreamUsage()
|
|
1457
1503
|
finalized = False
|
|
1504
|
+
completed = False
|
|
1458
1505
|
response_tier: str | None = None
|
|
1459
1506
|
|
|
1460
1507
|
async def body_stream() -> AsyncIterator[bytes]:
|
|
1461
|
-
nonlocal finalized, response_tier
|
|
1508
|
+
nonlocal finalized, completed, response_tier
|
|
1462
1509
|
try:
|
|
1463
1510
|
async for chunk in response.aiter_bytes():
|
|
1464
1511
|
if provider == "openai" and response_tier is None:
|
|
@@ -1469,24 +1516,35 @@ class _Proxy:
|
|
|
1469
1516
|
)
|
|
1470
1517
|
parser.feed(chunk)
|
|
1471
1518
|
yield chunk
|
|
1519
|
+
completed = True
|
|
1472
1520
|
finally:
|
|
1473
1521
|
if not finalized:
|
|
1474
1522
|
finalized = True
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
model,
|
|
1482
|
-
provider,
|
|
1483
|
-
usage,
|
|
1484
|
-
response_tier=response_tier,
|
|
1485
|
-
)
|
|
1486
|
-
else:
|
|
1523
|
+
# A stream that ends without reaching its natural end
|
|
1524
|
+
# (client abort, cancellation, upstream failure) may still
|
|
1525
|
+
# have produced and billed output that the provider has
|
|
1526
|
+
# not reported yet. Charge the full reservation; settling
|
|
1527
|
+
# from a partial usage block would make that output free.
|
|
1528
|
+
if not completed:
|
|
1487
1529
|
self._settle_missing(
|
|
1488
1530
|
reservation_id, reserved, model, provider
|
|
1489
1531
|
)
|
|
1532
|
+
else:
|
|
1533
|
+
usage = parser.finish()
|
|
1534
|
+
if usage is not None:
|
|
1535
|
+
self._settle_actual(
|
|
1536
|
+
reservation_id,
|
|
1537
|
+
reserved,
|
|
1538
|
+
settlement,
|
|
1539
|
+
model,
|
|
1540
|
+
provider,
|
|
1541
|
+
usage,
|
|
1542
|
+
response_tier=response_tier,
|
|
1543
|
+
)
|
|
1544
|
+
else:
|
|
1545
|
+
self._settle_missing(
|
|
1546
|
+
reservation_id, reserved, model, provider
|
|
1547
|
+
)
|
|
1490
1548
|
with contextlib.suppress(BaseException):
|
|
1491
1549
|
await response.aclose()
|
|
1492
1550
|
|
|
@@ -1566,15 +1624,14 @@ class _Proxy:
|
|
|
1566
1624
|
) -> None:
|
|
1567
1625
|
"""Settle a reservation after a client disconnect / cancellation.
|
|
1568
1626
|
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1627
|
+
A closed client connection does not mean the provider produced no
|
|
1628
|
+
output: it may have generated (and billed) the whole response before
|
|
1629
|
+
the disconnect, and for a non-stream request AgentCap cannot see how
|
|
1630
|
+
much was delivered. Charge the full reservation rather than assuming
|
|
1631
|
+
output is zero; the cap must not be bypassed by aborting a request.
|
|
1573
1632
|
"""
|
|
1574
1633
|
|
|
1575
|
-
charged =
|
|
1576
|
-
if charged is None:
|
|
1577
|
-
charged = reserved
|
|
1634
|
+
charged = reserved
|
|
1578
1635
|
try:
|
|
1579
1636
|
self.ledger.settle(reservation_id, charged)
|
|
1580
1637
|
except Exception:
|