Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python
24.5 KB
Raw Blame
"""Tests for the Goal capability adapter used by JiuwenSwarm."""
from __future__ import annotations

import sys
from pathlib import Path
from types import SimpleNamespace

import pytest

from jiuwenswarm.server.handlers import _default
from jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers import (
    _is_interrupt_resume_dispatch,
    _parse_goal_slash_intent,
    _record_goal_completed_history_if_needed,
    _record_goal_set_history_if_needed,
    _structured_goal_op_from_request,
)
from jiuwenswarm.server.runtime.agent_adapter.deep.stream_parse import _parse_stream_chunk

# Goal capability tests exercise the checked-out OpenJiuwen implementation,
# not whichever released package happens to be installed in the test venv.
_AGENT_CORE_ROOT = Path(__file__).resolve().parents[3].parent / "agent-core"
if str(_AGENT_CORE_ROOT) not in sys.path:
    sys.path.insert(0, str(_AGENT_CORE_ROOT))

from openjiuwen.core.session.stream import OutputSchema
from openjiuwen.harness.goal.schema import GoalOperationError, GoalRecord, GoalStatus
from openjiuwen.harness.schema.interaction import InteractionEventType

from jiuwenswarm.common.schema.agent import AgentRequest
from jiuwenswarm.common.schema.message import ReqMethod
from jiuwenswarm.server.runtime.agent_adapter.interface import (
    JiuWenSwarm,
    _should_record_user_history,
)
from jiuwenswarm.server.runtime.agent_adapter.interface_deep import JiuWenSwarmDeepAdapter


class _FakeGoals:
    def __init__(self) -> None:
        self.record: GoalRecord | None = None
        self.calls: list[tuple[str, object]] = []

    async def get(self) -> GoalRecord | None:
        self.calls.append(("get", None))
        return self.record

    def get_store(self):
        return SimpleNamespace(load=lambda: self.record)

    async def set(
        self,
        objective: str,
        *,
        overwrite_confirmed: bool = False,
        token_budget: int | None = None,
        max_attempts: int | None = None,
    ) -> GoalRecord:
        self.calls.append(("set", objective))
        if not objective.strip():
            raise GoalOperationError(
                operation="set",
                code="invalid_objective",
                message="goal objective must not be empty",
            )
        if self.record is not None and not overwrite_confirmed:
            raise GoalOperationError(
                operation="set",
                code="already_exists",
                message="a goal already exists for this session",
                goal=self.record,
            )
        self.record = GoalRecord.create(session_id="session-1", objective=objective)
        return self.record

    async def pause(self) -> GoalRecord | None:
        self.calls.append(("pause", None))
        if self.record is None:
            return None
        if self.record.status is GoalStatus.ACTIVE:
            self.record.status = GoalStatus.PAUSED
        return self.record

    async def resume(self) -> GoalRecord | None:
        self.calls.append(("resume", None))
        if self.record is None:
            return None
        if self.record.status in (GoalStatus.PAUSED, GoalStatus.BLOCKED):
            self.record.status = GoalStatus.ACTIVE
        return self.record

    async def clear(self) -> GoalRecord | None:
        self.calls.append(("clear", None))
        removed = self.record
        self.record = None
        return removed


def _adapter(goal_manager: _FakeGoals) -> JiuWenSwarmDeepAdapter:
    adapter = JiuWenSwarmDeepAdapter.__new__(JiuWenSwarmDeepAdapter)
    adapter._instance = SimpleNamespace(goal_manager=goal_manager)
    adapter._is_session_scoped_adapter = True
    adapter._stream_content_run_kind = None
    return adapter


def _goal_request(params: dict[str, object], *, session_id: str = "session-1") -> AgentRequest:
    return AgentRequest(
        request_id="req-goal-test",
        channel_id="web",
        session_id=session_id,
        params=params,
    )


class _FakeGoalAdapter:
    async def handle_goal_command_structured(
        self,
        request: AgentRequest,
        *,
        session_id: str | None = None,
    ) -> dict[str, object]:
        params = request.params if isinstance(request.params, dict) else {}
        return {
            "result_type": "goal_control",
            "action": params.get("action", "get"),
            "goal": None,
            "output": "No goal in this session.",
        }


class _FakeSessionManager:
    def get_session_id(self, session_id: str | None) -> str:
        return session_id or "default"


@pytest.mark.asyncio
async def test_structured_set_calls_goal_capability_and_returns_stream_intent() -> None:
    goals = _FakeGoals()
    result = await _adapter(goals).handle_goal_command_structured(
        _goal_request({"action": "set", "objective": "ship the feature"}),
        session_id="session-1",
    )

    assert goals.calls == [("set", "ship the feature")]
    assert result is not None
    assert result["result_type"] == "goal_stream"
    assert result["goal"]["objective"] == "ship the feature"


@pytest.mark.asyncio
async def test_resume_without_goal_is_a_normal_control_response() -> None:
    result = await _adapter(_FakeGoals()).handle_goal_command_structured(
        _goal_request({"action": "resume"}),
        session_id="session-1",
    )

    assert result is not None
    assert result["result_type"] == "goal_error"
    assert result["error_code"] == "no_goal"
    assert result["goal"] is None
    assert "cannot resume" in str(result["error"])


@pytest.mark.asyncio
async def test_pause_without_goal_returns_no_goal_error() -> None:
    result = await _adapter(_FakeGoals()).handle_goal_command_structured(
        _goal_request({"action": "pause"}),
        session_id="session-1",
    )
    assert result is not None
    assert result["result_type"] == "goal_error"
    assert result["error_code"] == "no_goal"


@pytest.mark.asyncio
async def test_clear_without_goal_returns_no_goal_error() -> None:
    result = await _adapter(_FakeGoals()).handle_goal_command_structured(
        _goal_request({"action": "clear"}),
        session_id="session-1",
    )
    assert result is not None
    assert result["result_type"] == "goal_error"
    assert result["error_code"] == "no_goal"


@pytest.mark.asyncio
async def test_resume_completed_goal_returns_invalid_state() -> None:
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="done")
    goals.record.status = GoalStatus.COMPLETED
    result = await _adapter(goals).handle_goal_command_structured(
        _goal_request({"action": "resume"}),
        session_id="session-1",
    )
    assert result is not None
    assert result["result_type"] == "goal_error"
    assert result["error_code"] == "invalid_state"
    assert result["goal"]["status"] == "completed"


@pytest.mark.asyncio
async def test_pause_non_active_goal_returns_invalid_state() -> None:
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="paused")
    goals.record.status = GoalStatus.PAUSED
    result = await _adapter(goals).handle_goal_command_structured(
        _goal_request({"action": "pause"}),
        session_id="session-1",
    )
    assert result is not None
    assert result["result_type"] == "goal_error"
    assert result["error_code"] == "invalid_state"


@pytest.mark.asyncio
async def test_set_existing_goal_returns_confirmation_data() -> None:
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="old goal")

    result = await _adapter(goals).handle_goal_command_structured(
        _goal_request({"action": "set", "objective": "new goal"}),
        session_id="session-1",
    )

    assert result is not None
    assert result["result_type"] == "goal_confirm_required"
    assert result["existing_goal"]["objective"] == "old goal"
    assert result["requested_objective"] == "new goal"


@pytest.mark.asyncio
async def test_slash_goal_parser_uses_set_argument_or_bare_objective() -> None:
    goals = _FakeGoals()
    adapter = _adapter(goals)

    explicit = await adapter._handle_goal_slash_command("/goal set write a report")
    bare = await adapter._handle_goal_slash_command("/goal write a report")
    empty = await adapter._handle_goal_slash_command("/goal set")

    assert explicit is not None
    assert explicit["goal"]["objective"] == "write a report"
    assert bare is not None
    assert bare["result_type"] == "goal_confirm_required"
    assert empty is not None
    assert empty["result_type"] == "goal_error"
    assert empty["error_code"] == "invalid_objective"


def test_runtime_events_are_adapted_without_leaking_runtime_objects() -> None:
    goal = GoalRecord.create(session_id="session-1", objective="ship the feature").to_dict()
    updated = _parse_stream_chunk(
        OutputSchema(
            type=InteractionEventType.GOAL_UPDATED.value,
            index=0,
            payload={"goal": goal},
        )
    )
    failed = _parse_stream_chunk(
        OutputSchema(
            type=InteractionEventType.EXECUTION_ERROR.value,
            index=0,
            payload={"code": "round_execution_error", "message": "round failed"},
        )
    )

    assert updated == {"event_type": "goal.updated", "goal": goal}
    assert failed == {
        "event_type": "execution.error",
        "code": "round_execution_error",
        "message": "round failed",
        "goal": None,
    }


