Files
Jyotisha/tests/test_consultation_workflow_domains.py
Jesse_Chen e1db576284
Independent Staging Quality Gate / validate (push) Successful in 14m21s
Independent Staging Quality Gate / publish (push) Has been cancelled
fix(consult): let the declared domain decide the route, not the question text
A staging consultation asked one question about two domains, career and
wealth. The model planned both, and all four attempts failed identically with
calculation_failed. The server resolves the workflow route from the question
text — an explicit-timing check, then keyword domain_tokens, falling back to
the themes argument only when the text yields nothing — while the frontend
declares strict_workflow_route from the domain it chose. The plan contract then
requires the text-derived route to equal the declared one, and each
RouteContract allows exactly one, so the mismatch became
ConsultationPlanContractError, BadRequest, HTTP 400, workflow_bad_request.
Here "事业" is in the career token list and "财运" is not in the wealth one, so
both calls resolved to career and the wealth call was rejected every time.

Widening the token list would only move the contradiction to the next
phrasing. The frontend sends one Python call per domain with the same question
text, so text routing can agree with at most one domain of a multi-domain plan
and every other domain is refused by construction. Now that 1955ba8c caps the
plan at three domains and merges the per-domain packets into one top-level
contract, two- and three-domain plans are expected to work end to end and this
is what stops them.

Make the server-issued declaration authoritative. declared_workflow_route()
returns the route a complete, version-supported, allowlisted plan declares, and
resolve_route() honours it instead of reading the text; a caller that sends no
plan metadata keeps the text heuristics verbatim, so the MCP strict_workflow
tool and the research callers behave exactly as before. The route packet
records which rule decided, because routing now has two legitimate sources.

This is not a way to silence the 400: the whole packet comes from the declared
domain's RouteDefinition, so the sync steps, the consumer_context required
layers, the evidence packet and the frontend's themes[primary_theme] lookup all
land on the domain that was declared. A call declaring wealth can no longer
execute career and label career evidence as wealth. The contract stays
fail-closed — a route off the allowlist is refused before execution, and
themes, layers, boundary, domains, categories, depth, horizon and precision are
still checked one by one. The surviving resolved_routes check changes meaning
rather than going away: it now asserts the workflow executed what was declared.

The timing prefix "应期与阶段问题:" existed only to inject 应期 so the text
router would agree with the declared route for one domain out of ten. With the
declaration authoritative it fixes nothing and still rewrites the question the
model's answer derives from, so it goes. It influences no other server
behaviour: it matches none of the consumer-context domain regexes, and the
timing route already sets precise_timing_requested.

test_consultation_workflow_domains.py deliberately sent no plan metadata, which
is why this was never caught — its per-domain question happened to route to its
own domain. It now sends the real plan for all ten canonical domains behind one
question whose text routes to career; nine of them fail without this change.

Refs BUG-259.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-17 18:10:00 +08:00

