Skip to content

Your lightweight open-source personal agent.

Merge origin/main into fix/discord-allow-channel-threads

Xubin Rene31273ebaa13
diff --cc nanobot/agent/loop.py
index 777cde1b9,0f7dfd90b..b66ca0a16
--- a/nanobot/agent/loop.py
+++ b/nanobot/agent/loop.py
@@@ -855,18 -966,35 +971,35 @@@ class AgentLoop
if isinstance(message_tool, MessageTool):
message_tool.start_turn()
- history = session.get_history(max_messages=0)
-
- initial_messages = self.context.build_messages(
- history=history,
- current_message=msg.content,
- session_summary=pending,
- media=msg.media if msg.media else None,
- channel=msg.channel,
- chat_id=self._runtime_chat_id(msg),
+ history = session.get_history(
+ max_tokens=self._replay_token_budget(),
+ include_timestamps=True,
)
- async def _bus_progress(content: str, *, tool_hint: bool = False) -> None:
+ pending_ask_id = pending_ask_user_id(history)
+ if pending_ask_id:
+ initial_messages = ask_user_tool_result_messages(
+ self.context.build_system_prompt(channel=msg.channel),
+ history,
+ pending_ask_id,
+ msg.content,
+ )
+ else:
+ initial_messages = self.context.build_messages(
+ history=history,
+ current_message=msg.content,
+ session_summary=pending,
+ media=msg.media if msg.media else None,
+ channel=msg.channel,
- chat_id=msg.chat_id,
++ chat_id=self._runtime_chat_id(msg),
+ )
+
+ async def _bus_progress(
+ content: str,
+ *,
+ tool_hint: bool = False,
+ tool_events: list[dict[str, Any]] | None = None,
+ ) -> None:
meta = dict(msg.metadata or {})
meta["_progress"] = True
meta["_tool_hint"] = tool_hint
diff --cc tests/agent/test_loop_save_turn.py
index dad5d8d68,883bf7045..52f41677f
--- a/tests/agent/test_loop_save_turn.py
+++ b/tests/agent/test_loop_save_turn.py
@@@ -348,61 -348,6 +348,61 @@@ async def test_process_message_does_not
assert AgentLoop._PENDING_USER_TURN_KEY not in session.metadata
+async def test_process_message_uses_context_chat_id_for_runtime_prompt(tmp_path: Path) -> None:
+ loop = _make_full_loop(tmp_path)
+ loop.consolidator.maybe_consolidate_by_tokens = AsyncMock(return_value=False) # type: ignore[method-assign]
+ loop.context.build_messages = MagicMock( # type: ignore[method-assign]
+ return_value=[
+ {"role": "system", "content": "system"},
+ {"role": "user", "content": "runtime + hello"},
+ ]
+ )
+ loop._run_agent_loop = AsyncMock(return_value=( # type: ignore[method-assign]
+ "done",
+ [],
+ [
+ {"role": "system", "content": "system"},
+ {"role": "user", "content": "runtime + hello"},
+ {"role": "assistant", "content": "done"},
+ ],
+ "stop",
+ False,
+ ))
+
+ result = await loop._process_message(
+ InboundMessage(
+ channel="discord",
+ sender_id="u1",
+ chat_id="thread-777",
+ content="hello",
+ metadata={"context_chat_id": "parent-456"},
+ session_key_override="discord:parent-456:thread:thread-777",
+ )
+ )
+
+ assert result is not None
+ assert result.chat_id == "thread-777"
+ assert loop.context.build_messages.call_args.kwargs["chat_id"] == "parent-456"
+ assert loop._run_agent_loop.call_args.kwargs["chat_id"] == "thread-777"
+
+
+def test_set_tool_context_uses_effective_key_for_spawn_tool(tmp_path: Path) -> None:
+ loop = _make_full_loop(tmp_path)
+ spawn_tool = loop.tools.get("spawn")
+ assert spawn_tool is not None
+
+ loop._set_tool_context(
+ "discord",
+ "thread-777",
- effective_key="discord:parent-456:thread:thread-777",
++ session_key="discord:parent-456:thread:thread-777",
+ )
+
+ assert spawn_tool._origin_channel.get() == "discord" # type: ignore[attr-defined]
+ assert spawn_tool._origin_chat_id.get() == "thread-777" # type: ignore[attr-defined]
+ assert spawn_tool._session_key.get() == "discord:parent-456:thread:thread-777" # type: ignore[attr-defined]
+
+
@pytest.mark.asyncio
async def test_next_turn_after_crash_closes_pending_user_turn_before_new_input(tmp_path: Path) -> None:
loop = _make_full_loop(tmp_path)