def test_empty_answer_chunk_is_dropped_not_emitted_as_chat_final() -> None:
    """Empty answer is not a UI closer — discard for both Goal and normal chat."""
    assert (
        _parse_stream_chunk(
            {
                "type": "answer",
                "payload": {"output": {"output": "", "chunked": False}},
            }
        )
        is None
    )
    assert (
        _parse_stream_chunk(
            {
                "type": "answer",
                "payload": {"output": {"output": "   ", "chunked": False}},
            },
            _has_streamed_content=True,
        )
        is None
    )


def test_runtime_goal_update_payload_is_always_nested_under_goal() -> None:
    goal = GoalRecord.create(session_id="session-1", objective="ship the feature").to_dict()
    updated = _parse_stream_chunk(
        {
            "type": InteractionEventType.GOAL_UPDATED.value,
            "payload": goal,
        }
    )
    cleared = _parse_stream_chunk(
        {
            "type": InteractionEventType.GOAL_UPDATED.value,
            "payload": {"goal": None},
        }
    )

    assert updated == {"event_type": "goal.updated", "goal": goal}
    assert cleared == {"event_type": "goal.updated", "goal": None}


@pytest.mark.asyncio
async def test_command_goal_unary_response_stays_rpc_payload() -> None:
    facade = JiuWenSwarm.__new__(JiuWenSwarm)
    facade._adapter = _FakeGoalAdapter()
    facade._session_manager = _FakeSessionManager()

    response = await facade.process_message(
        AgentRequest(
            request_id="goal-get-1",
            channel_id="tui",
            session_id="session-1",
            req_method=ReqMethod.COMMAND_GOAL,
            params={"action": "get"},
        )
    )

    assert response.ok is True
    assert response.payload is not None
    assert "event_type" not in response.payload
    assert response.payload["record"] is None
    assert response.payload["message"] == "No goal in this session."


def test_active_goal_demotes_goal_round_chat_final_to_delta() -> None:
    """Goal multi-attempt middle: demote final so the frontend does not split."""
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    adapter = _adapter(goals)
    adapter._instance.active_round = SimpleNamespace(run_kind="goal")
    adapter._instance.interaction_started = True
    adapter._stream_content_run_kind = "goal"

    payload = adapter._adapt_goal_intermediate_final(
        {"event_type": "chat.final", "content": "attempt output"}
    )

    assert payload == {
        "event_type": "chat.delta",
        "content": "attempt output",
        "goal_intermediate": True,
    }


def test_active_goal_keeps_user_round_chat_final_terminal() -> None:
    """User answer finishes while Goal is still ACTIVE: keep real chat.final."""
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    adapter = _adapter(goals)
    adapter._instance.active_round = SimpleNamespace(run_kind="user")
    adapter._instance.interaction_started = True
    adapter._stream_content_run_kind = "user"

    payload = adapter._adapt_goal_intermediate_final(
        {"event_type": "chat.final", "content": "ordinary answer"}
    )

    assert payload == {
        "event_type": "chat.final",
        "content": "ordinary answer",
    }


def test_late_user_final_stays_terminal_after_goal_round_started() -> None:
    """Provenance beats live active_round when user final arrives late."""
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    adapter = _adapter(goals)
    adapter._instance.active_round = SimpleNamespace(run_kind="goal")
    adapter._instance.interaction_started = True
    adapter._stream_content_run_kind = "user"

    payload = adapter._adapt_goal_intermediate_final(
        {"event_type": "chat.final", "content": "ordinary answer"}
    )

    assert payload == {
        "event_type": "chat.final",
        "content": "ordinary answer",
    }


def test_user_to_goal_content_injects_bubble_split_final() -> None:
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    adapter = _adapter(goals)
    adapter._instance.active_round = SimpleNamespace(run_kind="goal")
    adapter._instance.interaction_started = True
    adapter._stream_content_run_kind = "user"

    boundary = adapter._begin_visible_chat_content()

    assert boundary == {"event_type": "chat.final", "content": ""}
    assert adapter._stream_content_run_kind == "goal"


def test_active_goal_keeps_chat_final_when_no_goal_round_in_flight() -> None:
    """Between user end and next Goal start, do not demote (no goal round)."""
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    adapter = _adapter(goals)
    adapter._instance.active_round = None
    adapter._instance.interaction_started = True

    payload = adapter._adapt_goal_intermediate_final(
        {"event_type": "chat.final", "content": "ordinary answer"}
    )

    assert payload == {
        "event_type": "chat.final",
        "content": "ordinary answer",
    }


