153 lines
4.7 KiB
Python
153 lines
4.7 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from gfa_core.mas.agents.router_runtime import (
|
|
RouteDecision,
|
|
RouterRuntime,
|
|
)
|
|
from gfa_core.mas.workflow_models import GoalLedger
|
|
|
|
|
|
class _PassThroughTools:
|
|
@staticmethod
|
|
def resolve_analysis_handoff_request(
|
|
query: str,
|
|
*,
|
|
workflow_id: str | None = None,
|
|
guidance: bool = False,
|
|
) -> str:
|
|
return query
|
|
|
|
|
|
class _ContextEnrichingTools:
|
|
@staticmethod
|
|
def resolve_analysis_handoff_request(
|
|
query: str,
|
|
*,
|
|
workflow_id: str | None = None,
|
|
guidance: bool = False,
|
|
) -> str:
|
|
return (
|
|
f"{query}\n已确认上下文:当前 job 的 subfamily_confirm 已成功完成,"
|
|
"家族成员和亚家族分类表已确认。"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("query", "workflow_id", "clarification_question"),
|
|
(
|
|
(
|
|
"执行rnaseq",
|
|
"rnaseq_whole_genome",
|
|
"请选择全基因组还是基因家族成员RNA-seq",
|
|
),
|
|
("执行全基因组rnaseq", "rnaseq_whole_genome", None),
|
|
("对已确认家族成员执行rnaseq", "rnaseq_family_member", None),
|
|
("执行motif分析", "gene_family", None),
|
|
),
|
|
)
|
|
def test_new_analysis_proposal_preserves_original_request_without_main_agent_planning(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
query: str,
|
|
workflow_id: str,
|
|
clarification_question: str | None,
|
|
) -> None:
|
|
runtime = RouterRuntime(object(), _PassThroughTools(), max_iterations=5) # type: ignore[arg-type]
|
|
|
|
async def decide_route(**_: Any) -> tuple[RouteDecision, int, list[dict[str, Any]]]:
|
|
return (
|
|
RouteDecision(
|
|
goal="new_analysis",
|
|
needs=["analysis_intent"],
|
|
subject=query,
|
|
clarification_question=clarification_question,
|
|
workflow_id=workflow_id, # type: ignore[arg-type]
|
|
analysis_request_kind="execution",
|
|
),
|
|
1,
|
|
[],
|
|
)
|
|
|
|
monkeypatch.setattr(runtime, "_decide_route", decide_route)
|
|
|
|
outcome = asyncio.run(
|
|
runtime.run(
|
|
query=query,
|
|
history=[],
|
|
session_id="analysis-handoff-session",
|
|
job_id="job-analysis-handoff",
|
|
goal_ledger=GoalLedger(
|
|
conversation_id="analysis-handoff-session",
|
|
revision=0,
|
|
user_terms={"active_job_id": "job-analysis-handoff"},
|
|
),
|
|
)
|
|
)
|
|
|
|
assert outcome.action == "clarify"
|
|
assert outcome.proposal is not None
|
|
assert outcome.proposal["request"] == query
|
|
assert outcome.proposal["original_request"] == query
|
|
assert "workflow_id" not in outcome.proposal
|
|
assert "全基因组还是" not in outcome.message
|
|
|
|
|
|
def test_context_enrichment_does_not_replace_original_analysis_handoff() -> None:
|
|
runtime = RouterRuntime(object(), _ContextEnrichingTools(), max_iterations=5) # type: ignore[arg-type]
|
|
|
|
async def decide_route(**_: Any) -> tuple[RouteDecision, int, list[dict[str, Any]]]:
|
|
return (
|
|
RouteDecision(
|
|
goal="new_analysis",
|
|
needs=["analysis_intent"],
|
|
subject="执行kaks分析",
|
|
workflow_id="gene_family",
|
|
analysis_request_kind="execution",
|
|
),
|
|
1,
|
|
[],
|
|
)
|
|
|
|
runtime._decide_route = decide_route # type: ignore[method-assign]
|
|
outcome = asyncio.run(
|
|
runtime.run(
|
|
query="执行kaks分析",
|
|
history=[],
|
|
session_id="kaks-handoff-session",
|
|
job_id="job-kaks-handoff",
|
|
goal_ledger=GoalLedger(
|
|
conversation_id="kaks-handoff-session",
|
|
revision=0,
|
|
user_terms={"active_job_id": "job-kaks-handoff"},
|
|
),
|
|
)
|
|
)
|
|
|
|
assert outcome.proposal is not None
|
|
assert "subfamily_confirm" in outcome.proposal["request"]
|
|
assert outcome.proposal["original_request"] == "执行kaks分析"
|
|
|
|
|
|
def test_new_analysis_decision_discards_main_agent_workflow_and_clarification() -> None:
|
|
decision = RouteDecision(
|
|
goal="new_analysis",
|
|
needs=["analysis_intent"],
|
|
subject="执行rnaseq",
|
|
clarification_question="请选择RNA-seq模式",
|
|
workflow_id="rnaseq_whole_genome",
|
|
analysis_request_kind="execution",
|
|
)
|
|
|
|
normalized = RouterRuntime._analysis_handoff_decision(decision)
|
|
|
|
assert normalized.goal == "new_analysis"
|
|
assert normalized.needs == ["analysis_intent"]
|
|
assert normalized.subject == decision.subject
|
|
assert normalized.workflow_id is None
|
|
assert normalized.clarification_question is None
|
|
assert normalized.analysis_request_kind == "execution"
|