Skip to content

refactor(anthropic): extract idempotent fast-mode kwargs helper - #89982

Open
100yenadmin wants to merge 1 commit into
NousResearch:mainfrom
100yenadmin:upstream/fast-mode-idempotent-kwargs
Open

refactor(anthropic): extract idempotent fast-mode kwargs helper#89982
100yenadmin wants to merge 1 commit into
NousResearch:mainfrom
100yenadmin:upstream/fast-mode-idempotent-kwargs

Conversation

@100yenadmin

Copy link
Copy Markdown
Contributor

What does this PR do?

Extracts the Anthropic fast-mode kwargs logic in build_anthropic_kwargs into a helper, _apply_fast_mode_to_kwargs, that is idempotent and revocable: before deciding, it strips any prior extra_body["speed"] and the fast-mode beta token from an existing anthropic-beta header, 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

  • ♻️ Refactor (no behavior 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_kwargs delegates 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

bash scripts/run_tests.sh tests/agent/test_anthropic_adapter.py -q   → 96 passed

The new test covers: apply-twice-stable, apply-then-revoke strips the fast-mode artifacts, revoke-twice-stable, enabled=False no-op, non-mutation of unrelated headers, and enabled == inline.

Checklist

Code

  • I've read the Contributing Guide
  • Conventional Commits
  • Searched existing PRs
  • Only changes related to this refactor
  • Ran tests/agent/test_anthropic_adapter.py
  • Added a test
  • Tested on my platform: macOS 15

Documentation & Housekeeping

  • Docs — N/A (no behavior change)
  • cli-config.yaml.example — N/A
  • CONTRIBUTING.md/AGENTS.md — N/A
  • Cross-platform — N/A
  • Tool descriptions/schemas — N/A

Salvage-friendly: single commit on current main (13ce0c5c67), no dependencies — cherry-pick welcome, authorship preservation appreciated but optional.

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
@alt-glitch alt-glitch added type/refactor Code restructuring, no behavior change P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/anthropic Anthropic native Messages API labels 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)
@Enough1122

Copy link
Copy Markdown
Contributor

AI code review — automated review for reference; please use your judgment.

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 build_anthropic_kwargs. The docstring's strip-before-decide rationale is exactly what makes the revocable semantics trustworthy.

— reviewer-b (automated review)

No blocking issues found.

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

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have provider/anthropic Anthropic native Messages API type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants