refactor(anthropic): extract idempotent fast-mode kwargs helper - #89982
Open
100yenadmin wants to merge 1 commit into
Open
refactor(anthropic): extract idempotent fast-mode kwargs helper#89982100yenadmin wants to merge 1 commit into
100yenadmin wants to merge 1 commit into
Conversation
Pure refactor: lift the inline Anthropic Fast Mode block in build_anthropic_kwargs into a dedicated _apply_fast_mode_to_kwargs helper. The helper is idempotent and revocable — it strips any prior extra_body["speed"] and the fast-mode beta token from anthropic-beta before deciding, so applying it twice is stable and applying it with enabled=False cleanly reverts a prior application. This is needed because a later slice toggles fast mode per-turn on already-built kwargs; the strip-first design keeps that safe. No behavior change when enabled: the enabled path is byte-identical to the pre-refactor inline block (verified by test), and disabled on fresh kwargs is a no-op. Second slice of the bounded-fast-modes series (T1 = NousResearch#89960). Receipts: tests/agent/test_anthropic_adapter.py: 96 passed, 0 failed new test: test_apply_fast_mode_helper_idempotent_revocable_and_byte_identical built on pin 13ce0c5
This was referenced Aug 19, 2026
100yenadmin
added a commit
to electricsheephq/evaOS-hermes-desktop-app-adapter
that referenced
this pull request
Aug 20, 2026
Add a provider-neutral, turn-local fast-mode policy on top of the existing
normal/priority (fast) tiers. Two dynamic modes:
- auto: opens a time-bounded fast window (default 60s, configurable via
fast_auto_on_seconds) on every user turn.
- cold: opens the same window, but only for the first logical-session turn.
The window is resolved at each user-turn boundary (begin_fast_mode_turn at
run_conversation ingress) and re-validated immediately before every provider
dispatch (revalidate_fast_mode_request at the non-streaming, streaming,
Anthropic, Bedrock, and codex-summary boundaries). Re-validating per dispatch
means an expired window drops fast metadata even when relay/middleware
finalizes kwargs after the cutoff, so a fast turn can never leak across
turns. The policy touches request metadata only: conversation messages,
prompts, tools, and persisted overrides stay stable.
The new policy is inert for the existing normal and priority modes — their
dispatch path stays byte-identical to today. Only service_tier in
{auto, cold} activates the window.
Stacks on NousResearch#89960 (fail-closed fast-mode endpoints; resolve_fast_mode_overrides
identity kwargs) and NousResearch#89982 (idempotent _apply_fast_mode_to_kwargs helper),
both of which this policy calls.
The user-facing config/CLI/UI surface that exposes auto/cold to users
(service_tier: auto/cold across the CLI, gateway, tui, and desktop app) is a
deferred follow-up; this change wires the policy internally so it is exercised
by the default agent path and its unit + integration tests.
(cherry picked from commit ec09a3a)
Contributor
Textbook refactor: the extracted helper is genuinely more capable than the inline block it replaced (idempotent + revocable via strip-then-apply, preserving unrelated betas/extra_body keys through the revoke, never mutating the caller's dict) while remaining byte-identical on the enabled path — and the single test asserts all five of those properties explicitly, including end-to-end delegation through — reviewer-b (automated review) No blocking issues found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Extracts the Anthropic fast-mode kwargs logic in
build_anthropic_kwargsinto a helper,_apply_fast_mode_to_kwargs, that is idempotent and revocable: before deciding, it strips any priorextra_body["speed"]and the fast-mode beta token from an existinganthropic-betaheader, so applying it twice is stable and applying-then-disabling cleanly reverts. When enabled, its output is byte-identical to the current inline block.This is a pure refactor with no behavior change today. It's the groundwork for a later slice that toggles fast mode per turn (which needs to apply and un-apply the fast-mode kwargs on the same request object). Second slice of the bounded-fast-modes series split from #74730 (first slice: #89960).
Related Issue
Part of #64785 / #74730.
Type of Change
Changes Made
agent/anthropic_adapter.py—_apply_fast_mode_to_kwargs(kwargs, *, enabled, model, base_url, is_oauth, drop_context_1m_beta=False);build_anthropic_kwargsdelegates to it. The strip-then-rebuild makes it safe to apply repeatedly or revoke.tests/agent/test_anthropic_adapter.py— idempotency/revocability test plus an assertion that the enabled path reconstructs the pre-refactor kwargs exactly.How to Test
The new test covers: apply-twice-stable, apply-then-revoke strips the fast-mode artifacts, revoke-twice-stable,
enabled=Falseno-op, non-mutation of unrelated headers, andenabled == inline.Checklist
Code
tests/agent/test_anthropic_adapter.pyDocumentation & Housekeeping
cli-config.yaml.example— N/ACONTRIBUTING.md/AGENTS.md— N/ASalvage-friendly: single commit on current main (
13ce0c5c67), no dependencies — cherry-pick welcome, authorship preservation appreciated but optional.