mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: HarnessAgent: Disable compaction when max tokens not provided (#6410)
* HarnessAgent: Disable compaction when max tokens not provided * Fix regression. * Address PR comments * Require max_output_tokens to be positive Reject max_output_tokens=0 (must be positive), mirroring max_context_window_tokens. Addresses PR review feedback. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
93cbf6b3f0
commit
8dde9ef627
@@ -194,6 +194,63 @@ def test_create_harness_agent_returns_full_agent() -> None:
|
||||
assert isinstance(agent, FullAgent)
|
||||
|
||||
|
||||
def test_create_harness_agent_no_token_params_disables_compaction() -> None:
|
||||
"""When token params are omitted, compaction is automatically disabled."""
|
||||
agent = create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
)
|
||||
provider_types = [type(p) for p in agent.context_providers]
|
||||
assert CompactionProvider not in provider_types
|
||||
|
||||
|
||||
def test_create_harness_agent_no_token_params_skips_max_tokens_option() -> None:
|
||||
"""When max_output_tokens is omitted, max_tokens should not be set in default options."""
|
||||
agent = create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
)
|
||||
assert agent.default_options.get("max_tokens") is None
|
||||
|
||||
|
||||
def test_create_harness_agent_custom_before_strategy_enables_compaction_without_tokens() -> None:
|
||||
"""A custom before_compaction_strategy enables compaction even when token params are omitted."""
|
||||
from agent_framework import ToolResultCompactionStrategy
|
||||
|
||||
agent = create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
before_compaction_strategy=ToolResultCompactionStrategy(),
|
||||
)
|
||||
provider_types = [type(p) for p in agent.context_providers]
|
||||
assert CompactionProvider in provider_types
|
||||
|
||||
|
||||
def test_create_harness_agent_disable_compaction_overrides_custom_before_strategy() -> None:
|
||||
"""disable_compaction=True wins even when a custom before strategy is provided."""
|
||||
from agent_framework import ToolResultCompactionStrategy
|
||||
|
||||
agent = create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
before_compaction_strategy=ToolResultCompactionStrategy(),
|
||||
disable_compaction=True,
|
||||
)
|
||||
provider_types = [type(p) for p in agent.context_providers]
|
||||
assert CompactionProvider not in provider_types
|
||||
|
||||
|
||||
def test_create_harness_agent_custom_after_strategy_enables_compaction_without_tokens() -> None:
|
||||
"""A custom after_compaction_strategy enables compaction even when token params are omitted."""
|
||||
from agent_framework import ToolResultCompactionStrategy
|
||||
|
||||
agent = create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
after_compaction_strategy=ToolResultCompactionStrategy(),
|
||||
)
|
||||
compaction_providers = [p for p in agent.context_providers if isinstance(p, CompactionProvider)]
|
||||
assert len(compaction_providers) == 1
|
||||
# Before phase is skipped (no token budget, no custom before strategy), after phase is set.
|
||||
assert compaction_providers[0].before_strategy is None
|
||||
assert compaction_providers[0].after_strategy is not None
|
||||
|
||||
|
||||
# --- Validation Tests ---
|
||||
|
||||
|
||||
@@ -207,14 +264,15 @@ def test_create_harness_agent_rejects_invalid_context_tokens() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_create_harness_agent_rejects_negative_output_tokens() -> None:
|
||||
"""max_output_tokens must be non-negative."""
|
||||
with pytest.raises(ValueError, match="max_output_tokens must be non-negative"):
|
||||
create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
max_context_window_tokens=1000,
|
||||
max_output_tokens=-1,
|
||||
)
|
||||
def test_create_harness_agent_rejects_non_positive_output_tokens() -> None:
|
||||
"""max_output_tokens must be positive when provided."""
|
||||
for invalid_value in (0, -1):
|
||||
with pytest.raises(ValueError, match="max_output_tokens must be positive"):
|
||||
create_harness_agent(
|
||||
client=_FakeChatClient(), # type: ignore[arg-type]
|
||||
max_context_window_tokens=1000,
|
||||
max_output_tokens=invalid_value,
|
||||
)
|
||||
|
||||
|
||||
def test_create_harness_agent_rejects_output_gte_context() -> None:
|
||||
|
||||
Reference in New Issue
Block a user