refactor(agent): decompose init_agent into ordered phase helpers

init_agent (2711 LOC) becomes a ~280-line ordered orchestrator over
_resolve_api_mode / _finalize_routing / _init_* / _build_client /
_load_tools / _parse_compression_config -> CompressionSettings /
_resolve_context_length / _build_context_engine / ... phase helpers.
_build_client is further split per wire mode (_init_anthropic_client,
_init_moa_client, _init_bedrock_client, _init_openai_client with
_explicit_client_kwargs / _routed_client_kwargs). Statement order and
every side effect on the agent are preserved (AST body-parity checked
against origin/main).

Dedupe/dead code: drop _relay_moa_reference_event/_moa_reference_output_allowed
(zero callers; only their own test) and their test file; alias
_normalize_route_base_url; _parse_config_int replaces three copies of the
strict int parser; _cfg_flag replaces four inline truthy-set checks;
_client_kwargs_from_routed + _fallback_entries replace duplicated
routed-client/fallback-entry blocks; _warn_invalid_config_int unifies the
three log+stderr invalid-int warnings (byte-identical text);
_bedrock_region_from_url; _memory_provider_init_kwargs; the
host->default_headers if/elif chain becomes the _HOST_DEFAULT_HEADERS
dispatch table; callback params assigned from _CALLBACK_PARAMS.

Comments/docstrings hand-compacted to their rationale (invariants,
ordering, failure modes kept; issue numbers and narrative dropped).
test_pre_compress_checkpoint_contract source-check repointed at the
CompressionSettings field names.

Verified: tests/run_agent (2066 passed) + all agent_init-referencing tests
(1134 passed), get_tool_definitions() byte-identical vs origin/main, import
smokes for cli/run_agent/gateway.run/hermes_cli.main/agent.conversation_loop/
tui_gateway.server.
This commit is contained in:
Teknium
2026-09-02 12:00:05 -07:00
parent a3d33fe22f
commit 76062b1d6b
3 changed files with 2391 additions and 2651 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,81 +0,0 @@
"""Regression coverage for machine-readable MoA quiet output."""
from __future__ import annotations
from types import SimpleNamespace
import unittest
from agent.agent_init import _relay_moa_reference_event
class MoAQuietReferenceOutputTests(unittest.TestCase):
@staticmethod
def _agent(*, platform: str, tool_progress_mode: str, quiet_mode: bool = True):
calls = []
def callback(*args, **kwargs):
calls.append((args, kwargs))
return SimpleNamespace(
platform=platform,
tool_progress_mode=tool_progress_mode,
quiet_mode=quiet_mode,
tool_progress_callback=callback,
), calls
def test_machine_readable_cli_suppresses_reference_relay(self) -> None:
agent, calls = self._agent(platform="cli", tool_progress_mode="off")
_relay_moa_reference_event(
agent,
"moa.reference",
label="local:advisor",
text="hidden",
index=1,
count=1,
)
self.assertEqual(calls, [])
def test_interactive_cli_delivers_reference_relay(self) -> None:
agent, calls = self._agent(
platform="cli",
tool_progress_mode="all",
quiet_mode=True,
)
_relay_moa_reference_event(
agent,
"moa.reference",
label="local:advisor",
text="visible",
index=1,
count=2,
)
self.assertEqual(
calls,
[
(
("moa.reference", "local:advisor", "visible", None),
{"moa_index": 1, "moa_count": 2},
)
],
)
def test_gateway_delivers_even_when_progress_mode_is_off(self) -> None:
agent, calls = self._agent(platform="discord", tool_progress_mode="off")
_relay_moa_reference_event(
agent,
"moa.aggregating",
aggregator="local:aggregator",
ref_count=2,
)
self.assertEqual(
calls,
[
(
("moa.aggregating", "local:aggregator", None, None),
{"moa_ref_count": 2},
)
],
)
if __name__ == "__main__":
unittest.main()

View File

@@ -520,10 +520,8 @@ def test_agent_init_suppresses_micro_compaction_under_checkpoint_gate():
source = inspect.getsource(agent_init)
# The suppression must happen before the compressor attribute assignment.
suppress_idx = source.find(
"if compression_checkpoint_required and compression_micro_compact:"
)
assign_idx = source.find("_cc._micro_compact_enabled = compression_micro_compact")
suppress_idx = source.find("if cs.checkpoint_required and cs.micro_compact:")
assign_idx = source.find("_cc._micro_compact_enabled = cs.micro_compact")
assert suppress_idx != -1, (
"init_agent must suppress micro-compaction when checkpoint_required"
)