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:
1 parent
da96c5c6eb
commit
d81aa5a4ab
44 files changed
+1914
-1666
No files matched your search
+119
-38
@@ -13,6 +13,7 @@ import weakref
|
||||
from collections.abc import Coroutine, Iterable, Mapping
|
||||
from contextlib import AbstractContextManager, ExitStack, nullcontext, suppress
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from enum import Enum, auto
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
@@ -93,7 +94,11 @@ from nanobot.session.recovery import (
|
||||
restore_pending_interruption,
|
||||
restore_runtime_checkpoint,
|
||||
)
|
||||
from nanobot.session.summary import SessionSummary
|
||||
from nanobot.session.summary import (
|
||||
SUMMARY_CONTINUATION_TEXT,
|
||||
SessionSummary,
|
||||
SessionSummaryCheckpoint,
|
||||
)
|
||||
from nanobot.triggers.local_turns import LocalTriggerTurnCoordinator
|
||||
from nanobot.utils.cancellation import task_is_cancelling
|
||||
from nanobot.utils.document import reference_non_image_attachments
|
||||
@@ -161,6 +166,8 @@ class TurnContext:
|
||||
|
||||
pending_queue: asyncio.Queue[InboundMessage] | None = None
|
||||
pending_summary: SessionSummary | None = None
|
||||
summary_checkpoint: SessionSummaryCheckpoint | None = None
|
||||
provider_compaction_applied: bool = False
|
||||
|
||||
ephemeral: bool = False
|
||||
run_extra_hooks_for_ephemeral: bool = False
|
||||
@@ -923,19 +930,6 @@ class AgentLoop:
|
||||
return
|
||||
remember_last_channel(session.metadata, msg.channel, msg.chat_id)
|
||||
|
||||
@staticmethod
|
||||
def _replay_token_budget(runtime: LLMRuntime) -> int:
|
||||
"""Derive a token budget for session history replay from the context window."""
|
||||
if runtime.context_window_tokens <= 0:
|
||||
return 0
|
||||
max_output = runtime.generation.max_tokens
|
||||
try:
|
||||
reserved_output = int(max_output)
|
||||
except (TypeError, ValueError):
|
||||
reserved_output = 4096
|
||||
budget = runtime.context_window_tokens - max(1, reserved_output) - 1024
|
||||
return budget if budget > 0 else max(128, runtime.context_window_tokens // 2)
|
||||
|
||||
async def _run_agent_loop(
|
||||
self,
|
||||
transcript_input: TranscriptInput,
|
||||
@@ -1186,6 +1180,26 @@ class AgentLoop:
|
||||
provider_retry_mode=self.provider_retry_mode,
|
||||
retry_wait_callback=on_retry_wait,
|
||||
checkpoint_callback=_checkpoint,
|
||||
consolidate_history=(
|
||||
partial(
|
||||
self.consolidator.summarize_transcript,
|
||||
runtime=runtime,
|
||||
session_key=session.key,
|
||||
tools=effective_tools.get_definitions(),
|
||||
)
|
||||
if session is not None and not ephemeral
|
||||
else None
|
||||
),
|
||||
consolidate_provider_compaction=(
|
||||
partial(
|
||||
self.consolidator.summarize_provider_compaction,
|
||||
runtime=runtime,
|
||||
session_key=session.key,
|
||||
tools=effective_tools.get_definitions(),
|
||||
)
|
||||
if session is not None and not ephemeral
|
||||
else None
|
||||
),
|
||||
injection_callback=_drain_pending,
|
||||
terminal_injection_callback=_wait_for_pending,
|
||||
# Sustained goals may legitimately exceed NANOBOT_LLM_TIMEOUT_S; idle stall
|
||||
@@ -1886,12 +1900,6 @@ class AgentLoop:
|
||||
if ctx.on_runtime_admitted is not None:
|
||||
await ctx.on_runtime_admitted(runtime)
|
||||
if not ctx.ephemeral:
|
||||
await self.consolidator.maybe_consolidate_by_tokens(
|
||||
session,
|
||||
runtime=runtime,
|
||||
)
|
||||
# Token consolidation may have committed a replacement checkpoint
|
||||
# after the compact stage captured its summary for this request.
|
||||
ctx.session, ctx.pending_summary = self.auto_compact.prepare_session(
|
||||
session,
|
||||
ctx.session_key,
|
||||
@@ -1899,11 +1907,7 @@ class AgentLoop:
|
||||
session = ctx.require_session()
|
||||
is_subagent = ctx.kind is TurnKind.SYSTEM and ctx.msg.sender_id == "subagent"
|
||||
|
||||
_hist_kwargs: dict[str, Any] = {
|
||||
"max_tokens": self._replay_token_budget(runtime),
|
||||
"extend_to_user": is_subagent,
|
||||
}
|
||||
ctx.history = session.get_history(**_hist_kwargs)
|
||||
ctx.history = session.get_history(extend_to_user=is_subagent)
|
||||
stored_state = session.provider_state
|
||||
subagent_followup_persisted = False
|
||||
if is_subagent:
|
||||
@@ -2021,6 +2025,8 @@ class AgentLoop:
|
||||
)
|
||||
ctx.final_content = result.final_content
|
||||
ctx.all_messages = result.messages
|
||||
ctx.summary_checkpoint = result.summary_checkpoint
|
||||
ctx.provider_compaction_applied = result.provider_compaction_applied
|
||||
ctx.stop_reason = result.stop_reason
|
||||
if (
|
||||
ctx.kind is TurnKind.USER
|
||||
@@ -2034,7 +2040,6 @@ class AgentLoop:
|
||||
await turn_continuation.maybe_continue_turn(ctx)
|
||||
|
||||
async def _persist_turn(self, ctx: TurnContext) -> None:
|
||||
runtime = ctx.require_runtime()
|
||||
session = ctx.require_session()
|
||||
turn_continuation.prepare_save_boundary(ctx)
|
||||
|
||||
@@ -2060,15 +2065,18 @@ class AgentLoop:
|
||||
self._save_turn(
|
||||
session, ctx.all_messages, ctx.save_skip,
|
||||
turn_latency_ms=ctx.turn_latency_ms,
|
||||
summary_checkpoint=ctx.summary_checkpoint,
|
||||
input_persisted_early=ctx.input_persisted_early,
|
||||
)
|
||||
if (
|
||||
not ctx.ephemeral
|
||||
and ctx.provider_compaction_applied
|
||||
and ctx.summary_checkpoint is not None
|
||||
):
|
||||
# The next request must rebuild from the portable checkpoint;
|
||||
# the opaque continuation predates that transcript rewrite.
|
||||
session.provider_state = None
|
||||
ctx.delivery.record_latency(ctx.turn_latency_ms)
|
||||
if not ctx.ephemeral:
|
||||
self.schedule_background(
|
||||
self.consolidator.maybe_consolidate_by_tokens(
|
||||
session,
|
||||
runtime=runtime,
|
||||
)
|
||||
)
|
||||
self._clear_pending_user_turn(session)
|
||||
self._clear_runtime_checkpoint(session)
|
||||
self.sessions.save(session)
|
||||
@@ -2142,6 +2150,55 @@ class AgentLoop:
|
||||
|
||||
return filtered
|
||||
|
||||
@staticmethod
|
||||
def _insert_summary_checkpoint(
|
||||
session: Session,
|
||||
checkpoint: SessionSummaryCheckpoint,
|
||||
*,
|
||||
insert_at: int | None = None,
|
||||
) -> None:
|
||||
"""Commit a replacement summary and its hidden transcript boundary."""
|
||||
hint = {
|
||||
"role": "user",
|
||||
"content": SUMMARY_CONTINUATION_TEXT,
|
||||
HIDDEN_HISTORY_META: True,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
}
|
||||
if insert_at is None:
|
||||
session.messages.append(hint)
|
||||
checkpoint_session_index = len(session.messages) - 1
|
||||
else:
|
||||
session.messages.insert(insert_at, hint)
|
||||
checkpoint_session_index = insert_at
|
||||
session.metadata["_last_summary"] = {
|
||||
"text": checkpoint.summary,
|
||||
"last_active": session.updated_at.isoformat(),
|
||||
}
|
||||
session.last_archived = checkpoint_session_index
|
||||
|
||||
@staticmethod
|
||||
def _validated_checkpoint_boundary(
|
||||
checkpoint: SessionSummaryCheckpoint | None,
|
||||
*,
|
||||
skip: int,
|
||||
message_count: int,
|
||||
session_key: str,
|
||||
) -> int | None:
|
||||
"""Return a checkpoint boundary only when it belongs to this turn."""
|
||||
if checkpoint is None:
|
||||
return None
|
||||
boundary = checkpoint.transcript_boundary
|
||||
if skip - 1 <= boundary <= message_count:
|
||||
return boundary
|
||||
logger.warning(
|
||||
"Ignoring invalid summary boundary {} outside [{}, {}] for {}",
|
||||
boundary,
|
||||
skip - 1,
|
||||
message_count,
|
||||
session_key,
|
||||
)
|
||||
return None
|
||||
|
||||
def _save_turn(
|
||||
self,
|
||||
session: Session,
|
||||
@@ -2149,10 +2206,10 @@ class AgentLoop:
|
||||
skip: int,
|
||||
*,
|
||||
turn_latency_ms: int | None = None,
|
||||
summary_checkpoint: SessionSummaryCheckpoint | None = None,
|
||||
input_persisted_early: bool = False,
|
||||
) -> None:
|
||||
"""Save new-turn messages into session, truncating large tool results."""
|
||||
from datetime import datetime
|
||||
|
||||
"""Commit new-turn messages and an optional summary boundary."""
|
||||
declared_tool_call_ids = {
|
||||
str(tc["id"])
|
||||
for m in session.messages
|
||||
@@ -2169,8 +2226,30 @@ class AgentLoop:
|
||||
}
|
||||
last_assistant_idx: int | None = None
|
||||
saved_followup_ids: set[str] = set()
|
||||
for m in messages[skip:]:
|
||||
entry = dict(m)
|
||||
checkpoint_boundary = self._validated_checkpoint_boundary(
|
||||
summary_checkpoint,
|
||||
skip=skip,
|
||||
message_count=len(messages),
|
||||
session_key=session.key,
|
||||
)
|
||||
|
||||
# The trigger input may already be the session tail while still being
|
||||
# the first message after the replacement checkpoint.
|
||||
if summary_checkpoint is not None and checkpoint_boundary == skip - 1:
|
||||
insert_at = len(session.messages) - (1 if input_persisted_early else 0)
|
||||
self._insert_summary_checkpoint(
|
||||
session,
|
||||
summary_checkpoint,
|
||||
insert_at=insert_at,
|
||||
)
|
||||
|
||||
for message_index, message in enumerate(messages[skip:], start=skip):
|
||||
# Insert against the raw transcript index before filtering the
|
||||
# message so persistence cleanup cannot shift the H/Δ boundary.
|
||||
if summary_checkpoint is not None and checkpoint_boundary == message_index:
|
||||
self._insert_summary_checkpoint(session, summary_checkpoint)
|
||||
|
||||
entry = dict(message)
|
||||
followup_id_value = cast(object, entry.pop(PENDING_FOLLOWUP_ID_KEY, None))
|
||||
followup_ids = (
|
||||
[followup_id_value]
|
||||
@@ -2249,6 +2328,8 @@ class AgentLoop:
|
||||
for tc in (cast(dict[str, Any], tc_value),)
|
||||
if tc.get("id")
|
||||
)
|
||||
if summary_checkpoint is not None and checkpoint_boundary == len(messages):
|
||||
self._insert_summary_checkpoint(session, summary_checkpoint)
|
||||
if turn_latency_ms is not None and last_assistant_idx is not None:
|
||||
session.messages[last_assistant_idx]["latency_ms"] = int(turn_latency_ms)
|
||||
if saved_followup_ids:
|
||||
|
||||
Reference in new issue
Block a user