2026-04-14 13:42:59 +08:00
|
|
|
import asyncio
|
2026-07-30 22:39:43 +08:00
|
|
|
import json
|
2026-11-03 22:47:00 +08:00
|
|
|
import threading
|
2026-04-13 02:09:40 +00:00
|
|
|
from pathlib import Path
|
2026-06-16 19:42:52 +08:00
|
|
|
from types import SimpleNamespace
|
2026-04-13 02:09:40 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
|
|
|
|
|
|
import pytest
|
2026-07-21 00:14:27 +08:00
|
|
|
from loguru import logger
|
2026-04-13 02:09:40 +00:00
|
|
|
|
2026-10-09 14:04:18 +08:00
|
|
|
from agent.session_helpers import run_session, save_completed_subagent, visible_message
|
2026-08-31 00:06:15 +08:00
|
|
|
from nanobot.agent.context import ContextBuilder, TranscriptInput
|
2026-07-27 11:52:14 +08:00
|
|
|
from nanobot.agent.loop import AgentLoop
|
2026-08-26 15:18:53 +08:00
|
|
|
from nanobot.agent.runner import AgentRunResult
|
2026-07-10 14:16:56 +08:00
|
|
|
from nanobot.agent.tools.context import RequestContext, request_context
|
2026-04-13 02:09:40 +00:00
|
|
|
from nanobot.bus.events import InboundMessage
|
2026-06-30 00:03:07 +08:00
|
|
|
from nanobot.bus.outbound_events import (
|
|
|
|
|
GoalStatusEvent,
|
|
|
|
|
StreamDeltaEvent,
|
|
|
|
|
StreamedResponseEvent,
|
|
|
|
|
StreamEndEvent,
|
|
|
|
|
TurnEndEvent,
|
|
|
|
|
)
|
2026-04-13 02:09:40 +00:00
|
|
|
from nanobot.bus.queue import MessageBus
|
2026-11-03 22:47:00 +08:00
|
|
|
from nanobot.bus.runtime_events import TurnCompleted, TurnRunStatusChanged
|
2026-06-12 11:57:35 +08:00
|
|
|
from nanobot.cron.session_turns import CRON_HISTORY_META, CRON_TRIGGER_META
|
2026-11-03 22:47:00 +08:00
|
|
|
from nanobot.goal.assembly import install_goal
|
|
|
|
|
from nanobot.providers.base import (
|
|
|
|
|
GenerationSettings,
|
|
|
|
|
LLMProvider,
|
|
|
|
|
LLMResponse,
|
|
|
|
|
LLMUsage,
|
|
|
|
|
ProviderConversationState,
|
|
|
|
|
ToolCallRequest,
|
|
|
|
|
)
|
2026-07-10 15:17:20 +08:00
|
|
|
from nanobot.providers.factory import ProviderSnapshot
|
2026-07-11 18:52:56 +08:00
|
|
|
from nanobot.runtime_context import (
|
|
|
|
|
RUNTIME_CONTEXT_HISTORY_META,
|
|
|
|
|
RUNTIME_CONTEXT_MESSAGE_META,
|
2026-09-05 22:09:12 +08:00
|
|
|
RUNTIME_CONTEXT_TAG,
|
2026-07-11 18:52:56 +08:00
|
|
|
RuntimeContextBlock,
|
|
|
|
|
append_runtime_context,
|
|
|
|
|
public_history_message,
|
|
|
|
|
)
|
2026-06-30 01:03:16 +08:00
|
|
|
from nanobot.session.automation_turns import AUTOMATION_HISTORY_META
|
2026-05-16 11:14:56 +00:00
|
|
|
from nanobot.session.goal_state import GOAL_STATE_KEY
|
2026-07-14 21:52:26 +08:00
|
|
|
from nanobot.session.keys import (
|
|
|
|
|
LAST_CHANNEL_METADATA_KEY,
|
|
|
|
|
UNIFIED_SESSION_KEY,
|
|
|
|
|
)
|
2026-07-28 11:07:58 +08:00
|
|
|
from nanobot.session.manager import Session
|
2026-08-26 16:23:19 +08:00
|
|
|
from nanobot.session.recovery import (
|
|
|
|
|
PENDING_FOLLOWUP_ID_KEY,
|
|
|
|
|
PENDING_FOLLOWUPS_KEY,
|
|
|
|
|
PROVIDER_STATE_CHECKPOINT_VERSION,
|
|
|
|
|
PROVIDER_STATE_CHECKPOINT_VERSION_KEY,
|
|
|
|
|
RUNTIME_CHECKPOINT_KEY,
|
|
|
|
|
restore_runtime_checkpoint,
|
|
|
|
|
)
|
2026-09-02 18:05:54 +08:00
|
|
|
from nanobot.session.summary import (
|
|
|
|
|
SUMMARY_CONTINUATION_TEXT,
|
|
|
|
|
SessionSummaryCheckpoint,
|
|
|
|
|
)
|
2026-06-01 00:56:39 +08:00
|
|
|
from nanobot.session.turn_continuation import (
|
|
|
|
|
INTERNAL_CONTINUATION_META,
|
|
|
|
|
)
|
2026-05-19 22:42:38 +08:00
|
|
|
from nanobot.session.webui_turns import (
|
2026-05-17 23:51:52 +08:00
|
|
|
TITLE_GENERATION_MAX_TOKENS,
|
2026-05-06 14:15:36 +00:00
|
|
|
WEBUI_SESSION_METADATA_KEY,
|
|
|
|
|
WEBUI_TITLE_METADATA_KEY,
|
2026-05-18 00:35:12 +08:00
|
|
|
WebuiTurnCoordinator,
|
2026-05-30 23:45:26 +08:00
|
|
|
clean_generated_title,
|
2026-05-06 14:15:36 +00:00
|
|
|
maybe_generate_webui_title,
|
2026-08-25 13:58:57 +08:00
|
|
|
maybe_generate_webui_title_after_turn,
|
2026-05-06 14:15:36 +00:00
|
|
|
)
|
2026-06-30 14:36:51 +08:00
|
|
|
from nanobot.triggers.local_session_turns import LOCAL_TRIGGER_META
|
2026-03-03 05:13:17 +00:00
|
|
|
|
|
|
|
|
|
2026-08-26 15:18:53 +08:00
|
|
|
def _agent_run_result(
|
|
|
|
|
final_content: str,
|
|
|
|
|
messages: list[dict],
|
|
|
|
|
*,
|
|
|
|
|
stop_reason: str = "completed",
|
|
|
|
|
had_injections: bool = False,
|
|
|
|
|
usage: LLMUsage | None = None,
|
|
|
|
|
) -> AgentRunResult:
|
|
|
|
|
return AgentRunResult(
|
|
|
|
|
final_content=final_content,
|
|
|
|
|
messages=messages,
|
|
|
|
|
stop_reason=stop_reason,
|
|
|
|
|
had_injections=had_injections,
|
|
|
|
|
usage=usage,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
def _assembled_messages(
|
|
|
|
|
builder: ContextBuilder,
|
|
|
|
|
transcript_input: TranscriptInput,
|
|
|
|
|
) -> list[dict]:
|
|
|
|
|
return builder.build_transcript(transcript_input, include_memory=False)
|
|
|
|
|
|
|
|
|
|
|
2026-03-03 05:13:17 +00:00
|
|
|
def _mk_loop() -> AgentLoop:
|
|
|
|
|
loop = AgentLoop.__new__(AgentLoop)
|
2026-04-01 19:12:49 +00:00
|
|
|
from nanobot.config.schema import AgentDefaults
|
|
|
|
|
|
|
|
|
|
loop.max_tool_result_chars = AgentDefaults().max_tool_result_chars
|
2026-03-03 05:13:17 +00:00
|
|
|
return loop
|
|
|
|
|
|
|
|
|
|
|
2026-07-30 22:39:43 +08:00
|
|
|
def _provider_state() -> ProviderConversationState:
|
|
|
|
|
return ProviderConversationState(
|
|
|
|
|
kind="openai_responses",
|
|
|
|
|
provider="openai:test",
|
|
|
|
|
model="test-model",
|
|
|
|
|
version=1,
|
|
|
|
|
payload={"items": []},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 18:52:56 +08:00
|
|
|
def _runtime_message(content, blocks: list[RuntimeContextBlock]) -> dict:
|
|
|
|
|
merged, marker = append_runtime_context(content, blocks)
|
|
|
|
|
assert marker is not None
|
2026-07-11 16:51:25 +08:00
|
|
|
return {
|
|
|
|
|
"role": "user",
|
2026-07-11 18:52:56 +08:00
|
|
|
"content": merged,
|
|
|
|
|
"_meta": {RUNTIME_CONTEXT_MESSAGE_META: marker},
|
2026-07-11 16:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-13 02:09:40 +00:00
|
|
|
def _make_full_loop(tmp_path: Path) -> AgentLoop:
|
2026-11-03 22:47:00 +08:00
|
|
|
provider = MagicMock(aclose=AsyncMock())
|
2026-09-21 14:53:19 +08:00
|
|
|
provider.provider_name = "test"
|
2026-04-13 02:09:40 +00:00
|
|
|
provider.get_default_model.return_value = "test-model"
|
2026-11-03 22:47:00 +08:00
|
|
|
provider.generation = GenerationSettings(max_tokens=4096)
|
|
|
|
|
provider.estimate_prompt_tokens.return_value = (100, "test")
|
2026-09-11 13:06:43 +08:00
|
|
|
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="Test title"))
|
2026-10-13 13:56:28 +08:00
|
|
|
bus = MessageBus()
|
|
|
|
|
loop = AgentLoop(
|
|
|
|
|
bus=bus, provider=provider, workspace=tmp_path, model="test-model",
|
2026-11-03 22:47:00 +08:00
|
|
|
builtin_assembly=install_goal,
|
2026-10-13 13:56:28 +08:00
|
|
|
)
|
2026-06-01 14:16:38 +08:00
|
|
|
WebuiTurnCoordinator(
|
|
|
|
|
bus=loop.bus,
|
|
|
|
|
sessions=loop.sessions,
|
2026-07-30 15:01:35 +08:00
|
|
|
schedule_background=lambda coro: loop.schedule_background(coro),
|
2026-09-06 02:11:19 +08:00
|
|
|
).subscribe()
|
2026-06-01 14:16:38 +08:00
|
|
|
return loop
|
2026-04-13 02:09:40 +00:00
|
|
|
|
|
|
|
|
|
2026-05-18 00:35:12 +08:00
|
|
|
def test_agent_loop_llm_runtime_reflects_current_provider_and_model(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
runtime = loop.llm_runtime()
|
|
|
|
|
|
|
|
|
|
assert runtime.provider is loop.provider
|
|
|
|
|
assert runtime.model == "test-model"
|
|
|
|
|
|
|
|
|
|
next_provider = MagicMock()
|
2026-07-10 15:17:20 +08:00
|
|
|
next_provider.generation = SimpleNamespace(
|
|
|
|
|
temperature=0.1,
|
|
|
|
|
max_tokens=4096,
|
|
|
|
|
reasoning_effort=None,
|
|
|
|
|
)
|
|
|
|
|
loop.runtime_resolver.adopt_snapshot(ProviderSnapshot(
|
|
|
|
|
provider=next_provider,
|
|
|
|
|
model="next-model",
|
|
|
|
|
context_window_tokens=runtime.context_window_tokens,
|
|
|
|
|
signature=("next-model",),
|
|
|
|
|
))
|
2026-05-18 00:35:12 +08:00
|
|
|
runtime = loop.llm_runtime()
|
|
|
|
|
|
|
|
|
|
assert runtime.provider is next_provider
|
|
|
|
|
assert runtime.model == "next-model"
|
|
|
|
|
|
|
|
|
|
|
2026-10-16 17:28:47 +08:00
|
|
|
async def test_persist_cron_turn_uses_distinct_history_marker(tmp_path: Path) -> None:
|
2026-06-12 11:43:23 +08:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-16 17:28:47 +08:00
|
|
|
session = await loop.sessions.state.get("websocket:auto")
|
2026-06-12 11:43:23 +08:00
|
|
|
prompt_ref = {"id": "cron.agent_turn.reminder", "version": 1, "sha256": "abc"}
|
|
|
|
|
|
2026-10-16 17:28:47 +08:00
|
|
|
persisted = await loop._persist_user_message_early(
|
2026-06-12 11:43:23 +08:00
|
|
|
InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="cron",
|
|
|
|
|
chat_id="auto",
|
2026-06-12 11:57:35 +08:00
|
|
|
content="Cron job: internal prompt",
|
2026-06-12 11:43:23 +08:00
|
|
|
metadata={
|
2026-06-12 11:57:35 +08:00
|
|
|
CRON_TRIGGER_META: {
|
2026-06-12 11:43:23 +08:00
|
|
|
"job_id": "job-1",
|
|
|
|
|
"job_name": "Daily check",
|
|
|
|
|
"run_id": "job-1:1",
|
|
|
|
|
"prompt_ref": prompt_ref,
|
2026-06-12 11:57:35 +08:00
|
|
|
"persist_content": "Scheduled cron job triggered: Daily check",
|
2026-06-12 11:43:23 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
session,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert persisted is True
|
|
|
|
|
message = session.messages[-1]
|
2026-06-12 11:57:35 +08:00
|
|
|
assert message["content"] == "Scheduled cron job triggered: Daily check"
|
2026-06-30 01:03:16 +08:00
|
|
|
assert message[AUTOMATION_HISTORY_META] == {
|
|
|
|
|
"kind": "cron",
|
|
|
|
|
"cron_job_id": "job-1",
|
|
|
|
|
"cron_job_name": "Daily check",
|
|
|
|
|
"cron_run_id": "job-1:1",
|
|
|
|
|
"cron_prompt_ref": prompt_ref,
|
|
|
|
|
}
|
2026-06-12 11:57:35 +08:00
|
|
|
assert message[CRON_HISTORY_META] is True
|
|
|
|
|
assert CRON_TRIGGER_META not in message
|
|
|
|
|
assert message["cron_job_id"] == "job-1"
|
|
|
|
|
assert message["cron_job_name"] == "Daily check"
|
|
|
|
|
assert message["cron_run_id"] == "job-1:1"
|
|
|
|
|
assert message["cron_prompt_ref"] == prompt_ref
|
2026-06-12 11:43:23 +08:00
|
|
|
|
|
|
|
|
|
2026-10-16 17:28:47 +08:00
|
|
|
async def test_persist_user_message_acknowledges_durable_followup(tmp_path: Path) -> None:
|
2026-08-22 19:33:22 +08:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-19 11:31:19 +08:00
|
|
|
loop.provider.aclose = AsyncMock()
|
|
|
|
|
message = InboundMessage(
|
|
|
|
|
channel="websocket", sender_id="user", chat_id="chat", content="queued while busy",
|
2026-08-22 19:33:22 +08:00
|
|
|
)
|
2026-10-19 11:31:19 +08:00
|
|
|
try:
|
|
|
|
|
identifier = await loop.sessions.state.queue_followup(message.session_key, message)
|
|
|
|
|
message.metadata[PENDING_FOLLOWUP_ID_KEY] = identifier
|
|
|
|
|
session = await loop.sessions.state.get(message.session_key)
|
|
|
|
|
assert await loop._persist_user_message_early(message, session)
|
|
|
|
|
assert PENDING_FOLLOWUPS_KEY not in session.metadata
|
|
|
|
|
assert PENDING_FOLLOWUPS_KEY not in (await loop.sessions.state.get(session.key)).metadata
|
|
|
|
|
finally:
|
|
|
|
|
await loop.aclose()
|
2026-08-22 19:33:22 +08:00
|
|
|
|
|
|
|
|
|
2026-10-16 17:28:47 +08:00
|
|
|
async def test_persist_local_trigger_turn_uses_hidden_automation_marker(tmp_path: Path) -> None:
|
2026-06-30 01:03:16 +08:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-16 17:28:47 +08:00
|
|
|
session = await loop.sessions.state.get("websocket:auto")
|
2026-06-30 01:03:16 +08:00
|
|
|
|
2026-10-16 17:28:47 +08:00
|
|
|
persisted = await loop._persist_user_message_early(
|
2026-06-30 01:03:16 +08:00
|
|
|
InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="trigger",
|
|
|
|
|
chat_id="auto",
|
|
|
|
|
content="Review PR #4502",
|
|
|
|
|
metadata={
|
2026-06-30 14:36:51 +08:00
|
|
|
LOCAL_TRIGGER_META: {
|
2026-06-30 01:03:16 +08:00
|
|
|
"trigger_id": "trg_123",
|
|
|
|
|
"trigger_name": "PR review",
|
|
|
|
|
"delivery_id": "tdel_456",
|
|
|
|
|
"created_at_ms": 1_700_000_000_000,
|
2026-07-01 21:09:36 +08:00
|
|
|
"persist_content": "Local trigger received: PR review\n\nReview PR #4502",
|
2026-06-30 01:03:16 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
session,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert persisted is True
|
|
|
|
|
message = session.messages[-1]
|
2026-07-01 21:09:36 +08:00
|
|
|
assert message["content"] == "Local trigger received: PR review\n\nReview PR #4502"
|
2026-06-30 01:03:16 +08:00
|
|
|
assert message[AUTOMATION_HISTORY_META] == {
|
2026-06-30 14:36:51 +08:00
|
|
|
"kind": "local_trigger",
|
2026-06-30 01:03:16 +08:00
|
|
|
"trigger_id": "trg_123",
|
|
|
|
|
"trigger_name": "PR review",
|
|
|
|
|
"trigger_delivery_id": "tdel_456",
|
|
|
|
|
}
|
2026-06-30 14:36:51 +08:00
|
|
|
assert LOCAL_TRIGGER_META not in message
|
2026-06-30 01:03:16 +08:00
|
|
|
assert message["trigger_id"] == "trg_123"
|
|
|
|
|
assert message["trigger_name"] == "PR review"
|
|
|
|
|
assert message["trigger_delivery_id"] == "tdel_456"
|
|
|
|
|
|
|
|
|
|
|
2026-07-01 21:09:36 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_new_with_bot_suffix_does_not_persist_command(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
response = await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="user",
|
|
|
|
|
chat_id="chat-1",
|
|
|
|
|
content="/new@nanobot_bot",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert response is not None
|
|
|
|
|
assert response.content == "New session started."
|
|
|
|
|
session = loop.sessions.get_or_create("websocket:chat-1")
|
|
|
|
|
assert session.messages == []
|
|
|
|
|
|
|
|
|
|
|
2026-08-04 17:03:13 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
("content", "expected"),
|
|
|
|
|
[
|
2026-10-09 01:18:03 +08:00
|
|
|
(
|
|
|
|
|
"/stop now",
|
|
|
|
|
'Command "/stop" does not accept arguments. Did you mean "/stop"?',
|
|
|
|
|
),
|
2026-08-04 17:03:13 +08:00
|
|
|
(
|
|
|
|
|
"/status now",
|
|
|
|
|
'Command "/status" does not accept arguments. Did you mean "/status"?',
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
async def test_invalid_slash_command_is_rejected_without_calling_provider(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
content: str,
|
|
|
|
|
expected: str,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
response = await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="user",
|
|
|
|
|
chat_id="chat-1",
|
|
|
|
|
content=content,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert response is not None
|
|
|
|
|
assert response.content == expected
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.assert_not_awaited()
|
2026-08-04 17:03:13 +08:00
|
|
|
session = loop.sessions.get_or_create("websocket:chat-1")
|
|
|
|
|
persisted = [
|
|
|
|
|
(message["role"], message["content"], message.get("_command"))
|
|
|
|
|
for message in session.messages
|
|
|
|
|
]
|
|
|
|
|
assert persisted == [
|
|
|
|
|
("user", content, True),
|
|
|
|
|
("assistant", response.content, True),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-10-09 01:18:03 +08:00
|
|
|
@pytest.mark.parametrize(("channel", "content"), [
|
|
|
|
|
("websocket", "/tmp 看看这个目录"),
|
|
|
|
|
("weixin", "/home/user/project"),
|
|
|
|
|
("cli", "/new/file.txt"),
|
|
|
|
|
])
|
|
|
|
|
async def test_absolute_path_reaches_provider_as_chat(tmp_path, channel, content) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.provider.aclose = AsyncMock()
|
|
|
|
|
try:
|
|
|
|
|
response = await loop._process_message(InboundMessage(
|
|
|
|
|
channel=channel, sender_id="user", chat_id="path", content=content,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
assert response is not None and response.content == "Test title"
|
|
|
|
|
request = loop.provider.chat_stream_with_retry.await_args.kwargs
|
|
|
|
|
assert any(
|
|
|
|
|
message["role"] == "user" and content in message["content"]
|
|
|
|
|
for message in request["messages"]
|
|
|
|
|
)
|
|
|
|
|
session = loop.sessions.get_or_create(f"{channel}:path")
|
2026-10-09 14:04:18 +08:00
|
|
|
assert session.get_history(include_runtime_context=False)[0]["content"] == content
|
2026-10-09 01:18:03 +08:00
|
|
|
assert not session.messages[0].get("_command")
|
|
|
|
|
finally:
|
|
|
|
|
await loop.aclose()
|
|
|
|
|
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
def test_clean_generated_title_strips_reasoning_tags() -> None:
|
|
|
|
|
assert clean_generated_title("<think>reasoning</think> WebUI polish") == "WebUI polish"
|
|
|
|
|
assert clean_generated_title("Title: <think> The user said hello") == ""
|
|
|
|
|
|
|
|
|
|
|
2026-05-06 14:15:36 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_generate_webui_title_only_for_marked_webui_sessions(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(
|
2026-05-06 14:15:36 +00:00
|
|
|
return_value=LLMResponse(content='"优化 WebUI 侧边栏。"', finish_reason="stop")
|
|
|
|
|
)
|
|
|
|
|
session = loop.sessions.get_or_create("websocket:chat-title")
|
|
|
|
|
session.metadata[WEBUI_SESSION_METADATA_KEY] = True
|
|
|
|
|
session.add_message("user", "帮我优化一下 webui 的 sidebar")
|
|
|
|
|
session.add_message("assistant", "可以,我会先调整布局和视觉层级。")
|
|
|
|
|
loop.sessions.save(session)
|
|
|
|
|
|
|
|
|
|
generated = await maybe_generate_webui_title(
|
|
|
|
|
sessions=loop.sessions,
|
|
|
|
|
session_key="websocket:chat-title",
|
|
|
|
|
provider=loop.provider,
|
|
|
|
|
model=loop.model,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert generated is True
|
2026-10-16 17:28:47 +08:00
|
|
|
session = await loop.sessions.state.get(session.key)
|
2026-05-06 14:15:36 +00:00
|
|
|
assert session.metadata[WEBUI_TITLE_METADATA_KEY] == "优化 WebUI 侧边栏"
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.assert_awaited_once()
|
|
|
|
|
assert loop.provider.chat_stream_with_retry.await_args.kwargs["max_tokens"] == TITLE_GENERATION_MAX_TOKENS
|
2026-05-17 23:51:52 +08:00
|
|
|
assert (
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.await_args.kwargs["reasoning_effort"]
|
2026-09-28 14:50:21 +08:00
|
|
|
is None
|
2026-05-17 23:51:52 +08:00
|
|
|
)
|
2026-05-06 14:15:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_generate_webui_title_skips_plain_websocket_sessions(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(
|
2026-05-06 14:15:36 +00:00
|
|
|
return_value=LLMResponse(content="Plain websocket title", finish_reason="stop")
|
|
|
|
|
)
|
|
|
|
|
session = loop.sessions.get_or_create("websocket:custom-client")
|
|
|
|
|
session.add_message("user", "hello from a custom websocket client")
|
|
|
|
|
loop.sessions.save(session)
|
|
|
|
|
|
|
|
|
|
generated = await maybe_generate_webui_title(
|
|
|
|
|
sessions=loop.sessions,
|
|
|
|
|
session_key="websocket:custom-client",
|
|
|
|
|
provider=loop.provider,
|
|
|
|
|
model=loop.model,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert generated is False
|
|
|
|
|
assert WEBUI_TITLE_METADATA_KEY not in session.metadata
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.assert_not_awaited()
|
2026-05-06 14:15:36 +00:00
|
|
|
|
|
|
|
|
|
2026-05-17 23:51:52 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_generate_webui_title_ignores_command_only_sessions(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
session = loop.sessions.get_or_create("websocket:command-title")
|
|
|
|
|
session.metadata[WEBUI_SESSION_METADATA_KEY] = True
|
|
|
|
|
session.add_message("user", "/model deep", _command=True)
|
|
|
|
|
session.add_message(
|
|
|
|
|
"assistant",
|
|
|
|
|
"Switched model preset to `deep`.\n- Model: `deepseek-v4-pro`",
|
|
|
|
|
_command=True,
|
|
|
|
|
)
|
|
|
|
|
loop.sessions.save(session)
|
|
|
|
|
|
|
|
|
|
generated = await maybe_generate_webui_title(
|
|
|
|
|
sessions=loop.sessions,
|
|
|
|
|
session_key="websocket:command-title",
|
|
|
|
|
provider=loop.provider,
|
|
|
|
|
model=loop.model,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert generated is False
|
|
|
|
|
assert WEBUI_TITLE_METADATA_KEY not in session.metadata
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.assert_not_awaited()
|
2026-05-17 23:51:52 +08:00
|
|
|
|
|
|
|
|
|
2026-06-12 18:17:28 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_generate_webui_title_ignores_cron_internal_turns(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
session = loop.sessions.get_or_create("websocket:cron-title")
|
|
|
|
|
session.metadata[WEBUI_SESSION_METADATA_KEY] = True
|
|
|
|
|
session.add_message(
|
|
|
|
|
"user",
|
|
|
|
|
"Scheduled cron job triggered: 30s-test\n\nInternal reminder prompt",
|
|
|
|
|
**{CRON_HISTORY_META: True},
|
|
|
|
|
)
|
|
|
|
|
session.add_message("assistant", "提醒已经到期。")
|
|
|
|
|
loop.sessions.save(session)
|
|
|
|
|
|
|
|
|
|
generated = await maybe_generate_webui_title(
|
|
|
|
|
sessions=loop.sessions,
|
|
|
|
|
session_key="websocket:cron-title",
|
|
|
|
|
provider=loop.provider,
|
|
|
|
|
model=loop.model,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert generated is False
|
|
|
|
|
assert WEBUI_TITLE_METADATA_KEY not in session.metadata
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.assert_not_awaited()
|
2026-06-12 18:17:28 +08:00
|
|
|
|
|
|
|
|
|
2026-08-25 13:58:57 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_generate_webui_title_projects_onto_chat_session_under_unified_routing(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(
|
2026-08-25 13:58:57 +08:00
|
|
|
return_value=LLMResponse(content='"查询临期 IP"', finish_reason="stop")
|
|
|
|
|
)
|
|
|
|
|
unified = loop.sessions.get_or_create(UNIFIED_SESSION_KEY)
|
|
|
|
|
unified.metadata[WEBUI_SESSION_METADATA_KEY] = True
|
|
|
|
|
unified.metadata[WEBUI_TITLE_METADATA_KEY] = "开启私聊Topic功能"
|
|
|
|
|
unified.add_message("user", "很早以前的问题")
|
|
|
|
|
unified.add_message("assistant", "很久以前的回答。")
|
|
|
|
|
unified.add_message("user", "帮我查一下临期IP有哪些")
|
|
|
|
|
unified.add_message("assistant", "以下是临期 IP 列表。")
|
|
|
|
|
loop.sessions.save(unified)
|
|
|
|
|
|
|
|
|
|
generated = await maybe_generate_webui_title_after_turn(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
chat_id="chat-projection",
|
|
|
|
|
metadata={WEBUI_SESSION_METADATA_KEY: True},
|
|
|
|
|
sessions=loop.sessions,
|
|
|
|
|
session_key=UNIFIED_SESSION_KEY,
|
|
|
|
|
provider=loop.provider,
|
|
|
|
|
model=loop.model,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert generated is True
|
|
|
|
|
chat = loop.sessions.get_or_create("websocket:chat-projection")
|
|
|
|
|
assert chat.metadata[WEBUI_TITLE_METADATA_KEY] == "查询临期 IP"
|
|
|
|
|
assert unified.metadata[WEBUI_TITLE_METADATA_KEY] == "开启私聊Topic功能"
|
2026-09-11 13:06:43 +08:00
|
|
|
prompt = loop.provider.chat_stream_with_retry.await_args.args[0][1]["content"]
|
2026-08-25 13:58:57 +08:00
|
|
|
assert "帮我查一下临期IP有哪些" in prompt
|
|
|
|
|
assert "很早以前的问题" not in prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_projected_title_generation_skips_existing_chat_title(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
unified = loop.sessions.get_or_create(UNIFIED_SESSION_KEY)
|
|
|
|
|
unified.metadata[WEBUI_SESSION_METADATA_KEY] = True
|
|
|
|
|
unified.add_message("user", "帮我查一下临期IP有哪些")
|
|
|
|
|
unified.add_message("assistant", "以下是临期 IP 列表。")
|
|
|
|
|
chat = loop.sessions.get_or_create("websocket:chat-existing")
|
|
|
|
|
chat.metadata[WEBUI_TITLE_METADATA_KEY] = "Existing title"
|
2026-10-16 17:28:47 +08:00
|
|
|
loop.sessions.save(chat)
|
2026-08-25 13:58:57 +08:00
|
|
|
loop.sessions.save(unified)
|
|
|
|
|
|
|
|
|
|
generated = await maybe_generate_webui_title_after_turn(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
chat_id="chat-existing",
|
|
|
|
|
metadata={WEBUI_SESSION_METADATA_KEY: True},
|
|
|
|
|
sessions=loop.sessions,
|
|
|
|
|
session_key=UNIFIED_SESSION_KEY,
|
|
|
|
|
provider=loop.provider,
|
|
|
|
|
model=loop.model,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert generated is False
|
|
|
|
|
assert chat.metadata[WEBUI_TITLE_METADATA_KEY] == "Existing title"
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry.assert_not_awaited()
|
2026-08-25 13:58:57 +08:00
|
|
|
|
|
|
|
|
|
2026-07-11 18:52:56 +08:00
|
|
|
def test_save_turn_keeps_multimodal_runtime_context_for_model_replay() -> None:
|
2026-03-03 05:13:17 +00:00
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:runtime-only")
|
2026-07-11 18:52:56 +08:00
|
|
|
block = RuntimeContextBlock(source="test", content="provider context")
|
2026-03-03 05:13:17 +00:00
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
2026-07-11 18:52:56 +08:00
|
|
|
[_runtime_message([], [block])],
|
2026-03-03 05:13:17 +00:00
|
|
|
skip=0,
|
|
|
|
|
)
|
2026-07-11 18:52:56 +08:00
|
|
|
assert session.messages[0]["content"] == [
|
|
|
|
|
{"type": "text", "text": "provider context"}
|
|
|
|
|
]
|
|
|
|
|
assert public_history_message(session.messages[0])["content"] == []
|
2026-03-03 05:13:17 +00:00
|
|
|
|
|
|
|
|
|
2026-09-02 18:05:54 +08:00
|
|
|
def test_save_turn_commits_summary_boundary_without_rewriting_raw_history() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:summary-checkpoint")
|
|
|
|
|
session.add_message("user", "inspect the project")
|
|
|
|
|
messages = [
|
|
|
|
|
{"role": "system", "content": "system"},
|
|
|
|
|
{"role": "user", "content": "inspect the project"},
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [{
|
|
|
|
|
"id": "call-1",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "inspect", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call-1",
|
|
|
|
|
"name": "inspect",
|
|
|
|
|
"content": "full current result",
|
|
|
|
|
},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
messages,
|
|
|
|
|
skip=2,
|
|
|
|
|
summary_checkpoint=SessionSummaryCheckpoint(
|
|
|
|
|
summary="Current working-memory checkpoint.",
|
|
|
|
|
transcript_boundary=2,
|
|
|
|
|
),
|
|
|
|
|
input_persisted_early=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [message["role"] for message in session.messages] == [
|
|
|
|
|
"user", "user", "assistant", "tool", "assistant",
|
|
|
|
|
]
|
|
|
|
|
assert session.messages[0]["content"] == "inspect the project"
|
|
|
|
|
assert session.messages[1]["content"] == SUMMARY_CONTINUATION_TEXT
|
|
|
|
|
assert session.messages[1]["_hidden_history"] is True
|
|
|
|
|
assert session.last_archived == 1
|
|
|
|
|
assert session.metadata["_last_summary"]["text"] == (
|
|
|
|
|
"Current working-memory checkpoint."
|
|
|
|
|
)
|
|
|
|
|
assert [message["content"] for message in session.get_history()] == [
|
|
|
|
|
"",
|
|
|
|
|
"full current result",
|
|
|
|
|
"done",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 18:52:56 +08:00
|
|
|
def test_save_turn_keeps_image_placeholder_and_runtime_context() -> None:
|
2026-03-03 05:13:17 +00:00
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:image")
|
2026-07-11 18:52:56 +08:00
|
|
|
block = RuntimeContextBlock(source="test", content="provider context")
|
2026-03-03 05:13:17 +00:00
|
|
|
|
2026-03-17 14:33:19 +00:00
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
2026-07-11 18:52:56 +08:00
|
|
|
[_runtime_message(
|
|
|
|
|
[
|
2026-03-17 14:33:19 +00:00
|
|
|
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}, "_meta": {"path": "/media/feishu/photo.jpg"}},
|
|
|
|
|
],
|
2026-07-11 18:52:56 +08:00
|
|
|
[block],
|
|
|
|
|
)],
|
2026-03-17 14:33:19 +00:00
|
|
|
skip=0,
|
|
|
|
|
)
|
2026-07-11 18:52:56 +08:00
|
|
|
assert session.messages[0]["content"] == [
|
|
|
|
|
{"type": "text", "text": "[image: /media/feishu/photo.jpg]"},
|
|
|
|
|
{"type": "text", "text": "provider context"},
|
|
|
|
|
]
|
|
|
|
|
assert public_history_message(session.messages[0])["content"] == [
|
|
|
|
|
{"type": "text", "text": "[image: /media/feishu/photo.jpg]"}
|
|
|
|
|
]
|
2026-03-17 14:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_turn_keeps_image_placeholder_without_meta() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:image-no-meta")
|
2026-07-11 18:52:56 +08:00
|
|
|
block = RuntimeContextBlock(source="test", content="provider context")
|
2026-03-17 14:33:19 +00:00
|
|
|
|
2026-03-03 05:13:17 +00:00
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
2026-07-11 18:52:56 +08:00
|
|
|
[_runtime_message(
|
|
|
|
|
[
|
2026-03-03 05:13:17 +00:00
|
|
|
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}},
|
|
|
|
|
],
|
2026-07-11 18:52:56 +08:00
|
|
|
[block],
|
|
|
|
|
)],
|
2026-03-03 05:13:17 +00:00
|
|
|
skip=0,
|
|
|
|
|
)
|
2026-07-11 18:52:56 +08:00
|
|
|
assert session.messages[0]["content"] == [
|
|
|
|
|
{"type": "text", "text": "[image]"},
|
|
|
|
|
{"type": "text", "text": "provider context"},
|
|
|
|
|
]
|
2026-03-12 02:50:28 +00:00
|
|
|
|
|
|
|
|
|
2026-09-08 14:10:55 +08:00
|
|
|
def test_save_turn_redacts_tool_image_without_truncating_text() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:tool-image")
|
|
|
|
|
text = "start-" + ("x" * 20_000) + "-end"
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [{
|
|
|
|
|
"id": "call_image",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_image",
|
|
|
|
|
"name": "read_file",
|
|
|
|
|
"content": [
|
|
|
|
|
{
|
|
|
|
|
"type": "image_url",
|
|
|
|
|
"image_url": {"url": "data:image/png;base64,abc"},
|
|
|
|
|
"_meta": {"path": "media/photo.png"},
|
|
|
|
|
},
|
|
|
|
|
{"type": "text", "text": text},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert session.messages[1]["content"] == [
|
|
|
|
|
{"type": "text", "text": "[image: media/photo.png]"},
|
|
|
|
|
{"type": "text", "text": text},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 18:52:56 +08:00
|
|
|
def test_save_turn_persists_runtime_context_and_public_view_hides_it() -> None:
|
2026-05-15 17:51:36 +08:00
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:suffix-strip")
|
2026-07-11 18:52:56 +08:00
|
|
|
block = RuntimeContextBlock(source="goal", content="internal goal guidance")
|
2026-05-15 17:51:36 +08:00
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
2026-07-11 18:52:56 +08:00
|
|
|
[_runtime_message("hello world", [block])],
|
2026-05-15 17:51:36 +08:00
|
|
|
skip=0,
|
|
|
|
|
)
|
2026-07-11 18:52:56 +08:00
|
|
|
assert session.messages[0]["content"] == "hello world\n\ninternal goal guidance"
|
|
|
|
|
assert session.messages[0][RUNTIME_CONTEXT_HISTORY_META]["sources"] == ["goal"]
|
|
|
|
|
assert public_history_message(session.messages[0])["content"] == "hello world"
|
2026-05-15 17:51:36 +08:00
|
|
|
|
|
|
|
|
|
2026-07-11 16:51:25 +08:00
|
|
|
def test_build_and_save_preserves_user_text_containing_goal_guidance_tag(tmp_path: Path) -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:user-guidance-literal")
|
|
|
|
|
user_text = (
|
|
|
|
|
"Keep this prefix\n"
|
2026-07-11 18:52:56 +08:00
|
|
|
"[Goal Runtime Guidance — host instructions]\n"
|
2026-07-11 16:51:25 +08:00
|
|
|
"This label and everything after it are user-authored."
|
|
|
|
|
)
|
|
|
|
|
messages = ContextBuilder(tmp_path).build_messages(
|
|
|
|
|
[],
|
|
|
|
|
user_text,
|
|
|
|
|
channel="cli",
|
|
|
|
|
)
|
2026-07-11 18:52:56 +08:00
|
|
|
assert "_meta" not in messages[-1]
|
2026-07-11 16:51:25 +08:00
|
|
|
|
|
|
|
|
loop._save_turn(session, messages, skip=1)
|
|
|
|
|
|
|
|
|
|
assert session.messages[0]["content"] == user_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_and_save_preserves_multimodal_user_block_starting_with_runtime_tag(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:user-runtime-literal-block")
|
|
|
|
|
image = tmp_path / "user-tag.png"
|
|
|
|
|
image.write_bytes(_PNG_1X1)
|
|
|
|
|
user_text = (
|
2026-09-05 22:09:12 +08:00
|
|
|
f"{RUNTIME_CONTEXT_TAG}\n"
|
2026-07-11 16:51:25 +08:00
|
|
|
"This entire block is user-authored and must remain in history."
|
|
|
|
|
)
|
|
|
|
|
messages = ContextBuilder(tmp_path).build_messages(
|
|
|
|
|
[],
|
|
|
|
|
user_text,
|
|
|
|
|
media=[str(image)],
|
|
|
|
|
channel="cli",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._save_turn(session, messages, skip=1)
|
|
|
|
|
|
|
|
|
|
assert {"type": "text", "text": user_text} in session.messages[0]["content"]
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 18:52:56 +08:00
|
|
|
def test_save_turn_keeps_string_when_only_runtime_context() -> None:
|
2026-05-15 17:51:36 +08:00
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:suffix-only")
|
2026-07-11 18:52:56 +08:00
|
|
|
block = RuntimeContextBlock(source="test", content="provider context")
|
2026-05-15 17:51:36 +08:00
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
2026-07-11 18:52:56 +08:00
|
|
|
[_runtime_message("", [block])],
|
2026-05-15 17:51:36 +08:00
|
|
|
skip=0,
|
|
|
|
|
)
|
2026-07-11 18:52:56 +08:00
|
|
|
assert session.messages[0]["content"] == "provider context"
|
|
|
|
|
assert public_history_message(session.messages[0])["content"] == ""
|
2026-05-15 17:51:36 +08:00
|
|
|
|
|
|
|
|
|
2026-09-08 14:10:55 +08:00
|
|
|
def test_save_turn_keeps_full_processed_tool_result() -> None:
|
2026-03-12 02:50:28 +00:00
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:tool-result")
|
2026-09-08 14:10:55 +08:00
|
|
|
content = "start-" + ("x" * 20_000) + "-end"
|
2026-03-12 02:50:28 +00:00
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
2026-06-12 09:11:02 +08:00
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [{
|
|
|
|
|
"id": "call_1",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_1", "name": "read_file", "content": content},
|
|
|
|
|
],
|
2026-03-12 02:50:28 +00:00
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
2026-06-12 09:11:02 +08:00
|
|
|
assert session.messages[1]["content"] == content
|
2026-04-01 19:12:49 +00:00
|
|
|
|
|
|
|
|
|
2026-05-16 01:14:11 +08:00
|
|
|
def test_save_turn_stamps_latency_on_last_assistant() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:latency")
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[
|
|
|
|
|
{"role": "assistant", "content": "hello", "tool_calls": [{"id": "c1"}]},
|
|
|
|
|
{"role": "assistant", "content": "final answer"},
|
|
|
|
|
],
|
|
|
|
|
skip=0,
|
|
|
|
|
turn_latency_ms=12345,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert session.messages[-1]["role"] == "assistant"
|
|
|
|
|
assert session.messages[-1]["content"] == "final answer"
|
|
|
|
|
assert session.messages[-1]["latency_ms"] == 12345
|
|
|
|
|
|
|
|
|
|
|
2026-04-01 19:12:49 +00:00
|
|
|
def test_restore_runtime_checkpoint_rehydrates_completed_and_pending_tools() -> None:
|
|
|
|
|
session = Session(
|
|
|
|
|
key="test:checkpoint",
|
2026-07-30 22:39:43 +08:00
|
|
|
provider_state=_provider_state(),
|
2026-04-01 19:12:49 +00:00
|
|
|
metadata={
|
2026-08-26 16:23:19 +08:00
|
|
|
RUNTIME_CHECKPOINT_KEY: {
|
2026-04-01 19:12:49 +00:00
|
|
|
"assistant_message": {
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "working",
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_done",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
"completed_tool_results": [
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_done",
|
|
|
|
|
"name": "read_file",
|
|
|
|
|
"content": "ok",
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"pending_tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-26 16:23:19 +08:00
|
|
|
restored = restore_runtime_checkpoint(session)
|
2026-04-01 19:12:49 +00:00
|
|
|
|
|
|
|
|
assert restored is True
|
2026-08-26 16:23:19 +08:00
|
|
|
assert session.metadata.get(RUNTIME_CHECKPOINT_KEY) is None
|
2026-04-01 19:12:49 +00:00
|
|
|
assert session.messages[0]["role"] == "assistant"
|
|
|
|
|
assert session.messages[1]["tool_call_id"] == "call_done"
|
|
|
|
|
assert session.messages[2]["tool_call_id"] == "call_pending"
|
2026-11-02 17:55:14 +08:00
|
|
|
assert "outcome is unknown" in session.messages[2]["content"]
|
|
|
|
|
assert "Check its effects before retrying" in session.messages[2]["content"]
|
|
|
|
|
assert session.messages[2]["_recovery_interrupted"] is True
|
2026-07-30 22:39:43 +08:00
|
|
|
assert session.provider_state is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_final_response_checkpoint_preserves_matching_provider_state() -> None:
|
|
|
|
|
state = _provider_state()
|
|
|
|
|
session = Session(
|
|
|
|
|
key="test:final-checkpoint",
|
|
|
|
|
provider_state=state,
|
|
|
|
|
metadata={
|
2026-08-26 16:23:19 +08:00
|
|
|
RUNTIME_CHECKPOINT_KEY: {
|
2026-07-30 22:39:43 +08:00
|
|
|
"phase": "final_response",
|
2026-08-26 16:23:19 +08:00
|
|
|
PROVIDER_STATE_CHECKPOINT_VERSION_KEY: PROVIDER_STATE_CHECKPOINT_VERSION,
|
2026-07-30 22:39:43 +08:00
|
|
|
"assistant_message": {
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "finished",
|
|
|
|
|
},
|
|
|
|
|
"completed_tool_results": [],
|
|
|
|
|
"pending_tool_calls": [],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-26 16:23:19 +08:00
|
|
|
restored = restore_runtime_checkpoint(session)
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
assert restored is True
|
|
|
|
|
assert session.messages[-1]["content"] == "finished"
|
|
|
|
|
assert session.provider_state is state
|
2026-08-26 16:23:19 +08:00
|
|
|
assert session.metadata.get(RUNTIME_CHECKPOINT_KEY) is None
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_legacy_final_checkpoint_discards_unproven_provider_state() -> None:
|
|
|
|
|
session = Session(
|
|
|
|
|
key="test:legacy-final-checkpoint",
|
|
|
|
|
provider_state=_provider_state(),
|
|
|
|
|
metadata={
|
2026-08-26 16:23:19 +08:00
|
|
|
RUNTIME_CHECKPOINT_KEY: {
|
2026-07-30 22:39:43 +08:00
|
|
|
"phase": "final_response",
|
|
|
|
|
"assistant_message": {
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "finished",
|
|
|
|
|
},
|
|
|
|
|
"completed_tool_results": [],
|
|
|
|
|
"pending_tool_calls": [],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-26 16:23:19 +08:00
|
|
|
restored = restore_runtime_checkpoint(session)
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
assert restored is True
|
|
|
|
|
assert session.messages[-1]["content"] == "finished"
|
|
|
|
|
assert session.provider_state is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_completed_tools_checkpoint_preserves_matching_provider_state() -> None:
|
|
|
|
|
tool_result = {
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_done",
|
|
|
|
|
"name": "read_file",
|
|
|
|
|
"content": "compacted result",
|
|
|
|
|
}
|
|
|
|
|
state = _provider_state().with_pending_messages([tool_result])
|
|
|
|
|
session = Session(
|
|
|
|
|
key="test:completed-tools-checkpoint",
|
|
|
|
|
provider_state=state,
|
|
|
|
|
metadata={
|
2026-08-26 16:23:19 +08:00
|
|
|
RUNTIME_CHECKPOINT_KEY: {
|
2026-07-30 22:39:43 +08:00
|
|
|
"phase": "tools_completed",
|
2026-08-26 16:23:19 +08:00
|
|
|
PROVIDER_STATE_CHECKPOINT_VERSION_KEY: PROVIDER_STATE_CHECKPOINT_VERSION,
|
2026-07-30 22:39:43 +08:00
|
|
|
"assistant_message": {
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": None,
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_done",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
"completed_tool_results": [tool_result],
|
|
|
|
|
"pending_tool_calls": [],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-26 16:23:19 +08:00
|
|
|
restored = restore_runtime_checkpoint(session)
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
assert restored is True
|
|
|
|
|
assert session.messages[-1]["content"] == "compacted result"
|
|
|
|
|
assert session.provider_state is state
|
2026-04-01 19:12:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_runtime_checkpoint_dedupes_overlapping_tail() -> None:
|
|
|
|
|
session = Session(
|
|
|
|
|
key="test:checkpoint-overlap",
|
|
|
|
|
messages=[
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "working",
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_done",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_done",
|
|
|
|
|
"name": "read_file",
|
|
|
|
|
"content": "ok",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
metadata={
|
2026-08-26 16:23:19 +08:00
|
|
|
RUNTIME_CHECKPOINT_KEY: {
|
2026-04-01 19:12:49 +00:00
|
|
|
"assistant_message": {
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "working",
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_done",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
"completed_tool_results": [
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_done",
|
|
|
|
|
"name": "read_file",
|
|
|
|
|
"content": "ok",
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"pending_tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-26 16:23:19 +08:00
|
|
|
restored = restore_runtime_checkpoint(session)
|
2026-04-01 19:12:49 +00:00
|
|
|
|
|
|
|
|
assert restored is True
|
2026-08-26 16:23:19 +08:00
|
|
|
assert session.metadata.get(RUNTIME_CHECKPOINT_KEY) is None
|
2026-04-01 19:12:49 +00:00
|
|
|
assert len(session.messages) == 3
|
|
|
|
|
assert session.messages[0]["role"] == "assistant"
|
|
|
|
|
assert session.messages[1]["tool_call_id"] == "call_done"
|
|
|
|
|
assert session.messages[2]["tool_call_id"] == "call_pending"
|
2026-04-13 02:09:40 +00:00
|
|
|
|
|
|
|
|
|
2026-07-30 22:39:43 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_runtime_checkpoint_keeps_provider_state_out_of_public_metadata(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
state = ProviderConversationState(
|
|
|
|
|
kind="openai_responses",
|
|
|
|
|
provider="openai:test",
|
|
|
|
|
model="test-model",
|
|
|
|
|
version=1,
|
|
|
|
|
payload={
|
|
|
|
|
"items": [
|
|
|
|
|
{
|
|
|
|
|
"type": "reasoning",
|
|
|
|
|
"encrypted_content": "private-checkpoint-blob",
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
loop.provider.can_resume_conversation_state.return_value = True
|
2026-09-11 13:06:43 +08:00
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(
|
2026-07-30 22:39:43 +08:00
|
|
|
return_value=LLMResponse(content="done", provider_state=state)
|
|
|
|
|
)
|
2026-10-16 17:28:47 +08:00
|
|
|
session = await loop.sessions.state.get("cli:private-checkpoint")
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
await loop._run_agent_loop(
|
2026-08-31 00:06:15 +08:00
|
|
|
TranscriptInput(
|
|
|
|
|
history=[
|
|
|
|
|
{"role": "system", "content": "system"},
|
|
|
|
|
{"role": "user", "content": "question"},
|
|
|
|
|
],
|
|
|
|
|
current_message=None,
|
|
|
|
|
),
|
2026-07-30 22:39:43 +08:00
|
|
|
runtime=loop.llm_runtime(),
|
|
|
|
|
session=session,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert session.provider_state is not None
|
|
|
|
|
checkpoint = session.metadata[AgentLoop._RUNTIME_CHECKPOINT_KEY]
|
|
|
|
|
assert "provider_state" not in checkpoint
|
|
|
|
|
assert checkpoint[AgentLoop._PROVIDER_STATE_CHECKPOINT_VERSION_KEY] == (
|
|
|
|
|
AgentLoop._PROVIDER_STATE_CHECKPOINT_VERSION
|
|
|
|
|
)
|
|
|
|
|
assert "private-checkpoint-blob" not in json.dumps(session.metadata)
|
|
|
|
|
|
|
|
|
|
public_payload = loop.sessions.read_session_file(session.key)
|
|
|
|
|
assert public_payload is not None
|
|
|
|
|
assert "private-checkpoint-blob" not in json.dumps(public_payload)
|
2026-10-16 17:28:47 +08:00
|
|
|
stored = loop.sessions.read_session_snapshot(session.key)
|
|
|
|
|
assert stored is not None and stored.provider_state is not None
|
|
|
|
|
assert stored.provider_state.payload == state.payload
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
2026-04-13 02:09:40 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_process_message_persists_user_message_before_turn_completes(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._run_agent_loop = AsyncMock(side_effect=RuntimeError("boom")) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
msg = InboundMessage(channel="feishu", sender_id="u1", chat_id="c1", content="persist me")
|
|
|
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("feishu:c1")
|
|
|
|
|
persisted = loop.sessions.get_or_create("feishu:c1")
|
|
|
|
|
assert [m["role"] for m in persisted.messages] == ["user"]
|
2026-10-09 14:04:18 +08:00
|
|
|
assert public_history_message(persisted.messages[0])["content"] == "persist me"
|
2026-04-13 02:21:39 +00:00
|
|
|
assert persisted.metadata.get(AgentLoop._PENDING_USER_TURN_KEY) is True
|
2026-04-13 02:09:40 +00:00
|
|
|
assert persisted.updated_at >= persisted.created_at
|
|
|
|
|
|
|
|
|
|
|
2026-07-30 22:39:43 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_subagent_followup_stages_provider_state_before_turn_runs(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._run_agent_loop = AsyncMock(side_effect=RuntimeError("boom")) # type: ignore[method-assign]
|
|
|
|
|
loop.provider.can_resume_conversation_state.return_value = True
|
|
|
|
|
session = loop.sessions.get_or_create("cli:subagent-crash")
|
|
|
|
|
session.provider_state = _provider_state()
|
|
|
|
|
loop.sessions.save(session)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-1", session.key)
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:subagent-crash",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("cli:subagent-crash")
|
|
|
|
|
persisted = loop.sessions.get_or_create("cli:subagent-crash")
|
|
|
|
|
assert persisted.messages[-1]["content"] == "subagent result"
|
|
|
|
|
assert persisted.provider_state is not None
|
|
|
|
|
assert persisted.provider_state.pending_messages[-1]["role"] == "user"
|
|
|
|
|
assert persisted.provider_state.pending_messages[-1]["content"] == "subagent result"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_subagent_redelivery_does_not_duplicate_staged_provider_input(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.provider.can_resume_conversation_state.return_value = True
|
2026-08-31 00:06:15 +08:00
|
|
|
build_system_prompt = loop.context.build_system_prompt
|
|
|
|
|
loop.context.build_system_prompt = MagicMock( # type: ignore[method-assign]
|
2026-07-30 22:39:43 +08:00
|
|
|
side_effect=RuntimeError("prompt boom"),
|
|
|
|
|
)
|
|
|
|
|
session = loop.sessions.get_or_create("cli:subagent-redelivery")
|
|
|
|
|
session.provider_state = _provider_state()
|
|
|
|
|
loop.sessions.save(session)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-1", session.key)
|
2026-07-30 22:39:43 +08:00
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:subagent-redelivery",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError, match="prompt boom"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("cli:subagent-redelivery")
|
|
|
|
|
persisted = loop.sessions.get_or_create("cli:subagent-redelivery")
|
2026-10-21 14:47:55 +08:00
|
|
|
assert persisted.messages[-1]["content"] == "subagent result"
|
2026-07-30 22:39:43 +08:00
|
|
|
assert persisted.provider_state is not None
|
2026-10-21 14:47:55 +08:00
|
|
|
assert persisted.provider_state.pending_messages[-1]["content"] == (
|
|
|
|
|
"subagent result"
|
|
|
|
|
)
|
2026-07-30 22:39:43 +08:00
|
|
|
assert [
|
|
|
|
|
message.get("content")
|
|
|
|
|
for message in persisted.provider_state.pending_messages
|
|
|
|
|
].count("subagent result") == 1
|
2026-08-31 00:06:15 +08:00
|
|
|
loop.context.build_system_prompt = build_system_prompt # type: ignore[method-assign]
|
2026-07-30 22:39:43 +08:00
|
|
|
loop._run_agent_loop = AsyncMock( # type: ignore[method-assign]
|
|
|
|
|
side_effect=RuntimeError("provider boom"),
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(RuntimeError, match="provider boom"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
provider_state = loop._run_agent_loop.await_args.kwargs["provider_state"]
|
|
|
|
|
assert provider_state is not None
|
|
|
|
|
pending_results = [
|
|
|
|
|
message
|
|
|
|
|
for message in provider_state.pending_messages
|
|
|
|
|
if message.get("content") == "subagent result"
|
|
|
|
|
]
|
|
|
|
|
assert len(pending_results) == 1
|
|
|
|
|
assert LLMProvider._sanitize_empty_content(pending_results) == [
|
|
|
|
|
{"role": "user", "content": "subagent result"},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_subagent_followup_clears_state_before_compatibility_failure(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.provider.can_resume_conversation_state.side_effect = RuntimeError(
|
|
|
|
|
"compatibility boom"
|
|
|
|
|
)
|
|
|
|
|
session = loop.sessions.get_or_create("cli:subagent-compat-crash")
|
|
|
|
|
session.provider_state = _provider_state()
|
|
|
|
|
loop.sessions.save(session)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-1", session.key)
|
2026-07-30 22:39:43 +08:00
|
|
|
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:subagent-compat-crash",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(RuntimeError, match="compatibility boom"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("cli:subagent-compat-crash")
|
|
|
|
|
persisted = loop.sessions.get_or_create("cli:subagent-compat-crash")
|
|
|
|
|
assert persisted.messages[-1]["content"] == "subagent result"
|
|
|
|
|
assert persisted.provider_state is None
|
|
|
|
|
|
|
|
|
|
|
2026-07-14 21:52:26 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_process_message_persists_unified_session_delivery_route(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._unified_session = True
|
|
|
|
|
loop._run_agent_loop = AsyncMock(side_effect=RuntimeError("boom")) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="feishu",
|
|
|
|
|
sender_id="u1",
|
|
|
|
|
chat_id="oc_123",
|
|
|
|
|
content="persist my route",
|
|
|
|
|
session_key_override=UNIFIED_SESSION_KEY,
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate(UNIFIED_SESSION_KEY)
|
|
|
|
|
persisted = loop.sessions.get_or_create(UNIFIED_SESSION_KEY)
|
|
|
|
|
assert persisted.metadata[LAST_CHANNEL_METADATA_KEY] == "feishu:oc_123"
|
2026-09-05 20:21:54 +08:00
|
|
|
assert persisted.metadata["_compaction_route"] == {
|
|
|
|
|
"channel": "feishu", "chat_id": "oc_123", "metadata": {},
|
|
|
|
|
}
|
2026-07-14 21:52:26 +08:00
|
|
|
|
|
|
|
|
|
2026-07-26 23:59:00 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
("msg", "is_user_turn"),
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="cli",
|
|
|
|
|
sender_id="u1",
|
|
|
|
|
chat_id="direct",
|
|
|
|
|
content="cli input",
|
|
|
|
|
),
|
|
|
|
|
True,
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="system",
|
|
|
|
|
chat_id="discord:automation",
|
|
|
|
|
content="system event",
|
|
|
|
|
),
|
|
|
|
|
False,
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="discord",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="subagent-result",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
),
|
|
|
|
|
True,
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="discord",
|
|
|
|
|
sender_id="u1",
|
|
|
|
|
chat_id="automation",
|
|
|
|
|
content="scheduled turn",
|
|
|
|
|
metadata={CRON_TRIGGER_META: {"job_id": "job-1"}},
|
|
|
|
|
),
|
|
|
|
|
True,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_unified_session_route_ignores_non_user_destinations(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
msg: InboundMessage,
|
|
|
|
|
is_user_turn: bool,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._unified_session = True
|
|
|
|
|
session = loop.sessions.get_or_create(UNIFIED_SESSION_KEY)
|
|
|
|
|
session.metadata[LAST_CHANNEL_METADATA_KEY] = "telegram:existing"
|
|
|
|
|
|
2026-09-05 20:21:54 +08:00
|
|
|
loop._remember_session_route(
|
|
|
|
|
session, msg, is_user_turn=is_user_turn,
|
|
|
|
|
delivery=loop.turn_delivery_factory.unrouted(msg, session.key),
|
|
|
|
|
)
|
2026-07-26 23:59:00 +08:00
|
|
|
|
|
|
|
|
assert session.metadata[LAST_CHANNEL_METADATA_KEY] == "telegram:existing"
|
|
|
|
|
|
|
|
|
|
|
2026-07-28 13:33:06 +08:00
|
|
|
# 1x1 PNG used by the media-persistence tests. Attachment preparation filters
|
|
|
|
|
# ``msg.media`` down to paths that magic-byte-sniff as images, so the test
|
|
|
|
|
# fixture needs real bytes on disk (not just placeholder paths).
|
2026-04-21 10:29:47 +00:00
|
|
|
_PNG_1X1 = (
|
|
|
|
|
b"\x89PNG\r\n\x1a\n"
|
|
|
|
|
b"\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
|
|
|
|
|
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"
|
|
|
|
|
b"\x00\x00\x00\nIDATx\x9cc\x00\x00\x00\x02\x00\x01"
|
|
|
|
|
b"\x00\x00\x00\x00IEND\xaeB`\x82"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_process_message_persists_media_paths_on_user_turn(tmp_path: Path) -> None:
|
|
|
|
|
"""User turns that attach images must record the media paths alongside
|
|
|
|
|
the text so the webui can rehydrate previews on session replay.
|
|
|
|
|
|
2026-08-07 14:48:43 +08:00
|
|
|
The WebUI transcript replay can use these paths to restore attachment
|
|
|
|
|
previews when it backfills from canonical session history.
|
2026-04-21 10:29:47 +00:00
|
|
|
"""
|
|
|
|
|
img_a = tmp_path / "uuid-1.png"
|
|
|
|
|
img_a.write_bytes(_PNG_1X1)
|
|
|
|
|
img_b = tmp_path / "uuid-2.png"
|
|
|
|
|
img_b.write_bytes(_PNG_1X1)
|
|
|
|
|
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._run_agent_loop = AsyncMock(side_effect=RuntimeError("interrupt")) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="u1",
|
|
|
|
|
chat_id="c-media",
|
|
|
|
|
content="look",
|
|
|
|
|
media=[str(img_a), str(img_b)],
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(RuntimeError, match="interrupt"):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("websocket:c-media")
|
|
|
|
|
persisted = loop.sessions.get_or_create("websocket:c-media")
|
|
|
|
|
assert [m["role"] for m in persisted.messages] == ["user"]
|
2026-10-09 14:04:18 +08:00
|
|
|
assert public_history_message(persisted.messages[0])["content"] == "look"
|
2026-04-21 10:29:47 +00:00
|
|
|
assert persisted.messages[0]["media"] == [str(img_a), str(img_b)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_process_message_persists_media_only_turn_without_text(tmp_path: Path) -> None:
|
|
|
|
|
"""A turn with images but no text still persists (previously silent-dropped).
|
|
|
|
|
|
|
|
|
|
The old early-persist gate skipped messages without text, leaving pure
|
|
|
|
|
image turns un-checkpointed. They now materialise as an empty-content
|
|
|
|
|
user row with ``media`` attached.
|
|
|
|
|
"""
|
|
|
|
|
img = tmp_path / "only.png"
|
|
|
|
|
img.write_bytes(_PNG_1X1)
|
|
|
|
|
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._run_agent_loop = AsyncMock(side_effect=RuntimeError("boom")) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
sender_id="u1",
|
|
|
|
|
chat_id="c-images-only",
|
|
|
|
|
content="",
|
|
|
|
|
media=[str(img)],
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
await loop._process_message(msg)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("websocket:c-images-only")
|
|
|
|
|
persisted = loop.sessions.get_or_create("websocket:c-images-only")
|
|
|
|
|
assert len(persisted.messages) == 1
|
|
|
|
|
assert persisted.messages[0]["role"] == "user"
|
2026-10-09 14:04:18 +08:00
|
|
|
assert public_history_message(persisted.messages[0])["content"] == ""
|
2026-04-21 10:29:47 +00:00
|
|
|
assert persisted.messages[0]["media"] == [str(img)]
|
|
|
|
|
|
|
|
|
|
|
2026-04-13 02:09:40 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_process_message_does_not_duplicate_early_persisted_user_message(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-08-26 15:18:53 +08:00
|
|
|
loop._run_agent_loop = AsyncMock(return_value=_agent_run_result(
|
2026-04-13 02:09:40 +00:00
|
|
|
"done",
|
|
|
|
|
[
|
|
|
|
|
{"role": "system", "content": "system"},
|
|
|
|
|
{"role": "user", "content": "hello"},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-13 02:09:40 +00:00
|
|
|
)) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
result = await loop._process_message(
|
|
|
|
|
InboundMessage(channel="feishu", sender_id="u1", chat_id="c2", content="hello")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert result.content == "done"
|
|
|
|
|
session = loop.sessions.get_or_create("feishu:c2")
|
|
|
|
|
assert [
|
|
|
|
|
{k: v for k, v in m.items() if k in {"role", "content"}}
|
2026-10-09 14:04:18 +08:00
|
|
|
for m in map(public_history_message, session.messages)
|
2026-04-13 02:09:40 +00:00
|
|
|
] == [
|
|
|
|
|
{"role": "user", "content": "hello"},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
]
|
2026-04-13 02:21:39 +00:00
|
|
|
assert AgentLoop._PENDING_USER_TURN_KEY not in session.metadata
|
|
|
|
|
|
|
|
|
|
|
2026-06-01 00:56:39 +08:00
|
|
|
@pytest.mark.asyncio
|
2026-11-03 22:47:00 +08:00
|
|
|
async def test_explicit_recovery_continues_without_fake_user_history(tmp_path: Path) -> None:
|
2026-06-01 00:56:39 +08:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-11-03 22:47:00 +08:00
|
|
|
session = loop.sessions.get_or_create("feishu:recovery")
|
|
|
|
|
session.add_message("user", "Inspect the migration")
|
|
|
|
|
session.add_message("assistant", "Evidence collected before interruption.")
|
2026-06-01 00:56:39 +08:00
|
|
|
loop.sessions.save(session)
|
2026-11-03 22:47:00 +08:00
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="done"))
|
2026-10-24 02:41:23 +08:00
|
|
|
try:
|
2026-11-03 22:47:00 +08:00
|
|
|
result = await loop._process_message(InboundMessage(
|
|
|
|
|
channel="feishu", sender_id="system:recovery", chat_id="recovery",
|
|
|
|
|
content="Continue the interrupted request from saved context.",
|
|
|
|
|
metadata={INTERNAL_CONTINUATION_META: True},
|
|
|
|
|
))
|
|
|
|
|
assert result is not None and result.content == "done"
|
|
|
|
|
saved = await loop.sessions.state.read(session.key)
|
|
|
|
|
assert [
|
|
|
|
|
{k: v for k, v in row.items() if k in {"role", "content"}}
|
|
|
|
|
for row in map(public_history_message, saved.messages)
|
|
|
|
|
] == [
|
|
|
|
|
{"role": "user", "content": "Inspect the migration"},
|
|
|
|
|
{"role": "assistant", "content": "Evidence collected before interruption."},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
]
|
|
|
|
|
prompt = loop.provider.chat_stream_with_retry.await_args.kwargs["messages"]
|
|
|
|
|
assert "Continue the interrupted request from saved context." in str(prompt)
|
|
|
|
|
finally:
|
|
|
|
|
await loop.aclose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_same_run_goal_preserves_streaming_route_metadata(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.max_iterations = 1
|
|
|
|
|
session = await loop.sessions.state.get("feishu:c-stream")
|
|
|
|
|
await loop.sessions.state.update_metadata(session.key, {GOAL_STATE_KEY: {
|
|
|
|
|
"status": "active", "objective": "Finish the streamed long goal.",
|
|
|
|
|
}})
|
|
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(side_effect=[
|
|
|
|
|
LLMResponse(content=None, tool_calls=[ToolCallRequest(
|
|
|
|
|
id="list", name="list_dir", arguments={"path": "."},
|
|
|
|
|
)]),
|
|
|
|
|
LLMResponse(content=None, tool_calls=[ToolCallRequest(
|
|
|
|
|
id="complete", name="update_goal", arguments={"action": "complete", "recap": "done"},
|
|
|
|
|
)]),
|
|
|
|
|
LLMResponse(content="done"),
|
|
|
|
|
])
|
|
|
|
|
loop.runner.run = AsyncMock(wraps=loop.runner.run)
|
|
|
|
|
try:
|
|
|
|
|
await run_session(loop, InboundMessage(
|
|
|
|
|
channel="feishu", sender_id="u1", chat_id="c-stream", content="start the goal",
|
|
|
|
|
metadata={"_wants_stream": True, "message_id": "om_001", "origin_message_id": "root_001"},
|
|
|
|
|
))
|
|
|
|
|
outbound = []
|
|
|
|
|
while loop.bus.outbound_size:
|
|
|
|
|
outbound.append(await loop.bus.consume_outbound())
|
|
|
|
|
deltas = [m for m in outbound if isinstance(m.event, StreamDeltaEvent)]
|
|
|
|
|
ends = [m for m in outbound if isinstance(m.event, StreamEndEvent)]
|
|
|
|
|
streamed_markers = [m for m in outbound if isinstance(m.event, StreamedResponseEvent)]
|
|
|
|
|
assert [m.content for m in deltas] == ["done"]
|
|
|
|
|
assert [item.event.resuming for item in ends] == [True, True, False]
|
|
|
|
|
assert all(item.metadata["message_id"] == "om_001" for item in ends)
|
|
|
|
|
assert all(item.metadata["origin_message_id"] == "root_001" for item in ends)
|
|
|
|
|
assert all(isinstance(item.event.stream_id, str) for item in ends)
|
|
|
|
|
assert streamed_markers and streamed_markers[-1].content == "done"
|
|
|
|
|
assert loop.bus.inbound_size == 0
|
|
|
|
|
assert loop.provider.chat_stream_with_retry.await_count == 3
|
|
|
|
|
loop.runner.run.assert_awaited_once()
|
|
|
|
|
finally:
|
|
|
|
|
await loop.aclose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_stop_during_committed_goal_checkpoint_settles_queued_command(
|
|
|
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
):
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.provider.aclose = AsyncMock()
|
|
|
|
|
loop.consolidator.compact_idle_session = AsyncMock()
|
|
|
|
|
loop.max_iterations = 1
|
|
|
|
|
gate = asyncio.Semaphore(1)
|
|
|
|
|
loop._concurrency_gate = gate
|
|
|
|
|
session = loop.sessions.get_or_create("websocket:committed-checkpoint")
|
|
|
|
|
session.metadata[GOAL_STATE_KEY] = {"status": "active", "objective": "Finish the goal."}
|
|
|
|
|
loop.sessions.save(session)
|
|
|
|
|
calls = 0
|
2026-11-06 20:48:57 +08:00
|
|
|
model_progress: asyncio.Queue[int] = asyncio.Queue()
|
2026-11-03 22:47:00 +08:00
|
|
|
|
|
|
|
|
async def respond(*args, **kwargs):
|
|
|
|
|
nonlocal calls
|
|
|
|
|
if args and "You write short, neutral chat titles" in str(args[0]):
|
|
|
|
|
return LLMResponse(content="Goal checkpoint")
|
|
|
|
|
calls += 1
|
2026-11-06 20:48:57 +08:00
|
|
|
model_progress.put_nowait(calls)
|
2026-11-03 22:47:00 +08:00
|
|
|
if calls == 3:
|
|
|
|
|
await loop._dispatch_inbound(InboundMessage(
|
|
|
|
|
channel="websocket", sender_id="user", chat_id="committed-checkpoint",
|
|
|
|
|
content="/compact", metadata={"webui": True, "message_id": "queued-compact"},
|
|
|
|
|
))
|
|
|
|
|
if calls <= 3:
|
|
|
|
|
return LLMResponse(content=None, tool_calls=[ToolCallRequest(
|
|
|
|
|
id=f"list-{calls}", name="list_dir", arguments={"path": "."},
|
|
|
|
|
)])
|
|
|
|
|
if calls == 4:
|
|
|
|
|
assert "list-1" in str(kwargs["messages"]) and "list-3" in str(kwargs["messages"])
|
|
|
|
|
return LLMResponse(content=None, tool_calls=[ToolCallRequest(
|
|
|
|
|
id="cancel", name="update_goal", arguments={"action": "cancel", "recap": "Stopped work."},
|
|
|
|
|
)])
|
|
|
|
|
assert calls == 5
|
|
|
|
|
return LLMResponse(content="handled later")
|
|
|
|
|
|
|
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(side_effect=respond)
|
|
|
|
|
loop.runner.run = AsyncMock(wraps=loop.runner.run)
|
|
|
|
|
committed, release = threading.Event(), threading.Event()
|
2026-11-06 20:48:57 +08:00
|
|
|
checkpoint_ready = asyncio.Event()
|
|
|
|
|
event_loop = asyncio.get_running_loop()
|
2026-11-03 22:47:00 +08:00
|
|
|
publish = loop.sessions.state._publish
|
|
|
|
|
|
|
|
|
|
def hold_after_commit(saved, *args, **kwargs):
|
|
|
|
|
checkpoint = saved.metadata.get(RUNTIME_CHECKPOINT_KEY, {})
|
|
|
|
|
if checkpoint.get("phase") == "tools_completed" and calls == 3 and not committed.is_set():
|
|
|
|
|
committed.set()
|
2026-11-06 20:48:57 +08:00
|
|
|
event_loop.call_soon_threadsafe(checkpoint_ready.set)
|
2026-11-03 22:47:00 +08:00
|
|
|
if not release.wait(5):
|
|
|
|
|
raise TimeoutError("test did not release the committed save")
|
|
|
|
|
return publish(saved, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(loop.sessions.state, "_publish", hold_after_commit)
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="websocket", sender_id="user", chat_id="committed-checkpoint", content="Continue",
|
|
|
|
|
metadata={"webui": True, "message_id": "goal-input"},
|
|
|
|
|
)
|
|
|
|
|
completed = []
|
|
|
|
|
statuses = []
|
|
|
|
|
loop.bus.subscribe(completed.append, TurnCompleted)
|
|
|
|
|
loop.bus.subscribe(statuses.append, TurnRunStatusChanged)
|
|
|
|
|
stop_acknowledged = asyncio.Event()
|
|
|
|
|
publish_outbound = loop.bus.publish_outbound
|
|
|
|
|
|
|
|
|
|
async def observe_stop_reply(message):
|
|
|
|
|
await publish_outbound(message)
|
|
|
|
|
if message.metadata.get("message_id") == "stop-attempt" and message.content.startswith("Stopped "):
|
|
|
|
|
stop_acknowledged.set()
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(loop.bus, "publish_outbound", observe_stop_reply)
|
|
|
|
|
bus_task = asyncio.create_task(loop.run())
|
|
|
|
|
try:
|
|
|
|
|
await loop.bus.publish_inbound(msg)
|
2026-11-06 20:48:57 +08:00
|
|
|
# Establish each preparation step before measuring Stop responsiveness.
|
|
|
|
|
# An async observer also cancels without leaving an executor thread waiting.
|
|
|
|
|
for expected_call in (1, 2, 3):
|
|
|
|
|
assert await asyncio.wait_for(model_progress.get(), 5) == expected_call
|
|
|
|
|
await asyncio.wait_for(checkpoint_ready.wait(), 5)
|
|
|
|
|
assert committed.is_set()
|
2026-11-03 22:47:00 +08:00
|
|
|
worker, = loop._active_tasks[msg.session_key]
|
|
|
|
|
assert gate.locked()
|
2026-10-24 02:41:23 +08:00
|
|
|
assert loop._pending_queues[msg.session_key].qsize() == 1
|
|
|
|
|
stop_msg = InboundMessage(
|
2026-11-03 22:47:00 +08:00
|
|
|
channel="websocket", sender_id="user", chat_id=msg.chat_id, content="/stop",
|
|
|
|
|
metadata={"message_id": "stop-attempt"},
|
2026-10-24 02:41:23 +08:00
|
|
|
)
|
2026-11-03 22:47:00 +08:00
|
|
|
await loop.bus.publish_inbound(stop_msg)
|
|
|
|
|
async with asyncio.timeout(5):
|
|
|
|
|
while not worker.cancelling():
|
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
release.set()
|
|
|
|
|
await asyncio.wait_for(stop_acknowledged.wait(), 5)
|
|
|
|
|
assert not bus_task.done(), "Stop terminated the inbound loop"
|
|
|
|
|
assert not gate.locked()
|
|
|
|
|
saved = await loop.sessions.state.read(msg.session_key)
|
|
|
|
|
assert saved.metadata[GOAL_STATE_KEY]["status"] == "active"
|
|
|
|
|
assert RUNTIME_CHECKPOINT_KEY not in saved.metadata
|
|
|
|
|
assert [row["tool_call_id"] for row in saved.messages if row["role"] == "tool"] == [
|
|
|
|
|
"list-1", "list-2", "list-3",
|
|
|
|
|
]
|
|
|
|
|
assert msg.session_key not in loop._pending_queues
|
|
|
|
|
assert not loop._active_tasks.get(msg.session_key)
|
|
|
|
|
assert calls == 3
|
|
|
|
|
loop.runner.run.assert_awaited_once()
|
|
|
|
|
assert [(event.context.metadata.get("message_id"), event.outcome) for event in completed
|
|
|
|
|
if event.context.metadata.get("message_id") in {"goal-input", "queued-compact"}] == [
|
|
|
|
|
("goal-input", "cancelled"), ("queued-compact", "completed"),
|
|
|
|
|
]
|
|
|
|
|
assert [(event.context.metadata.get("message_id"), event.status) for event in statuses] == [
|
|
|
|
|
("goal-input", "running"), ("goal-input", "idle"),
|
|
|
|
|
]
|
|
|
|
|
loop.consolidator.compact_idle_session.assert_not_awaited()
|
2026-10-24 02:41:23 +08:00
|
|
|
|
2026-11-03 22:47:00 +08:00
|
|
|
outbound = []
|
|
|
|
|
while loop.bus.outbound_size:
|
|
|
|
|
outbound.append(await loop.bus.consume_outbound())
|
|
|
|
|
stop_responses = [item.content for item in outbound if item.content.startswith("Stopped ")]
|
|
|
|
|
assert stop_responses == ["Stopped 1 task(s)."]
|
|
|
|
|
endings = [item.event for item in outbound if isinstance(item.event, TurnEndEvent)]
|
|
|
|
|
cancelled = [event for event in endings if event.outcome == "cancelled"]
|
|
|
|
|
assert len(cancelled) == 1 and cancelled[0].goal_state["status"] == "active"
|
|
|
|
|
assert [item.event.status for item in outbound if isinstance(item.event, GoalStatusEvent)] == [
|
|
|
|
|
"running", "idle",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
followup = InboundMessage(
|
|
|
|
|
channel="websocket", sender_id="user", chat_id=msg.chat_id, content="Independent input",
|
|
|
|
|
metadata={"webui": True, "message_id": "independent-input"},
|
|
|
|
|
)
|
|
|
|
|
await loop.bus.publish_inbound(followup)
|
|
|
|
|
async with asyncio.timeout(5):
|
|
|
|
|
while msg.session_key in loop._pending_queues or not any(
|
|
|
|
|
event.context.metadata.get("message_id") == "independent-input"
|
|
|
|
|
for event in completed
|
|
|
|
|
):
|
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
assert calls == 5 and loop.runner.run.await_count == 2
|
|
|
|
|
assert msg.session_key not in loop._pending_queues
|
|
|
|
|
followup_outbound = []
|
|
|
|
|
while loop.bus.outbound_size:
|
|
|
|
|
followup_outbound.append(await loop.bus.consume_outbound())
|
|
|
|
|
assert any(item.content == "handled later" for item in followup_outbound)
|
|
|
|
|
loop.consolidator.compact_idle_session.assert_not_awaited()
|
|
|
|
|
finally:
|
|
|
|
|
release.set()
|
|
|
|
|
loop.stop()
|
|
|
|
|
await asyncio.wait_for(asyncio.gather(bus_task, return_exceptions=True), 5)
|
|
|
|
|
await loop.aclose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("operation", ["reset", "delete"])
|
|
|
|
|
async def test_active_goal_checkpoint_cannot_revive_an_old_generation(tmp_path: Path, operation):
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.max_iterations = 1
|
|
|
|
|
session = await loop.sessions.state.get("websocket:old-generation")
|
|
|
|
|
await loop.sessions.state.update_metadata(session.key, {
|
|
|
|
|
GOAL_STATE_KEY: {"status": "active", "objective": "Old goal."},
|
|
|
|
|
})
|
|
|
|
|
waiting, release = asyncio.Event(), asyncio.Event()
|
|
|
|
|
calls = 0
|
|
|
|
|
|
|
|
|
|
async def respond(**kwargs):
|
|
|
|
|
nonlocal calls
|
|
|
|
|
calls += 1
|
|
|
|
|
assert calls <= 2
|
|
|
|
|
if calls == 1:
|
|
|
|
|
return LLMResponse(content=None, tool_calls=[ToolCallRequest(
|
|
|
|
|
id="list", name="list_dir", arguments={"path": "."},
|
|
|
|
|
)])
|
|
|
|
|
waiting.set()
|
|
|
|
|
await release.wait()
|
|
|
|
|
return LLMResponse(content="Old generation's late answer.")
|
|
|
|
|
|
|
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(side_effect=respond)
|
|
|
|
|
worker = asyncio.create_task(run_session(loop, InboundMessage(
|
|
|
|
|
channel="websocket", sender_id="user", chat_id="old-generation", content="Continue",
|
|
|
|
|
metadata={"webui": True},
|
|
|
|
|
)))
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.wait_for(waiting.wait(), 5)
|
|
|
|
|
if operation == "reset":
|
|
|
|
|
await loop.sessions.state.reset(session.key)
|
|
|
|
|
else:
|
|
|
|
|
assert await loop.sessions.state.delete(session.key, expected_generation=session.generation)
|
|
|
|
|
release.set()
|
|
|
|
|
await asyncio.wait_for(worker, 5)
|
|
|
|
|
current = await loop.sessions.state.read(session.key)
|
|
|
|
|
if operation == "reset":
|
|
|
|
|
assert current is not None and current.generation != session.generation
|
|
|
|
|
assert current.messages == []
|
|
|
|
|
assert GOAL_STATE_KEY not in current.metadata
|
|
|
|
|
else:
|
|
|
|
|
assert current is None
|
|
|
|
|
assert calls == 2
|
|
|
|
|
finally:
|
|
|
|
|
release.set()
|
|
|
|
|
await loop._cancel_active_tasks(session.key)
|
|
|
|
|
await asyncio.gather(worker, return_exceptions=True)
|
|
|
|
|
await loop.aclose()
|
2026-10-24 02:41:23 +08:00
|
|
|
|
|
|
|
|
|
2026-04-23 03:02:42 +09:00
|
|
|
@pytest.mark.asyncio
|
2026-07-27 11:52:14 +08:00
|
|
|
async def test_process_message_keeps_delivery_chat_for_thread_session(tmp_path: Path) -> None:
|
2026-04-23 03:02:42 +09:00
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.context.build_messages = MagicMock( # type: ignore[method-assign]
|
|
|
|
|
return_value=[
|
|
|
|
|
{"role": "system", "content": "system"},
|
|
|
|
|
{"role": "user", "content": "runtime + hello"},
|
|
|
|
|
]
|
|
|
|
|
)
|
2026-08-26 15:18:53 +08:00
|
|
|
loop._run_agent_loop = AsyncMock(return_value=_agent_run_result( # type: ignore[method-assign]
|
2026-04-23 03:02:42 +09:00
|
|
|
"done",
|
|
|
|
|
[
|
|
|
|
|
{"role": "system", "content": "system"},
|
|
|
|
|
{"role": "user", "content": "runtime + hello"},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-23 03:02:42 +09:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
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"
|
2026-08-26 17:33:14 +08:00
|
|
|
request = loop._run_agent_loop.call_args.kwargs["request_context"]
|
|
|
|
|
assert request.chat_id == "thread-777"
|
2026-04-23 03:02:42 +09:00
|
|
|
|
|
|
|
|
|
2026-05-16 11:14:56 +00:00
|
|
|
@pytest.mark.asyncio
|
2026-07-27 11:52:14 +08:00
|
|
|
async def test_process_message_uses_explicit_session_for_goal_context(
|
2026-05-16 11:14:56 +00:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
chat_session = loop.sessions.get_or_create("websocket:chat-with-goal")
|
|
|
|
|
chat_session.metadata[GOAL_STATE_KEY] = {
|
|
|
|
|
"status": "active",
|
2026-05-27 21:28:59 +08:00
|
|
|
"objective": "This chat goal must not leak into system.",
|
2026-05-16 11:14:56 +00:00
|
|
|
}
|
|
|
|
|
loop.sessions.save(chat_session)
|
2026-05-27 21:28:59 +08:00
|
|
|
system_session = loop.sessions.get_or_create("system")
|
2026-05-16 11:14:56 +00:00
|
|
|
system_session.metadata = {}
|
|
|
|
|
loop.sessions.save(system_session)
|
|
|
|
|
|
|
|
|
|
loop.context.build_messages = MagicMock( # type: ignore[method-assign]
|
|
|
|
|
return_value=[
|
|
|
|
|
{"role": "system", "content": "system"},
|
2026-05-27 21:28:59 +08:00
|
|
|
{"role": "user", "content": "runtime + system"},
|
2026-05-16 11:14:56 +00:00
|
|
|
]
|
|
|
|
|
)
|
2026-08-26 15:18:53 +08:00
|
|
|
loop._run_agent_loop = AsyncMock(return_value=_agent_run_result( # type: ignore[method-assign]
|
2026-05-16 11:14:56 +00:00
|
|
|
"ok",
|
|
|
|
|
[
|
|
|
|
|
{"role": "system", "content": "system"},
|
2026-05-27 21:28:59 +08:00
|
|
|
{"role": "user", "content": "runtime + system"},
|
2026-05-16 11:14:56 +00:00
|
|
|
{"role": "assistant", "content": "ok"},
|
|
|
|
|
],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-05-16 11:14:56 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
result = await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="websocket",
|
2026-05-27 21:28:59 +08:00
|
|
|
sender_id="system",
|
2026-05-16 11:14:56 +00:00
|
|
|
chat_id="chat-with-goal",
|
2026-05-27 21:28:59 +08:00
|
|
|
content="system work",
|
2026-05-16 11:14:56 +00:00
|
|
|
),
|
2026-05-27 21:28:59 +08:00
|
|
|
session_key="system",
|
2026-05-16 11:14:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert result.content == "ok"
|
2026-07-27 11:52:14 +08:00
|
|
|
kwargs = loop._run_agent_loop.call_args.kwargs
|
2026-10-16 17:28:47 +08:00
|
|
|
assert kwargs["session"].key == system_session.key
|
|
|
|
|
assert kwargs["session"] is not system_session
|
2026-08-26 17:33:14 +08:00
|
|
|
assert kwargs["request_context"].session_key == "system"
|
2026-07-27 11:52:14 +08:00
|
|
|
assert GOAL_STATE_KEY not in kwargs["session"].metadata
|
2026-05-16 11:14:56 +00:00
|
|
|
|
|
|
|
|
|
2026-06-16 10:21:28 +08:00
|
|
|
@pytest.mark.asyncio
|
2026-08-26 17:33:14 +08:00
|
|
|
async def test_run_agent_loop_continuation_reads_latest_goal_metadata(
|
2026-06-16 10:21:28 +08:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
from nanobot.agent.runner import AgentRunResult
|
|
|
|
|
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-16 17:28:47 +08:00
|
|
|
session = await loop.sessions.state.get("websocket:late-goal")
|
2026-06-16 10:21:28 +08:00
|
|
|
seen: dict[str, str | None] = {}
|
|
|
|
|
|
|
|
|
|
async def fake_run(spec):
|
2026-08-26 17:33:14 +08:00
|
|
|
assert callable(spec.continuation_callback)
|
2026-10-16 17:28:47 +08:00
|
|
|
await loop.sessions.state.update_metadata(session.key, {GOAL_STATE_KEY: {
|
2026-06-16 10:21:28 +08:00
|
|
|
"status": "active",
|
|
|
|
|
"objective": "Goal created during this runner call.",
|
2026-10-16 17:28:47 +08:00
|
|
|
}})
|
|
|
|
|
seen["goal_continue"] = await spec.continuation_callback()
|
2026-06-16 10:21:28 +08:00
|
|
|
return AgentRunResult(
|
|
|
|
|
final_content="ok",
|
|
|
|
|
messages=[{"role": "assistant", "content": "ok"}],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop.runner.run = fake_run # type: ignore[method-assign]
|
|
|
|
|
|
2026-08-26 17:33:14 +08:00
|
|
|
runtime = loop.llm_runtime()
|
2026-06-16 10:21:28 +08:00
|
|
|
await loop._run_agent_loop(
|
2026-08-31 00:06:15 +08:00
|
|
|
TranscriptInput(history=[], current_message=None),
|
2026-08-26 17:33:14 +08:00
|
|
|
runtime=runtime,
|
2026-06-16 10:21:28 +08:00
|
|
|
session=session,
|
2026-08-26 17:33:14 +08:00
|
|
|
request_context=RequestContext(
|
|
|
|
|
channel="websocket",
|
|
|
|
|
chat_id="late-goal",
|
|
|
|
|
session_key=session.key,
|
|
|
|
|
runtime=runtime,
|
|
|
|
|
),
|
2026-06-16 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert "Goal created during this runner call." in (seen["goal_continue"] or "")
|
|
|
|
|
|
|
|
|
|
|
2026-07-21 00:14:27 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_process_direct_rejects_reserved_system_channel(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop._process_message = AsyncMock(return_value=None) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="reserved for internal messages"):
|
|
|
|
|
await loop.process_direct("external input", channel="system")
|
|
|
|
|
|
|
|
|
|
loop._process_message.assert_not_awaited()
|
|
|
|
|
|
|
|
|
|
|
2026-07-10 14:16:56 +08:00
|
|
|
@pytest.mark.asyncio
|
2026-10-05 05:16:33 +08:00
|
|
|
async def test_request_context_uses_effective_key_for_subagent_tool(tmp_path: Path) -> None:
|
2026-04-23 03:02:42 +09:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-05 05:16:33 +08:00
|
|
|
subagent_tool = loop.tools.get("subagent")
|
|
|
|
|
assert subagent_tool is not None
|
|
|
|
|
subagent_tool._manager.spawn = AsyncMock(return_value="started") # type: ignore[attr-defined]
|
2026-07-10 14:46:22 +08:00
|
|
|
runtime = loop.llm_runtime()
|
2026-04-23 03:02:42 +09:00
|
|
|
|
2026-07-10 14:16:56 +08:00
|
|
|
with request_context(RequestContext(
|
|
|
|
|
channel="discord",
|
|
|
|
|
chat_id="thread-777",
|
2026-04-27 09:26:24 +00:00
|
|
|
session_key="discord:parent-456:thread:thread-777",
|
2026-07-10 14:46:22 +08:00
|
|
|
runtime=runtime,
|
2026-07-10 14:16:56 +08:00
|
|
|
)):
|
2026-10-05 05:16:33 +08:00
|
|
|
await subagent_tool.execute(action="create", task="inspect context")
|
2026-04-23 03:02:42 +09:00
|
|
|
|
2026-10-05 05:16:33 +08:00
|
|
|
call = subagent_tool._manager.spawn.await_args.kwargs # type: ignore[attr-defined]
|
2026-07-10 14:16:56 +08:00
|
|
|
assert call["origin_channel"] == "discord"
|
|
|
|
|
assert call["origin_chat_id"] == "thread-777"
|
|
|
|
|
assert call["session_key"] == "discord:parent-456:thread:thread-777"
|
2026-07-10 14:46:22 +08:00
|
|
|
assert call["runtime"] is runtime
|
2026-04-23 03:02:42 +09:00
|
|
|
|
|
|
|
|
|
2026-04-13 02:21:39 +00:00
|
|
|
@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)
|
2026-10-30 13:07:03 +08:00
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(return_value=MagicMock())
|
2026-04-13 02:21:39 +00:00
|
|
|
|
|
|
|
|
session = loop.sessions.get_or_create("feishu:c3")
|
|
|
|
|
session.add_message("user", "old question")
|
|
|
|
|
session.metadata[AgentLoop._PENDING_USER_TURN_KEY] = True
|
2026-07-30 22:39:43 +08:00
|
|
|
session.provider_state = _provider_state().with_pending_messages([
|
|
|
|
|
{"role": "user", "content": "old question"},
|
|
|
|
|
])
|
2026-04-13 02:21:39 +00:00
|
|
|
loop.sessions.save(session)
|
|
|
|
|
|
2026-08-26 15:18:53 +08:00
|
|
|
loop._run_agent_loop = AsyncMock(return_value=_agent_run_result(
|
2026-04-13 02:21:39 +00:00
|
|
|
"new answer",
|
|
|
|
|
[
|
|
|
|
|
{"role": "system", "content": "system"},
|
|
|
|
|
{"role": "user", "content": "old question"},
|
|
|
|
|
{"role": "assistant", "content": "Error: Task interrupted before a response was generated."},
|
|
|
|
|
{"role": "user", "content": "new question"},
|
|
|
|
|
{"role": "assistant", "content": "new answer"},
|
|
|
|
|
],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-13 02:21:39 +00:00
|
|
|
)) # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
result = await loop._process_message(
|
|
|
|
|
InboundMessage(channel="feishu", sender_id="u1", chat_id="c3", content="new question")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert result.content == "new answer"
|
|
|
|
|
session = loop.sessions.get_or_create("feishu:c3")
|
|
|
|
|
assert [
|
|
|
|
|
{k: v for k, v in m.items() if k in {"role", "content"}}
|
2026-10-09 14:04:18 +08:00
|
|
|
for m in map(public_history_message, session.messages)
|
2026-04-13 02:21:39 +00:00
|
|
|
] == [
|
|
|
|
|
{"role": "user", "content": "old question"},
|
|
|
|
|
{"role": "assistant", "content": "Error: Task interrupted before a response was generated."},
|
|
|
|
|
{"role": "user", "content": "new question"},
|
|
|
|
|
{"role": "assistant", "content": "new answer"},
|
|
|
|
|
]
|
|
|
|
|
assert AgentLoop._PENDING_USER_TURN_KEY not in session.metadata
|
2026-07-30 22:39:43 +08:00
|
|
|
assert session.provider_state is None
|
2026-04-14 13:42:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_stop_preserves_runtime_checkpoint_for_next_turn(tmp_path: Path) -> None:
|
|
|
|
|
from nanobot.command.builtin import cmd_stop
|
|
|
|
|
from nanobot.command.router import CommandContext
|
|
|
|
|
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
checkpoint_saved = asyncio.Event()
|
|
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def interrupted_run_agent_loop(_transcript_input, *, session=None, **_kwargs):
|
2026-04-14 13:42:59 +08:00
|
|
|
assert session is not None
|
2026-10-16 17:28:47 +08:00
|
|
|
await loop._set_runtime_checkpoint(
|
2026-04-14 13:42:59 +08:00
|
|
|
session,
|
|
|
|
|
{
|
|
|
|
|
"assistant_message": {
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "working",
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_done",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "read_file", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
"completed_tool_results": [
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_done",
|
|
|
|
|
"name": "read_file",
|
|
|
|
|
"content": "ok",
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"pending_tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_pending",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
checkpoint_saved.set()
|
|
|
|
|
await asyncio.Event().wait()
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = interrupted_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
first_msg = InboundMessage(channel="feishu", sender_id="u1", chat_id="c4", content="keep progress")
|
|
|
|
|
task = asyncio.create_task(loop._process_message(first_msg))
|
2026-07-28 11:07:58 +08:00
|
|
|
loop._active_tasks[first_msg.session_key] = {task}
|
2026-10-16 17:28:47 +08:00
|
|
|
checkpoint_wait = asyncio.create_task(checkpoint_saved.wait())
|
|
|
|
|
try:
|
|
|
|
|
done, _ = await asyncio.wait(
|
|
|
|
|
(checkpoint_wait, task), timeout=5, return_when=asyncio.FIRST_COMPLETED,
|
|
|
|
|
)
|
|
|
|
|
if task in done:
|
|
|
|
|
await task
|
|
|
|
|
assert checkpoint_wait in done, "the turn did not reach its durable checkpoint"
|
|
|
|
|
except BaseException:
|
|
|
|
|
task.cancel()
|
|
|
|
|
await asyncio.gather(task, return_exceptions=True)
|
|
|
|
|
raise
|
|
|
|
|
finally:
|
|
|
|
|
checkpoint_wait.cancel()
|
|
|
|
|
await asyncio.gather(checkpoint_wait, return_exceptions=True)
|
2026-04-14 13:42:59 +08:00
|
|
|
|
|
|
|
|
stop_msg = InboundMessage(channel="feishu", sender_id="u1", chat_id="c4", content="/stop")
|
|
|
|
|
stop_ctx = CommandContext(msg=stop_msg, session=None, key=stop_msg.session_key, raw="/stop", loop=loop)
|
|
|
|
|
stop_result = await cmd_stop(stop_ctx)
|
|
|
|
|
|
|
|
|
|
assert "Stopped 1 task" in stop_result.content
|
|
|
|
|
assert task.done()
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("feishu:c4")
|
|
|
|
|
interrupted = loop.sessions.get_or_create("feishu:c4")
|
|
|
|
|
assert interrupted.metadata.get(AgentLoop._PENDING_USER_TURN_KEY) is True
|
|
|
|
|
assert interrupted.metadata.get(AgentLoop._RUNTIME_CHECKPOINT_KEY) is not None
|
|
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def resumed_run_agent_loop(transcript_input, **_kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-04-14 13:42:59 +08:00
|
|
|
"next answer",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "next answer"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-14 13:42:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = resumed_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
result = await loop._process_message(
|
|
|
|
|
InboundMessage(channel="feishu", sender_id="u1", chat_id="c4", content="continue here")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert result.content == "next answer"
|
|
|
|
|
|
|
|
|
|
session = loop.sessions.get_or_create("feishu:c4")
|
|
|
|
|
assert [
|
|
|
|
|
{k: v for k, v in m.items() if k in {"role", "content", "tool_call_id", "name"}}
|
2026-10-09 14:04:18 +08:00
|
|
|
for m in map(public_history_message, session.messages)
|
2026-04-14 13:42:59 +08:00
|
|
|
] == [
|
|
|
|
|
{"role": "user", "content": "keep progress"},
|
|
|
|
|
{"role": "assistant", "content": "working"},
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_done", "name": "read_file", "content": "ok"},
|
|
|
|
|
{
|
|
|
|
|
"role": "tool",
|
|
|
|
|
"tool_call_id": "call_pending",
|
|
|
|
|
"name": "exec",
|
2026-11-02 17:55:14 +08:00
|
|
|
"content": "Error: Task interrupted; this tool's outcome is unknown. Check its effects before retrying.",
|
2026-04-14 13:42:59 +08:00
|
|
|
},
|
|
|
|
|
{"role": "user", "content": "continue here"},
|
|
|
|
|
{"role": "assistant", "content": "next answer"},
|
|
|
|
|
]
|
|
|
|
|
assert AgentLoop._PENDING_USER_TURN_KEY not in session.metadata
|
|
|
|
|
assert AgentLoop._RUNTIME_CHECKPOINT_KEY not in session.metadata
|
2026-04-17 13:40:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_system_subagent_followup_is_persisted_before_prompt_assembly(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
session = loop.sessions.get_or_create("cli:test")
|
|
|
|
|
session.add_message("user", "question")
|
|
|
|
|
session.add_message("assistant", "working")
|
|
|
|
|
loop.sessions.save(session)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-1", session.key)
|
2026-04-17 13:40:27 +08:00
|
|
|
|
2026-07-10 14:30:09 +08:00
|
|
|
runtime = loop.llm_runtime()
|
|
|
|
|
seen: dict[str, object] = {}
|
2026-07-28 11:07:58 +08:00
|
|
|
record_runtime = MagicMock(wraps=loop.runtime_event_publisher.record_turn_runtime)
|
2026-07-10 14:30:09 +08:00
|
|
|
loop.runtime_event_publisher.record_turn_runtime = record_runtime
|
2026-04-17 13:40:27 +08:00
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-04-17 13:40:27 +08:00
|
|
|
seen["initial_messages"] = initial_messages
|
2026-07-10 14:30:09 +08:00
|
|
|
seen["runtime"] = kwargs["runtime"]
|
2026-07-21 00:14:27 +08:00
|
|
|
seen["request_context"] = kwargs["request_context"]
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-04-17 13:40:27 +08:00
|
|
|
"done",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "done"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-17 13:40:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:test",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
2026-07-10 14:30:09 +08:00
|
|
|
),
|
|
|
|
|
runtime=runtime,
|
2026-04-17 13:40:27 +08:00
|
|
|
)
|
|
|
|
|
|
2026-07-10 14:30:09 +08:00
|
|
|
assert seen["runtime"] is runtime
|
2026-07-21 00:14:27 +08:00
|
|
|
request = seen["request_context"]
|
|
|
|
|
assert isinstance(request, RequestContext)
|
|
|
|
|
assert request.channel == "cli"
|
|
|
|
|
assert request.chat_id == "test"
|
|
|
|
|
assert request.session_key == "cli:test"
|
|
|
|
|
assert request.original_user_text is None
|
|
|
|
|
assert request.sender_id == "subagent"
|
|
|
|
|
assert request.metadata == {"subagent_task_id": "sub-1"}
|
|
|
|
|
assert request.turn_id
|
2026-11-03 22:47:00 +08:00
|
|
|
assert record_runtime.call_count == 1
|
|
|
|
|
assert record_runtime.call_args.args == ("cli:test", runtime)
|
|
|
|
|
assert isinstance(record_runtime.call_args.kwargs["run_id"], str)
|
2026-07-10 14:30:09 +08:00
|
|
|
initial_messages = seen["initial_messages"]
|
|
|
|
|
assert isinstance(initial_messages, list)
|
|
|
|
|
non_system = [m for m in initial_messages if m.get("role") != "system"]
|
2026-04-26 17:42:58 +00:00
|
|
|
assert "question" in non_system[0]["content"]
|
|
|
|
|
assert "working" in non_system[1]["content"]
|
2026-06-27 00:25:56 +08:00
|
|
|
assert "[Message Time:" not in non_system[0]["content"]
|
2026-04-27 07:11:20 +00:00
|
|
|
assert "[Message Time:" not in non_system[1]["content"]
|
2026-07-22 23:04:36 +08:00
|
|
|
assert non_system[2]["role"] == "user"
|
2026-04-17 13:40:27 +08:00
|
|
|
assert non_system[2]["content"].count("subagent result") == 1
|
2026-07-11 18:52:56 +08:00
|
|
|
assert non_system[2]["content"] == "subagent result"
|
2026-04-17 13:40:27 +08:00
|
|
|
|
|
|
|
|
loop.sessions.invalidate("cli:test")
|
|
|
|
|
persisted = loop.sessions.get_or_create("cli:test")
|
|
|
|
|
assert [
|
|
|
|
|
{k: v for k, v in m.items() if k in {"role", "content", "injected_event", "subagent_task_id"}}
|
|
|
|
|
for m in persisted.messages
|
|
|
|
|
] == [
|
|
|
|
|
{"role": "user", "content": "question"},
|
|
|
|
|
{"role": "assistant", "content": "working"},
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "subagent result",
|
|
|
|
|
"injected_event": "subagent_result",
|
|
|
|
|
"subagent_task_id": "sub-1",
|
|
|
|
|
},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-08-16 16:34:11 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_turn_usage_is_persisted_with_the_saved_session(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-08-26 15:18:53 +08:00
|
|
|
turn_usage = LLMUsage.reported(input_tokens=64, output_tokens=9)
|
2026-08-16 16:34:11 +08:00
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **_kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-08-16 16:34:11 +08:00
|
|
|
"done",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "done"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
|
|
|
|
usage=turn_usage,
|
2026-08-16 16:34:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
await loop._process_message(
|
|
|
|
|
InboundMessage(channel="cli", sender_id="user", chat_id="usage", content="hello")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("cli:usage")
|
2026-08-22 00:53:16 +08:00
|
|
|
assert loop.sessions.get_or_create("cli:usage").metadata["_last_usage"] == (
|
2026-08-26 15:18:53 +08:00
|
|
|
turn_usage.to_dict()
|
2026-08-22 00:53:16 +08:00
|
|
|
)
|
2026-08-16 16:34:11 +08:00
|
|
|
|
|
|
|
|
|
2026-07-21 00:14:27 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_system_subagent_followup_does_not_log_content(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-logs", "cli:logs")
|
2026-07-21 00:14:27 +08:00
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **_kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-07-21 00:14:27 +08:00
|
|
|
"done",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "done"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-07-21 00:14:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
secret = "LEAKME42"
|
|
|
|
|
content = f"[Subagent 'research' completed]\n\nTask: inspect logs\n\nResult:\n{secret}"
|
|
|
|
|
logs: list[str] = []
|
|
|
|
|
sink_id = logger.add(logs.append, level="INFO", format="{message}")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:logs",
|
|
|
|
|
content=content,
|
|
|
|
|
metadata={"subagent_task_id": "sub-logs"},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
logger.remove(sink_id)
|
|
|
|
|
|
|
|
|
|
logged = "".join(logs)
|
|
|
|
|
assert "Processing system message from subagent" in logged
|
|
|
|
|
assert secret not in logged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-07-27 11:52:14 +08:00
|
|
|
async def test_system_subagent_followup_uses_common_turn_lifecycle(tmp_path: Path) -> None:
|
2026-07-21 00:14:27 +08:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-1", "cli:test")
|
2026-07-27 11:52:14 +08:00
|
|
|
visited: list[str] = []
|
2026-07-21 00:14:27 +08:00
|
|
|
|
2026-07-27 11:52:14 +08:00
|
|
|
for name in (
|
|
|
|
|
"_restore_turn",
|
|
|
|
|
"_compact_session",
|
|
|
|
|
"_dispatch_command",
|
|
|
|
|
"_build_turn",
|
|
|
|
|
"_run_turn",
|
|
|
|
|
"_persist_turn",
|
|
|
|
|
"_prepare_outbound",
|
2026-07-21 00:14:27 +08:00
|
|
|
):
|
|
|
|
|
original = getattr(loop, name)
|
|
|
|
|
|
2026-07-27 11:52:14 +08:00
|
|
|
async def record(ctx, *, _original=original, _name=name):
|
|
|
|
|
visited.append(_name)
|
2026-07-21 00:14:27 +08:00
|
|
|
return await _original(ctx)
|
|
|
|
|
|
|
|
|
|
setattr(loop, name, record)
|
|
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **_kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-07-21 00:14:27 +08:00
|
|
|
"done",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "done"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-07-21 00:14:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
|
2026-09-21 14:53:19 +08:00
|
|
|
records = []
|
|
|
|
|
sink_id = logger.add(lambda message: records.append(message.record), level="DEBUG")
|
2026-07-27 11:52:14 +08:00
|
|
|
try:
|
|
|
|
|
await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:test",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
|
|
|
|
)
|
2026-07-21 00:14:27 +08:00
|
|
|
)
|
2026-07-27 11:52:14 +08:00
|
|
|
finally:
|
|
|
|
|
logger.remove(sink_id)
|
2026-07-21 00:14:27 +08:00
|
|
|
|
|
|
|
|
assert visited == [
|
2026-07-27 11:52:14 +08:00
|
|
|
"_restore_turn",
|
|
|
|
|
"_compact_session",
|
|
|
|
|
"_dispatch_command",
|
|
|
|
|
"_build_turn",
|
|
|
|
|
"_run_turn",
|
|
|
|
|
"_persist_turn",
|
|
|
|
|
"_prepare_outbound",
|
2026-07-21 00:14:27 +08:00
|
|
|
]
|
2026-09-21 14:53:19 +08:00
|
|
|
logged = "\n".join(record["message"] for record in records)
|
2026-07-27 11:52:14 +08:00
|
|
|
for stage in ("restore", "compact", "command", "build", "run", "save", "respond"):
|
|
|
|
|
assert f"Stage {stage} completed in" in logged
|
2026-09-21 14:53:19 +08:00
|
|
|
stage_records = [record for record in records if record["extra"].get("event") == "turn_stage"]
|
|
|
|
|
assert {record["extra"]["stage"] for record in stage_records} == {
|
|
|
|
|
"restore",
|
|
|
|
|
"compact",
|
|
|
|
|
"command",
|
|
|
|
|
"build",
|
|
|
|
|
"run",
|
|
|
|
|
"save",
|
|
|
|
|
"respond",
|
|
|
|
|
}
|
|
|
|
|
assert {record["extra"]["session_key"] for record in stage_records} == {"cli:test"}
|
|
|
|
|
assert len({record["extra"]["turn_id"] for record in stage_records}) == 1
|
|
|
|
|
completion = next(
|
|
|
|
|
record for record in records if record["extra"].get("event") == "turn_completed"
|
|
|
|
|
)
|
|
|
|
|
assert completion["extra"]["outcome"] == "stop"
|
|
|
|
|
assert completion["extra"]["duration_ms"] >= 0
|
|
|
|
|
assert completion["extra"]["provider"] == "test"
|
|
|
|
|
assert completion["extra"]["model"] == "test-model"
|
|
|
|
|
assert "response=[content hidden]" in completion["message"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_failed_turn_stage_logs_exception_and_correlation(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
async def fail_restore(_ctx) -> None:
|
|
|
|
|
raise RuntimeError("restore failed")
|
|
|
|
|
|
|
|
|
|
loop._restore_turn = fail_restore # type: ignore[method-assign]
|
|
|
|
|
records = []
|
|
|
|
|
sink_id = logger.add(lambda message: records.append(message.record), level="ERROR")
|
|
|
|
|
try:
|
|
|
|
|
with pytest.raises(RuntimeError, match="restore failed"):
|
|
|
|
|
await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="cli",
|
|
|
|
|
sender_id="user",
|
|
|
|
|
chat_id="failure",
|
|
|
|
|
content="hello",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
logger.remove(sink_id)
|
|
|
|
|
|
|
|
|
|
failure = next(
|
|
|
|
|
record
|
|
|
|
|
for record in records
|
|
|
|
|
if record["extra"].get("event") == "turn_stage"
|
|
|
|
|
and record["extra"].get("outcome") == "error"
|
|
|
|
|
)
|
|
|
|
|
assert failure["exception"] is not None
|
|
|
|
|
assert failure["extra"]["stage"] == "restore"
|
|
|
|
|
assert failure["extra"]["session_key"] == "cli:failure"
|
|
|
|
|
assert failure["extra"]["turn_id"].startswith("cli:failure:")
|
2026-07-21 00:14:27 +08:00
|
|
|
|
|
|
|
|
|
2026-04-17 13:40:27 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_multiple_subagent_followups_all_persist_as_standalone_history(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **_kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-04-17 13:40:27 +08:00
|
|
|
"ack",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "ack"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-17 13:40:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
for idx in range(3):
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, f"sub-{idx}", "cli:multi")
|
2026-04-17 13:40:27 +08:00
|
|
|
await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:multi",
|
|
|
|
|
content=f"subagent result {idx}",
|
|
|
|
|
metadata={"subagent_task_id": f"sub-{idx}"},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop.sessions.invalidate("cli:multi")
|
|
|
|
|
persisted = loop.sessions.get_or_create("cli:multi")
|
|
|
|
|
followups = [m for m in persisted.messages if m.get("injected_event") == "subagent_result"]
|
|
|
|
|
assert [m["content"] for m in followups] == [
|
|
|
|
|
"subagent result 0",
|
|
|
|
|
"subagent result 1",
|
|
|
|
|
"subagent result 2",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-07-22 23:04:36 +08:00
|
|
|
def test_subagent_followup_uses_user_model_input_and_assistant_history(tmp_path: Path) -> None:
|
2026-04-17 13:40:27 +08:00
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="cli:merge")
|
|
|
|
|
session.add_message("assistant", "previous assistant")
|
2026-07-22 23:04:36 +08:00
|
|
|
history = session.get_history(max_messages=0)
|
2026-04-17 13:40:27 +08:00
|
|
|
|
|
|
|
|
inserted = loop._persist_subagent_followup(
|
|
|
|
|
session,
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:merge",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert inserted is True
|
|
|
|
|
|
|
|
|
|
builder = ContextBuilder(tmp_path)
|
|
|
|
|
projected = builder.build_messages(
|
2026-07-22 23:04:36 +08:00
|
|
|
history=history,
|
|
|
|
|
current_message="subagent result",
|
2026-04-17 13:40:27 +08:00
|
|
|
channel="cli",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
non_system = [m for m in projected if m.get("role") != "system"]
|
|
|
|
|
assert len(non_system) == 2
|
2026-07-22 23:04:36 +08:00
|
|
|
assert non_system[-1]["role"] == "user"
|
2026-04-17 13:40:27 +08:00
|
|
|
assert "subagent result" in non_system[-1]["content"]
|
2026-07-22 23:04:36 +08:00
|
|
|
assert session.messages[-1]["role"] == "assistant"
|
2026-04-17 13:40:27 +08:00
|
|
|
assert session.messages[-1]["content"] == "subagent result"
|
|
|
|
|
assert session.messages[-1]["injected_event"] == "subagent_result"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_subagent_followup_dedupes_by_task_id() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="cli:dedupe")
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:dedupe",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
metadata={"subagent_task_id": "sub-1"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert loop._persist_subagent_followup(session, msg) is True
|
|
|
|
|
assert loop._persist_subagent_followup(session, msg) is False
|
|
|
|
|
assert len(session.messages) == 1
|
2026-04-18 05:24:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_subagent_followup_skips_empty_content() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="cli:empty")
|
|
|
|
|
msg = InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="cli:empty",
|
|
|
|
|
content="",
|
|
|
|
|
metadata={"subagent_task_id": "sub-empty"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert loop._persist_subagent_followup(session, msg) is False
|
|
|
|
|
assert session.messages == []
|
2026-04-27 06:35:40 +00:00
|
|
|
|
|
|
|
|
|
2026-07-10 14:16:56 +08:00
|
|
|
@pytest.mark.asyncio
|
2026-10-05 05:16:33 +08:00
|
|
|
async def test_request_context_passes_thread_session_key_to_subagent_create(tmp_path: Path) -> None:
|
2026-04-27 06:35:40 +00:00
|
|
|
loop = _make_full_loop(tmp_path)
|
2026-10-05 05:16:33 +08:00
|
|
|
subagent_tool = loop.tools.get("subagent")
|
|
|
|
|
assert subagent_tool is not None
|
|
|
|
|
subagent_tool._manager.spawn = AsyncMock(return_value="started") # type: ignore[attr-defined]
|
2026-07-10 14:46:22 +08:00
|
|
|
runtime = loop.llm_runtime()
|
2026-04-27 06:35:40 +00:00
|
|
|
|
2026-07-10 14:16:56 +08:00
|
|
|
with request_context(RequestContext(
|
|
|
|
|
channel="slack",
|
|
|
|
|
chat_id="C123",
|
2026-05-01 11:47:24 +00:00
|
|
|
message_id="msg-123",
|
2026-04-27 06:35:40 +00:00
|
|
|
metadata={"slack": {"thread_ts": "1700.42", "channel_type": "channel"}},
|
|
|
|
|
session_key="slack:C123:1700.42",
|
2026-07-10 14:46:22 +08:00
|
|
|
runtime=runtime,
|
2026-07-10 14:16:56 +08:00
|
|
|
)):
|
2026-10-05 05:16:33 +08:00
|
|
|
await subagent_tool.execute(action="create", task="inspect thread")
|
2026-04-27 06:35:40 +00:00
|
|
|
|
2026-10-05 05:16:33 +08:00
|
|
|
call = subagent_tool._manager.spawn.await_args.kwargs # type: ignore[attr-defined]
|
2026-07-10 14:16:56 +08:00
|
|
|
assert call["session_key"] == "slack:C123:1700.42"
|
|
|
|
|
assert call["origin_message_id"] == "msg-123"
|
2026-07-10 14:46:22 +08:00
|
|
|
assert call["runtime"] is runtime
|
2026-04-27 06:35:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_system_subagent_followup_uses_thread_session_and_slack_metadata(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
thread_session = loop.sessions.get_or_create("slack:C123:1700.42")
|
|
|
|
|
thread_session.add_message("user", "thread question")
|
|
|
|
|
loop.sessions.save(thread_session)
|
2026-10-16 17:28:47 +08:00
|
|
|
await save_completed_subagent(loop, "sub-1", thread_session.key)
|
2026-04-27 06:35:40 +00:00
|
|
|
|
2026-07-21 00:14:27 +08:00
|
|
|
seen: dict[str, object] = {}
|
2026-04-27 06:35:40 +00:00
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
2026-04-27 06:35:40 +00:00
|
|
|
seen["initial_messages"] = initial_messages
|
2026-07-21 00:14:27 +08:00
|
|
|
seen["request_context"] = kwargs["request_context"]
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-04-27 06:35:40 +00:00
|
|
|
"done",
|
|
|
|
|
[*initial_messages, {"role": "assistant", "content": "done"}],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-04-27 06:35:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
outbound = await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="system",
|
|
|
|
|
sender_id="subagent",
|
|
|
|
|
chat_id="slack:C123",
|
|
|
|
|
content="subagent result",
|
|
|
|
|
session_key_override="slack:C123:1700.42",
|
2026-05-01 11:47:24 +00:00
|
|
|
metadata={"subagent_task_id": "sub-1", "origin_message_id": "msg-123"},
|
2026-04-27 06:35:40 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert outbound is not None
|
|
|
|
|
assert outbound.channel == "slack"
|
|
|
|
|
assert outbound.chat_id == "C123"
|
2026-05-01 11:47:24 +00:00
|
|
|
assert outbound.metadata == {
|
|
|
|
|
"slack": {"thread_ts": "1700.42"},
|
|
|
|
|
"origin_message_id": "msg-123",
|
|
|
|
|
}
|
2026-07-21 00:14:27 +08:00
|
|
|
request = seen["request_context"]
|
|
|
|
|
assert isinstance(request, RequestContext)
|
|
|
|
|
assert request.channel == "slack"
|
|
|
|
|
assert request.chat_id == "C123"
|
|
|
|
|
assert request.metadata == {
|
|
|
|
|
"subagent_task_id": "sub-1",
|
|
|
|
|
"origin_message_id": "msg-123",
|
|
|
|
|
}
|
|
|
|
|
assert "slack" not in request.metadata
|
|
|
|
|
initial_messages = seen["initial_messages"]
|
|
|
|
|
assert isinstance(initial_messages, list)
|
|
|
|
|
assert "thread question" in initial_messages[1]["content"]
|
2026-04-27 06:35:40 +00:00
|
|
|
|
|
|
|
|
loop.sessions.invalidate("slack:C123:1700.42")
|
|
|
|
|
persisted = loop.sessions.get_or_create("slack:C123:1700.42")
|
|
|
|
|
assert any(m.get("subagent_task_id") == "sub-1" for m in persisted.messages)
|
2026-06-12 09:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_turn_after_unanswered_user_keeps_tool_call_pairing(tmp_path: Path) -> None:
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
|
|
|
|
|
session = loop.sessions.get_or_create("feishu:c-merge")
|
|
|
|
|
session.add_message("user", "earlier question that never got an answer")
|
|
|
|
|
loop.sessions.save(session)
|
|
|
|
|
|
2026-08-31 00:06:15 +08:00
|
|
|
async def fake_run_agent_loop(transcript_input, **_kwargs):
|
|
|
|
|
initial_messages = _assembled_messages(loop.context, transcript_input)
|
|
|
|
|
assert [m["role"] for m in initial_messages] == ["system", "user", "user"]
|
|
|
|
|
assert initial_messages[-2]["content"] == "earlier question that never got an answer"
|
2026-10-09 14:04:18 +08:00
|
|
|
assert visible_message(initial_messages[-1])["content"] == "and another thing"
|
2026-08-26 15:18:53 +08:00
|
|
|
return _agent_run_result(
|
2026-06-12 09:07:58 +08:00
|
|
|
"done",
|
|
|
|
|
[
|
|
|
|
|
*initial_messages,
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [{
|
|
|
|
|
"id": "call_ls",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": '{"command": "ls"}'},
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_ls", "name": "exec", "content": "file.txt"},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
],
|
2026-08-26 15:18:53 +08:00
|
|
|
stop_reason="stop",
|
2026-06-12 09:07:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
|
|
|
|
|
|
|
|
|
result = await loop._process_message(
|
|
|
|
|
InboundMessage(
|
|
|
|
|
channel="feishu", sender_id="u1", chat_id="c-merge", content="and another thing"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
loop.sessions.invalidate("feishu:c-merge")
|
|
|
|
|
persisted = loop.sessions.get_or_create("feishu:c-merge")
|
|
|
|
|
|
|
|
|
|
declared: set[str] = set()
|
|
|
|
|
for message in persisted.messages:
|
|
|
|
|
if message.get("role") == "assistant":
|
|
|
|
|
declared.update(
|
|
|
|
|
str(tc["id"]) for tc in message.get("tool_calls") or [] if tc.get("id")
|
|
|
|
|
)
|
|
|
|
|
if message.get("role") == "tool":
|
|
|
|
|
assert str(message.get("tool_call_id")) in declared, (
|
|
|
|
|
f"orphaned tool result {message.get('tool_call_id')!r}: "
|
|
|
|
|
f"{[m.get('role') for m in persisted.messages]}"
|
|
|
|
|
)
|
|
|
|
|
assert [m["role"] for m in persisted.messages] == [
|
|
|
|
|
"user", "user", "assistant", "tool", "assistant",
|
|
|
|
|
]
|
2026-06-12 09:09:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_turn_keeps_placeholder_for_empty_tool_result_blocks() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:empty-tool-blocks")
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [{
|
|
|
|
|
"id": "call_empty",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_empty", "name": "exec", "content": []},
|
|
|
|
|
],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [m["role"] for m in session.messages] == ["assistant", "tool"]
|
|
|
|
|
assert session.messages[1]["content"] == [
|
|
|
|
|
{"type": "text", "text": "[tool result omitted during persistence]"}
|
|
|
|
|
]
|
2026-06-12 09:11:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_turn_drops_orphaned_tool_results() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:orphan-guard")
|
|
|
|
|
session.add_message("user", "hi")
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_ghost", "name": "exec", "content": "boo"},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [m["role"] for m in session.messages] == ["user", "assistant"]
|
|
|
|
|
|
|
|
|
|
|
2026-06-12 11:23:36 +08:00
|
|
|
def test_save_turn_drops_tool_results_without_tool_call_id() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:missing-tool-call-id")
|
|
|
|
|
session.add_message("user", "hi")
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[
|
|
|
|
|
{"role": "tool", "name": "exec", "content": "missing id"},
|
|
|
|
|
{"role": "assistant", "content": "done"},
|
|
|
|
|
],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [m["role"] for m in session.messages] == ["user", "assistant"]
|
|
|
|
|
|
|
|
|
|
|
2026-06-12 09:11:02 +08:00
|
|
|
def test_save_turn_keeps_tool_results_declared_in_prior_history() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:prior-declared")
|
|
|
|
|
session.add_message(
|
|
|
|
|
"assistant",
|
|
|
|
|
"working",
|
|
|
|
|
tool_calls=[{
|
|
|
|
|
"id": "call_prior",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[{"role": "tool", "tool_call_id": "call_prior", "name": "exec", "content": "ok"}],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [m["role"] for m in session.messages] == ["assistant", "tool"]
|
2026-07-02 21:40:12 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_turn_drops_tool_result_already_fulfilled_in_history() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:prior-fulfilled")
|
|
|
|
|
session.add_message(
|
|
|
|
|
"assistant",
|
|
|
|
|
"",
|
|
|
|
|
tool_calls=[{
|
|
|
|
|
"id": "call_prior",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
)
|
|
|
|
|
session.add_message(
|
|
|
|
|
"tool",
|
|
|
|
|
"first",
|
|
|
|
|
tool_call_id="call_prior",
|
|
|
|
|
name="exec",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[{"role": "tool", "tool_call_id": "call_prior", "name": "exec", "content": "duplicate"}],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [m["role"] for m in session.messages] == ["assistant", "tool"]
|
|
|
|
|
assert session.messages[1]["content"] == "first"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_turn_drops_duplicate_tool_result_ids() -> None:
|
|
|
|
|
loop = _mk_loop()
|
|
|
|
|
session = Session(key="test:duplicate-tool-result")
|
|
|
|
|
|
|
|
|
|
loop._save_turn(
|
|
|
|
|
session,
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [{
|
|
|
|
|
"id": "call_dupe",
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {"name": "exec", "arguments": "{}"},
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_dupe", "name": "exec", "content": "first"},
|
|
|
|
|
{"role": "tool", "tool_call_id": "call_dupe", "name": "exec", "content": "second"},
|
|
|
|
|
],
|
|
|
|
|
skip=0,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert [m["role"] for m in session.messages] == ["assistant", "tool"]
|
|
|
|
|
assert session.messages[1]["content"] == "first"
|
2026-11-03 22:47:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_stop_during_goal_completion_publishes_one_terminal_event(tmp_path):
|
|
|
|
|
loop = _make_full_loop(tmp_path)
|
|
|
|
|
loop.max_iterations = 1
|
|
|
|
|
key = "websocket:goal-completion"
|
|
|
|
|
await loop.sessions.state.get(key)
|
|
|
|
|
await loop.sessions.state.update_metadata(key, {
|
|
|
|
|
GOAL_STATE_KEY: {"status": "active", "objective": "Verify the result."},
|
|
|
|
|
"title": "Goal completion", "title_user_edited": True,
|
|
|
|
|
})
|
|
|
|
|
loop.provider.chat_stream_with_retry = AsyncMock(side_effect=[
|
|
|
|
|
LLMResponse(content=None, tool_calls=[ToolCallRequest(
|
|
|
|
|
id="complete", name="update_goal", arguments={"action": "complete", "recap": "Verified."},
|
|
|
|
|
)]),
|
|
|
|
|
LLMResponse(content="Verified."),
|
|
|
|
|
])
|
|
|
|
|
publishing, release = asyncio.Event(), asyncio.Event()
|
|
|
|
|
completed = []
|
|
|
|
|
|
|
|
|
|
async def hold_completion(event):
|
|
|
|
|
completed.append(event)
|
|
|
|
|
publishing.set()
|
|
|
|
|
await release.wait()
|
|
|
|
|
|
|
|
|
|
loop.bus.subscribe(hold_completion, TurnCompleted)
|
|
|
|
|
stop = None
|
|
|
|
|
try:
|
|
|
|
|
await loop._enqueue_session_message(InboundMessage(
|
|
|
|
|
channel="websocket", sender_id="user", chat_id="goal-completion", content="Continue",
|
|
|
|
|
metadata={"webui": True},
|
|
|
|
|
))
|
|
|
|
|
await asyncio.wait_for(publishing.wait(), 5)
|
|
|
|
|
worker, = loop._active_tasks[key]
|
|
|
|
|
stop = asyncio.create_task(loop._cancel_active_tasks(key))
|
|
|
|
|
async with asyncio.timeout(5):
|
|
|
|
|
while not worker.cancelling():
|
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
release.set()
|
|
|
|
|
assert await asyncio.wait_for(stop, 5) == 1
|
|
|
|
|
assert [event.outcome for event in completed] == ["completed"]
|
|
|
|
|
outbound = []
|
|
|
|
|
while not loop.bus.outbound.empty():
|
|
|
|
|
outbound.append(loop.bus.outbound.get_nowait())
|
|
|
|
|
endings = [item.event for item in outbound if isinstance(item.event, TurnEndEvent)]
|
|
|
|
|
assert len(endings) == 1 and endings[0].outcome == "completed"
|
|
|
|
|
assert [item.event.status for item in outbound if isinstance(item.event, GoalStatusEvent)] == [
|
|
|
|
|
"running", "idle",
|
|
|
|
|
]
|
|
|
|
|
saved = await loop.sessions.state.read(key)
|
|
|
|
|
assert saved.metadata[GOAL_STATE_KEY]["status"] == "completed"
|
|
|
|
|
assert saved.messages[-1]["content"] == "Verified."
|
|
|
|
|
finally:
|
|
|
|
|
release.set()
|
|
|
|
|
if stop is not None:
|
|
|
|
|
await asyncio.gather(stop, return_exceptions=True)
|
|
|
|
|
await loop.aclose()
|