185 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Focused consultation-workflow coverage for canonical domain adapters."""
from __future__ import annotations
import pytest
from scripts.consultation_domain_registry import CANONICAL_DOMAINS
from scripts.consultation_plan_contract import plan_route_contract
from scripts.jyotish_api_server import BadRequest, JyotishAPIHandler
from scripts.report_orchestrator import (
INDEPENDENT_REPORT_THEMES,
ThemeName,
consultation_thematic_capabilities,
)
_NATIVE_THEMATIC_DOMAINS = {"career", "marriage", "wealth", "health"}
def _handler() -> JyotishAPIHandler:
return JyotishAPIHandler.__new__(JyotishAPIHandler)
def _stub_consultation_runtime(monkeypatch, handler: JyotishAPIHandler) -> None:
chart = {
"success": True,
"birth_info": {"date": "1997-08-08", "time": "05:00", "tz": 8},
"planets": {},
"ascendant": {},
"modules": {},
"ai_prompt_pack": {
"evidence_snapshot": {
"strict_workflow_contracts": {
domain: {"status": "available", "domain": domain}
for domain in CANONICAL_DOMAINS
}
}
},
}
monkeypatch.setattr(handler, "_compute_chart", lambda body: chart)
monkeypatch.setattr(
handler,
"_compute_rectification_gate",
lambda body: {
"success": True,
"endpoint": "rectification_gate",
"summary": {"recommended_events": [], "warned": [], "disabled": []},
},
)
monkeypatch.setattr(
handler,
"_compute_muhurta_panchanga",
lambda body: {"status": "ok", "scope": "muhurta_panchanga"},
)
@pytest.mark.parametrize("domain", CANONICAL_DOMAINS)
def test_complete_consultation_workflow_handles_every_canonical_domain(
monkeypatch,
domain: str,
) -> None:
handler = _handler()
_stub_consultation_runtime(monkeypatch, handler)
result = handler._compute_consultation_workflow({
"entry_mode": "direct_chart",
"question": domain,
"theme": [domain],
"year": 1997,
"month": 8,
"day": 8,
"hour": 5,
"minute": 0,
"lat": 36.420487,
"lon": 114.209936,
"tz": 8,
"western_mode": False,
"defer_optional_external_evidence": True,
})
thematic = result["thematic_report"]
domain_report = thematic["themes"][domain]
adapter = domain_report["thematic_adapter"]
assert result["success"] is True
assert result["routes"] == list(CANONICAL_DOMAINS)
assert thematic["available_themes"] == list(CANONICAL_DOMAINS)
assert domain_report["theme"] == domain
assert adapter["fallback_theme"] is None
if domain in _NATIVE_THEMATIC_DOMAINS:
assert domain_report["status"] == "supported"
assert adapter["report_theme"] == domain
else:
assert domain_report["status"] == "degraded"
assert adapter["capability_status"] == "blocked"
assert adapter["report_theme"] is None
assert adapter["upstream_contract_available"] is True
assert "No general-theme fallback" in domain_report["boundary"]
@pytest.mark.parametrize("domain", CANONICAL_DOMAINS)
def test_versioned_plan_domains_execute_their_declared_route_not_the_question_text_route(
monkeypatch,
domain: str,
) -> None:
"""The product runtime sends one question per domain, so text routing agrees with at most one."""
handler = _handler()
_stub_consultation_runtime(monkeypatch, handler)
contract = plan_route_contract(domain)
assert contract is not None
result = handler._compute_consultation_workflow({
"entry_mode": "direct_chart",
"question": "我的事业和财运接下来会怎么走,两者之间该怎么取舍",
"theme": [domain],
"plan_version": "consultation-plan-v2",
"strict_workflow_route": domain,
"required_layers": list(contract.required_layers),
"claim_boundary": contract.claim_boundary,
"plan_depth": "standard",
"requested_domains": list(contract.requested_domains),
"timing_horizon": "next_12_months" if domain in {"timing", "annual"} else None,
"precision_boundary": "server_evidence_required",
"required_evidence_categories": list(contract.required_evidence_categories),
"year": 1997,
"month": 8,
"day": 8,
"hour": 5,
"minute": 0,
"lat": 36.420487,
"lon": 114.209936,
"tz": 8,
"western_mode": False,
"defer_optional_external_evidence": True,
})
primary_theme = result["routing"]["primary_theme"]
assert result["success"] is True
assert result["routing"]["question_type"] == domain
assert primary_theme == domain
assert result["consumer_context"]["route"] == domain
# The consumer reads thematic_report.themes[primary_theme] as this domain's evidence.
assert result["thematic_report"]["themes"][primary_theme]["theme"] == domain
def test_consultation_thematic_capabilities_are_registry_derived_and_exclude_spirituality() -> None:
capabilities = consultation_thematic_capabilities()
assert tuple(capabilities) == CANONICAL_DOMAINS
assert "spirituality" not in capabilities
assert INDEPENDENT_REPORT_THEMES == (ThemeName.SPIRITUALITY,)
assert capabilities["general"]["fallback_theme"] is None
@pytest.mark.parametrize("domain", ["spirituality", "muhurta", "unknown-domain"])
def test_consultation_workflow_rejects_independent_or_unknown_domains(domain: str) -> None:
handler = _handler()
with pytest.raises(BadRequest, match="Unknown high-rigor theme"):
handler._compute_consultation_workflow({
"entry_mode": "direct_chart",
"question": domain,
"theme": [domain],
"year": 1997,
"month": 8,
"day": 8,
"hour": 5,
"minute": 0,
"lat": 36.420487,
"lon": 114.209936,
"tz": 8,
})
def test_independent_spirituality_report_remains_available_outside_consultation_registry() -> None:
result = _handler()._compute_thematic_report({"theme": "spirituality"})
spirituality = result["themes"]["spirituality"]
assert result["success"] is True
assert "spirituality" not in result["available_themes"]
assert result["independent_report_themes"] == ["spirituality"]
assert spirituality["status"] == "supported"
assert spirituality["thematic_adapter"]["scope"] == "independent_report"