def test_interrupt_resume_dispatch_injects_even_without_output_lease() -> None:
    """Permission answers must send_input when Goal already holds the lease."""
    assert _is_interrupt_resume_dispatch(
        {
            "source": "permission_interrupt",
            "request_id": "call_1",
            "answers": [{"selected_options": ["allow_once"]}],
        }
    )
    adapter = _adapter(_FakeGoals())
    assert adapter._should_inject_into_existing_interaction(
        {
            "source": "permission_interrupt",
            "request_id": "call_1",
            "answers": [{"selected_options": ["allow_once"]}],
        }
    )
    assert not adapter._should_inject_into_existing_interaction(
        {"query": "hello", "mode": "agent"}
    )


def test_paused_goal_keeps_chat_final_terminal_even_if_round_still_unwinding() -> None:
    """User cancel pauses GoalRecord first; finals during abort must stay final."""
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    goals.record.status = GoalStatus.PAUSED
    adapter = _adapter(goals)
    # Simulate an in-flight goal round still unwinding after pause+cancel.
    adapter._instance.active_round = SimpleNamespace(run_kind="goal")
    adapter._instance.interaction_started = True

    payload = adapter._adapt_goal_intermediate_final(
        {"event_type": "chat.final", "content": "cancelled output"}
    )

    assert payload == {
        "event_type": "chat.final",
        "content": "cancelled output",
    }
    assert adapter._has_active_goal_interaction() is True
    assert adapter._goal_record_is_active() is False


def test_internal_goal_resume_is_not_recorded_as_user_history() -> None:
    assert _should_record_user_history(
        {
            "query": "/goal resume",
            "runtime_mode": "follow_up",
            "log_as_user": False,
        }
    ) is False
    assert _should_record_user_history({"query": "hello"}) is True


def test_completed_goal_keeps_chat_final_terminal() -> None:
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship the feature")
    goals.record.status = GoalStatus.COMPLETED
    adapter = _adapter(goals)

    payload = adapter._adapt_goal_intermediate_final(
        {"event_type": "chat.final", "content": "final output"}
    )

    assert payload == {"event_type": "chat.final", "content": "final output"}


def test_structured_command_goal_set_maps_to_pending_op() -> None:
    req = AgentRequest(
        request_id="g1",
        channel_id="web",
        session_id="session-1",
        req_method=ReqMethod.COMMAND_GOAL,
        params={
            "action": "set",
            "objective": "ship it",
            "overwrite_confirmed": True,
        },
    )
    op = _structured_goal_op_from_request(req)
    assert op == {
        "action": "set",
        "objective": "ship it",
        "overwrite_confirmed": True,
    }


def test_chat_send_does_not_map_to_structured_goal_op() -> None:
    req = AgentRequest(
        request_id="c1",
        channel_id="web",
        session_id="session-1",
        req_method=ReqMethod.CHAT_SEND,
        params={"query": "/goal set do not parse on web", "mode": "agent"},
    )
    assert _structured_goal_op_from_request(req) is None


def test_readonly_goal_get_skips_metadata_sync_flag() -> None:
    """command.goal get is read-only; set/resume/pause/clear are not."""
    from jiuwenswarm.server.agent_ws_server import AgentWebSocketServer

    get_req = AgentRequest(
        request_id="g1",
        channel_id="web",
        session_id="ghost_goal_get",
        req_method=ReqMethod.COMMAND_GOAL,
        params={"action": "get", "mode": "agent"},
    )
    assert _default._is_readonly_goal_get_request(get_req) is True
    # Default action is get when omitted.
    assert _default._is_readonly_goal_get_request(
        AgentRequest(
            request_id="g2",
            channel_id="web",
            session_id="ghost_goal_get",
            req_method=ReqMethod.COMMAND_GOAL,
            params={},
        )
    ) is True
    for action in ("set", "resume", "pause", "clear"):
        assert _default._is_readonly_goal_get_request(
            AgentRequest(
                request_id=f"a-{action}",
                channel_id="web",
                session_id="s1",
                req_method=ReqMethod.COMMAND_GOAL,
                params={"action": action},
            )
        ) is False


def test_web_channel_does_not_treat_goal_slash_as_intent() -> None:
    assert (
        _parse_goal_slash_intent("/goal set write a report")
        == {"action": "set", "objective": "write a report"}
    )
    # Parsing exists, but stream path only applies it when channel_id == "tui".
    # Web must use command.goal; this documents the contract for callers.
    req = AgentRequest(
        request_id="w1",
        channel_id="web",
        session_id="session-1",
        req_method=ReqMethod.CHAT_SEND,
        params={"query": "/goal set write a report"},
    )
    assert _structured_goal_op_from_request(req) is None


