agentcap-cli 0.5.0__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.
Files changed (116) hide show
  1. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/PKG-INFO +28 -6
  2. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/README.md +27 -5
  3. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/pyproject.toml +1 -1
  4. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/__init__.py +1 -1
  5. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/quota.py +121 -0
  6. agentcap_cli-0.5.1/tests/test_agent_set.py +265 -0
  7. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_cc_plugin.py +43 -11
  8. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_codex_plugin.py +63 -11
  9. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/.gitignore +0 -0
  10. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/LICENSE +0 -0
  11. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/codex-plugin/LICENSE +0 -0
  12. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/landing/v2/README.md +0 -0
  13. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/plugin/LICENSE +0 -0
  14. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/_lifeline_exec.py +0 -0
  15. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/_lifeline_watch.py +0 -0
  16. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/adapters.py +0 -0
  17. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/alerts.py +0 -0
  18. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/analytics.py +0 -0
  19. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/budget.py +0 -0
  20. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/clamp.py +0 -0
  21. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/cli.py +0 -0
  22. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/cli_history.py +0 -0
  23. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/data/pricing.json +0 -0
  24. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/demo.py +0 -0
  25. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/devtools/__init__.py +0 -0
  26. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/devtools/fake_upstream.py +0 -0
  27. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/estimate.py +0 -0
  28. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/eta.py +0 -0
  29. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/failsafe.py +0 -0
  30. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/harness.py +0 -0
  31. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/history.py +0 -0
  32. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/ledger.py +0 -0
  33. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/license.py +0 -0
  34. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/lifeline.py +0 -0
  35. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/live_quota.py +0 -0
  36. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/logschema.py +0 -0
  37. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/loop.py +0 -0
  38. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/plans.py +0 -0
  39. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/pricing.py +0 -0
  40. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/private.py +0 -0
  41. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/procstop.py +0 -0
  42. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/proxy.py +0 -0
  43. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/report.py +0 -0
  44. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/runner.py +0 -0
  45. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/upstreams.py +0 -0
  46. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/usage.py +0 -0
  47. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/src/agentcap/waste.py +0 -0
  48. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/conftest.py +0 -0
  49. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/agents/claude/projects/-home-dev-shop/sess-p/subagents/agent-a1b2c3.jsonl +0 -0
  50. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/agents/claude/projects/-home-dev-shop/sess-p/subagents/agent-d4e5f6.jsonl +0 -0
  51. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/agents/claude/projects/-home-dev-shop/sess-p.jsonl +0 -0
  52. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/agents/codex/sessions/2026/10/29/rollout-2026-10-29T09-00-00-thr-parent.jsonl +0 -0
  53. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/agents/codex/sessions/2026/10/29/rollout-2026-10-29T09-00-50-thr-child.jsonl +0 -0
  54. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/agents/codex/sessions/2026/10/29/rollout-2026-10-29T09-01-20-thr-fork.jsonl +0 -0
  55. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/agy-1.4.11-2026-10-30.jsonl +0 -0
  56. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/claude-2.1.316-pretooluse.json +0 -0
  57. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/claude-2.1.316-statusline.json +0 -0
  58. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/codex-rollout-2026-10-29.jsonl +0 -0
  59. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/harness_logs/opencode-2026-10-30.jsonl +0 -0
  60. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/history/claude/.credentials.json +0 -0
  61. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/history/claude/projects/-home-dev-shop/1f0c-sess-a.jsonl +0 -0
  62. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/history/codex/auth.json +0 -0
  63. {agentcap_cli-0.5.0 → 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
  64. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/upstream/README.md +0 -0
  65. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/upstream/anthropic_403_permission_error.json +0 -0
  66. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openai_403_region_blocked.json +0 -0
  67. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openrouter_401_user_not_found.json +0 -0
  68. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openrouter_403_moderation.json +0 -0
  69. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/fixtures/upstream/openrouter_429_rate_limited.json +0 -0
  70. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/license_support.py +0 -0
  71. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/proxy_support.py +0 -0
  72. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_adapters.py +0 -0
  73. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_agents.py +0 -0
  74. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_alerts.py +0 -0
  75. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_analytics.py +0 -0
  76. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_anthropic_proxy.py +0 -0
  77. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_anthropic_units.py +0 -0
  78. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_budget.py +0 -0
  79. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_child_status.py +0 -0
  80. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_clamp.py +0 -0
  81. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_claude_adapter_and_tiers.py +0 -0
  82. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_cli.py +0 -0
  83. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_concurrent_forgive.py +0 -0
  84. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_e2e_claude.py +0 -0
  85. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_estimate.py +0 -0
  86. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_exit_codes.py +0 -0
  87. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_failsafe.py +0 -0
  88. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_harness.py +0 -0
  89. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_history.py +0 -0
  90. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_ledger.py +0 -0
  91. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_license.py +0 -0
  92. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_lifeline.py +0 -0
  93. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_live_quota.py +0 -0
  94. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_log_contract.py +0 -0
  95. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_log_contract_live.py +0 -0
  96. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_loop.py +0 -0
  97. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_plugin_package.py +0 -0
  98. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_pricing.py +0 -0
  99. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_private_dirs.py +0 -0
  100. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_procstop.py +0 -0
  101. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_proxy_auth.py +0 -0
  102. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_proxy_cancel.py +0 -0
  103. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_proxy_core.py +0 -0
  104. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_proxy_extras.py +0 -0
  105. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_proxy_streaming.py +0 -0
  106. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_quota.py +0 -0
  107. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_release.py +0 -0
  108. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_report_cli.py +0 -0
  109. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_runaway.py +0 -0
  110. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_runner.py +0 -0
  111. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_self_approve.py +0 -0
  112. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_stop_matrix.py +0 -0
  113. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_upstream_refusals.py +0 -0
  114. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_upstreams.py +0 -0
  115. {agentcap_cli-0.5.0 → agentcap_cli-0.5.1}/tests/test_usage.py +0 -0
  116. {agentcap_cli-0.5.0 → 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.5.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
@@ -278,14 +278,29 @@ The easiest path on a Claude Max or Pro plan. Inside Claude Code:
278
278
  /plugin install agentcap@agentcap
279
279
  /agentcap:setup # once: adds AgentCap's read-only statusLine (your own line keeps showing)
280
280
  /agentcap:default 10% 5h # every new session (also `claude -p`) starts with this limit
281
- /agentcap:quota 5% # this session may use 5% of the weekly quota (`2% 5h` for the 5-hour window)
282
- /agentcap:status # session 3.1% / 5% (weekly)
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)
283
284
  /agentcap:off # clear it; /agentcap:unsetup restores your old statusLine
284
285
  ```
285
286
 
286
287
  `/agentcap:setup` is optional: the quota is measured with `/usage`; setup only adds a live
287
288
  statusline meter.
288
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
+
289
304
  No pip install: the plugin is stdlib Python (`python3` on PATH). The marketplace installs only
290
305
  the self-contained `plugin/` folder (commands, hooks, the script and the few `agentcap` modules it
291
306
  imports), not the whole repo. Contributors: `plugin/` is generated, edit the sources and run
@@ -327,8 +342,8 @@ the stop at the limit, `/agentcap:approve`, `/agentcap:unsetup`, and the self-gu
327
342
  `env -u CLAUDECODE agentcap approve` from the model.
328
343
 
329
344
  When the quota hook ends a headless `claude -p` turn, the command can exit 0 with no output.
330
- The plugin writes a receipt on the first quota denial; that receipt path is covered by automated
331
- tests but has not yet been verified in a live headless run.
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`).
332
347
  In an isolated Claude Code 2.1.325 test, `--output-format json` reported
