refactor(sessions)!: retire the global shared session (#6621)
Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
1 parent
28d73b6942
commit
e6b0aec2a8
77 files changed
+311
-1255
No files matched your search
@@ -17,7 +17,6 @@ from nanobot.runtime_context import (
|
||||
append_runtime_context,
|
||||
)
|
||||
from nanobot.security.workspace_access import WorkspaceScopeResolver
|
||||
from nanobot.session.keys import last_channel_from_metadata
|
||||
from nanobot.session.manager import Session
|
||||
from nanobot.session.summary import SessionSummary
|
||||
from nanobot.utils.helpers import detect_image_mime, image_placeholder_text, load_bundled_template
|
||||
@@ -38,14 +37,9 @@ class PersistedPromptContextResolver:
|
||||
"""Restore prompt routing context when no inbound message is available."""
|
||||
|
||||
workspace_scopes: WorkspaceScopeResolver
|
||||
unified_session: bool = False
|
||||
|
||||
def __call__(self, session: Session) -> tuple[str | None, Path]:
|
||||
channel = session.key.split(":", 1)[0] if ":" in session.key else None
|
||||
if self.unified_session:
|
||||
route = last_channel_from_metadata(session.metadata)
|
||||
if route is not None:
|
||||
channel = route[0]
|
||||
scope = self.workspace_scopes.for_turn(
|
||||
channel=channel,
|
||||
message_metadata=None,
|
||||
|
||||
+24
-52
@@ -81,7 +81,6 @@ from nanobot.security.workspace_access import (
|
||||
from nanobot.session import turn_continuation
|
||||
from nanobot.session.automation_turns import automation_history_overrides
|
||||
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
|
||||
from nanobot.session.keys import UNIFIED_SESSION_KEY, remember_last_channel
|
||||
from nanobot.session.manager import SESSION_CACHE_MAX_SIZE, Session, SessionManager, SessionPolicy
|
||||
from nanobot.session.message_metadata import input_history_metadata, persisted_message
|
||||
from nanobot.session.model_selection import (
|
||||
@@ -291,7 +290,6 @@ class AgentLoop:
|
||||
session_ttl_minutes: int = 0,
|
||||
hooks: list[AgentHook] | None = None,
|
||||
hook_factories: list[AgentTurnHookFactory] | None = None,
|
||||
unified_session: bool = False,
|
||||
disabled_skills: list[str] | None = None,
|
||||
tools_config: ToolsConfig | None = None,
|
||||
network_config: NetworkConfig | None = None,
|
||||
@@ -392,7 +390,6 @@ class AgentLoop:
|
||||
get_tool_definitions=self.tools.get_definitions,
|
||||
resolve_prompt_context=PersistedPromptContextResolver(
|
||||
workspace_scopes=self.workspace_scopes,
|
||||
unified_session=unified_session,
|
||||
),
|
||||
)
|
||||
self.subagents = SubagentManager(
|
||||
@@ -408,7 +405,6 @@ class AgentLoop:
|
||||
consolidator=self.consolidator,
|
||||
session_manager=self.sessions,
|
||||
)
|
||||
self._unified_session = unified_session
|
||||
self._running = False
|
||||
self._runtime_context_providers: list[RuntimeContextProvider] = []
|
||||
self._active_tasks: dict[str, set[asyncio.Task[Any]]] = {}
|
||||
@@ -510,7 +506,6 @@ class AgentLoop:
|
||||
restrict_to_workspace=config.tools.restrict_to_workspace,
|
||||
channels_config=config.channels,
|
||||
timezone=defaults.timezone,
|
||||
unified_session=defaults.unified_session,
|
||||
disabled_skills=defaults.disabled_skills,
|
||||
session_ttl_minutes=defaults.session_ttl_minutes,
|
||||
idle_compact_check_interval_seconds=defaults.idle_compact_check_interval_seconds,
|
||||
@@ -854,7 +849,6 @@ class AgentLoop:
|
||||
async def _dispatch_command_inline(
|
||||
self,
|
||||
msg: InboundMessage,
|
||||
key: str,
|
||||
raw: str,
|
||||
dispatch_fn: Callable[
|
||||
[CommandContext], Awaitable[CommandExpansion | InboundMessage | OutboundMessage | None],
|
||||
@@ -867,7 +861,7 @@ class AgentLoop:
|
||||
async def dispatch_and_publish() -> None:
|
||||
_, automation_metadata = automation_history_overrides(msg.metadata)
|
||||
ctx = CommandContext(
|
||||
msg=msg, session=None, key=key, raw=raw, loop=self,
|
||||
msg=msg, session=None, key=msg.session_key, raw=raw, loop=self,
|
||||
is_user_turn=(
|
||||
msg.is_user_input and msg.channel != "system"
|
||||
and msg.sender_id != "subagent" and not automation_metadata
|
||||
@@ -964,11 +958,6 @@ class AgentLoop:
|
||||
"""Forget ephemeral file-read state for a reset or removed session."""
|
||||
self._file_state_store.discard(key)
|
||||
|
||||
def _effective_session_key(self, msg: InboundMessage) -> str:
|
||||
if self._unified_session and not msg.session_key_override:
|
||||
return UNIFIED_SESSION_KEY
|
||||
return msg.session_key
|
||||
|
||||
def _can_inject_message(self, msg: InboundMessage) -> bool:
|
||||
if turn_continuation.internal_continuation_inbound(msg.metadata) or any(
|
||||
coordinator.owns_turn(msg) for coordinator in self._automation_turn_coordinators
|
||||
@@ -1007,8 +996,6 @@ class AgentLoop:
|
||||
if automation_metadata:
|
||||
return
|
||||
delivery.remember_session_route(session.metadata)
|
||||
if self._unified_session and session.key == UNIFIED_SESSION_KEY:
|
||||
remember_last_channel(session.metadata, msg.channel, msg.chat_id)
|
||||
|
||||
async def _run_agent_loop(
|
||||
self,
|
||||
@@ -1439,7 +1426,7 @@ class AgentLoop:
|
||||
try:
|
||||
await self._dispatch_inbound(msg)
|
||||
except (SessionOperationQueueFullError, SessionConflictError, ExecutionControlError) as exc:
|
||||
key = self._effective_session_key(msg)
|
||||
key = msg.session_key
|
||||
logger.warning("Session input rejected for {}: {}", key, exc)
|
||||
if msg.is_user_input:
|
||||
delivery = self.turn_delivery_factory.unrouted(msg, key)
|
||||
@@ -1449,48 +1436,36 @@ class AgentLoop:
|
||||
|
||||
async def _dispatch_inbound(self, msg: InboundMessage) -> None:
|
||||
raw = command_text(msg)
|
||||
effective_key = self._effective_session_key(msg)
|
||||
key = msg.session_key
|
||||
if msg.metadata.get(INBOUND_META_RUNTIME_CONTROL) == RUNTIME_CONTROL_SESSION_DISCARD:
|
||||
await self.discard_session(msg.session_key)
|
||||
await self.discard_session(key)
|
||||
return
|
||||
if (
|
||||
msg.require_existing_session
|
||||
and await self.sessions.state.read_metadata(effective_key) is None
|
||||
and await self.sessions.state.read_metadata(key) is None
|
||||
):
|
||||
return
|
||||
if msg.is_user_input:
|
||||
await self.runtime_event_publisher.user_input_accepted(msg, effective_key)
|
||||
await self.runtime_event_publisher.user_input_accepted(msg, key)
|
||||
if msg.channel != "system" and self.commands.is_priority(raw):
|
||||
await self._dispatch_command_inline(
|
||||
msg, effective_key, raw,
|
||||
self.commands.dispatch_priority,
|
||||
)
|
||||
await self._dispatch_command_inline(msg, raw, self.commands.dispatch_priority)
|
||||
return
|
||||
routed_msg = msg
|
||||
if effective_key != msg.session_key:
|
||||
routed_msg = dataclasses.replace(
|
||||
msg,
|
||||
session_key_override=effective_key,
|
||||
)
|
||||
# Mid-turn injection bypasses _dispatch_one, so admit before joining
|
||||
# an active queue. Admission may cancel its worker and release the queue.
|
||||
# Mid-turn injection bypasses _dispatch_one. Admit before joining an
|
||||
# active queue; admission may cancel its worker and release the queue.
|
||||
if (
|
||||
effective_key in self._pending_queues
|
||||
key in self._pending_queues
|
||||
and self._recovery_admission is not None
|
||||
and not await self._recovery_admission.admit(routed_msg)
|
||||
and not await self._recovery_admission.admit(msg)
|
||||
):
|
||||
return
|
||||
# If this session already has an active pending queue (i.e. a task
|
||||
# is processing this session), route the message there for mid-turn
|
||||
# injection instead of creating a competing task.
|
||||
if effective_key in self._pending_queues:
|
||||
if msg.channel != "system" and self.commands.is_dispatchable_command(raw):
|
||||
await self._dispatch_command_inline(
|
||||
msg, effective_key, raw,
|
||||
self.commands.dispatch,
|
||||
)
|
||||
return
|
||||
await self._enqueue_session_message(routed_msg)
|
||||
if (
|
||||
key in self._pending_queues
|
||||
and msg.channel != "system"
|
||||
and self.commands.is_dispatchable_command(raw)
|
||||
):
|
||||
await self._dispatch_command_inline(msg, raw, self.commands.dispatch)
|
||||
return
|
||||
await self._enqueue_session_message(msg)
|
||||
|
||||
def maintenance(self) -> AbstractContextManager[None]:
|
||||
"""Close task intake only when this runtime's accepted work is finished.
|
||||
@@ -1540,11 +1515,9 @@ class AgentLoop:
|
||||
|
||||
async def _enqueue_session_message(self, msg: InboundMessage) -> None:
|
||||
self.bus.admission.check()
|
||||
session_key = self._effective_session_key(msg)
|
||||
session_key = msg.session_key
|
||||
if not self.subagents.accepts_result(msg, session_key):
|
||||
return
|
||||
if session_key != msg.session_key:
|
||||
msg = dataclasses.replace(msg, session_key_override=session_key)
|
||||
|
||||
for coordinator in self._automation_turn_coordinators:
|
||||
if coordinator.defer_if_active(
|
||||
@@ -1629,7 +1602,7 @@ class AgentLoop:
|
||||
msg: InboundMessage,
|
||||
pending: asyncio.Queue[InboundMessage],
|
||||
) -> None:
|
||||
session_key = self._effective_session_key(msg)
|
||||
session_key = msg.session_key
|
||||
# The request context is reset before errors reach this boundary, and
|
||||
# discard may evict the session while a turn is still unwinding.
|
||||
session = self.sessions.state.peek_header(session_key)
|
||||
@@ -1699,19 +1672,18 @@ class AgentLoop:
|
||||
or self._preserve_inflight_turns_on_shutdown
|
||||
):
|
||||
raise exc
|
||||
key = self._effective_session_key(msg)
|
||||
generation = delivery.delivery_message.session_generation
|
||||
try:
|
||||
session = await self.sessions.state.read(key)
|
||||
session = await self.sessions.state.read(session_key)
|
||||
if session is None or (generation is not None and session.generation != generation):
|
||||
self.runtime_event_publisher.clear_turn(session_key, run_id=delivery.run_id)
|
||||
skip_idle = True
|
||||
raise exc
|
||||
restore_runtime_checkpoint(session)
|
||||
await self.sessions.state.finish_turn(session)
|
||||
logger.info("Saved cancelled turn for session {}", key)
|
||||
logger.info("Saved cancelled turn for session {}", session_key)
|
||||
except SessionConflictError:
|
||||
current = await self.sessions.state.read_header(key)
|
||||
current = await self.sessions.state.read_header(session_key)
|
||||
if current is None or (generation is not None and current.generation != generation):
|
||||
self.runtime_event_publisher.clear_turn(session_key, run_id=delivery.run_id)
|
||||
skip_idle = True
|
||||
|
||||
@@ -908,7 +908,7 @@ class SubagentManager:
|
||||
|
||||
# Inject as system message to trigger main agent.
|
||||
# Use session_key_override to align with the main agent's effective
|
||||
# session key (which accounts for unified sessions) so the result is
|
||||
# session key (including any explicit override) so the result is
|
||||
# routed to the correct pending queue (mid-turn injection) instead of
|
||||
# being dispatched as a competing independent task.
|
||||
override = origin.get("session_key") or f"{origin['channel']}:{origin['chat_id']}"
|
||||
|
||||
@@ -17,7 +17,6 @@ from nanobot.agent.tools.schema import (
|
||||
from nanobot.cron.service import CronService
|
||||
from nanobot.cron.session_turns import is_bound_cron_job, is_cron_turn
|
||||
from nanobot.cron.types import CronJob, CronJobState, CronSchedule
|
||||
from nanobot.session.keys import UNIFIED_SESSION_KEY
|
||||
|
||||
_CRON_PARAMETERS = tool_parameters_schema(
|
||||
action=StringSchema(
|
||||
@@ -81,11 +80,7 @@ class CronTool(Tool):
|
||||
ctx = current_request_context()
|
||||
if ctx is None:
|
||||
return "", "", "", {}
|
||||
raw_key = f"{ctx.channel}:{ctx.chat_id}" if ctx.channel and ctx.chat_id else ""
|
||||
session_key = (
|
||||
raw_key if ctx.session_key == UNIFIED_SESSION_KEY else (ctx.session_key or "")
|
||||
)
|
||||
return session_key, ctx.channel or "", ctx.chat_id or "", dict(ctx.metadata or {})
|
||||
return ctx.session_key or "", ctx.channel or "", ctx.chat_id or "", dict(ctx.metadata or {})
|
||||
|
||||
@staticmethod
|
||||
def _validate_timezone(tz: str) -> str | None:
|
||||
|
||||
@@ -62,7 +62,7 @@ class MyTool(Tool):
|
||||
"_session_locks", "_active_tasks", "_background_tasks",
|
||||
# Security boundaries (inspect + modify both blocked)
|
||||
"restrict_to_workspace", "channels_config",
|
||||
"_concurrency_gate", "_unified_session", "_extra_hooks", "_hook_factories",
|
||||
"_concurrency_gate", "_extra_hooks", "_hook_factories",
|
||||
})
|
||||
|
||||
READ_ONLY = frozenset({
|
||||
|
||||
@@ -30,11 +30,7 @@ from nanobot.events import (
|
||||
)
|
||||
from nanobot.providers.base import LLMUsage
|
||||
from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEY
|
||||
from nanobot.session.keys import (
|
||||
UNIFIED_SESSION_KEY,
|
||||
is_internal_session,
|
||||
last_channel_from_metadata,
|
||||
)
|
||||
from nanobot.session.keys import is_internal_session
|
||||
from nanobot.session.message_metadata import input_history_metadata
|
||||
from nanobot.utils.cancellation import shield_and_drain
|
||||
from nanobot.utils.helpers import strip_think
|
||||
@@ -165,12 +161,8 @@ class TurnDeliveryFactory:
|
||||
else:
|
||||
# Older sessions have no route snapshot. Only direct clients have a
|
||||
# session key that is also an unambiguous delivery address.
|
||||
route = (
|
||||
last_channel_from_metadata(session_metadata)
|
||||
if session_key == UNIFIED_SESSION_KEY
|
||||
else tuple(session_key.split(":", 1))
|
||||
)
|
||||
if not route or len(route) != 2 or route[0] not in {"websocket", "cli"}:
|
||||
route = session_key.split(":", 1)
|
||||
if len(route) != 2 or route[0] not in {"websocket", "cli"}:
|
||||
return EventSink()
|
||||
channel, chat_id = route
|
||||
metadata = {}
|
||||
|
||||
Reference in new issue
Block a user