import { describe, expect, it } from "vitest"; import { applyAssistantMcpDiscoveryResponsePolicy } from "../src/services/assistantMcpDiscoveryResponsePolicy"; function entryPoint(overrides: Record = {}) { return { schema_version: "assistant_mcp_discovery_runtime_entry_point_v1", policy_owner: "assistantMcpDiscoveryRuntimeEntryPoint", entry_status: "bridge_executed", hot_runtime_wired: false, discovery_attempted: true, turn_input: { adapter_status: "ready" }, bridge: { bridge_status: "answer_draft_ready", user_facing_response_allowed: true, business_fact_answer_allowed: true, requires_user_clarification: false, answer_draft: { answer_mode: "confirmed_with_bounded_inference", headline: "Confirmed scoped answer.", confirmed_lines: ["Confirmed fact"], inference_lines: ["Bounded inference"], unknown_lines: ["Unconfirmed fact"], limitation_lines: ["Limited source window"], next_step_line: null } }, reason_codes: ["runtime_entry_point_bridge_executed"], ...overrides } as any; } describe("assistant MCP discovery response policy", () => { it("applies a guarded candidate only for unsupported current-turn boundary replies", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "route is not wired", currentReplySource: "deterministic_unsupported_current_turn_boundary", modeDecisionReason: "unsupported_current_turn_meaning_boundary", addressRuntimeMeta: { mcpDiscoveryRuntimeEntryPoint: entryPoint() } }); expect(result.applied).toBe(true); expect(result.decision).toBe("apply_candidate"); expect(result.reply_source).toBe("mcp_discovery_response_candidate_guarded"); expect(result.reply_text).toContain("Confirmed fact"); expect(result.reply_text).not.toContain("query_documents"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_candidate_applied"); }); it("keeps the current reply when the turn is not an unsupported boundary", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "regular chat", currentReplySource: "llm_chat", modeDecisionReason: "living_chat_signal_detected", addressRuntimeMeta: { mcpDiscoveryRuntimeEntryPoint: entryPoint() } }); expect(result.applied).toBe(false); expect(result.decision).toBe("keep_current_reply"); expect(result.reply_text).toBe("regular chat"); expect(result.reply_source).toBe("llm_chat"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_not_unsupported_boundary"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_not_discovery_ready_chat_candidate"); }); it("applies a guarded candidate for discovery-ready llm chat business answers", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "stale llm business answer", currentReplySource: "llm_chat", modeDecisionReason: "non_domain_query_indexed", addressRuntimeMeta: { mcpDiscoveryRuntimeEntryPoint: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true } }) } }); expect(result.applied).toBe(true); expect(result.decision).toBe("apply_candidate"); expect(result.reply_source).toBe("mcp_discovery_response_candidate_guarded"); expect(result.reply_text).toContain("Confirmed fact"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_not_unsupported_boundary"); expect(result.reason_codes).not.toContain("mcp_discovery_response_policy_not_discovery_ready_chat_candidate"); }); it("applies a guarded candidate for discovery-ready deep partial answers", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "wrong deep partial answer", currentReplySource: "deep_analysis", addressRuntimeMeta: { mcpDiscoveryRuntimeEntryPoint: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true } }) } }); expect(result.applied).toBe(true); expect(result.reply_source).toBe("mcp_discovery_response_candidate_guarded"); expect(result.reply_text).toContain("Confirmed fact"); expect(result.reason_codes).not.toContain("mcp_discovery_response_policy_not_discovery_ready_deep_candidate"); }); it("applies a guarded candidate for discovery-ready address lane answers", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "stale exact route answer", currentReplySource: "address_query_runtime_v1", currentReplyType: "factual", addressRuntimeMeta: { detected_intent: "list_documents_by_counterparty", assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true, turn_meaning_ref: { asked_domain_family: "counterparty_value", asked_action_family: "payout" } } }) } }); expect(result.applied).toBe(true); expect(result.reply_source).toBe("mcp_discovery_response_candidate_guarded"); expect(result.reply_text).toContain("Confirmed fact"); expect(result.reason_codes).not.toContain("mcp_discovery_response_policy_not_discovery_ready_address_candidate"); }); it("keeps aligned factual address lane answers when the exact lane already matched the same semantic intent", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "ИП Калинин Н.М. | сумма: 216600 | операций: 2", currentReplySource: "address_query_runtime_v1", currentReplyType: "factual", addressRuntimeMeta: { detected_intent: "customer_revenue_and_payments", assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true, turn_meaning_ref: { asked_domain_family: "counterparty_value", asked_action_family: "turnover" } } }) } }); expect(result.applied).toBe(false); expect(result.decision).toBe("keep_current_reply"); expect(result.reply_text).toBe("ИП Калинин Н.М. | сумма: 216600 | операций: 2"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_keep_aligned_factual_address_reply"); }); it("keeps factual address follow-up replies when they already match the continuation target intent", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "ИП Калинин Н.М. | сумма: 216600 | операций: 2", currentReplySource: "address_query_runtime_v1", currentReplyType: "factual", addressRuntimeMeta: { detected_intent: "customer_revenue_and_payments", dialogContinuationContract: { target_intent: "customer_revenue_and_payments" }, assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true, turn_meaning_ref: { asked_domain_family: "counterparty_value", asked_action_family: "turnover" } } }) } }); expect(result.applied).toBe(false); expect(result.decision).toBe("keep_current_reply"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_keep_factual_address_continuation_target"); }); it("keeps full-confirmed factual address replies even when discovery has a guarded candidate", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "ООО Ромашка | сумма: 128000 | операций: 3", currentReplySource: "address_query_runtime_v1", currentReplyType: "factual", addressRuntimeMeta: { detected_intent: "receivables_confirmed_as_of_date", truth_gate_contract_status: "full_confirmed", assistant_truth_answer_policy_v1: { truth_gate: { coverage_status: "full", grounding_status: "grounded", source_truth_gate_status: "full_confirmed" } }, assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true, turn_meaning_ref: { asked_domain_family: "receivables", asked_action_family: "confirmed_snapshot" } } }) } }); expect(result.applied).toBe(false); expect(result.decision).toBe("keep_current_reply"); expect(result.reply_text).toBe("ООО Ромашка | сумма: 128000 | операций: 3"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_keep_full_confirmed_factual_address_reply"); }); it("overrides a stale full-confirmed lifecycle reply when discovery proves a different net-flow question", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "Коротко: активных заказчиков в 2020 году — 1.", currentReplySource: "address_query_runtime_v1", currentReplyType: "factual", addressRuntimeMeta: { detected_intent: "counterparty_activity_lifecycle", truth_gate_contract_status: "full_confirmed", assistant_truth_answer_policy_v1: { truth_gate: { coverage_status: "full", grounding_status: "grounded", source_truth_gate_status: "full_confirmed" } }, dialog_continuation_contract_v2: { target_intent: "counterparty_activity_lifecycle" }, assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true, turn_meaning_ref: { asked_domain_family: "counterparty_value", asked_action_family: "net_value_flow", explicit_entity_candidates: ["Группа СВК"], explicit_organization_scope: "ООО Альтернатива Плюс", explicit_date_scope: "2020", unsupported_but_understood_family: "counterparty_bidirectional_value_flow_or_netting" } }, bridge: { bridge_status: "answer_draft_ready", user_facing_response_allowed: true, business_fact_answer_allowed: true, requires_user_clarification: false, answer_draft: { answer_mode: "confirmed_with_bounded_inference", headline: "По данным 1С найдены строки входящих и исходящих денежных движений.", confirmed_lines: ["Получили 47 628 853,03 руб.; заплатили 43 763 351,53 руб.; нетто 3 865 501,50 руб."], inference_lines: [], unknown_lines: ["Полное сальдо вне проверенного окна не подтверждено."], limitation_lines: [], next_step_line: null } } }) } }); expect(result.applied).toBe(true); expect(result.decision).toBe("apply_candidate"); expect(result.reply_source).toBe("mcp_discovery_response_candidate_guarded"); expect(result.reply_text).toContain("47 628 853,03"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_semantic_conflict_allows_candidate_override"); expect(result.reason_codes).not.toContain("mcp_discovery_response_policy_keep_full_confirmed_factual_address_reply"); }); it("keeps address lane answers when discovery was not requested for the current turn", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "supported exact route answer", currentReplySource: "address_query_runtime_v1", addressRuntimeMeta: { assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "not_applicable", should_run_discovery: false } }) } }); expect(result.applied).toBe(false); expect(result.reply_text).toBe("supported exact route answer"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_not_discovery_ready_address_candidate"); }); it("keeps the current reply when the candidate has no grounded text", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "route is not wired", currentReplySource: "deterministic_unsupported_current_turn_boundary", modeDecisionReason: "unsupported_current_turn_meaning_boundary", addressRuntimeMeta: { mcpDiscoveryRuntimeEntryPoint: entryPoint({ bridge: { bridge_status: "unsupported", user_facing_response_allowed: true, business_fact_answer_allowed: false, requires_user_clarification: false, answer_draft: null } }) } }); expect(result.applied).toBe(false); expect(result.reply_text).toBe("route is not wired"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_candidate_not_eligible"); expect(result.reason_codes).toContain("mcp_discovery_response_policy_kept_current_reply"); }); it("keeps deterministic broad business evaluation summary instead of replacing it with a clarification candidate", () => { const result = applyAssistantMcpDiscoveryResponsePolicy({ currentReply: "Коротко: по уже подтвержденным данным в 1С компания выглядит живой операционно.", currentReplySource: "deterministic_broad_business_evaluation_contract", livingChatSource: "deterministic_broad_business_evaluation_contract", modeDecisionReason: "unsupported_current_turn_meaning_boundary", addressRuntimeMeta: { assistant_mcp_discovery_entry_point_v1: entryPoint({ turn_input: { adapter_status: "ready", should_run_discovery: true, turn_meaning_ref: { asked_domain_family: "business_summary", asked_action_family: "broad_evaluation", unsupported_but_understood_family: "broad_business_evaluation", stale_replay_forbidden: true } }, bridge: { bridge_status: "needs_clarification", user_facing_response_allowed: true, business_fact_answer_allowed: false, requires_user_clarification: true, answer_draft: { answer_mode: "needs_clarification", headline: "Нужно уточнить контекст перед поиском в 1С.", confirmed_lines: [], inference_lines: [], unknown_lines: ["MCP discovery pilot needs more scope before execution"], limitation_lines: ["MCP discovery pilot needs more scope before execution"], next_step_line: "Уточните контрагента, период или организацию." } } }) } }); expect(result.applied).toBe(false); expect(result.decision).toBe("keep_current_reply"); expect(result.reply_source).toBe("deterministic_broad_business_evaluation_contract"); expect(result.reply_text).toContain("компания выглядит живой операционно"); expect(result.reason_codes).toContain( "mcp_discovery_response_policy_keep_broad_business_summary_over_clarification_candidate" ); }); });