333
348
  `terminal_reason: "hook_stopped"` and the denied tool in `permission_denials`. A hook can stop
334
349
  for other reasons too, so check the receipt's `reason` to identify a quota denial.
@@ -356,13 +371,20 @@ quota is spent and only you can type them.
356
371
 
357
372
  | Type in Codex | Effect |
358
373
  | --- | --- |
359
- | `agentcap 10% 5h` | this session may use 10% of the 5-hour quota (`week` for the weekly quota) |
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% |
360
376
  | `agentcap status` | current share of the limit |
361
377
  | `agentcap off` | clear this session's limit |
362
378
  | `agentcap approve` | raise the limit by 50% after a stop (Pro, at most twice) |
363
379
  | `agentcap default 10% 5h` | every new session, `codex exec` included, starts with this limit |
364
380
  | `agentcap default off` | no default limit |
365
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
+
366
388
  `PreToolUse` denies a tool call at the limit, on the 4th identical call in a row and after 8
367
389
  failing calls in a row; `sleep N` waits count for neither. Quota comes from `codex app-server`
368
390
  (at set time, every 30 s and at turn end) plus the rollout.
@@ -248,14 +248,29 @@ The easiest path on a Claude Max or Pro plan. Inside Claude Code:
248
248
  /plugin install agentcap@agentcap
