Skip to content

refactor(agent): extract turn assembly helpers - #17987

Open
Aliciawque wants to merge 1 commit into
NousResearch:mainfrom
Aliciawque:refactor/turn-assembly-helpers
Open

refactor(agent): extract turn assembly helpers#17987
Aliciawque wants to merge 1 commit into
NousResearch:mainfrom
Aliciawque:refactor/turn-assembly-helpers

Conversation

@Aliciawque

Copy link
Copy Markdown

What does this PR do?

Extracts the API-call-time turn assembly helpers from run_agent.py into agent/turn_assembly.py.

This is a narrower follow-up to #6441. The original PR also included a BOOT.md gateway fix, but that motivation is now obsolete after #17093 removed the built-in BOOT.md hook. This PR keeps only the refactor portion that still has value: composing ephemeral user-context blocks, effective system prompts, and prefill message insertion without changing persisted conversation state.

Related Issue

Follow-up to #6441.
Related to #17093.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Security fix
  • Documentation update
  • Tests (adding or improving test coverage)
  • Refactor (no behavior change)
  • New skill (bundled or hub)

Changes Made

  • agent/turn_assembly.py - adds helpers for user context blocks, effective system prompt composition, and prefill insertion.
  • run_agent.py - uses those helpers in the max-iteration summary path and the main conversation loop.
  • tests/agent/test_turn_assembly.py - adds focused coverage for memory/plugin context assembly, non-string user content, system prompt composition, and prefill ordering.

How to Test

  1. Run the focused CI-parity wrapper tests:

    scripts/run_tests.sh tests/agent/test_turn_assembly.py tests/run_agent/test_run_agent.py::TestRunConversation tests/run_agent/test_413_compression.py::TestHTTP413Compression::test_413_triggers_compression -q

  2. Confirm the result:

    38 passed

  3. Syntax check:

    python3 -m py_compile agent/turn_assembly.py run_agent.py

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass (not run; focused scripts/run_tests.sh coverage above passed)
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 26.3, Apple Silicon

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — pure Python message assembly only
  • I've updated tool descriptions/schemas if I changed tool behavior — N/A

Screenshots / Logs

Focused validation:

38 passed, 38 warnings in 19.10s

@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 labels Apr 30, 2026
@Aliciawque
Aliciawque marked this pull request as ready for review April 30, 2026 15:47
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the focused, behavior-preserving extraction.

Problems

  • The patch's two call sites have moved since this branch: run_conversation is now agent/conversation_loop.py:523, and handle_max_iterations is now agent/chat_completion_helpers.py:1576; run_agent.py:5770-5798 only forwards to them. GitHub accordingly reports this PR as conflicting.

Suggested changes

  • Salvage the helper into the current owners: the primary assembly is at agent/conversation_loop.py:787-881, while the summary assembly is at agent/chat_completion_helpers.py:1591-1624. Preserve main's current ordering around MoA context (conversation_loop.py:853-875) and prefill insertion (conversation_loop.py:876-881).

Automated hermes-sweeper review.

@teknium1 teknium1 added sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) sweeper:blast-massive Sweeper blast radius: massive — everyone, every turn (invariant surface) labels Jul 12, 2026
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 sweeper:blast-massive Sweeper blast radius: massive — everyone, every turn (invariant surface) sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants