fix(compaction): retain native state across retry and recovery (#6558)

* fix(compaction): retain native state across retry and recovery

Accept successful native compaction before generation, checkpoint its durable continuation, and reuse it across provider retry and fallback scopes. Preserve pending tool pairs and temporary-session ownership boundaries.

Resolves NAN-404

* test: fix native compaction regression imports
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-04 16:28:45 +08:00
1 parent 8ff04aad2b
commit 34bab234b5
12 files changed
+628 -68

No files matched your search

+20 -1
View File
@@ -40,6 +40,7 @@ from nanobot.providers.base import (
ProviderCallContext,
ProviderConversationState,
)
from nanobot.providers.compaction import ProviderStateCommitError
from nanobot.providers.conversation_state import ProviderConversationStateController
from nanobot.session.history_visibility import HIDDEN_HISTORY_META, is_hidden_history_message
from nanobot.session.summary import SessionSummaryCheckpoint
@@ -379,12 +380,25 @@ class AgentRunner:
pending_length_segment: tuple[AgentHookContext, str] | None = None
had_injections = False
pending_stream_content: str | None = None
async def checkpoint_compaction(state: ProviderConversationState) -> None:
await self._emit_checkpoint(spec, {
"phase": "model_input",
"iteration": iteration,
"model": spec.runtime.model,
"assistant_message": None,
"completed_tool_results": [],
"pending_tool_calls": [],
"provider_state": state,
}, messages)
conversation_state = ProviderConversationStateController(
provider=spec.runtime.provider,
model=spec.runtime.model,
messages=messages,
state=spec.provider_state,
session_id=spec.session_key,
checkpoint_callback=checkpoint_compaction,
)
governance_config = ContextGovernanceConfig(
provider=spec.runtime.provider,
@@ -1287,6 +1301,8 @@ class AgentRunner:
request_state=request_state,
transcript=messages,
)
except ProviderStateCommitError:
raise
except Exception:
logger.opt(exception=tool_log_content_allowed()).error(
"Budget-exhausted finalization failed for {}; using fallback",
@@ -1465,7 +1481,10 @@ class AgentRunner:
start = len(spec.initial_messages)
# Replacing the checkpoint must retain every earlier round of this
# run, including follow-ups consumed since the accepted input.
batch_size = 1 + len(payload["completed_tool_results"])
batch_size = (
0 if payload["phase"] == "model_input"
else 1 + len(payload["completed_tool_results"])
)
await callback({
**payload,
"preceding_messages": messages[start:len(messages) - batch_size],