249
249
  /agentcap:setup # once: adds AgentCap's read-only statusLine (your own line keeps showing)
250
250
  /agentcap:default 10% 5h # every new session (also `claude -p`) starts with this limit
251
- /agentcap:quota 5% # this session may use 5% of the weekly quota (`2% 5h` for the 5-hour window)
252
- /agentcap:status # session 3.1% / 5% (weekly)
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)
253
254
  /agentcap:off # clear it; /agentcap:unsetup restores your old statusLine
254
255
  ```
255
256
 
256
257
  `/agentcap:setup` is optional: the quota is measured with `/usage`; setup only adds a live
257
258
  statusline meter.
258
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
+
259
274
  No pip install: the plugin is stdlib Python (`python3` on PATH). The marketplace installs only
260
275
  the self-contained `plugin/` folder (commands, hooks, the script and the few `agentcap` modules it
261
276
  imports), not the whole repo. Contributors: `plugin/` is generated, edit the sources and run
@@ -297,8 +312,8 @@ the stop at the limit, `/agentcap:approve`, `/agentcap:unsetup`, and the self-gu
297
312
  `env -u CLAUDECODE agentcap approve` from the model.
298
313
 
299
314
  When the quota hook ends a headless `claude -p` turn, the command can exit 0 with no output.
300
- The plugin writes a receipt on the first quota denial; that receipt path is covered by automated
301
- tests but has not yet been verified in a live headless run.
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`).
302
317
  In an isolated Claude Code 2.1.325 test, `--output-format json` reported
303
318
  `terminal_reason: "hook_stopped"` and the denied tool in `permission_denials`. A hook can stop
304
319
  for other reasons too, so check the receipt's `reason` to identify a quota denial.
@@ -326,13 +341,20 @@ quota is spent and only you can type them.
326
341
 
327
342
  | Type in Codex | Effect |
328
343
  | --- | --- |
329
- | `agentcap 10% 5h` | this session may use 10% of the 5-hour quota (`week` for the weekly quota) |
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% |
330
346
  | `agentcap status` | current share of the limit |
331
347
  | `agentcap off` | clear this session's limit |
332
348
  | `agentcap approve` | raise the limit by 50% after a stop (Pro, at most twice) |
333
349
  | `agentcap default 10% 5h` | every new session, `codex exec` included, starts with this limit |
334
350
  | `agentcap default off` | no default limit |
335
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
+
336
358
  `PreToolUse` denies a tool call at the limit, on the 4th identical call in a row and after 8
337
359
  failing calls in a row; `sleep N` waits count for neither. Quota comes from `codex app-server`
338
360
  (at set time, every 30 s and at turn end) plus the rollout.
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "agentcap-cli"
7
- version = "0.5.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"
@@ -2,6 +2,6 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- __version__ = "0.5.0"
5
+ __version__ = "0.5.1"
6
6
 
7
7
  __all__ = ["__version__"]
@@ -300,6 +300,127 @@ def parse_quota(raw: str) -> float:
300
300
  return value
301
301
 
302
302
 
