fix(agent): remove local context tail truncation (#5820)

* fix(agent): remove local context tail truncation

* fix(agent): defer subagent type annotations

* fix(agent): require context consolidator for runner

* refactor(agent): minimize consolidator contract diff
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-20 16:00:44 +08:00
1 parent 8d0430c761
commit 29ba4c7f2d
18 files changed
+483 -855

No files matched your search

+86 -171
View File
@@ -42,9 +42,7 @@ from nanobot.session.summary import (
SessionSummaryCheckpoint,
)
from nanobot.utils.helpers import (
estimate_message_tokens,
estimate_prompt_tokens_chain,
find_legal_message_start,
maybe_persist_tool_result,
truncate_text,
)
@@ -55,6 +53,7 @@ if TYPE_CHECKING:
from nanobot.providers.base import LLMProvider
TranscriptBuilder = Callable[[TranscriptInput], list[dict[str, Any]]]
SummaryTranscriptBuilder = Callable[[str], list[dict[str, Any]]]
HistoryConsolidator = Callable[
[list[dict[str, Any]], str | None],
Awaitable[str | None],
@@ -64,7 +63,7 @@ ProviderCompactionConsolidator = Callable[
Awaitable[str | None],
]
SNIP_SAFETY_BUFFER = 1024
CONTEXT_SAFETY_BUFFER = 1024
# read_file has its own bound; exempt it to avoid persist->read->persist loops.
TOOL_RESULT_OFFLOAD_EXEMPT_TOOLS = frozenset({"read_file"})
BACKFILL_CONTENT = "[Tool result unavailable — call was interrupted or lost]"
@@ -74,7 +73,7 @@ PLACEHOLDER_TEXTS = frozenset({
class ContextWindowExceededError(RuntimeError):
"""Raised before a locally fitted request that still exceeds its budget."""
"""Raised before a request that exceeds its local context budget."""
def __init__(
self,
@@ -89,7 +88,7 @@ class ContextWindowExceededError(RuntimeError):
self.input_budget = input_budget
self.source = source
super().__init__(
"Model input still exceeds the local context budget after request fitting "
"Model input exceeds the local context budget "
f"for {session_key or 'default'}: {estimated_tokens}/{input_budget} via {source}"
)
@@ -129,8 +128,7 @@ class ContextCompactionState:
accepted_messages: list[dict[str, Any]]
raw_accepted_boundary: int
active_summary: str | None
transcript_input: TranscriptInput
transcript_builder: TranscriptBuilder
summary_transcript_builder: SummaryTranscriptBuilder
consolidate_history: HistoryConsolidator
consolidate_provider_compaction: ProviderCompactionConsolidator | None
summary_checkpoint: SessionSummaryCheckpoint | None = None
@@ -140,14 +138,28 @@ class ContextCompactionState:
cls,
transcript_input: TranscriptInput,
transcript_builder: TranscriptBuilder,
consolidate_history: HistoryConsolidator | None,
consolidate_history: HistoryConsolidator,
consolidate_provider_compaction: ProviderCompactionConsolidator | None,
) -> tuple[list[dict[str, Any]], ContextCompactionState | None]:
) -> tuple[list[dict[str, Any]], ContextCompactionState]:
"""Build the raw transcript and its initial H/delta boundary."""
messages = list(transcript_builder(transcript_input))
if consolidate_history is None:
return messages, None
accepted_history_boundary = 1 + len(transcript_input.history)
def build_summary_transcript(summary: str) -> list[dict[str, Any]]:
return transcript_builder(
replace(
transcript_input,
history=[],
current_message=None,
media=None,
session_summary={
"text": summary,
"last_active": datetime.now().astimezone().isoformat(),
},
runtime_context_blocks=None,
)
)
return messages, cls(
raw_messages=messages,
accepted_messages=deepcopy(messages[:accepted_history_boundary]),
@@ -157,8 +169,48 @@ class ContextCompactionState:
if transcript_input.session_summary is not None
else None
),
transcript_input=transcript_input,
transcript_builder=transcript_builder,
summary_transcript_builder=build_summary_transcript,
consolidate_history=consolidate_history,
consolidate_provider_compaction=consolidate_provider_compaction,
)
@classmethod
def from_messages(
cls,
messages: list[dict[str, Any]],
consolidate_history: HistoryConsolidator,
consolidate_provider_compaction: ProviderCompactionConsolidator | None,
) -> ContextCompactionState:
"""Create compaction state for a standalone runner transcript."""
raw_messages = list(messages)
instruction_prefix: list[dict[str, Any]] = []
for message in raw_messages:
if message.get("role") not in {"system", "developer"}:
break
instruction_prefix.append(dict(message))
def build_summary_transcript(summary: str) -> list[dict[str, Any]]:
archived_context = (
"[Archived Context Summary]\n\n"
"Previous conversation summary:\n"
f"{summary}"
)
prefix = deepcopy(instruction_prefix)
for index in range(len(prefix) - 1, -1, -1):
content = prefix[index].get("content")
if isinstance(content, str):
prefix[index]["content"] = (
f"{content}\n\n---\n\n{archived_context}"
)
return prefix
return [{"role": "system", "content": archived_context}, *prefix]
return cls(
raw_messages=raw_messages,
accepted_messages=deepcopy(raw_messages),
raw_accepted_boundary=len(raw_messages),
active_summary=None,
summary_transcript_builder=build_summary_transcript,
consolidate_history=consolidate_history,
consolidate_provider_compaction=consolidate_provider_compaction,
)
@@ -195,10 +247,10 @@ class ModelRequestState:
config: ContextGovernanceConfig
conversation: ProviderConversationStateController
compaction: ContextCompactionState
usage: LLMUsage | None = None
messages: list[dict[str, Any]] | None = None
tool_definitions: list[dict[str, Any]] | None = None
compaction: ContextCompactionState | None = None
provider_compaction_applied: bool = False
compacted_tool_results: set[str] = field(default_factory=set)
events: EventSink = NO_EVENTS
@@ -333,28 +385,6 @@ class ContextGovernor:
updated = self.backfill_missing_tool_results(updated)
return self.apply_tool_result_budget(config, updated)
def fit_to_budget(
self,
config: ContextGovernanceConfig,
messages: list[dict[str, Any]],
*,
tool_definitions: list[dict[str, Any]] | None,
) -> list[dict[str, Any]]:
"""Fit a model-facing copy while keeping the source transcript intact."""
updated = self.snip_history(
config,
messages,
tool_definitions=tool_definitions,
force=True,
)
updated = self.drop_orphan_tool_results(updated)
updated = self.backfill_missing_tool_results(updated)
return self.ensure_request_fits(
config,
updated,
tool_definitions=tool_definitions,
)
def ensure_request_fits(
self,
config: ContextGovernanceConfig,
@@ -422,19 +452,7 @@ class ContextGovernor:
summary: str,
) -> list[dict[str, Any]]:
"""Rebuild only the stable system prefix around a replacement summary."""
return compaction.transcript_builder(
replace(
compaction.transcript_input,
history=[],
current_message=None,
media=None,
session_summary={
"text": summary,
"last_active": datetime.now().astimezone().isoformat(),
},
runtime_context_blocks=None,
)
)
return compaction.summary_transcript_builder(summary)
async def summarize_provider_compaction(
self,
@@ -450,7 +468,7 @@ class ContextGovernor:
# their full text. They no longer prove what the model can read.
replaced_messages = (
compaction.accepted_messages
if response.provider_compaction_scope == "prior_context" and compaction is not None
if response.provider_compaction_scope == "prior_context"
else state.messages or []
)
state.compacted_tool_results.update(
@@ -461,7 +479,6 @@ class ContextGovernor:
if (
not response.provider_compaction_applied
or response.provider_compaction_state is None
or compaction is None
or compaction.consolidate_provider_compaction is None
):
return
@@ -605,7 +622,7 @@ class ContextGovernor:
tool_definitions: list[dict[str, Any]] | None,
transcript: list[dict[str, Any]] | None = None,
) -> tuple[list[dict[str, Any]], ProviderCallContext | None]:
"""Prepare, compact or fit, and record the exact provider payload."""
"""Prepare or compact and record the exact provider payload."""
prepared = self.prepare_messages_for_model(state.config, messages)
model_messages: list[dict[str, Any]] | None = prepared
supplemental_messages: list[dict[str, Any]] | None = None
@@ -625,63 +642,30 @@ class ContextGovernor:
and prepared == state.messages
and tool_definitions == state.tool_definitions
)
request_was_fitted = False
compaction = state.compaction
if compaction is None:
pressure = self.request_pressure(
state.config,
prepared,
state.usage,
usage_matches_messages=usage_matches_messages,
pressure = self.request_pressure(
state.config,
prepared,
state.usage,
usage_matches_messages=usage_matches_messages,
tool_definitions=tool_definitions,
request_context_tokens=request_context_tokens,
)
if pressure is not None:
prepared = await self._compact_request_history(
state,
state.compaction,
messages,
pressure,
tool_definitions=tool_definitions,
request_context_tokens=request_context_tokens,
)
if pressure is not None:
compaction_id = uuid4().hex
await state.events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="started"),
)
try:
prepared = self.fit_to_budget(
state.config,
prepared,
tool_definitions=tool_definitions,
)
except Exception:
await state.events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="failed"),
)
raise
await state.events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="succeeded"),
)
request_was_fitted = True
else:
pressure = self.request_pressure(
state.config,
prepared,
state.usage,
usage_matches_messages=usage_matches_messages,
tool_definitions=tool_definitions,
request_context_tokens=request_context_tokens,
)
if pressure is not None:
prepared = await self._compact_request_history(
state,
compaction,
messages,
pressure,
tool_definitions=tool_definitions,
)
model_messages = prepared
supplemental_messages = None
model_messages = prepared
supplemental_messages = None
provider_context = (
state.conversation.prepare_request(
transcript,
context_window_tokens=state.config.context_window_tokens,
model_messages=model_messages,
supplemental_messages=supplemental_messages,
resume_state=not request_was_fitted,
)
if transcript is not None
else state.conversation.independent_request_context(
@@ -709,7 +693,7 @@ class ContextGovernor:
max_output = config.max_tokens if isinstance(config.max_tokens, int) else (
provider_max_tokens if isinstance(provider_max_tokens, int) else 4096
)
budget = config.context_window_tokens - max_output - SNIP_SAFETY_BUFFER
budget = config.context_window_tokens - max_output - CONTEXT_SAFETY_BUFFER
return budget if budget > 0 else 0
@staticmethod
@@ -954,72 +938,3 @@ class ContextGovernor:
updated = [dict(m) for m in messages]
updated[idx]["content"] = normalized
return updated
def snip_history(
self,
config: ContextGovernanceConfig,
messages: list[dict[str, Any]],
*,
tool_definitions: list[dict[str, Any]] | None,
force: bool = False,
) -> list[dict[str, Any]]:
if not messages or not config.context_window_tokens:
return messages
budget = self.input_budget(config)
if budget <= 0:
return messages
if not force:
estimate, _ = estimate_prompt_tokens_chain(
config.provider,
config.model,
messages,
tool_definitions,
)
if estimate <= budget:
return messages
system_messages = [dict(msg) for msg in messages if msg.get("role") == "system"]
non_system = [dict(msg) for msg in messages if msg.get("role") != "system"]
if not non_system:
return messages
system_tokens = sum(estimate_message_tokens(msg) for msg in system_messages)
fixed_tokens, _ = estimate_prompt_tokens_chain(
config.provider,
config.model,
system_messages,
tool_definitions,
)
remaining_budget = max(0, budget - max(system_tokens, fixed_tokens))
kept: list[dict[str, Any]] = []
kept_tokens = 0
for message in reversed(non_system):
msg_tokens = estimate_message_tokens(message)
if kept and kept_tokens + msg_tokens > remaining_budget:
break
kept.append(message)
kept_tokens += msg_tokens
kept.reverse()
return system_messages + self._legal_history_tail(kept, non_system)
def _legal_history_tail(
self,
kept: list[dict[str, Any]],
non_system: list[dict[str, Any]],
) -> list[dict[str, Any]]:
fallback = kept if kept else (non_system[-1:] if non_system else [])
kept = self._user_tail(kept) or self._user_tail(non_system, last=True) or fallback
start = find_legal_message_start(kept)
return kept[start:] if start else kept
@staticmethod
def _user_tail(messages: list[dict[str, Any]], *, last: bool = False) -> list[dict[str, Any]]:
indexes = range(len(messages) - 1, -1, -1) if last else range(len(messages))
for idx in indexes:
if messages[idx].get("role") == "user":
return messages[idx:]
return []
+25 -29
View File
@@ -378,6 +378,16 @@ class AgentLoop:
self.tools = tool_registry if tool_registry is not None else ToolRegistry()
self._exec_session_manager = ExecSessionManager()
self.runner = AgentRunner()
self.consolidator = Consolidator(
store=self.context.memory,
sessions=self.sessions,
build_messages=self.context.build_messages,
get_tool_definitions=self.tools.get_definitions,
resolve_prompt_context=PersistedPromptContextResolver(
workspace_scopes=self.workspace_scopes,
unified_session=unified_session,
),
)
self.subagents = SubagentManager(
workspace=workspace,
bus=bus,
@@ -387,6 +397,7 @@ class AgentLoop:
disabled_skills=disabled_skills,
max_iterations=self.max_iterations,
max_concurrent_subagents=max_concurrent_subagents,
consolidator=self.consolidator,
)
self._unified_session = unified_session
self._running = False
@@ -421,16 +432,6 @@ class AgentLoop:
self._concurrency_gate: asyncio.Semaphore | None = (
asyncio.Semaphore(_max) if _max > 0 else None
)
self.consolidator = Consolidator(
store=self.context.memory,
sessions=self.sessions,
build_messages=self.context.build_messages,
get_tool_definitions=self.tools.get_definitions,
resolve_prompt_context=PersistedPromptContextResolver(
workspace_scopes=self.workspace_scopes,
unified_session=unified_session,
),
)
self.auto_compact = AutoCompact(
sessions=self.sessions,
consolidator=self.consolidator,
@@ -1146,6 +1147,7 @@ class AgentLoop:
runtime=runtime,
)
active_session_key = session.key if session else request_ctx.session_key
consolidation_session_key = active_session_key or "agent:transient"
request_metadata = request_ctx.metadata
effective_scope = self.workspace_scopes.for_turn(
channel=request_ctx.channel,
@@ -1216,25 +1218,19 @@ class AgentLoop:
session_key=session.key if session else None,
provider_retry_mode=self.provider_retry_mode,
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_history=partial(
self.consolidator.summarize_transcript,
runtime=runtime,
session_key=consolidation_session_key,
tools=effective_tools.get_definitions(),
persist=session is not None and not ephemeral,
),
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
consolidate_provider_compaction=partial(
self.consolidator.summarize_provider_compaction,
runtime=runtime,
session_key=consolidation_session_key,
tools=effective_tools.get_definitions(),
persist=session is not None and not ephemeral,
),
injection_callback=_drain_pending,
terminal_injection_callback=_wait_for_pending,
@@ -2114,7 +2110,7 @@ class AgentLoop:
self._save_turn(
session, ctx.all_messages, ctx.save_skip,
turn_latency_ms=ctx.turn_latency_ms,
summary_checkpoint=ctx.summary_checkpoint,
summary_checkpoint=None if ctx.ephemeral else ctx.summary_checkpoint,
input_persisted_early=ctx.input_persisted_early,
)
if (
+27 -13
View File
@@ -778,11 +778,11 @@ _ARCHIVE_TOOL_RESULT = (
class MemoryArchiver:
"""Write durable transcript batches to the Memory ingestion journal.
"""Generate transcript checkpoints and optionally journal their source.
The archiver deliberately has no SessionManager dependency: it may read a
captured transcript batch and append to history.jsonl, but it cannot mutate
provider continuation state or advance a session watermark.
captured transcript batch and, for durable sessions, append to history.jsonl,
but it cannot mutate provider continuation state or advance a session watermark.
"""
def __init__(
@@ -804,9 +804,14 @@ class MemoryArchiver:
session_key: str,
previous_summary: str | None,
max_tokens: int,
persist: bool = True,
) -> str:
"""Persist the failed chunk and return a bounded replacement checkpoint."""
raw = self.store.raw_archive(messages, session_key=session_key)
"""Return a bounded raw checkpoint, optionally persisting its source."""
raw = (
self.store.raw_archive(messages, session_key=session_key)
if persist
else self.store._build_raw_checkpoint(messages)
)
return self._combine_raw_checkpoint(
raw,
previous_summary=previous_summary,
@@ -857,8 +862,9 @@ class MemoryArchiver:
input_token_budget: int | None = None,
fallback_max_tokens: int | None = None,
provider_state: ProviderConversationState | None = None,
persist: bool = True,
) -> str | None:
"""Append the archive prompt to H and persist its summary."""
"""Generate a replacement checkpoint and optionally persist it."""
if not source_messages:
return None
@@ -872,6 +878,7 @@ class MemoryArchiver:
if fallback_max_tokens is not None
else runtime.generation.max_tokens
),
persist=persist,
)
prompt = render_template(
@@ -920,7 +927,8 @@ class MemoryArchiver:
)
if input_token_budget <= 0 or estimated > input_token_budget:
logger.debug(
"Memory archive input does not fit for {}: {}/{} via {}; raw-dumping",
"Memory archive input does not fit for {}: {}/{} via {}; "
"using raw checkpoint",
session_key,
estimated,
input_token_budget,
@@ -944,7 +952,7 @@ class MemoryArchiver:
except Exception:
phase = "provider call" if attempt == 0 else "tool-call recovery"
logger.warning(
"Memory archive {} failed, raw-dumping to history",
"Memory archive {} failed; using raw checkpoint",
phase,
)
return raw_fallback()
@@ -996,22 +1004,24 @@ class MemoryArchiver:
assert response is not None
if response.finish_reason in {"error", "length"}:
logger.warning(
"Memory archive provider did not complete ({}), raw-dumping to history",
"Memory archive provider did not complete ({}); using raw checkpoint",
response.finish_reason,
)
return raw_fallback()
if response.has_tool_calls is True:
logger.warning("Memory archive provider returned tool calls, raw-dumping to history")
logger.warning("Memory archive provider returned tool calls; using raw checkpoint")
return raw_fallback()
summary = response.content
if not summary or not summary.strip():
logger.warning("Memory archive provider returned no summary, raw-dumping to history")
logger.warning("Memory archive provider returned no summary; using raw checkpoint")
return raw_fallback()
summary = self.store._normalize_history_entry(summary)
if not summary:
logger.warning("Memory archive provider summary was not safe to replay, raw-dumping")
logger.warning(
"Memory archive provider summary was not safe to replay; using raw checkpoint"
)
return raw_fallback()
if summary != "(nothing)":
if persist and summary != "(nothing)":
self.store.append_history(summary, session_key=session_key)
return summary
@@ -1131,6 +1141,7 @@ class Consolidator:
session_key: str,
tools: list[dict[str, Any]],
provider_state: ProviderConversationState | None = None,
persist: bool = True,
) -> str | None:
"""Summarize the exact transcript prefix already accepted by the model."""
source_messages = [
@@ -1158,6 +1169,7 @@ class Consolidator:
input_token_budget=input_token_budget,
fallback_max_tokens=max(1, checkpoint_tokens),
provider_state=provider_state,
persist=persist,
)
if summary is None:
return None
@@ -1172,6 +1184,7 @@ class Consolidator:
runtime: LLMRuntime,
session_key: str,
tools: list[dict[str, Any]],
persist: bool = True,
) -> str | None:
"""Prompt a native compacted state without replaying its raw history."""
return await self.summarize_transcript(
@@ -1181,6 +1194,7 @@ class Consolidator:
session_key=session_key,
tools=tools,
provider_state=state,
persist=persist,
)
@staticmethod
+21 -28
View File
@@ -328,8 +328,11 @@ class AgentRunner:
@staticmethod
def _initial_transcript_and_compaction(
spec: AgentRunSpec,
) -> tuple[list[dict[str, Any]], ContextCompactionState | None]:
"""Build the initial transcript and its optional compaction state."""
) -> tuple[list[dict[str, Any]], ContextCompactionState]:
"""Build the initial transcript and its compaction state."""
consolidate_history = spec.consolidate_history
if consolidate_history is None:
raise ValueError("consolidate_history is required")
transcript_input = spec.transcript_input
if transcript_input is not None:
if spec.initial_messages is not None:
@@ -340,21 +343,24 @@ class AgentRunner:
return ContextCompactionState.from_transcript(
transcript_input,
transcript_builder,
spec.consolidate_history,
consolidate_history,
spec.consolidate_provider_compaction,
)
if spec.initial_messages is None:
raise ValueError("initial_messages is required without transcript_input")
if spec.consolidate_history is not None:
raise ValueError("consolidate_history requires transcript_input")
return list(spec.initial_messages), None
messages = list(spec.initial_messages)
return messages, ContextCompactionState.from_messages(
messages,
consolidate_history,
spec.consolidate_provider_compaction,
)
async def _run_core(
self,
spec: AgentRunSpec,
hook: AgentHook,
messages: list[dict[str, Any]],
compaction: ContextCompactionState | None,
compaction: ContextCompactionState,
) -> AgentRunResult:
final_content: str | None = None
tools_used: list[str] = []
@@ -434,11 +440,7 @@ class AgentRunner:
)
await hook.before_iteration(context)
request_message_count = len(messages)
request_messages = (
request_state.compaction.request_messages(messages)
if request_state.compaction is not None
else messages
)
request_messages = request_state.compaction.request_messages(messages)
response, raw_usage = await self._request_model(
spec,
request_messages,
@@ -450,11 +452,10 @@ class AgentRunner:
assert request_state.messages is not None
messages_for_model = request_state.messages
conversation_state.observe_response(response, messages)
if request_state.compaction is not None:
request_state.compaction.accept_request(
messages_for_model,
raw_boundary=request_message_count,
)
request_state.compaction.accept_request(
messages_for_model,
raw_boundary=request_message_count,
)
context.response = response
context.tool_calls = list(response.tool_calls)
@@ -834,11 +835,7 @@ class AgentRunner:
had_injections=had_injections,
pending_stream_content=pending_stream_content,
provider_state=conversation_state.finish(messages),
summary_checkpoint=(
request_state.compaction.summary_checkpoint
if request_state.compaction is not None
else None
),
summary_checkpoint=request_state.compaction.summary_checkpoint,
provider_compaction_applied=request_state.provider_compaction_applied,
)
@@ -1174,18 +1171,14 @@ class AgentRunner:
round_usages: list[LLMUsage],
) -> tuple[str | None, LLMUsage | None]:
compaction = request_state.compaction
request_messages = (
compaction.request_messages(messages)
if compaction is not None
else messages
)
request_messages = compaction.request_messages(messages)
retry_messages = self._budget_exhausted_finalization_messages(request_messages)
try:
response = await self._request_no_tools(
spec,
retry_messages,
request_state=request_state,
transcript=messages if compaction is not None else None,
transcript=messages,
)
except Exception:
logger.exception(
+34 -1
View File
@@ -1,5 +1,7 @@
"""Subagent manager for background task execution."""
from __future__ import annotations
import asyncio
import json
import time
@@ -7,8 +9,9 @@ import uuid
import warnings
from collections.abc import Mapping
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import Any, NotRequired, TypedDict
from typing import TYPE_CHECKING, Any, NotRequired, TypedDict
from loguru import logger
@@ -39,6 +42,9 @@ from nanobot.security.workspace_access import (
from nanobot.utils.llm_runtime import LLMRuntime
from nanobot.utils.prompt_templates import render_template
if TYPE_CHECKING:
from nanobot.agent.memory import Consolidator
class _SubagentOrigin(TypedDict):
channel: str
@@ -105,6 +111,7 @@ class SubagentManager:
disabled_skills: list[str] | None = None,
max_iterations: int | None = None,
max_concurrent_subagents: int | None = None,
consolidator: Consolidator | None = None,
):
if workspace is None:
raise TypeError("SubagentManager.__init__() missing required argument: 'workspace'")
@@ -147,6 +154,7 @@ class SubagentManager:
if max_concurrent_subagents is not None
else defaults.max_concurrent_subagents
)
self.consolidator = consolidator
self._run_slots = asyncio.Semaphore(self.max_concurrent_subagents)
self.runner = AgentRunner()
self._exec_session_manager = ExecSessionManager()
@@ -424,6 +432,29 @@ class SubagentManager:
))
token = bind_workspace_scope(workspace_scope) if workspace_scope is not None else None
try:
tool_definitions = tools.get_definitions()
consolidate_history = (
partial(
self.consolidator.summarize_transcript,
runtime=runtime,
session_key=f"subagent:{task_id}",
tools=tool_definitions,
persist=False,
)
if self.consolidator is not None
else None
)
consolidate_provider_compaction = (
partial(
self.consolidator.summarize_provider_compaction,
runtime=runtime,
session_key=f"subagent:{task_id}",
tools=tool_definitions,
persist=False,
)
if self.consolidator is not None
else None
)
result = await self.runner.run(AgentRunSpec(
initial_messages=messages,
tools=tools,
@@ -441,6 +472,8 @@ class SubagentManager:
"llm_usage_source",
current_llm_usage_source(),
),
consolidate_history=consolidate_history,
consolidate_provider_compaction=consolidate_provider_compaction,
))
finally:
if token is not None:
+2 -3
View File
@@ -1195,9 +1195,8 @@ class LLMProvider(ABC):
# Safety net: ensure the first non-system message is not a bare
# ``assistant`` message. Providers like GLM reject system→assistant
# with error 1214. This can happen when upstream truncation (e.g.
# _snip_history) drops the only user message. Insert a synthetic
# user message to keep the sequence valid.
# with error 1214. Insert a synthetic user message to keep the
# sequence valid when replayed history starts at an assistant turn.
for i, msg in enumerate(merged):
if msg.get("role") != "system":
if msg.get("role") == "assistant" and not msg.get("tool_calls"):
+1 -10
View File
@@ -114,20 +114,11 @@ class ProviderConversationStateController:
context_window_tokens: int | None,
model_messages: list[dict[str, Any]] | None = None,
supplemental_messages: list[dict[str, Any]] | None = None,
resume_state: bool = True,
) -> ProviderCallContext | None:
"""Build context for the next request and remember its durable delta.
``resume_state=False`` abandons opaque history when local request
fitting has produced a new independent model-facing context.
"""
"""Build context for the next request and remember its durable delta."""
independent_context = self.independent_request_context(
context_window_tokens=context_window_tokens,
)
if not resume_state:
self._state = None
self._request_messages = []
return independent_context
if self._state is None:
self._request_messages = []
return independent_context
+8
View File
@@ -10,6 +10,13 @@ from nanobot.providers.base import GenerationSettings, LLMProvider
from nanobot.utils.llm_runtime import LLMRuntime
async def failed_test_consolidator(
_messages: list[dict[str, Any]],
_previous_summary: str | None,
) -> None:
"""Model a failed summary and raw fallback in tests not exercising compaction."""
def make_run_spec(provider: LLMProvider, **kwargs: Any) -> AgentRunSpec:
"""Build a run spec from the pre-runtime test arguments.
@@ -51,4 +58,5 @@ def make_run_spec(provider: LLMProvider, **kwargs: Any) -> AgentRunSpec:
),
context_window_tokens=context_window_tokens,
)
kwargs.setdefault("consolidate_history", failed_test_consolidator)
return AgentRunSpec(runtime=runtime, **kwargs)
+28
View File
@@ -160,6 +160,34 @@ class TestTurnTranscriptSummary:
assert "SNIP" in call["messages"][-1]["content"]
assert call["tools"] == tools
async def test_nonpersistent_failure_returns_raw_checkpoint_without_journaling(
self,
consolidator,
mock_provider,
runtime,
store,
):
accepted = [
{"role": "system", "content": "stable system"},
{"role": "user", "content": "accepted history"},
]
mock_provider.chat_stream_with_retry.return_value = LLMResponse(content="")
consolidator._SAFETY_BUFFER = 0
result = await consolidator.summarize_transcript(
accepted,
None,
runtime=runtime,
session_key="test:ephemeral-turn",
tools=[],
persist=False,
)
assert result is not None
assert "[RAW]" in result
assert "accepted history" in result
assert store.read_unprocessed_history(since_cursor=0) == []
async def test_native_compaction_appends_only_archive_prompt(
self,
consolidator,
@@ -8,6 +8,7 @@ from nanobot.providers.base import (
GenerationSettings,
LLMResponse,
ProviderConversationState,
ToolCallRequest,
)
from nanobot.session.summary import SUMMARY_CONTINUATION_TEXT
@@ -89,6 +90,114 @@ async def test_runner_pressure_commits_summary_and_current_delta(tmp_path) -> No
]
@pytest.mark.asyncio
async def test_ephemeral_runner_pressure_summarizes_without_persisting(tmp_path) -> None:
loop = _make_loop(
tmp_path, estimated_tokens=100, context_window_tokens=1_624, max_tokens=100,
)
loop.provider.can_resume_conversation_state.return_value = False
loop.schedule_background = lambda coro: coro.close() # type: ignore[method-assign]
session = loop.sessions.get_or_create("cli:ephemeral")
session.messages = [
{"role": role, "content": f"old-{role}-{turn}"}
for turn in range(6)
for role in ("user", "assistant")
]
original_messages = [dict(message) for message in session.messages]
loop.sessions.save(session)
def estimate(messages, _tools, _model):
contents = [str(message.get("content")) for message in messages]
if contents and "SNIP" in contents[-1]:
return 300, "test-counter"
if any(content.startswith("old-") for content in contents):
return 600, "test-counter"
return 100, "test-counter"
loop.provider.estimate_prompt_tokens.side_effect = estimate
loop.provider.chat_stream_with_retry = AsyncMock(side_effect=[
LLMResponse(content="Transient checkpoint.", tool_calls=[]),
LLMResponse(content="done", tool_calls=[]),
])
result = await loop.process_direct(
"continue the task",
session_key="cli:ephemeral",
ephemeral=True,
)
assert result.content == "done"
assert loop.provider.chat_stream_with_retry.await_count == 2
model_request = loop.provider.chat_stream_with_retry.await_args_list[1].kwargs["messages"]
assert "Transient checkpoint." in model_request[0]["content"]
assert model_request[1]["content"] == "continue the task"
reloaded = loop.sessions.get_or_create("cli:ephemeral")
assert reloaded.messages[: len(original_messages)] == original_messages
assert all(
"Transient checkpoint." not in str(message.get("content"))
for message in reloaded.messages
)
assert "_last_summary" not in reloaded.metadata
assert loop.context.memory.read_unprocessed_history(since_cursor=0) == []
@pytest.mark.asyncio
async def test_ephemeral_tool_loop_pressure_summarizes_before_next_model_call(
tmp_path,
) -> None:
loop = _make_loop(
tmp_path, estimated_tokens=100, context_window_tokens=1_624, max_tokens=100,
)
loop.provider.can_resume_conversation_state.return_value = False
loop.schedule_background = lambda coro: coro.close() # type: ignore[method-assign]
loop.tools.execute = AsyncMock(return_value="large-result")
def estimate(messages, _tools, _model):
contents = [str(message.get("content")) for message in messages]
if contents and "SNIP" in contents[-1]:
return 300, "test-counter"
if sum(message.get("role") == "tool" for message in messages) >= 2:
return 600, "test-counter"
return 100, "test-counter"
normal_calls = 0
async def respond(*, messages, **_kwargs):
nonlocal normal_calls
contents = [str(message.get("content")) for message in messages]
if contents and "SNIP" in contents[-1]:
return LLMResponse(content="Tool-loop checkpoint.", tool_calls=[])
normal_calls += 1
if normal_calls <= 2:
return LLMResponse(
content="checking",
tool_calls=[ToolCallRequest(
id=f"call-{normal_calls}",
name="list_dir",
arguments={"path": "."},
)],
)
return LLMResponse(content="done", tool_calls=[])
loop.provider.estimate_prompt_tokens.side_effect = estimate
loop.provider.chat_stream_with_retry = AsyncMock(side_effect=respond)
result = await loop.process_direct(
"inspect the workspace",
session_key="cli:ephemeral-tool-loop",
ephemeral=True,
)
assert result.content == "done"
assert loop.provider.chat_stream_with_retry.await_count == 4
model_request = loop.provider.chat_stream_with_retry.await_args_list[3].kwargs["messages"]
assert "Tool-loop checkpoint." in model_request[0]["content"]
assert sum(message.get("role") == "tool" for message in model_request) == 1
assert loop.context.memory.read_unprocessed_history(since_cursor=0) == []
@pytest.mark.asyncio
async def test_native_provider_compaction_commits_portable_terminal_checkpoint(
tmp_path,
@@ -688,6 +688,7 @@ async def test_subagent_max_iterations_announces_existing_fallback(tmp_path, mon
workspace=tmp_path,
bus=bus,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
consolidator=MagicMock(),
max_iterations=2,
)
mgr._announce_result = AsyncMock()
+2 -1
View File
@@ -62,7 +62,8 @@ def test_initial_transcript_is_built_from_structured_turn_input() -> None:
messages, compaction = AgentRunner._initial_transcript_and_compaction(spec)
assert messages == expected
assert compaction is None
assert compaction is not None
assert compaction.raw_messages == expected
transcript_builder.assert_called_once_with(transcript_input)
+13 -16
View File
@@ -77,8 +77,7 @@ async def test_repeat_requires_original_read_result_in_current_context(tmp_path,
assert repeated == (stub if retained == "original" else contents)
@pytest.mark.parametrize("rewrite", ["compact", "snip"])
async def test_dedup_uses_governed_request_instead_of_raw_transcript(tmp_path, monkeypatch, rewrite):
async def test_dedup_uses_compacted_request_instead_of_raw_transcript(tmp_path, monkeypatch):
tools = _tools(tmp_path)
first, contents, _ = await _read(tools, "read-1")
@@ -87,7 +86,6 @@ async def test_dedup_uses_governed_request_instead_of_raw_transcript(tmp_path, m
return (600 if contains_original else 100), "test-counter"
monkeypatch.setattr("nanobot.agent.context_governance.estimate_prompt_tokens_chain", estimate)
monkeypatch.setattr("nanobot.agent.context_governance.estimate_message_tokens", lambda _: 300)
consolidate = AsyncMock(return_value="The file was inspected; its original text was omitted.")
def build(transcript):
@@ -97,25 +95,24 @@ async def test_dedup_uses_governed_request_instead_of_raw_transcript(tmp_path, m
messages.append({"role": "user", "content": transcript.current_message})
return messages
options = {}
if rewrite == "compact":
options = {
"transcript_input": TranscriptInput(
history=first.messages, current_message="Read data.txt again.",
),
"transcript_builder": build,
"consolidate_history": consolidate,
}
result, repeated, requests = await _read(
tools, "read-2", first.messages, context_window_tokens=1_624, max_tokens=100, **options,
tools,
"read-2",
first.messages,
context_window_tokens=1_624,
max_tokens=100,
transcript_input=TranscriptInput(
history=first.messages, current_message="Read data.txt again.",
),
transcript_builder=build,
consolidate_history=consolidate,
)
assert any(message.get("tool_call_id") == "read-1" for message in result.messages)
assert not any(message.get("tool_call_id") == "read-1" for message in requests[0])
assert repeated == contents
if rewrite == "compact":
consolidate.assert_awaited_once()
assert result.summary_checkpoint is not None
consolidate.assert_awaited_once()
assert result.summary_checkpoint is not None
async def test_direct_read_without_model_context_never_omits_contents(tmp_path):
+56 -583
View File
@@ -1,4 +1,4 @@
"""Tests for AgentRunner context governance: repair and request fitting."""
"""Tests for AgentRunner context governance, repair, and compaction."""
from __future__ import annotations
@@ -10,6 +10,7 @@ from agent.runner_helpers import make_run_spec
from nanobot.agent.context import TranscriptInput
from nanobot.agent.context_governance import (
BACKFILL_CONTENT,
ContextCompactionState,
ContextGovernanceConfig,
ContextGovernor,
ContextWindowExceededError,
@@ -60,6 +61,15 @@ def _governance_config(
)
def _compaction_state(spec: AgentRunSpec) -> ContextCompactionState:
assert spec.initial_messages is not None
return ContextCompactionState.from_messages(
spec.initial_messages,
spec.consolidate_history,
spec.consolidate_provider_compaction,
)
def _make_loop(tmp_path):
from nanobot.agent.loop import AgentLoop
from nanobot.bus.queue import MessageBus
@@ -130,56 +140,6 @@ async def test_runner_propagates_context_governance_failure():
provider.chat_stream_with_retry.assert_not_awaited()
@pytest.mark.asyncio
async def test_runner_locally_fits_oversized_initial_transcript(monkeypatch):
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="done"))
tools = MagicMock()
tools.get_definitions.return_value = []
old_content = "x" * 20_000
estimate = MagicMock(
side_effect=lambda _provider, _model, messages, _tools: (
(600, "test-counter")
if any(message.get("content") == old_content for message in messages)
else (100, "test-counter")
)
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
estimate,
)
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[
{"role": "system", "content": "system"},
{"role": "user", "content": "old question"},
{"role": "assistant", "content": old_content},
{"role": "user", "content": "continue"},
],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
))
assert provider.chat_stream_with_retry.await_args.kwargs["messages"] == [
{"role": "system", "content": "system"},
{"role": "user", "content": "continue"},
]
estimated_messages = [call.args[2] for call in estimate.call_args_list]
assert sum(
any(message.get("content") == old_content for message in messages)
for messages in estimated_messages
) == 1
assert len(estimated_messages) == 3
assert any(message.get("content") == old_content for message in result.messages)
async def test_runner_summarizes_history_and_preserves_current_input(monkeypatch):
provider = MagicMock(spec=LLMProvider)
provider.can_resume_conversation_state.return_value = True
@@ -664,265 +624,14 @@ async def test_runner_governs_messages_added_by_before_iteration_hook(monkeypatc
provider.chat_stream_with_retry.assert_not_awaited()
@pytest.mark.asyncio
async def test_runner_drops_resumable_provider_state_when_request_is_fitted(monkeypatch):
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
provider.can_resume_conversation_state.return_value = True
captured_contexts = []
old_content = "old-oversized-history"
candidate = ProviderConversationState(
kind="openai_responses",
provider="openai:test",
model="local-model",
version=1,
payload={"items": [{"type": "message", "content": "fresh state"}]},
)
async def chat_stream_with_retry(*, provider_context=None, **_kwargs):
captured_contexts.append(provider_context)
return LLMResponse(
content="done",
usage=LLMUsage.reported(input_tokens=100, output_tokens=10),
provider_state=candidate,
)
provider.chat_stream_with_retry = chat_stream_with_retry
tools = MagicMock()
tools.get_definitions.return_value = []
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
lambda _provider, _model, messages, _tools: (
(600, "test-counter")
if any(message.get("content") == old_content for message in messages)
else (100, "test-counter")
),
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda message: 450 if message.get("content") == old_content else 50,
)
saved_state = ProviderConversationState(
kind="openai_responses",
provider="openai:test",
model="local-model",
version=1,
payload={"items": [{"type": "message", "content": "stale state"}]},
)
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[
{"role": "assistant", "content": old_content},
{"role": "user", "content": "continue"},
],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
provider_state=saved_state,
))
assert captured_contexts[0].conversation_state is None
assert result.provider_state is not None
assert result.provider_state.payload == candidate.payload
@pytest.mark.asyncio
async def test_runner_fits_each_malformed_retry_with_its_actual_tools(monkeypatch):
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
calls: list[dict] = []
estimated_tools: list[object] = []
definitions = [{"type": "function", "function": {"name": "read_file"}}]
async def chat_stream_with_retry(*, messages, tools=None, **_kwargs):
calls.append({"messages": [dict(message) for message in messages], "tools": tools})
if len(calls) < 3:
return LLMResponse(
content="bad tool request",
tool_calls=[ToolCallRequest(id=f"bad_{len(calls)}", name=None, arguments={})],
finish_reason="tool_calls",
usage=LLMUsage.reported(input_tokens=100, output_tokens=10),
)
return LLMResponse(
content="recovered",
usage=LLMUsage.reported(input_tokens=100, output_tokens=10),
)
def estimate(_provider, _model, messages, _tools):
estimated_tools.append(_tools)
user_count = sum(message.get("role") == "user" for message in messages)
return (600 if user_count > 1 else 100), "test-counter"
provider.chat_stream_with_retry = chat_stream_with_retry
tools = MagicMock()
tools.get_definitions.return_value = definitions
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
estimate,
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda _message: 300,
)
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[{"role": "user", "content": "use a tool"}],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
))
assert [call["tools"] for call in calls] == [definitions, definitions, None]
assert definitions in estimated_tools
assert None in estimated_tools
assert [len(call["messages"]) for call in calls] == [1, 1, 1]
assert result.final_content == "recovered"
assert result.usage is not None
assert result.usage.input_tokens == 300
assert result.usage.output_tokens == 30
assert result.usage.request_count == 3
# Recovery dispatches contribute to the total without becoming extra chart bars.
assert result.round_usages == [result.usage]
assert result.messages == [
{"role": "user", "content": "use a tool"},
{"role": "assistant", "content": "recovered"},
]
@pytest.mark.asyncio
async def test_runner_fits_empty_response_finalization_before_dispatch(monkeypatch):
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
calls: list[dict] = []
async def chat_stream_with_retry(*, messages, tools=None, **_kwargs):
calls.append({"messages": [dict(message) for message in messages], "tools": tools})
if len(calls) < 3:
return LLMResponse(
content=None,
usage=LLMUsage.reported(input_tokens=100, output_tokens=1),
)
return LLMResponse(
content="finalized",
usage=LLMUsage.reported(input_tokens=100, output_tokens=10),
)
def estimate(_provider, _model, messages, _tools):
contents = [str(message.get("content") or "") for message in messages]
has_original = "do task" in contents
has_finalization = any("conversation above" in content for content in contents)
return (600 if has_original and has_finalization else 100), "test-counter"
provider.chat_stream_with_retry = chat_stream_with_retry
tools = MagicMock()
tools.get_definitions.return_value = []
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
estimate,
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda _message: 300,
)
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[{"role": "user", "content": "do task"}],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=3,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
))
assert len(calls) == 3
assert calls[-1]["tools"] is None
assert all(message.get("content") != "do task" for message in calls[-1]["messages"])
assert result.final_content == "finalized"
@pytest.mark.asyncio
async def test_runner_fits_max_iteration_finalization_before_dispatch(monkeypatch):
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
calls: list[dict] = []
oversized_result = "oversized-current-tool-result"
async def chat_stream_with_retry(*, messages, tools=None, **_kwargs):
calls.append({"messages": [dict(message) for message in messages], "tools": tools})
if len(calls) == 1:
return LLMResponse(
content="working",
tool_calls=[ToolCallRequest(id="call_1", name="read_file", arguments={})],
finish_reason="tool_calls",
usage=LLMUsage.reported(input_tokens=100, output_tokens=10),
)
return LLMResponse(
content="safe summary",
usage=LLMUsage.reported(input_tokens=100, output_tokens=10),
)
def estimate(_provider, _model, messages, _tools):
has_oversized = any(
message.get("content") == oversized_result for message in messages
)
return (600 if has_oversized else 100), "test-counter"
provider.chat_stream_with_retry = chat_stream_with_retry
tools = MagicMock()
tools.get_definitions.return_value = []
tools.execute = AsyncMock(return_value=oversized_result)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
estimate,
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda message: 600 if message.get("content") == oversized_result else 50,
)
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[{"role": "user", "content": "inspect"}],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
))
assert len(calls) == 2
assert calls[-1]["tools"] is None
assert all(
message.get("content") != oversized_result
for message in calls[-1]["messages"]
)
assert any(message.get("content") == oversized_result for message in result.messages)
assert result.final_content == "safe summary"
@pytest.mark.parametrize(
("input_tokens", "expected_fitted"),
("input_tokens", "expected_pressure"),
[(500, True), (100, False)],
)
async def test_matching_reported_provider_usage_avoids_local_estimate(
monkeypatch,
input_tokens,
expected_fitted,
expected_pressure,
):
provider = MagicMock(spec=LLMProvider)
tools = MagicMock()
@@ -945,23 +654,30 @@ async def test_matching_reported_provider_usage_avoids_local_estimate(
)
governor = ContextGovernor()
monkeypatch.setattr(governor, "fit_to_budget", lambda *_args, **_kwargs: [])
state = ModelRequestState(
config=_governance_config(provider, tools, spec),
conversation=ProviderConversationStateController(
provider=provider, model=spec.runtime.model, messages=spec.initial_messages,
),
compaction=_compaction_state(spec),
usage=LLMUsage.reported(input_tokens=input_tokens, output_tokens=10),
messages=spec.initial_messages,
tool_definitions=[],
)
messages, _context = await governor.prepare_request(
state,
spec.initial_messages,
tool_definitions=tools.get_definitions(),
)
assert messages == ([] if expected_fitted else spec.initial_messages)
if expected_pressure:
with pytest.raises(ContextWindowExceededError):
await governor.prepare_request(
state,
spec.initial_messages,
tool_definitions=tools.get_definitions(),
)
else:
messages, _context = await governor.prepare_request(
state,
spec.initial_messages,
tool_definitions=tools.get_definitions(),
)
assert messages == spec.initial_messages
async def test_changed_messages_use_local_estimate_after_reported_usage(monkeypatch):
@@ -985,23 +701,22 @@ async def test_changed_messages_use_local_estimate_after_reported_usage(monkeypa
)
governor = ContextGovernor()
monkeypatch.setattr(governor, "fit_to_budget", lambda *_args, **_kwargs: [])
state = ModelRequestState(
config=_governance_config(provider, tools, spec),
conversation=ProviderConversationStateController(
provider=provider, model=spec.runtime.model, messages=spec.initial_messages,
),
compaction=_compaction_state(spec),
usage=LLMUsage.reported(input_tokens=900, output_tokens=10),
messages=[{"role": "user", "content": "previous request"}],
tool_definitions=[],
)
messages, _context = await governor.prepare_request(
state,
spec.initial_messages,
tool_definitions=tools.get_definitions(),
)
assert messages == []
with pytest.raises(ContextWindowExceededError):
await governor.prepare_request(
state,
spec.initial_messages,
tool_definitions=tools.get_definitions(),
)
estimate.assert_called_once()
@@ -1039,7 +754,7 @@ def test_resumed_provider_context_avoids_full_transcript_estimate(monkeypatch):
@pytest.mark.asyncio
async def test_runner_counts_resumed_provider_state_before_dispatch(monkeypatch):
async def test_runner_refuses_oversized_resumed_state_without_compactor(monkeypatch):
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
@@ -1077,23 +792,22 @@ async def test_runner_counts_resumed_provider_state_before_dispatch(monkeypatch)
lambda *_args, **_kwargs: (100, "test-counter"),
)
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[current_message],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
provider_state=saved_state,
))
with pytest.raises(ContextWindowExceededError) as exc_info:
await AgentRunner().run(make_run_spec(
provider,
initial_messages=[current_message],
tools=tools,
model="local-model",
context_window_tokens=1_624,
max_tokens=100,
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
provider_state=saved_state,
))
assert captured_contexts[0].conversation_state is None
assert result.messages == [
current_message,
{"role": "assistant", "content": "done"},
]
assert exc_info.value.estimated_tokens == 550
assert exc_info.value.source == "resumed provider state plus pending messages"
assert captured_contexts == []
@pytest.mark.asyncio
@@ -1101,7 +815,7 @@ async def test_runner_counts_resumed_provider_state_before_dispatch(monkeypatch)
("context_window_tokens", "expected_budget"),
[(1_624, 500), (1_000, 0)],
)
async def test_runner_refuses_locally_fitted_request_that_still_cannot_fit(
async def test_runner_refuses_oversized_request_without_compactor(
monkeypatch,
context_window_tokens,
expected_budget,
@@ -1137,113 +851,6 @@ async def test_runner_refuses_locally_fitted_request_that_still_cannot_fit(
provider.chat_stream_with_retry.assert_not_awaited()
def test_snip_history_drops_orphaned_tool_results_from_trimmed_slice(monkeypatch):
provider = MagicMock()
tools = MagicMock()
tools.get_definitions.return_value = []
messages = [
{"role": "system", "content": "system"},
{"role": "user", "content": "old user"},
{
"role": "assistant",
"content": "tool call",
"tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "ls", "arguments": "{}"}}],
},
{"role": "tool", "tool_call_id": "call_1", "content": "tool output"},
{"role": "assistant", "content": "after tool"},
]
spec = make_run_spec(provider,
initial_messages=messages,
tools=tools,
model="test-model",
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
context_window_tokens=1_224,
max_tokens=100,
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
lambda *_args, **_kwargs: (500, None),
)
token_sizes = {
"old user": 120,
"tool call": 120,
"tool output": 40,
"after tool": 40,
"system": 0,
}
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda msg: token_sizes.get(str(msg.get("content")), 40),
)
trimmed = ContextGovernor().snip_history(
_governance_config(provider, tools, spec),
messages,
tool_definitions=tools.get_definitions(),
)
# After the fix, the user message is recovered so the sequence is valid
# for providers that require system → user (e.g. GLM error 1214).
assert trimmed[0]["role"] == "system"
non_system = [m for m in trimmed if m["role"] != "system"]
assert non_system[0]["role"] == "user", f"Expected user after system, got {non_system[0]['role']}"
def test_snip_history_reserves_budget_for_tool_definitions(monkeypatch):
provider = MagicMock()
tools = MagicMock()
tools.get_definitions.return_value = [{"type": "function", "function": {"name": "large_tool"}}]
messages = [
{"role": "system", "content": "system"},
{"role": "user", "content": "old user"},
{"role": "assistant", "content": "old assistant"},
{"role": "user", "content": "recent one"},
{"role": "assistant", "content": "recent answer"},
{"role": "user", "content": "recent two"},
]
spec = make_run_spec(provider,
initial_messages=messages,
tools=tools,
model="test-model",
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
context_window_tokens=1_624,
max_tokens=100,
)
def _estimate(_provider, _model, estimate_messages, estimate_tools):
if estimate_messages == messages:
return 1000, None
assert estimate_messages == [{"role": "system", "content": "system"}]
assert estimate_tools == tools.get_definitions.return_value
return 350, None
monkeypatch.setattr("nanobot.agent.context_governance.estimate_prompt_tokens_chain", _estimate)
token_sizes = {
"system": 50,
"old user": 200,
"old assistant": 200,
"recent one": 200,
"recent answer": 200,
"recent two": 200,
}
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda msg: token_sizes.get(str(msg.get("content")), 40),
)
trimmed = ContextGovernor().snip_history(
_governance_config(provider, tools, spec),
messages,
tool_definitions=tools.get_definitions(),
)
contents = [message.get("content") for message in trimmed]
assert contents == ["system", "recent two"]
async def test_backfill_missing_tool_results_inserts_error():
"""Orphaned tool_use (no matching tool_result) should get a synthetic error."""
@@ -1519,11 +1126,9 @@ async def test_runner_backfill_only_mutates_model_context_not_returned_messages(
]
def test_governance_repairs_orphans_after_snip():
"""After snipping clips an assistant+tool_calls, orphan repair cleans up the tail."""
# Simulate snipping that keeps only the tail: drop the assistant with
# tool_calls but keep its tool result (orphan).
snipped = [
def test_governance_repairs_orphans_after_history_rewrite():
"""A rewritten history drops tool results whose calls are no longer present."""
rewritten = [
{"role": "system", "content": "system"},
{"role": "tool", "tool_call_id": "tc_old", "name": "search",
"content": "old result"},
@@ -1531,7 +1136,7 @@ def test_governance_repairs_orphans_after_snip():
{"role": "user", "content": "new msg"},
]
cleaned = ContextGovernor.drop_orphan_tool_results(snipped)
cleaned = ContextGovernor.drop_orphan_tool_results(rewritten)
# The orphan tool result should be removed.
assert not any(
m.get("role") == "tool" and m.get("tool_call_id") == "tc_old"
@@ -1555,138 +1160,6 @@ def test_governance_fallback_still_repairs_orphans():
assert not any(m.get("tool_call_id") == "orphan_tc" for m in repaired)
def test_snip_history_preserves_user_message_after_truncation(monkeypatch):
"""When _snip_history truncates messages and the only user message ends up
outside the kept window, the method must recover the nearest user message
so the resulting sequence is valid for providers like GLM (which reject
system→assistant with error 1214).
This reproduces the exact scenario from the bug report:
- Normal interaction: user asks, assistant calls tool, tool returns,
assistant replies.
- Injection adds a phantom user message, triggering more tool calls.
- _snip_history activates, keeping only recent assistant/tool pairs.
- The injected user message is in the truncated prefix and gets lost.
"""
provider = MagicMock()
tools = MagicMock()
tools.get_definitions.return_value = []
messages = [
{"role": "system", "content": "system"},
{"role": "assistant", "content": "previous reply"},
{"role": "user", "content": ".nanobot的同目录"},
{
"role": "assistant",
"content": None,
"tool_calls": [{"id": "tc_1", "type": "function", "function": {"name": "exec", "arguments": "{}"}}],
},
{"role": "tool", "tool_call_id": "tc_1", "content": "tool output 1"},
{
"role": "assistant",
"content": None,
"tool_calls": [{"id": "tc_2", "type": "function", "function": {"name": "exec", "arguments": "{}"}}],
},
{"role": "tool", "tool_call_id": "tc_2", "content": "tool output 2"},
]
spec = make_run_spec(provider,
initial_messages=messages,
tools=tools,
model="test-model",
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
context_window_tokens=1_224,
max_tokens=100,
)
# Make estimate_prompt_tokens_chain report above budget so _snip_history activates.
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
lambda *_a, **_kw: (500, None),
)
# Make kept window small: only the last 2 messages fit the budget.
token_sizes = {
"system": 0,
"previous reply": 200,
".nanobot的同目录": 80,
"tool output 1": 80,
"tool output 2": 80,
}
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda msg: token_sizes.get(str(msg.get("content")), 100),
)
trimmed = ContextGovernor().snip_history(
_governance_config(provider, tools, spec),
messages,
tool_definitions=tools.get_definitions(),
)
# The first non-system message MUST be user (not assistant).
non_system = [m for m in trimmed if m.get("role") != "system"]
assert non_system, "trimmed should contain at least one non-system message"
assert non_system[0]["role"] == "user", (
f"First non-system message must be 'user', got '{non_system[0]['role']}'. "
f"Roles: {[m['role'] for m in trimmed]}"
)
def test_snip_history_no_user_at_all_falls_back_gracefully(monkeypatch):
"""Edge case: if non_system has zero user messages, _snip_history should
still return a valid sequence (not crash or produce system→assistant)."""
provider = MagicMock()
tools = MagicMock()
tools.get_definitions.return_value = []
messages = [
{"role": "system", "content": "system"},
{"role": "assistant", "content": "reply"},
{"role": "tool", "tool_call_id": "tc_1", "content": "result"},
{"role": "assistant", "content": "reply 2"},
{"role": "tool", "tool_call_id": "tc_2", "content": "result 2"},
]
spec = make_run_spec(provider,
initial_messages=messages,
tools=tools,
model="test-model",
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
context_window_tokens=1_224,
max_tokens=100,
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_prompt_tokens_chain",
lambda *_a, **_kw: (500, None),
)
monkeypatch.setattr(
"nanobot.agent.context_governance.estimate_message_tokens",
lambda msg: 100,
)
trimmed = ContextGovernor().snip_history(
_governance_config(provider, tools, spec),
messages,
tool_definitions=tools.get_definitions(),
)
# Should not crash. The result should still be a valid list.
assert isinstance(trimmed, list)
# Must have at least system.
assert any(m.get("role") == "system" for m in trimmed)
# The _enforce_role_alternation safety net must be able to fix whatever
# _snip_history returns here — verify it produces a valid sequence.
fixed = LLMProvider._enforce_role_alternation(trimmed)
non_system = [m for m in fixed if m["role"] != "system"]
if non_system:
assert non_system[0]["role"] in ("user", "tool"), (
f"Safety net should ensure first non-system is user/tool, got {non_system[0]['role']}"
)
# ---------------------------------------------------------------------------
# Malformed tool_call name guard (missing/non-string name wedges the session
# upstream: messages.content.N.tool_use.name: Input should be a valid string)
@@ -2,6 +2,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from agent.runner_helpers import failed_test_consolidator
from nanobot.agent.runner import AgentRunner, AgentRunSpec
from nanobot.config.schema import AgentDefaults
from nanobot.providers.base import (
@@ -68,6 +69,7 @@ async def test_active_run_keeps_provider_captured_at_admission() -> None:
runtime=selected_runtime,
max_iterations=2,
max_tool_result_chars=AgentDefaults().max_tool_result_chars,
consolidate_history=failed_test_consolidator,
session_key="webui:cache-test",
))
+62
View File
@@ -188,6 +188,7 @@ async def test_subagent_recovers_from_tool_error_in_same_run(tmp_path):
workspace=tmp_path,
bus=MessageBus(),
max_tool_result_chars=16_000,
consolidator=MagicMock(),
)
result = await sm.run_inline(
@@ -200,6 +201,67 @@ async def test_subagent_recovers_from_tool_error_in_same_run(tmp_path):
assert provider.chat_stream_with_retry.await_count == 2
@pytest.mark.asyncio
async def test_subagent_pressure_uses_transient_summary(tmp_path):
provider = MagicMock(spec=LLMProvider)
provider.get_default_model.return_value = "test"
provider.can_resume_conversation_state.return_value = False
provider.generation = GenerationSettings(max_tokens=100)
def estimate(messages, _tools, _model):
if sum(message.get("role") == "tool" for message in messages) >= 2:
return 600, "test-counter"
return 100, "test-counter"
provider.estimate_prompt_tokens = MagicMock(side_effect=estimate)
provider.chat_stream_with_retry = AsyncMock(side_effect=[
LLMResponse(
content="checking",
tool_calls=[ToolCallRequest(
id="call-1",
name="list_dir",
arguments={"path": "."},
)],
),
LLMResponse(
content="checking again",
tool_calls=[ToolCallRequest(
id="call-2",
name="list_dir",
arguments={"path": "."},
)],
),
LLMResponse(content="done", tool_calls=[]),
])
consolidator = MagicMock()
consolidator.summarize_transcript = AsyncMock(return_value="Subagent checkpoint.")
consolidator.summarize_provider_compaction = AsyncMock(return_value=None)
manager = SubagentManager(
workspace=tmp_path,
bus=MessageBus(),
max_tool_result_chars=16_000,
consolidator=consolidator,
)
runtime = LLMRuntime.capture(
provider,
"test",
context_window_tokens=1_624,
)
result = await manager.run_inline(
task="inspect the workspace",
session_key="test:direct",
runtime=runtime,
)
assert result == "done"
consolidator.summarize_transcript.assert_awaited_once()
assert consolidator.summarize_transcript.await_args.kwargs["persist"] is False
model_request = provider.chat_stream_with_retry.await_args_list[2].kwargs["messages"]
assert "Subagent checkpoint." in model_request[0]["content"]
assert sum(message.get("role") == "tool" for message in model_request) == 1
@pytest.mark.asyncio
async def test_spawned_subagent_inherits_llm_usage_source(tmp_path):
provider = MagicMock(spec=LLMProvider)
+3
View File
@@ -439,6 +439,7 @@ class TestSubagentCancellation:
workspace=tmp_path,
bus=bus,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
consolidator=MagicMock(),
)
async def fake_execute(self, **kwargs):
@@ -538,6 +539,7 @@ class TestSubagentCancellation:
workspace=tmp_path,
bus=bus,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
consolidator=MagicMock(),
)
mgr._announce_result = AsyncMock()
@@ -586,6 +588,7 @@ class TestSubagentCancellation:
workspace=tmp_path,
bus=bus,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
consolidator=MagicMock(),
)
mgr._announce_result = AsyncMock()
@@ -6,6 +6,7 @@ from collections.abc import AsyncIterator
import httpx
import pytest
from agent.runner_helpers import failed_test_consolidator
from openai import AsyncOpenAI
from nanobot.agent.hook import AgentHook
@@ -196,6 +197,7 @@ async def test_runner_streams_past_old_wall_limit_with_optional_ui(
tools=ToolRegistry(),
runtime=LLMRuntime.capture(provider, "gpt-5.2", context_window_tokens=128_000),
max_iterations=1, max_tool_result_chars=1_000, hook=Hook(),
consolidate_history=failed_test_consolidator,
))
assert result.stop_reason == "completed"
assert result.final_content == "done"
@@ -231,6 +233,7 @@ async def test_chat_only_provider_still_has_a_timeout(monkeypatch, finalize):
tools=ToolRegistry(),
runtime=LLMRuntime.capture(provider, "test", context_window_tokens=128_000),
max_iterations=1, max_tool_result_chars=1_000,
consolidate_history=failed_test_consolidator,
max_iterations_message="Tool budget exhausted.",
)), timeout=1)
assert provider.calls == (2 if finalize else 1)