Skip to content

refactor(anthropic): extract 7 helpers from convert_messages_to_anthropic (salvage #27784)#30386

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-c939ba47
May 22, 2026
Merged

refactor(anthropic): extract 7 helpers from convert_messages_to_anthropic (salvage #27784)#30386
teknium1 merged 2 commits into
mainfrom
hermes/hermes-c939ba47

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

Pure refactor: convert_messages_to_anthropic in agent/anthropic_adapter.py was the largest cyclomatic-complexity hotspot in the file (C=79, 185 statements). Extracts 7 single-responsibility helpers; the main function drops to ~63 LOC and C<10.

Salvage of #27784 (which was a rebase of #23968) onto current main. The contributor's branch was stale; one conflict resolved (the inline thinking-block management comment block in convert_messages_to_anthropic was extracted into _manage_thinking_signatures's docstring, which already had stricter wording — "Azure AI Foundry" + "AWS Bedrock" supersedes main's "Microsoft Foundry" comment).

The 3 recent commits that landed on anthropic_adapter.py since the contributor's branch point (#9df9816d Azure Entra ID, #f0c6d591 MiniMax beta scoping, #73407b1e Bearer auth for Azure Foundry) all live outside convert_messages_to_anthropic and merge cleanly through.

Changes

agent/anthropic_adapter.py — extract 7 helpers; main function shrinks 395 → 63 LOC.

Helper C Role
_convert_assistant_message <10 Assistant msg → content blocks
_convert_tool_message_to_result 12 Tool msg → tool_result + merge
_convert_user_message <10 User msg validation + conversion
_strip_orphaned_tool_blocks 15 Orphan tool_use + tool_result removal
_merge_consecutive_roles 13 Anthropic role-alternation enforce
_manage_thinking_signatures 18 Strip/preserve/downgrade by endpoint
_evict_old_screenshots <10 Keep most recent 3 images

Mutating helpers update result in place (matches inline original). _merge_consecutive_roles returns a new list — caller rebinds.

Honest accounting

Metric Before After Δ
Max function cyclomatic 79 18 −77%
Max statements/function 185 ~70 −62%
Main function LOC 395 63 −84%
File-wide LOC −4
ruff C901 violations 3 8 +5 (intentional split)

The +5 violation-count "regression" is the rule working as intended — one C=79 monolith becomes 4 still-above-threshold helpers (C=12/13/15/18). Max complexity drops 77%, each new helper fits on one screen and is individually testable. That's the actual goal of C901.

Test plan

  • tests/agent/test_anthropic_adapter.py: 152/152 pass
  • tests/agent/test_auxiliary_client.py: 172/172 pass
  • tests/agent/test_azure_identity_adapter.py: 39/39 pass
  • tests/agent/test_bedrock_1m_context.py: 2/2 pass
  • py_compile clean on agent/anthropic_adapter.py

Original PR author (@kshitijk4poor) noted 14 OAuth-credential tests failing on their branch — those all pass cleanly on current main after this cherry-pick.

Attribution

Authorship preserved via cherry-pick: commit by @kshitijk4poor, committer by @teknium1. AUTHOR_MAP already has 82637225+kshitijk4poor@users.noreply.github.com → kshitijk4poor.

Closes #27784.

Infographic

pr27784-anthropic-refactor

kshitijk4poor and others added 2 commits May 22, 2026 04:12
Split convert_messages_to_anthropic (complexity 79) into 7 focused helpers:

- _convert_assistant_message    — assistant msg to content blocks
- _convert_tool_message_to_result — tool msg to tool_result + merge
- _convert_user_message         — user msg validation + conversion
- _strip_orphaned_tool_blocks   — orphan tool_use + tool_result removal
- _merge_consecutive_roles      — role alternation enforcement
- _manage_thinking_signatures   — strip/preserve/downgrade by endpoint
- _evict_old_screenshots        — keep only 3 most recent images

Main function complexity: 79 → 10 (below C901 threshold).
Zero logic changes — pure extraction. Net -4 lines (refactor itself);
+45/-17 follow-up polish for annotation tightening (List[Dict] →
List[Dict[str, Any]]), restored rationale comments in
_manage_thinking_signatures (third-party endpoint examples, #13848/#16748
issue refs, redacted_thinking 'data'-as-signature note), and "Mutates
``result`` in place." docstring lines on the four mutating helpers.
@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: hermes/hermes-c939ba47 vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 9017 on HEAD, 9017 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 4766 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@teknium1 teknium1 merged commit 6f436a4 into main May 22, 2026
18 checks passed
@teknium1 teknium1 deleted the hermes/hermes-c939ba47 branch May 22, 2026 11:23
@alt-glitch alt-glitch added type/refactor Code restructuring, no behavior change P3 Low — cosmetic, nice to have comp/agent Core agent loop, run_agent.py, prompt builder provider/anthropic Anthropic native Messages API labels May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder 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