303
+ WINDOW_WORDS = {"5h": "5h", "5-hour": "5h", "5hour": "5h", "session": "5h",
304
+ "week": "week", "weekly": "week", "7d": "week"}
305
+
306
+
307
+ def parse_limit_words(words: list[str]) -> tuple[float, str, bool]:
308
+ """In-agent limit: ``50% 5h`` (this session may use 50 more points) or ``until 70% 5h``
309
+ (stop when the window reaches 70%). Returns (value, window, until)."""
310
+
311
+ words = [w for w in words if w]
312
+ until = bool(words) and words[0].lower() in ("until", "to", "upto", "up-to")
313
+ if until:
314
+ words = words[1:]
315
+ if not words:
316
+ raise ValueError("give a limit, e.g. `50% 5h` or `until 70% 5h`")
317
+ try:
318
+ value = parse_quota(words[0])
319
+ except ValueError:
320
+ raise ValueError(f"a limit looks like 50% or until 70% (got {words[0]!r})") from None
321
+ window = "week"
322
+ for w in words[1:]:
323
+ if w.lower() not in WINDOW_WORDS:
324
+ raise ValueError(f"unknown window {w!r}: use 5h or week")
325
+ window = WINDOW_WORDS[w.lower()]
326
+ return value, window, until
327
+
328
+
329
+ def resolve_until(state: dict[str, Any]) -> bool:
330
+ """An ``until N%`` limit becomes a share (N - baseline) once the baseline is known."""
331
+
332
+ until = state.get("until")
333
+ if until is None or state.get("until_done"):
334
+ return False
335
+ tracker = ShareTracker.from_dict(state.get("tracker"))
336
+ if tracker.baseline is None:
337
+ return False
338
+ state["quota"] = round(max(float(until) - float(tracker.baseline), 0.0), 3)
339
+ state["until_done"] = True
340
+ return True
341
+
342
+
343
+ def _pct(value: float) -> str:
344
+ return f"{round(float(value), 1):g}%"
345
+
346
+
347
+ def stop_text(quota: float, share: float, used_now: float | None, window_name: str) -> str:
348
+ """'5-hour quota at 20% now → stops at 70%' - the absolute point the user can check."""
349
+
350
+ left = max(float(quota) - float(share), 0.0)
351
+ if used_now is None:
352
+ return f"{window_name} quota: the stop point is shown after the first reading"
353
+ stop = float(used_now) + float(quota) - float(share) # past the limit: the point already passed
354
+ if float(share) >= float(quota):
355
+ return f"{window_name} quota at {_pct(used_now)} now; this session's stop point was {_pct(stop)}"
356
+ if stop >= 100:
357
+ return (f"{window_name} quota at {_pct(used_now)} now → this session can run until the window "
358
+ f"is used up ({round(left, 1):g} points left)")
359
+ return f"{window_name} quota at {_pct(used_now)} now → stops at {_pct(stop)}"
360
+
361
+
362
+ def approve_quota(quota: float, factor: float) -> float:
363
+ """+50% of the session's points; an `until` target that was already reached (0 points) gets 1."""
364
+
365
+ return round(float(quota) * factor, 4) if float(quota) > 0 else 1.0
366
+
367
+
368
+ # The agent proposes a limit by running this harmless shell command; the host's own approval
369
+ # prompt is the user's one button, and the hook sets the limit only after the command ran.
370
+ # The agent may set or tighten a limit, never raise, clear or approve it.
371
+ AGENT_SET_RE = re.compile(
372
+ r"""^\s*echo\s+(['"]?)agentcap\s+set\s+([A-Za-z0-9%.\- ]+?)\1\s*$""", re.IGNORECASE)
373
+
374
+
375
+ def parse_agent_set(command: Any) -> tuple[float, str, bool] | None:
376
+ """``echo agentcap set 50% 5h`` / ``echo agentcap set until 70% 5h`` -> (value, window, until).
377
+
378
+ None when ``command`` is not that command; ValueError when it is, with bad arguments."""
379
+
380
+ if not isinstance(command, str):
381
+ return None
382
+ m = AGENT_SET_RE.match(command)
383
+ if not m:
384
+ return None
385
+ return parse_limit_words(m.group(2).split())
386
+
387
+
388
+ def stop_point(quota: Any, share: Any, used_now: Any) -> float | None:
389
+ """The window % at which the current limit stops (None while it is unknown)."""
390
+
391
+ if quota is None or used_now is None:
392
+ return None
393
+ return float(used_now) + float(quota) - float(share or 0)
394
+
395
+
396
+ def agent_set_refusal(current: Mapping[str, Any], used_now: float | None, share: float,
397
+ value: float, window: str, until: bool) -> str | None:
398
+ """Why the agent may not set this limit (it would loosen the current one), else None.
399
+
400
+ ``current`` is the session state: ``quota``/``window``/``until`` of the limit in force."""
401
+
402
+ if current.get("quota") is None:
403
+ return None
404
+ names = {"week": "weekly", "5h": "5-hour"}
405
+ cur_window = current.get("window", "week")
406
+ if window != cur_window:
407
+ return (f"this session already has a {names.get(cur_window, cur_window)} limit; replacing it with "
408
+ f"a {names.get(window, window)} limit would remove it, so only the user can do that")
409
+ cur_until = current.get("until") if not current.get("until_done") else None
410
+ cur_stop = stop_point(current.get("quota"), share, used_now)
411
+ if cur_stop is not None:
412
+ new_stop = float(value) if until else float(used_now) + float(value)
413
+ if new_stop > cur_stop + 1e-6:
414
+ return (f"the current limit stops at {_pct(cur_stop)} and this one would stop at {_pct(new_stop)}; "
415
+ "the agent may only lower the stop point, raising it is the user's")
416
+ return None
417
+ if cur_until is not None and until:
418
+ return None if float(value) <= float(cur_until) else "the agent may only lower the stop point"
419
+ if cur_until is None and not until:
420
+ return None if float(value) <= float(current["quota"]) else "the agent may only lower the limit"
421
+ return "the current stop point is not known yet, so it cannot be compared; ask the user to set it"
422
+
423
+
303
424
  def parse_duration(raw: str) -> float | None:
