Skip to content

Review gates step aside when the Codex quota is nearly spent - #38

Open
razbenaharon wants to merge 1 commit into
mainfrom
feature/quota-aware-gates
Open

razbenaharon wants to merge 1 commit into
mainfrom
feature/quota-aware-gates

Conversation

@razbenaharon

Copy link
Copy Markdown
Owner

Why

On 2026-12-01 the Codex 5-hour window was at 99%, yet the PreToolUse consultation gate denied a Write and the Stop review gate demanded a review_gate call. The user's own rule is no new Codex jobs at 80% (5-hour) or 90% (weekly), so the gates and the rule contradicted each other.

Where the quota comes from

Not from Codex's logs and not from the status tool:

  • ~/.codex/sessions/*.jsonl are Codex rollout files (full conversation text). Invariant 6 says the hook never opens a conversation file, and the "no quota reading" stance was deliberate.
  • status reports no quota, and a hook cannot call an MCP tool.

Instead the hook reads a small snapshot the user's own tooling writes: <state root>/codex-quota.json, or the path in preferences.json under codex_quota_file. Its shape is {"captured_at": ..., "rate_limits": <Codex's own rate_limits object>}, at most 64 KiB.

Behaviour

  • Limits in preferences.json: quota_hold_five_hour (default 80) and quota_hold_weekly (default 90). A value must be above 0 and at most 100; anything else falls back to the default.

  • At or above a limit:

    • the consultation gate does not deny;
    • the review gate does not block;
    • no reminder asks for Codex.

    Where a gate would have stopped, the user gets one line (systemMessage) with the window, the % used, the limit and the reset time. The ledger records quota_skipped_consult or quota_skipped_review, and modelquad review report shows the counts.

  • Below the limits, or unknown (no file, malformed, or the window's resets_at already passed): exactly today's behaviour.

  • The quota is read only when a gate or reminder is about to act.

Tests

tests/test_quota_hold.py (30 tests) covers:

  • above the limit (5-hour, weekly, both);
  • below the limit;
  • unknown (missing, malformed, wrong types, unknown window, oversized, already reset);
  • preferences overrides and bad values;
  • the hook end to end with a real snapshot file: no deny or block, one line, the ledger event, said once per change set.

Full suite: python scripts/check.py fast and python scripts/check.py python both pass.

Not in this PR

Nothing writes the snapshot yet. Until the user's own quota script writes it, the gates behave as before.

🤖 Generated with Claude Code

The consultation gate (PreToolUse) and the review gate (Stop) asked for a
Codex call even while the user's Codex 5-hour window was at 99%, against the
user's own rule of no new Codex jobs at 80% (5-hour) or 90% (weekly).

The hook now reads a quota snapshot the user's own tooling writes
(codex-quota.json in the state root, or preferences.json codex_quota_file),
with limits quota_hold_five_hour (default 80) and quota_hold_weekly
(default 90). At or above a limit, neither gate stops anything and no
reminder asks for Codex; where a gate would have stopped, the user is told
once, with the reset time, and the ledger records quota_skipped_review or
quota_skipped_consult. A missing, unreadable or already-reset snapshot is
unknown and keeps today's behaviour.

The quota does not come from Codex's logs: those are Codex rollout files,
which the hook must never open (invariant 6), and the status tool has no
quota and cannot be called from a hook.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant