Skip to content

Commit daa1bcb

Browse files
authored
feat(sandbox): allow configuring memory consolidation turns (#5135)
1 parent 7120585 commit daa1bcb

3 files changed

Lines changed: 130 additions & 2 deletions

File tree

‎src/agents/sandbox/config.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class MemoryGenerateConfig:
8080
evidence you actually want it to summarize.
8181
"""
8282

83+
phase_two_max_turns: int = 500
84+
"""Maximum agent turns for the separate phase-2 consolidation run.
85+
86+
The application's outer ``Runner`` turn limit does not apply to this run. This limit
87+
counts model turns, not elapsed time, tokens, or monetary cost. Exhaustion uses the
88+
runner's ``MaxTurnsExceeded`` error; the memory manager logs the failure and does not
89+
record the consolidation selection as successful. Earlier tool writes are not rolled back.
90+
"""
91+
8392
if TYPE_CHECKING:
8493

8594
def __init__(
@@ -90,6 +99,7 @@ def __init__(
9099
phase_two_model: str | Model = "gpt-5.5",
91100
phase_two_model_settings: ModelSettings | dict[str, Any] | None = ...,
92101
extra_prompt: str | None = None,
102+
phase_two_max_turns: int = 500,
93103
) -> None: ...
94104

95105
def __post_init__(self) -> None:

‎src/agents/sandbox/memory/phase_two.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ async def run_phase_two(
3434
selection=selection,
3535
extra_prompt=config.extra_prompt,
3636
)
37-
await Runner.run(agent, prompt, run_config=run_config, max_turns=500)
37+
await Runner.run(agent, prompt, run_config=run_config, max_turns=config.phase_two_max_turns)

‎tests/sandbox/test_memory.py‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
SQLiteSession,
3232
TResponseInputItem,
3333
)
34-
from agents.exceptions import UserError
34+
from agents.exceptions import MaxTurnsExceeded, UserError
3535
from agents.items import (
3636
CompactionItem,
3737
MessageOutputItem,
@@ -57,6 +57,7 @@
5757
get_or_create_memory_generation_manager,
5858
)
5959
from agents.sandbox.memory.phase_one import render_phase_one_prompt
60+
from agents.sandbox.memory.phase_two import run_phase_two
6061
from agents.sandbox.memory.prompts import (
6162
render_memory_consolidation_prompt,
6263
render_rollout_extraction_prompt,
@@ -1668,6 +1669,123 @@ async def test_sandbox_memory_caps_phase_two_selection_and_surfaces_removed_roll
16681669
await _cleanup_session(client, session, close=not closed)
16691670

16701671

1672+
@pytest.mark.asyncio
1673+
async def test_phase_two_enforces_configured_turn_limit() -> None:
1674+
client = FilesystemTestSandboxClient()
1675+
session = await client.create(manifest=Manifest())
1676+
model = ScriptedModel(
1677+
steps=[
1678+
[_patch_update_call("first-turn", "memories/MEMORY.md", "entry")],
1679+
[get_final_output_message("done")],
1680+
]
1681+
)
1682+
try:
1683+
memory_file = Path(session.state.manifest.root) / "memories" / "MEMORY.md"
1684+
memory_file.parent.mkdir(parents=True)
1685+
memory_file.write_text("")
1686+
with pytest.raises(MaxTurnsExceeded):
1687+
await run_phase_two(
1688+
config=MemoryGenerateConfig(phase_two_model=model, phase_two_max_turns=1),
1689+
memory_root="memories",
1690+
selection=_empty_phase_two_selection(),
1691+
run_config=_run_config_for_session(session),
1692+
)
1693+
assert len(model.calls) == 1
1694+
finally:
1695+
await _cleanup_session(client, session)
1696+
1697+
1698+
def test_memory_generate_config_preserves_positional_arguments_and_default_turn_limit() -> None:
1699+
phase_one_settings = ModelSettings(temperature=0.1)
1700+
phase_two_settings = ModelSettings(temperature=0.2)
1701+
config = MemoryGenerateConfig(
1702+
123, "phase-one", phase_one_settings, "phase-two", phase_two_settings, "guidance"
1703+
)
1704+
assert config.max_raw_memories_for_consolidation == 123
1705+
assert config.phase_one_model == "phase-one"
1706+
assert config.phase_one_model_settings is phase_one_settings
1707+
assert config.phase_two_model == "phase-two"
1708+
assert config.phase_two_model_settings is phase_two_settings
1709+
assert config.extra_prompt == "guidance"
1710+
assert config.phase_two_max_turns == 500
1711+
1712+
1713+
@pytest.mark.parametrize("turn_limit", [1, 2, None], ids=["exhausted", "completed", "default"])
1714+
@pytest.mark.asyncio
1715+
async def test_sandbox_memory_consolidation_turn_budget_on_session_close(
1716+
turn_limit: int | None,
1717+
) -> None:
1718+
client = FilesystemTestSandboxClient()
1719+
session = await client.create(manifest=Manifest())
1720+
phase_two_model = ScriptedModel(
1721+
steps=[
1722+
[_patch_update_call("memory-md", "memories/MEMORY.md", "consolidated entry")],
1723+
[get_final_output_message("done")],
1724+
]
1725+
)
1726+
memory = _memory_config(phase_two_model=phase_two_model)
1727+
assert memory.generate is not None
1728+
if turn_limit is not None:
1729+
memory.generate.phase_two_max_turns = turn_limit
1730+
agent = SandboxAgent(
1731+
name="worker",
1732+
model=ScriptedModel(steps=[[get_final_output_message("done")]]),
1733+
capabilities=[memory],
1734+
)
1735+
root = Path(session.state.manifest.root)
1736+
try:
1737+
result = await Runner.run(
1738+
agent, "hello", max_turns=1, run_config=_run_config_for_session(session)
1739+
)
1740+
assert result.final_output == "done"
1741+
manager = get_or_create_memory_generation_manager(session=session, memory=memory)
1742+
selection_path = root / "memories" / "phase_two_selection.json"
1743+
previous_selection = '{"selected": []}\n'
1744+
selection_path.write_text(previous_selection)
1745+
1746+
await session.aclose()
1747+
1748+
assert len(phase_two_model.calls) == (1 if turn_limit == 1 else 2)
1749+
assert (root / "memories" / "MEMORY.md").read_text() == "consolidated entry\n"
1750+
if turn_limit == 1:
1751+
assert selection_path.read_text() == previous_selection
1752+
assert len(manager._pending_phase_two_rollout_ids) == 1
1753+
else:
1754+
assert len(json.loads(selection_path.read_text())["selected"]) == 1
1755+
assert manager._pending_phase_two_rollout_ids == []
1756+
assert manager._worker_task is None
1757+
assert manager._queue.empty()
1758+
assert memory_manager_module._MEMORY_GENERATION_MANAGERS.get(session) is None
1759+
finally:
1760+
await client.delete(session)
1761+
1762+
1763+
@pytest.mark.asyncio
1764+
async def test_sandbox_memory_turn_exhaustion_releases_runner_owned_session() -> None:
1765+
client = _DeleteTrackingFilesystemTestSandboxClient()
1766+
phase_two_model = ScriptedModel(
1767+
steps=[
1768+
[_patch_update_call("first-turn", "memories/MEMORY.md", "entry")],
1769+
[get_final_output_message("done")],
1770+
]
1771+
)
1772+
memory = _memory_config(phase_two_model=phase_two_model)
1773+
assert memory.generate is not None
1774+
memory.generate.phase_two_max_turns = 1
1775+
agent = SandboxAgent(
1776+
name="worker",
1777+
model=ScriptedModel(steps=[[get_final_output_message("done")]]),
1778+
capabilities=[memory],
1779+
)
1780+
result = await Runner.run(
1781+
agent, "hello", max_turns=1, run_config=RunConfig(sandbox=SandboxRunConfig(client=client))
1782+
)
1783+
assert result.final_output == "done"
1784+
assert len(phase_two_model.calls) == 1
1785+
assert len(client.deleted_roots) == 1
1786+
assert not client.deleted_roots[0].exists()
1787+
1788+
16711789
@pytest.mark.asyncio
16721790
async def test_sandbox_memory_runs_phase_one_and_phase_two_on_session_close() -> None:
16731791
client = FilesystemTestSandboxClient()

0 commit comments

Comments
 (0)