fix(compaction): align native lifecycle and reconcile replay notices

Refs: NAN-326, NAN-327
This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-22 14:39:30 +08:00
1 parent f9257c3548
commit be43d2a5f8
8 files changed
+298 -78

No files matched your search

+10 -26
View File
@@ -43,6 +43,7 @@ from nanobot.session.summary import (
SUMMARY_CONTINUATION_TEXT,
SessionSummaryCheckpoint,
)
from nanobot.utils.cancellation import shield_and_drain
from nanobot.utils.helpers import (
estimate_prompt_tokens_chain,
maybe_persist_tool_result,
@@ -529,6 +530,11 @@ class ContextGovernor:
if message.get("role") == "tool"
and isinstance(message.get("tool_call_id"), str)
)
if response.provider_compaction_event_id is None:
response.provider_compaction_event_id = uuid4().hex
await shield_and_drain(state.events.emit(ContextCompactionEvent(
response.provider_compaction_event_id, "succeeded",
)))
if (
not response.provider_compaction_applied
or response.provider_compaction_state is None
@@ -553,40 +559,18 @@ class ContextGovernor:
)
return
compaction_id = uuid4().hex
await state.events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="started"),
summary = await compaction.consolidate_provider_compaction(
response.provider_compaction_state,
await self.prepare_in_background(deepcopy, accepted_messages),
compaction.active_summary,
)
try:
summary = await compaction.consolidate_provider_compaction(
response.provider_compaction_state,
await self.prepare_in_background(deepcopy, accepted_messages),
compaction.active_summary,
)
except (Exception, asyncio.CancelledError) as exc:
await state.events.emit(
ContextCompactionEvent(
compaction_id=compaction_id,
phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
),
)
raise
if not summary:
await state.events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="failed"),
)
return
compaction.active_summary = summary
compaction.summary_checkpoint = SessionSummaryCheckpoint(
summary=summary,
transcript_boundary=transcript_boundary,
)
await state.events.emit(
ContextCompactionEvent(
compaction_id=compaction_id,
phase="succeeded",
),
)
async def _compact_request_history(
self,