def test_stream_end_synthesizes_final_after_goal_cleared() -> None:
    """pause→clear ends the iterator without a final; host must synthesize one."""
    goals = _FakeGoals()
    adapter = _adapter(goals)
    goals.record = None

    assert adapter._should_emit_stream_end_chat_final(
        had_assistant_output=True,
        emitted_terminal_chat_final=False,
    )


def test_stream_end_synthesizes_final_even_before_assistant_tokens() -> None:
    """Goal.set may be processing before the first delta; clear still needs a final."""
    goals = _FakeGoals()
    adapter = _adapter(goals)
    goals.record = None

    assert adapter._should_emit_stream_end_chat_final(
        had_assistant_output=False,
        emitted_terminal_chat_final=False,
    )


def test_stream_end_skips_final_when_goal_still_active() -> None:
    goals = _FakeGoals()
    goals.record = GoalRecord.create(session_id="session-1", objective="ship")
    adapter = _adapter(goals)

    assert not adapter._should_emit_stream_end_chat_final(
        had_assistant_output=True,
        emitted_terminal_chat_final=False,
    )


def test_stream_end_skips_final_when_already_emitted() -> None:
    goals = _FakeGoals()
    adapter = _adapter(goals)
    goals.record = None

    assert not adapter._should_emit_stream_end_chat_final(
        had_assistant_output=True,
        emitted_terminal_chat_final=True,
    )


def test_record_goal_set_history_writes_objective_flags(monkeypatch: pytest.MonkeyPatch) -> None:
    captured: list[dict] = []
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers.append_history_record",
        lambda **kwargs: captured.append(kwargs),
    )
    req = AgentRequest(
        request_id="r1",
        channel_id="web",
        session_id="s1",
        req_method=ReqMethod.COMMAND_GOAL,
        params={"mode": "agent"},
    )
    _record_goal_set_history_if_needed(
        req,
        action="set",
        result_type="goal_stream",
        goal_payload={"goal_id": "g1", "objective": "ship it"},
    )
    assert len(captured) == 1
    assert captured[0]["role"] == "user"
    assert captured[0]["content"] == "ship it"
    assert captured[0]["extra"]["is_goal_objective_message"] is True
    assert captured[0]["extra"]["goal_id"] == "g1"

    captured.clear()
    _record_goal_set_history_if_needed(
        req,
        action="pause",
        result_type="ok",
        goal_payload={"goal_id": "g1", "objective": "ship it"},
    )
    assert captured == []


def test_record_goal_completed_history_is_idempotent(monkeypatch: pytest.MonkeyPatch) -> None:
    captured: list[dict] = []
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers._GOAL_COMPLETED_PERSISTED", set())
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers.append_history_record",
        lambda **kwargs: captured.append(kwargs),
    )
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers.load_history_records",
        lambda _sid: [],
    )
    _record_goal_completed_history_if_needed(
        session_id="s1",
        channel_id="web",
        channel_metadata=None,
        mode="agent",
        goal_payload={
            "goal_id": "g1",
            "status": "completed",
            "last_assessment": {"evidence": "all done"},
        },
    )
    assert len(captured) == 1
    assert captured[0]["extra"]["is_goal_completed_message"] is True
    assert captured[0]["extra"]["id"] == "goal-completed-g1"
    assert captured[0]["content"].startswith("goal.completed:")

    # 异步落盘未完成(磁盘仍为空)时同 goal 再次触发,由进程内守卫去重
    _record_goal_completed_history_if_needed(
        session_id="s1",
        channel_id="web",
        channel_metadata=None,
        mode="agent",
        goal_payload={
            "goal_id": "g1",
            "status": "completed",
            "last_assessment": {"evidence": "all done"},
        },
    )
    assert len(captured) == 1

    # 跨进程场景:守卫为空、完成卡已在磁盘,由磁盘检查去重
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers._GOAL_COMPLETED_PERSISTED", set())
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_adapter.deep.pure_helpers.load_history_records",
        lambda _sid: [
            {
                "id": "goal-completed-g1",
                "is_goal_completed_message": True,
                "goal_id": "g1",
            }
        ],
    )
    captured.clear()
    _record_goal_completed_history_if_needed(
        session_id="s1",
        channel_id="web",
        channel_metadata=None,
        mode="agent",
        goal_payload={"goal_id": "g1", "status": "completed"},
    )
    assert captured == []

    _record_goal_completed_history_if_needed(
        session_id="s1",
        channel_id="web",
        channel_metadata=None,
        mode="agent",
        goal_payload={"goal_id": "g2", "status": "active"},
    )
    assert captured == []