refactor(agent): let runner own context compaction (#5568)

* refactor(agent): consolidate accepted history under pressure

* fix(agent): align provider and session compaction

* refactor(agent): simplify runner context compaction

* refactor(agent): remove background token consolidation

* fix(agent): keep injected transcript messages distinct

* refactor(agent): unify native compaction summaries

* fix(agent): preserve native compaction boundary

* fix(agent): unify context compaction paths

* fix(agent): preserve exact compaction request boundaries
This commit is contained in:
chengyongru
2026-09-02 18:05:54 +08:00
committed by GitHub
parent da96c5c6eb
commit d81aa5a4ab
44 changed files with 1914 additions and 1666 deletions
+29 -5
View File
@@ -22,7 +22,10 @@ from nanobot.providers.openai_codex_provider import (
_request_codex,
_should_retry_status,
)
from nanobot.providers.openai_responses import build_responses_state
from nanobot.providers.openai_responses import (
build_responses_state,
responses_state_items,
)
from nanobot.providers.registry import find_by_name
@@ -811,12 +814,33 @@ async def test_codex_compacts_state_at_ninety_percent_before_next_request(
)
assert response.content == "done"
assert len(bodies) == 2
assert bodies[0]["input"][-1] == {"type": "compaction_trigger"}
assert bodies[1]["input"][-1] == {
assert response.provider_compaction_applied is True
assert response.provider_compaction_state is not None
assert response.provider_compaction_scope == "prior_context"
assert responses_state_items(response.provider_compaction_state) == [{
"type": "compaction",
"encrypted_content": "compacted opaque state",
}
}]
assert len(bodies) == 2
assert bodies[0]["input"][-1] == {"type": "compaction_trigger"}
assert not any(
item.get("role") == "user"
and "new question" in str(item.get("content"))
for item in bodies[0]["input"]
)
assert {
"type": "compaction",
"encrypted_content": "compacted opaque state",
} in bodies[1]["input"]
assert bodies[1]["input"].index({
"type": "compaction",
"encrypted_content": "compacted opaque state",
}) < next(
index
for index, item in enumerate(bodies[1]["input"])
if item.get("role") == "user"
and "new question" in str(item.get("content"))
)
assert not any(
item.get("type") == "reasoning"
for item in bodies[1]["input"]
+39
View File
@@ -712,6 +712,45 @@ class TestParseResponseOutput:
assert result.provider_state is not None
assert responses_state_items(result.provider_state) == [*input_items, *output]
def test_marks_only_a_new_response_compaction(self):
compacted = parse_response_output(
{
"output": [
{"type": "compaction", "encrypted_content": "opaque"},
{"type": "message", "role": "assistant", "content": "done"},
],
"status": "completed",
"usage": {},
},
state_provider="openai:test",
state_model="gpt-5.6",
state_input_items=[{"role": "user", "content": "old"}],
)
replayed = parse_response_output(
{
"output": [
{"type": "message", "role": "assistant", "content": "continued"},
],
"status": "completed",
"usage": {},
},
state_provider="openai:test",
state_model="gpt-5.6",
state_input_items=[
{"type": "compaction", "encrypted_content": "opaque"},
],
)
assert compacted.provider_compaction_applied is True
assert compacted.provider_compaction_state is not None
assert compacted.provider_compaction_scope == "current_request"
assert responses_state_items(compacted.provider_compaction_state) == [
{"type": "compaction", "encrypted_content": "opaque"},
]
assert replayed.provider_compaction_applied is False
assert replayed.provider_compaction_state is None
assert replayed.provider_compaction_scope is None
class TestResponsesConversationState:
def test_server_compaction_prunes_superseded_prefix(self):