refactor(agent): remove loop iteration state (#5549)

* refactor(agent): remove loop iteration state

* docs(my): remove stale iteration guidance
This commit is contained in:
chengyongru
2026-08-26 15:57:47 +08:00
committed by GitHub
parent 4f6c0aedfa
commit 9d34fc5af2
10 changed files with 22 additions and 81 deletions
+1 -18
View File
@@ -31,33 +31,18 @@ def test_turn_hook_context_preserves_legacy_positional_arguments(tmp_path) -> No
assert context.attributes == {}
@pytest.mark.asyncio
async def test_turn_hook_builder_runs_progress_hook_before_extra_hooks() -> None:
events: list[str] = []
hook = build_agent_turn_hook(AgentTurnHookSpec(
on_iteration=lambda iteration: events.append(f"progress:{iteration}"),
registered_hooks=[RecordingHook(events)],
))
await hook.before_iteration(AgentHookContext(iteration=2, messages=[]))
assert events == ["progress:2", "hook:2"]
@pytest.mark.asyncio
async def test_turn_hook_builder_runs_registered_hooks_before_turn_hooks() -> None:
events: list[str] = []
hook = build_agent_turn_hook(AgentTurnHookSpec(
on_iteration=lambda iteration: events.append(f"progress:{iteration}"),
registered_hooks=[RecordingHook(events, "registered")],
turn_hooks=[RecordingHook(events, "turn")],
))
await hook.before_iteration(AgentHookContext(iteration=2, messages=[]))
assert events == ["progress:2", "registered:2", "turn:2"]
assert events == ["registered:2", "turn:2"]
@pytest.mark.asyncio
@@ -75,7 +60,6 @@ async def test_turn_hook_builder_runs_factories_with_matching_registration_order
return _create
hook = build_agent_turn_hook(AgentTurnHookSpec(
on_iteration=lambda iteration: events.append(f"progress:{iteration}"),
channel="websocket",
chat_id="chat-1",
message_id="msg-1",
@@ -92,7 +76,6 @@ async def test_turn_hook_builder_runs_factories_with_matching_registration_order
await hook.before_iteration(AgentHookContext(iteration=2, messages=[]))
assert events == [
"progress:2",
"registered_factory:2",
"registered:2",
"turn_factory:2",
+16 -1
View File
@@ -57,7 +57,22 @@ def test_runtime_snapshot_has_exact_allowlist_and_redacts_secrets(tmp_path: Path
snapshot = _my_tool(loop)._runtime_control.snapshot()
values = snapshot.as_mapping()
assert frozenset(values) == RUNTIME_SNAPSHOT_KEYS
expected_snapshot_keys = frozenset({
"model",
"model_preset",
"model_presets",
"max_iterations",
"context_window_tokens",
"workspace",
"provider_retry_mode",
"max_tool_result_chars",
"tool_names",
"web_config",
"exec_config",
"subagents",
})
assert RUNTIME_SNAPSHOT_KEYS == expected_snapshot_keys
assert frozenset(values) == expected_snapshot_keys
assert RUNTIME_COMMAND_KEYS == frozenset({
"model",
"model_preset",
-29
View File
@@ -32,8 +32,6 @@ def _make_mock_loop(**overrides):
loop._start_time = 1000.0
loop.exec_config = ExecToolConfig()
loop.channels_config = MagicMock()
loop._current_iteration = 0
loop.current_iteration = loop._current_iteration
loop.provider_retry_mode = "standard"
loop.max_tool_result_chars = 16000
loop.model_preset = None
@@ -110,7 +108,6 @@ class TestInspectSummary:
assert "workspace" in result
assert "provider_retry_mode" in result
assert "max_tool_result_chars" in result
assert "_current_iteration" in result
# ---------------------------------------------------------------------------
@@ -1080,32 +1077,6 @@ class TestSecurityAttributeProtection:
assert result == "model_presets.fast.model: 'fast-model'"
# ---------------------------------------------------------------------------
# current iteration count (Fix #2)
# ---------------------------------------------------------------------------
class TestCurrentIteration:
@pytest.mark.asyncio
async def test_inspect_current_iteration(self):
tool = _make_tool()
result = await tool.execute(action="check", key="_current_iteration")
assert "0" in result
@pytest.mark.asyncio
async def test_current_iteration_in_summary(self):
tool = _make_tool()
result = await tool.execute(action="check")
assert "_current_iteration" in result
@pytest.mark.asyncio
async def test_modify_current_iteration_blocked(self):
"""_current_iteration is READ_ONLY — cannot be set manually."""
tool = _make_tool()
result = await tool.execute(action="set", key="_current_iteration", value=5)
assert "read-only" in result
# ---------------------------------------------------------------------------
# request context (audit session tracking)
# ---------------------------------------------------------------------------