fix(agent): don't resume abandoned work after max_iterations
This commit is contained in:
1 parent
4bd1b74c6b
commit
f4aacdbec2
6 files changed
+227
-1
No files matched your search
@@ -43,6 +43,7 @@ _SESSION_PREVIEW_MAX_CHARS = 120
|
||||
_SESSION_LIST_PREVIEW_MAX_RECORDS = 200
|
||||
_SESSION_LIST_PREVIEW_MAX_CHARS = 1_000_000
|
||||
_FORK_VOLATILE_METADATA_KEYS = {
|
||||
"_stopped_at_iteration_limit",
|
||||
PARENT_SESSION_KEY,
|
||||
SESSION_TYPE_KEY,
|
||||
"goal_state",
|
||||
@@ -353,6 +354,7 @@ class Session:
|
||||
self.provider_state = None
|
||||
self.updated_at = datetime.now()
|
||||
self.metadata.pop("_last_summary", None)
|
||||
self.metadata.pop("_stopped_at_iteration_limit", None)
|
||||
|
||||
class SessionPayload(TypedDict):
|
||||
key: str
|
||||
|
||||
@@ -1119,7 +1119,7 @@ class SessionState:
|
||||
if old is not None:
|
||||
session.policy = old.policy
|
||||
session.metadata = deepcopy(old.metadata)
|
||||
for name in (*_RESERVED_KEYS, "webui_recovery", "_last_summary"):
|
||||
for name in (*_RESERVED_KEYS, "webui_recovery", "_last_summary", "_stopped_at_iteration_limit"):
|
||||
session.metadata.pop(name, None)
|
||||
if session.policy.persist:
|
||||
self._store.delete(key)
|
||||
|
||||
@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, Mapping, MutableMapping
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.runtime_context import RuntimeContextBlock, wrap_runtime_context_lines
|
||||
from nanobot.session.goal_state import (
|
||||
goal_state_runtime_lines,
|
||||
sustained_goal_active,
|
||||
@@ -21,11 +22,14 @@ from nanobot.session.recovery import REQUEST_ORIGIN_METADATA_KEY
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.agent.loop import TurnContext
|
||||
from nanobot.agent.tools.context import RequestContext
|
||||
from nanobot.session.state import SessionState
|
||||
|
||||
INTERNAL_CONTINUATION_META = "_internal_continuation"
|
||||
INTERNAL_CONTINUATION_KIND_META = "_internal_continuation_kind"
|
||||
INTERNAL_CONTINUATION_RUN_STARTED_AT_META = "_internal_continuation_run_started_at"
|
||||
SKIP_USER_PERSIST_META = "_skip_user_persist"
|
||||
_ITERATION_LIMIT_META = "_stopped_at_iteration_limit"
|
||||
|
||||
_GOAL_CONTINUATION_KIND = "sustained_goal"
|
||||
_GOAL_CONTINUATION_SENDER = "system:continuation"
|
||||
@@ -37,6 +41,37 @@ _STRIPPED_INBOUND_META_KEYS = {
|
||||
}
|
||||
|
||||
|
||||
def record_iteration_limit(
|
||||
metadata: MutableMapping[str, Any], stop_reason: str, *, continuation_pending: bool,
|
||||
) -> None:
|
||||
"""Remember a terminal budget stop, not an internal continuation handoff."""
|
||||
if stop_reason == "max_iterations" and not continuation_pending:
|
||||
metadata[_ITERATION_LIMIT_META] = True
|
||||
else:
|
||||
metadata.pop(_ITERATION_LIMIT_META, None)
|
||||
|
||||
|
||||
async def iteration_limit_context(
|
||||
state: SessionState, request: RequestContext,
|
||||
) -> RuntimeContextBlock | None:
|
||||
"""Attach the previous terminal budget outcome to the new input suffix."""
|
||||
if not request.session_key or not request.persist_session:
|
||||
return None
|
||||
if internal_continuation_inbound(request.metadata):
|
||||
return None
|
||||
payload = await state.read_metadata(request.session_key)
|
||||
if payload is None or not payload["metadata"].get(_ITERATION_LIMIT_META):
|
||||
return None
|
||||
return RuntimeContextBlock(
|
||||
source="iteration_limit",
|
||||
content=wrap_runtime_context_lines([
|
||||
"The previous turn stopped at the tool-iteration limit without finishing its work.",
|
||||
"Follow the current request. Resume unfinished work only if the user asks for it.",
|
||||
"Completed tool calls and their results remain in the history; do not repeat them blindly.",
|
||||
]),
|
||||
)
|
||||
|
||||
|
||||
def internal_continuation_inbound(metadata: Mapping[str, Any] | None) -> bool:
|
||||
"""True for an inbound message created by an internal continuation policy."""
|
||||
return bool(metadata and metadata.get(INTERNAL_CONTINUATION_META) is True)
|
||||
|
||||
Reference in new issue
Block a user