304
425
  """'5m', '90s', '1h', '300' (seconds) or 'off' -> seconds (None = off)."""
305
426
 
@@ -0,0 +1,265 @@
1
+ """The agent proposes a limit (`echo agentcap set ...`), the user approves it with one button.
2
+
3
+ Claude Code: PreToolUse answers "ask" (the permission prompt shows the stop point).
4
+ Codex: PreToolUse lets an escalated call through (Codex's approval prompt); a call
5
+ without escalation in the default mode is denied with the exact retry.
6
+ Both: the limit is set in PostToolUse (after the command ran), and the agent may only
7
+ set or tighten a limit, never raise or clear it.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import importlib.util
13
+ import io
14
+ import json
15
+ import time
16
+ from pathlib import Path
17
+
18
+ import pytest
19
+
20
+ from agentcap.quota import Reading, agent_set_refusal, parse_agent_set
21
+
22
+ ROOT = Path(__file__).resolve().parent.parent
23
+
24
+
25
+ def _load(name):
26
+ spec = importlib.util.spec_from_file_location(name, ROOT / "scripts" / f"{name}.py")
27
+ mod = importlib.util.module_from_spec(spec)
28
+ spec.loader.exec_module(mod)
29
+ return mod
30
+
31
+
32
+ cx = _load("agentcap_codex")
33
+ cc = _load("agentcap_cc")
34
+
35
+ SID = "0aa1da0b-e835-7cf0-9dcd-5365269e5129"
36
+ R5 = 2_000_000_000.0
37
+ RW = 2_000_500_000.0
38
+
39
+
40
+ @pytest.fixture(autouse=True)
41
+ def env(tmp_path, monkeypatch):
42
+ monkeypatch.setenv("AGENTCAP_QUOTA_DIR", str(tmp_path / "state"))
43
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / "claude"))
44
+ monkeypatch.setenv("AGENTCAP_LICENSE_FILE", str(tmp_path / "license.json"))
45
+ (tmp_path / "license.json").write_text(json.dumps(
46
+ {"source": "polar", "valid": True, "tier": "pro", "key": "X", "checked_at": 1, "reason": "ok"}))
47
+ monkeypatch.setattr(cx, "LIVE_READER", lambda: {"5h": Reading(10.0, R5), "week": Reading(40.0, RW)})
48
+ monkeypatch.setattr(cx, "SPAWN", lambda argv, **kw: None)
49
+ return tmp_path
50
+
51
+
52
+ # ------------------------------------------------------------------ shared parsing
53
+ @pytest.mark.parametrize("command, want", [
54
+ ("echo agentcap set 50% 5h", (50.0, "5h", False)),
55
+ ("echo 'agentcap set until 70% 5h'", (70.0, "5h", True)),
56
+ (' echo "agentcap set 5%" ', (5.0, "week", False)),
57
+ ("echo agentcap set until 30% week", (30.0, "week", True)),
58
+ ])
59
+ def test_parse_agent_set(command, want):
60
+ assert parse_agent_set(command) == want
61
+
62
+
63
+ @pytest.mark.parametrize("command", [
64
+ "echo agentcap set 50% 5h; rm -rf /tmp/x", "echo agentcap set 50% 5h && agentcap off",
65
+ "echo agentcap set $(cat x) 5h", "agentcap set 50% 5h", "echo agentcap off", "echo hello",
66
+ "echo 'agentcap set 50% 5h", None,
67
+ ])
68
+ def test_anything_else_is_not_the_agent_set_command(command):
69
+ assert parse_agent_set(command) is None
70
+
71
+
72
+ def test_bad_arguments_are_an_error():
73
+ with pytest.raises(ValueError):
74
+ parse_agent_set("echo agentcap set off")
75
+ with pytest.raises(ValueError):
76
+ parse_agent_set("echo agentcap set 50% fortnight")
77
+
78
+
79
+ def test_refusal_is_tighten_only():
80
+ none = {}
81
+ assert agent_set_refusal(none, None, 0.0, 90.0, "5h", True) is None # no limit yet: any
82
+ cur = {"quota": 40.0, "window": "5h", "tracker": {"baseline": 10.0}} # stops at 50%
83
+ assert agent_set_refusal(cur, 10.0, 0.0, 40.0, "5h", True) is None # until 40 < 50
84
+ assert agent_set_refusal(cur, 10.0, 0.0, 40.0, "5h", False) is None # 10+40 = 50
85
+ assert "only lower" in agent_set_refusal(cur, 10.0, 0.0, 60.0, "5h", True)
86
+ assert "only lower" in agent_set_refusal(cur, 20.0, 5.0, 40.0, "5h", False) # 20+40 = 60 > 55
87
+ assert "remove it" in agent_set_refusal(cur, 10.0, 0.0, 1.0, "week", False)
88
+ unknown = {"quota": 5.0, "window": "5h"}
89
+ assert agent_set_refusal(unknown, None, 0.0, 4.0, "5h", False) is None
90
+ assert agent_set_refusal(unknown, None, 0.0, 6.0, "5h", False)
91
+ assert "not known yet" in agent_set_refusal(unknown, None, 0.0, 30.0, "5h", True)
92
+
93
+
94
+ # ------------------------------------------------------------------ Codex
95
+ def cx_hook(event, payload):
96
+ out = io.StringIO()
97
+ cx.main(["hook", event], stdin=io.StringIO(json.dumps(payload)), stdout=out)
98
+ text = out.getvalue().strip()
99
+ return json.loads(text) if text else None
100
+
101
+
102
+ def cx_pre(command, *, escalated=False, mode="default"):
103
+ tool_input = {"cmd": command}
104
+ if escalated:
105
+ tool_input.update({"sandbox_permissions": "require_escalated", "justification": "AgentCap: limit"})
106
+ return cx_hook("PreToolUse", {"session_id": SID, "turn_id": "t", "cwd": "/w", "transcript_path": None,
107
+ "hook_event_name": "PreToolUse", "permission_mode": mode,
108
+ "tool_name": "exec_command", "tool_input": tool_input, "tool_use_id": "c1"})
109
+
110
+
111
+ def cx_post(command, response="Exit code: 0\nagentcap set 50% 5h"):
112
+ return cx_hook("PostToolUse", {"session_id": SID, "turn_id": "t", "cwd": "/w", "transcript_path": None,
113
+ "hook_event_name": "PostToolUse", "permission_mode": "default",
114
+ "tool_name": "exec_command", "tool_input": {"cmd": command},
115
+ "tool_response": response, "tool_use_id": "c1"})
116
+
117
+
118
+ def cx_prompt(text):
119
+ return cx_hook("UserPromptSubmit", {"session_id": SID, "turn_id": "t", "cwd": "/w", "transcript_path": None,
120
+ "hook_event_name": "UserPromptSubmit", "permission_mode": "default",
121
+ "prompt": text})["reason"]
122
+
123
+
124
+ def deny_reason(out):
125
+ hso = (out or {}).get("hookSpecificOutput", {})
126
+ return hso.get("permissionDecisionReason", "") if hso.get("permissionDecision") == "deny" else None
127
+
128
+
129
+ def test_codex_preview_text_says_where_it_would_stop():
130
+ got = cx.preview_text(60.0, "5h", True, Reading(10.0, R5))
131
+ assert got == "stop this session when the 5-hour quota reaches 60%: 5-hour quota at 10% now → stops at 60%"
132
+ assert "right away" in cx.preview_text(5.0, "5h", True, Reading(10.0, R5))
133
+
134
+
135
+ def test_codex_escalated_call_goes_to_the_users_approval_and_sets_the_limit_after_it_ran():
136
+ cx.read_live(SID, time.time())
137
+ assert cx_pre("echo agentcap set until 60% 5h", escalated=True) is None # Codex asks the user
138
+ assert cx.load(SID).get("quota") is None # nothing set before it ran
139
+ msg = cx_post("echo agentcap set until 60% 5h")["systemMessage"]
140
+ assert "you approved" in msg and "stops at 60%" in msg and "agentcap off" in msg
141
+ st = cx.load(SID)
142
+ assert st["quota"] == 50.0 and st["source"] == "agent" and st["window"] == "5h"
143
+
144
+
145
+ def test_codex_hooks_see_only_the_command_so_any_mode_passes_to_codex():
146
+ # live 16 Nov 2026 (Codex 0.169): PreToolUse tool_input is {"command": ...} even for an
147
+ # escalated call, so the hook cannot require escalation; Codex's own prompt is the button
148
+ assert cx_pre("echo agentcap set 5% 5h") is None
149
+ assert cx_pre("echo agentcap set 5% 5h", mode="bypassPermissions") is None
150
+
151
+
152
+ def test_codex_declined_or_failed_command_sets_nothing():
153
+ cx_post("echo agentcap set 5% 5h", response={"exit_code": 1, "output": "denied"})
154
+ assert cx.load(SID).get("quota") is None
155
+
156
+
157
+ def test_codex_agent_may_tighten_but_not_loosen_the_users_limit():
158
+ cx_prompt("agentcap until 30% 5h") # stops at 30%
159
+ assert "only lower the stop point" in deny_reason(cx_pre("echo agentcap set until 50% 5h", escalated=True))
160
+ assert "only lower the stop point" in deny_reason(cx_pre("echo agentcap set 25% 5h", escalated=True))
161
+ assert "remove it" in deny_reason(cx_pre("echo agentcap set 1% week", escalated=True))
162
+ assert cx_pre("echo agentcap set until 20% 5h", escalated=True) is None
163
+ # loosened between the prompt and the run: still refused at apply time
164
+ assert "limit not changed" in cx_post("echo agentcap set until 50% 5h")["systemMessage"]
165
+ assert cx.load(SID)["quota"] == 20.0
166
+ cx_post("echo agentcap set until 20% 5h")
167
+ assert cx.load(SID)["quota"] == 10.0
168
+
169
+
170
+ def test_codex_bad_arguments_are_denied_with_the_format():
171
+ reason = deny_reason(cx_pre("echo agentcap set lots 5h", escalated=True))
172
+ assert "echo agentcap set until 70% 5h" in reason
173
+
174
+
175
+ def test_codex_approve_after_two_approvals_points_to_off():
176
+ cx_prompt("agentcap 2% 5h")
177
+ cx_prompt("agentcap approve")
178
+ cx_prompt("agentcap approve")
179
+ out = cx_prompt("agentcap approve")
180
+ assert "already approved twice" in out and "agentcap off" in out
181
+
182
+
183
+ def test_codex_skill_documents_the_command():
184
+ text = (ROOT / "codex" / "skills" / "agentcap" / "SKILL.md").read_text()
185
+ assert "echo agentcap set until 70% 5h" in text and "require_escalated" in text
186
+
187
+
188
+ # ------------------------------------------------------------------ Claude Code
189
+ def cc_hook(event, payload):
190
+ out = io.StringIO()
191
+ cc.main(["hook", event], stdin=io.StringIO(json.dumps(payload)), stdout=out)
192
+ text = out.getvalue().strip()
193
+ return json.loads(text) if text else None
194
+
195
+
196
+ def cc_payload(command, event):
197
+ return {"session_id": SID, "transcript_path": "/t.jsonl", "cwd": "/w", "permission_mode": "default",
198
+ "hook_event_name": event, "tool_name": "Bash", "tool_input": {"command": command},
199
+ "tool_use_id": "t1"}
200
+
201
+
202
+ def cc_statusline(week=40.0, five=10.0):
203
+ payload = {"hook_event_name": "Status", "session_id": SID, "cwd": "/w",
204
+ "rate_limits": {"five_hour": {"used_percentage": five, "resets_at": R5 - 3600},
205
+ "seven_day": {"used_percentage": week, "resets_at": R5}}}
206
+ cc.main(["statusline"], stdin=io.StringIO(json.dumps(payload)), stdout=io.StringIO())
207
+
208
+
209
+ def cc_cmd(*args):
210
+ out = io.StringIO()
211
+ cc.main(list(args) + ["--session", SID], stdout=out)
212
+ return out.getvalue()
213
+
214
+
215
+ def test_claude_asks_the_user_with_the_stop_point():
216
+ cc_statusline()
217
+ out = cc_hook("pre", cc_payload("echo agentcap set until 30% 5h", "PreToolUse"))
218
+ hso = out["hookSpecificOutput"]
219
+ assert hso["permissionDecision"] == "ask"
220
+ assert "5-hour quota at 10% now → stops at 30%" in hso["permissionDecisionReason"]
221
+ assert cc.load(SID).get("quota") is None
222
+
223
+
224
+ def test_claude_sets_the_limit_after_the_approved_command_ran():
225
+ cc_statusline()
226
+ out = cc_hook("post", cc_payload("echo agentcap set until 30% 5h", "PostToolUse"))
227
+ assert "you approved" in out["systemMessage"] and "stops at 30%" in out["systemMessage"]
228
+ assert out["hookSpecificOutput"]["additionalContext"] == out["systemMessage"]
229
+ st = cc.load(SID)
230
+ assert st["quota"] == 20.0 and st["source"] == "agent" and st["used"] == 10.0
231
+
232
+
233
+ def test_claude_failed_command_sets_nothing():
234
+ cc_statusline()
235
+ cc_hook("postfail", cc_payload("echo agentcap set 5% 5h", "PostToolUseFailure"))
236
+ assert cc.load(SID).get("quota") is None
237
+
238
+
239
+ def test_claude_agent_may_tighten_but_not_loosen():
240
+ cc_statusline(week=40.0)
241
+ cc_cmd("quota", "2%") # weekly, stops at 42%
242
+ out = cc_hook("pre", cc_payload("echo agentcap set 5% week", "PreToolUse"))
243
+ assert out["hookSpecificOutput"]["permissionDecision"] == "deny"
244
+ assert "only lower" in out["hookSpecificOutput"]["permissionDecisionReason"]
245
+ out = cc_hook("pre", cc_payload("echo agentcap set until 41% week", "PreToolUse"))
246
+ assert out["hookSpecificOutput"]["permissionDecision"] == "ask"
247
+
248
+
249
+ def test_claude_marker_is_not_mistaken_for_a_self_call():
250
+ assert not cc.self_guard(cc_payload("echo agentcap set 50% 5h", "PreToolUse"))
251
+
252
+
253
+ def test_claude_approve_after_two_approvals_points_to_off():
254
+ cc_statusline()
255
+ cc_cmd("quota", "2%")
256
+ cc_cmd("approve")
257
+ cc_cmd("approve")
258
+ out = cc_cmd("approve")
259
+ assert "already approved twice" in out and "/agentcap:off" in out
260
+
261
+
262
+ def test_claude_plugin_ships_the_skill():
263
+ text = (ROOT / "plugin" / "skills" / "agentcap" / "SKILL.md").read_text()
264
+ assert "echo agentcap set until 70% 5h" in text
265
+ assert text == (ROOT / "skills" / "agentcap" / "SKILL.md").read_text()