Skip to content

feat(llm): add budget orchestrator, complexity gate, 429 circuit breaker, and prompt trimming - #356

Open
Coding-Dev-Tools wants to merge 7 commits into
mainfrom
feat/llm-budget-orchestrator-and-fixes
Open

Coding-Dev-Tools wants to merge 7 commits into
mainfrom
feat/llm-budget-orchestrator-and-fixes

Conversation

@Coding-Dev-Tools

Copy link
Copy Markdown
Owner

Summary

  • Adds engraphis/llm/budget.py with thread-safe ManagedAllowanceTracker, deterministic complexity gate (should_llm_extract), interactive headroom reservation (reserves the last 15 daily calls for interactive Q&A/briefings/consolidation), and bounded in-memory content-hash LLMResultCache.
  • Upgrades engraphis/llm/client.py:
    • Zero-sleep 429 quota-exhaustion fail-fast + local circuit breaker (eliminates 6-second retry hangs).
    • Live �llowance tracking from managed responses and logical model profiles (�uto, ast,
      easoning).
    • Idempotent
      equest_key reuse across transient retries, 22k-char managed prompt trimming, native
      esponse_format={type: json_object} with 400 fallback, and pooled managed LLMClient reuse.
  • Wires complexity and headroom gating into engine.ingest() / actory.py and enforces an 18k-char proactive synthesis prompt budget in �i_context.py.

Verification

  • pytest tests/test_llm_budget_orchestrator.py tests/test_llm_dashboard.py tests/test_llm_config.py tests/test_llm_anthropic_models.py tests/test_extractor.py tests/test_chunking_extractor.py tests/test_graph_extractor.py tests/test_proactive_context.py: 187 passed, 1 skipped (POSIX chmod on Windows).

uff check: All checks passed.

…ker, and prompt trimming

Add ManagedAllowanceTracker, deterministic complexity gate, and bounded content-hash LLMResultCache in engraphis.llm.budget; upgrade LLMClient with 0ms quota-exhausted 429 fail-fast, live allowance recording, logical managed profiles, idempotent request_key, 22k prompt trimming, json_object response_format with fallback, and pooled managed client reuse; wire background complexity/headroom gating into engine.ingest/factory and enforce an 18k proactive synthesis prompt budget.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-27T03:50:00.967245Z
We couldn’t complete this review. Comment @codex review to try again.
0fc0f99 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

…and refresh offline evidence

Validate credentials before result-cache lookups and isolate cache/allowance state by active Cloud session or provider scope, preserve and fail-closed on system prompts in managed trimming, isolate BYOK 429 quota errors from the managed tracker, cache response_format incompatibility per provider/model, harden pooled client lifecycle and model keying, remove the core-to-llm import inversion, and refresh the offline benchmark evidence registry.
@Coding-Dev-Tools

Copy link
Copy Markdown
Owner Author

2-agent independent review loop completed at 29e4be4:

  • Fixed: pre-auth credential gate before LLMResultCache, session/endpoint-scoped cache & ManagedAllowanceTracker, system safety-fence preservation in _trim_messages_for_managed_limit, BYOK 429 isolation, PBKDF2-HMAC-SHA256 BYOK cache tag (clearing CodeQL), cached JSON-mode fallback, pooled client lifecycle cleanup, removed core -> llm import in engine.py + fixed Callable[..., Any] pyright signature, and refreshed offline benchmark evidence + SVG/PNG charts.
  • Verification: local pytest (87 passed, 0 failed),
    uff check (0 errors), pyright (0 new errors), media_gate.py OK; GitHub CI CodeQL, typecheck, and Python 3.10/3.12/3.13/3.14 stacks green.
  • Schema-v2 approval prepared (engraphis-356-29e4be46.json, 0 Blockers, 0 Majors). Merge after feat(ui): vendor-neutral managed AI LLM messaging, memory benefits, and live quota visibility #355 (note generated offline evidence JSON adds 702 lines).

`managed_session_scope()` hashed the raw cloud session bytes via
`saved_session_digest()`. Every `access_for_workspace()` call rotates the
single-use refresh credential and rewrites that file, so the scope tuple
changed on every managed request. That silently reset
`ManagedAllowanceTracker`'s 429 breaker and headroom, missed or cleared
`LLMResultCache` on every call, and let `_cache_store()` re-key an
in-flight response onto a different account when the operator switched
Cloud accounts mid-request, filing one account's output under another's
cache entry.

Key the scope on the immutable identity fields instead (control URL,
organization_id, and the member/device/token_subject triple). The byte
digest remains correct where a caller genuinely wants to detect that the
session was revoked, which is what it was written for.

Also drop a response whose scope changed mid-flight rather than re-keying
it, and key BYOK cache entries on the real model: `effective_profile` is
honored only for provider="managed", so two distinct BYOK models sharing
profile="fast" collided on one cache entry.
`tests/test_benchmark_evidence.py` hashes every `.py` under `engraphis/`
and `eval/`, so the budget.py and client.py changes invalidated the
committed registry artifact and failed the whole Python matrix. Export a
new immutable artifact and repoint the test constants, README prefix,
BENCHMARKS.md name/sha, and the fixture-suite digest. The SVGs and PNGs
were regenerated so their embedded provenance matches.

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