Skip to content

GEPA: the reflection request cap ends the run, refusals count on both sides, the selection survives a usage mismatch - #1337

Open
drewstone wants to merge 6 commits into
mainfrom
fix/optimizer-request-cap
Open

drewstone wants to merge 6 commits into
mainfrom
fix/optimizer-request-cap

Conversation

@drewstone

Copy link
Copy Markdown
Member

Problem

A Casework GEPA omni search failed on 2026-11-23 at 08:09Z with SelfImproveRunError: docket-reader-gepa-omni: GEPA reported 691 model attempts but the proxy received 300. It had run 4 h 14 min, and the method threw away its selection with the error.

Cause, from the run's ledger, GEPA state and bridge log:

  1. From about 06:20Z the model plan answered reflection requests with errors. Between 06:40Z and 06:55Z, 289 reflection requests failed. GEPA retries a failed batched reflection once per component, and the model client retries each call, so each failed iteration sent about 8 requests.
  2. Those failures used up the proxy's maxRequests: 300 at 06:55:55Z. Only 11 of the 300 requests had succeeded.
  3. From then on the proxy answered every reflection request with 429 optimizer model request limit reached. nextReservation refused these before requestAttempts was incremented, so the proxy did not count them. The bridge's httpx hook counted all 391 of them. 300 + 391 = 691.
  4. GEPA treats a failed reflection as a proposal that did not happen. It kept iterating for 75 minutes (continue iterations 45 to 134) and spent the rest of its 2,900-evaluation budget on minibatches it could no longer reflect on.
  5. When the bridge returned, gepaCosts threw on the count mismatch. The bridge output was in the subprocess temp directory, which is removed on return, so the selection was lost with it. (The recovered pick was the seed, so in this run nothing better was lost.)

Change

  • Refusals are counted on both sides. On a request or cost-limit refusal, the proxy sends x-agent-eval-refusal: <reason> and counts the request as refusedRequests on its wire. The bridge counts responses that carry the header as refusedAttempts and reports them in tokenUsage. The completion check compares the bridge's attempts with the proxy's admitted plus refused requests, and compares the two refused counts. A request that never reached the proxy still shows up as a mismatch. The refusal now also returns the header on the Anthropic wire, and the provenance carries the OpenAI-wire counts as optimizerModelRequests.
  • Reaching the cap ends the engine run. The bridge adds a GEPA stopper (ProxyBudgetStopper) to every proxied standard gepa run, in both config shapes. It stops the run at the first refusal, and later stages of a composed recipe stop at once. A caller-supplied engineConfig.stop_callbacks is refused in both shapes. Before this change it was refused only alongside stopAtScore.
  • The selection survives the checks. optimize writes the bridge output to external/selection-<attempt>.json before it runs any check. The usage check (execution receipts, OpenAI-wire counts, Anthropic receipts) now returns its failures instead of throwing. Any failure marks the method cost accountingComplete: false, with reason usage check failed: .... A run that declares a dollar budget then holds at the gate, and the pick is kept.

Proof

All tests run the real Python bridge and official GEPA against a local model server. maxRequests: 1 and a reply that never scores push the run past the cap.

Test Before (tests-only commit a87b2605) After (cb393af1 TS, 2c1914bd Python)
test_official_gepa_engine_stops_at_the_first_proxy_refusal assert 40 <= 4: the engine spent all 40 evaluations passes: stops within 4 evaluations, refused count equals the server's refusals
TS ends the GEPA run at the reflection request cap and counts refusals on both sides GEPA reported 297 model attempts but the proxy received 1 (the production failure shape) passes on GEPA source and on published GEPA 0.1.4
TS keeps the selection and reports a failed usage check when the attempt counts disagree (the runner adds one uncounted attempt) GEPA reported 298 model attempts but the proxy received 1 passes: winner returned, accountingComplete: false, selection-<attempt>.json present

Receipts:

  • Red: beelink2-21abad4ed7caa107a4adce4a636acbdb, exit 1 after 880 s. Most of that time was the unfixed engine running on past the cap.
  • TypeScript: beelink1-8a964405a7686291cf462f90cd5d028c, exit 0 in 106 s. It ran pnpm typecheck, pnpm lint and 10 optimizer test files (105 passed), including both official integration files on GEPA source.
  • Python: beelink1-ac02b313d926ff3b2c4918273cc5349c, exit 0 in 71 s. It ran ruff check . and the whole clients/python suite. The skips are the published-GEPA, dspy and pnpm build tests.
  • Published GEPA: beelink1-77065c85acb1c26a06856ad225c3e37b, exit 0 in 79 s. It ran the release compatibility set from publish.yml, then the TypeScript official integration file against GEPA 0.1.4 (3 passed).

2c1914bd differs from cb393af1 by one Python test line.

Before the fix, the 40-evaluation red run sent 297 requests, about 7.4 per iteration. Production sent about 7.8 per failed iteration.

Growth: +152 source lines (TypeScript and Python) and +202 test lines. That count already subtracts one unit test that was deleted (test_gepa_proxy_counts_failed_attempt_before_success_separately); the official LM test covers the same counts against a real server. This keeps a finished search from being discarded by an accounting mismatch, and ends a search when it can no longer reflect instead of letting it run up to 75 minutes of evaluations. No concept is duplicated: the refusal count lives on the proxy's existing wire counts, and the stopper is GEPA's own stopper protocol.

Not changed here, for the owner's decision:

  • The bridge environment lacks tenacity, so the reflection client's num_retries raises ModuleNotFoundError instead of retrying.
  • A rollout whose paid call fails is scored 0 and given to GEPA as a wrong answer. In the incident, 445 evaluations scored 0 this way, and one candidate's 0.1067 selection score was an outage artifact.

Run ID: run_f0dcc5c9530cf0786dfa8ba93b8909b8
Session: 5642814c-05e6-4613-aff1-cdce74cc5dab

…n both sides, the selection survives a usage mismatch
…wire name, the run length checked first; drop unit tests the integration tests cover
… sides, the selection survives a usage mismatch

The proxy marks a request it refuses for its request or cost limit with
x-agent-eval-refusal and counts it per wire. The Python bridge counts responses
carrying that header as refused attempts, reports them in tokenUsage, and adds
a stopper that ends the GEPA engine run at the first refusal: GEPA otherwise
treats a refused reflection as a proposal that did not happen and spends the
rest of its evaluation budget on minibatches it cannot reflect on.

The completion check compares the bridge's attempts with the proxy's admitted
plus refused requests, and the refused counts with each other. The method
writes the bridge output to external/selection-<attempt>.json before any check
runs, and a usage-count disagreement now marks the cost incomplete with the
reason instead of throwing away a finished search.

@tangletools tangletools left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Auto-approved PR — 2c1914bd

Blanket team auto-approval is intentional. This is not a code review.
No automated review runs on this PR. This approval rests on the rule above alone.

tangletools · auto-approval · reason: blanket_auto_approve · 2026-11-23T09:12:48Z

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.

2 participants