Files
planet/backend/tests/test_workflow_workbench_v2.py
T
linkong 842cba4dc7
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
release / client_release (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.77.0
2026-11-20 16:59:38 +08:00

1549 lines
69 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""End-to-end Gate 0–Gate 3 regression suite for workflow-driven workbench, R1–R8 fixes, and 32 natural expressions."""
from __future__ import annotations
import asyncio
import copy
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import pytest
from fastapi import FastAPI
from httpx import ASGITransport, AsyncClient
from sqlalchemy import create_engine, func, select
from sqlalchemy.orm import Session
from app.api.v1 import situational_awareness
from app.core.security import create_access_token
from app.db.session import get_db
from app.models.bgp_anomaly import BGPAnomaly
from app.models.bgp_incident import BGPIncident
from app.models.collected_data import CollectedData
from app.models.situational_assessment import (
ComputeEvidenceCandidate,
EvidenceRecord,
InvestigationSession,
SituationalAssessment,
StagePresentation,
TopicWatch,
WorkflowRun,
)
from app.models.system_log import AuditLog
from app.models.system_setting import SystemSetting
from app.models.user import User
from app.services.ai import proactive_pipeline
from app.services.ai.access_policy import reset_memory_quota_ledger
from app.services.ai.incident_agent import investigate_infrastructure_incident
from app.services.ai.proactive_pipeline import (
audit_situational_hygiene,
process_bgp_snapshot_proactive_delta,
)
from app.services.ai.workflow_runtime import classify_turn_intent
from app.services.ai_client import get_ai_provider_client
from app.services.bgp_incidents import create_bgp_incidents_for_anomalies
from app.services.compute_configuration import CATEGORY as COMPUTE_CONFIG_CATEGORY, default_envelope
from app.services.earth_db_change_listener import build_earth_update_from_db_payload
class _FakeRedisClient:
def sismember(self, *_args, **_kwargs):
return False
@pytest.fixture(autouse=True)
def fake_workbench_redis(monkeypatch):
fake = _FakeRedisClient()
monkeypatch.setattr("app.core.security.redis_client", fake)
monkeypatch.setattr("app.services.ai.access_policy.redis_client", fake)
def _make_sqlite_async_session():
engine = create_engine("sqlite://")
for model in (
User,
SystemSetting,
AuditLog,
BGPAnomaly,
BGPIncident,
CollectedData,
EvidenceRecord,
SituationalAssessment,
InvestigationSession,
WorkflowRun,
TopicWatch,
StagePresentation,
ComputeEvidenceCandidate,
):
model.__table__.create(engine)
raw = Session(engine)
async_facade = SimpleNamespace(
execute=AsyncMock(side_effect=raw.execute),
add=raw.add,
flush=AsyncMock(side_effect=raw.flush),
commit=AsyncMock(side_effect=raw.commit),
refresh=AsyncMock(side_effect=raw.refresh),
)
return raw, async_facade
def _make_app(session, ai_analyze_mock=None, ai_client_override=None):
app = FastAPI()
app.include_router(situational_awareness.router, prefix="/api/v1/situational-awareness")
app.dependency_overrides[get_db] = lambda: session
app.dependency_overrides[get_ai_provider_client] = (
(lambda: ai_client_override)
if ai_client_override is not None
else (
lambda: SimpleNamespace(
service_url="http://mock-ai" if ai_analyze_mock else "",
analyze=ai_analyze_mock or AsyncMock(),
)
)
)
return app
@pytest.mark.asyncio
async def test_r1_hundred_consecutive_gets_zero_db_writes_zero_llm_and_outbox_isolation():
raw, session = _make_sqlite_async_session()
raw.add(
BGPIncident(
source="ripe_ris",
incident_key="path_flap:206.207.158.0/24:11809",
incident_type="path_flap",
title="AS11809 prefix 206.207.158.0/24 path_flap",
summary="Multi-peer path oscillation observed.",
severity="high",
status="active",
confidence=0.88,
started_at=datetime.now(UTC) - timedelta(minutes=25),
affected_prefixes=["206.207.158.0/24"],
affected_asns=[11809],
affected_collectors=["rrc00", "rrc11"],
affected_regions=[{"country": "美国", "latitude": 39.8, "longitude": -98.5}],
related_cables=[],
)
)
raw.commit()
ai_mock = AsyncMock()
app = _make_app(session, ai_analyze_mock=ai_mock)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
for _ in range(100):
r_pres = await client.get("/api/v1/situational-awareness/presentations?countries=CN,US")
assert r_pres.status_code == 200
r_posture = await client.get("/api/v1/situational-awareness/posture/compute?countries=CN,US")
assert r_posture.status_code == 200
assessment_count = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
stage_count = raw.execute(select(func.count(StagePresentation.id))).scalar_one()
assert assessment_count == 0
assert stage_count == 0
assert ai_mock.call_count == 0
# Private SituationalAssessment rows MUST NOT trigger global Earth situationalAwareness broadcasts
assert (
build_earth_update_from_db_payload(
{"table": "situational_assessments", "operation": "INSERT", "entity_key": "1"}
)
is None
)
stage_update = build_earth_update_from_db_payload(
{"table": "stage_presentations", "operation": "INSERT", "entity_key": "1"}
)
assert stage_update is not None
assert stage_update["layers"] == ["situationalAwareness"]
assert stage_update["refresh_strategy"] == "delta"
# Content-fingerprint idempotency on write-side investigation: 10 calls -> 1 row, revision=1
for _ in range(10):
res = await investigate_infrastructure_incident(
session,
incident_key="path_flap:206.207.158.0/24:11809",
persist=True,
)
assert res["revision"] == 1
raw.commit()
assert raw.execute(select(func.count(SituationalAssessment.id))).scalar_one() == 1
@pytest.mark.asyncio
async def test_r2_strict_drill_isolation_and_honest_unknown_cable_state():
raw, session = _make_sqlite_async_session()
# 1. Empty BGP DB -> zero AS13335 leakage
empty_res = await investigate_infrastructure_incident(session, allow_drill=False)
assert empty_res["found"] is False
assert "13335" not in str(empty_res["payload"])
assert raw.execute(select(func.count(SituationalAssessment.id))).scalar_one() == 0
# 2. Real BGP incident with empty related_cables -> cable state stays UNKNOWN, never "ruled out"
raw.add(
BGPIncident(
source="ripe_ris",
incident_key="origin_hijack:198.51.100.0/24:64500",
incident_type="origin_hijack",
title="AS64500 unexpected origin on 198.51.100.0/24",
summary="Origin ASN shifted unexpectedly.",
severity="critical",
status="active",
confidence=0.92,
started_at=datetime.now(UTC) - timedelta(hours=1),
affected_prefixes=["198.51.100.0/24"],
affected_asns=[64500, 64501],
affected_collectors=["rrc00", "rrc04"],
affected_regions=[{"country": "德国", "city": "Frankfurt", "latitude": 50.11, "longitude": 8.68}],
related_cables=[],
)
)
raw.commit()
real_res = await investigate_infrastructure_incident(
session, incident_key="origin_hijack:198.51.100.0/24:64500", allow_drill=False
)
serialized = str(real_res)
assert "13335" not in serialized
assert "São Paulo" not in serialized
assert not any("排除海底光缆物理断缆" in item for item in real_res["payload"]["counter_evidence"])
assert any("未知" in gap for gap in real_res["payload"]["open_gaps"])
@pytest.mark.asyncio
async def test_r3_proactive_snapshot_delta_watch_and_failure_suppression():
raw, session = _make_sqlite_async_session()
now = datetime.now(UTC)
raw.add(
TopicWatch(
watch_key="watch-bgp-global",
domain="bgp",
target_scope={},
trigger_policy={"severities": ["high", "critical"]},
owner_key="user:analyst1",
status="active",
)
)
raw.add(
BGPIncident(
source="ripe_ris",
incident_key="route_leak:203.0.113.0/24:65010",
incident_type="route_leak",
title="AS65010 transit route leak",
summary="Unexpected transit propagation.",
severity="high",
status="active",
confidence=0.91,
started_at=now - timedelta(minutes=15),
affected_prefixes=["203.0.113.0/24"],
affected_asns=[65010],
affected_collectors=["rrc00", "rrc11"],
affected_regions=[{"country": "法国", "city": "Marseille", "latitude": 43.29, "longitude": 5.37}],
related_cables=[
{"landing_point": "Marseille", "cable_names": ["SEA-ME-WE 5"], "distance_km": 18.4}
],
)
)
raw.commit()
# 1. Valid new change -> emits StagePresentation & triggers TopicWatch
first = await process_bgp_snapshot_proactive_delta(
session, source="ripe_ris", record_count=120, status="success", completed_at=now
)
raw.commit()
assert first["delta"]["verdict"] == "new_change"
assert len(first["emitted_presentations"]) == 1
assert len(first["triggered_watches"]) == 1
assert first["emitted_presentations"][0]["presentation_key"] == "incident-route_leak:203.0.113.0/24:65010"
# 2. Duplicate retry -> 0 new presentations
dup = await process_bgp_snapshot_proactive_delta(
session, source="ripe_ris", record_count=120, status="success", completed_at=now
)
assert dup["delta"]["is_duplicate_retry"] is True
assert dup["emitted_presentations"] == []
# 3. Collector failure & coverage drop -> indeterminate, preserves baseline, 0 false recovery events
failed = await process_bgp_snapshot_proactive_delta(
session, source="ripe_ris", record_count=0, status="failed", error="upstream_timeout"
)
assert failed["delta"]["verdict"] == "indeterminate"
assert failed["emitted_presentations"] == []
dropped = await process_bgp_snapshot_proactive_delta(
session, source="ripe_ris", record_count=20, status="success", completed_at=now + timedelta(minutes=5)
)
assert dropped["delta"]["verdict"] == "indeterminate"
assert dropped["emitted_presentations"] == []
@pytest.mark.asyncio
async def test_r4_three_tier_access_and_quota_ladder(monkeypatch):
reset_memory_quota_ledger()
monkeypatch.setattr("app.core.security.redis_client.sismember", lambda *_: False)
raw, session = _make_sqlite_async_session()
viewer = User(id=10, username="plain_viewer", email="v@planet.local", password_hash="x", role="viewer", gatekeeper_groups=[], is_active=True)
analyst = User(id=11, username="sec_analyst", email="a@planet.local", password_hash="x", role="viewer", gatekeeper_groups=["research"], is_active=True)
admin = User(id=12, username="root_admin", email="r@planet.local", password_hash="x", role="admin", gatekeeper_groups=[], is_active=True)
raw.add_all([viewer, analyst, admin])
raw.commit()
viewer_jwt = create_access_token({"sub": str(viewer.id)})
analyst_jwt = create_access_token({"sub": str(analyst.id)})
admin_jwt = create_access_token({"sub": str(admin.id)})
monkeypatch.setenv("PLANET_KIOSK_TOKEN", "kiosk-demo-secret-2026")
monkeypatch.setenv("PLANET_KIOSK_HOURLY_QUOTA", "2")
app = _make_app(session)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
# Tier 1: Anonymous -> 401 on paid /investigate or prefer_llm=True
r_anon_inv = await client.post(
"/api/v1/situational-awareness/investigate",
json={"domain": "compute", "question": "分析中美算力差距"},
)
assert r_anon_inv.status_code == 401
# Anonymous CAN create session and run deterministic 0-token workbench turns
sess_res = await client.post("/api/v1/situational-awareness/sessions", json={"domain": "compute"})
assert sess_res.status_code == 200
sid = sess_res.json()["session_id"]
r_anon_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
json={"question": "看看中美算力对比"},
)
assert r_anon_turn.status_code == 200
assert r_anon_turn.json()["zero_token"] is True
assert r_anon_turn.json()["access_tier"] == "public_zero_cost"
r_anon_paid_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
json={"question": "深度推演中美算力", "prefer_llm": True},
)
assert r_anon_paid_turn.status_code == 401
# Plain viewer -> 403 on paid LLM research
r_viewer = await client.post(
"/api/v1/situational-awareness/investigate",
headers={"Authorization": f"Bearer {viewer_jwt}"},
json={"domain": "compute", "question": "分析中美算力差距"},
)
assert r_viewer.status_code == 403
# Tier 2: Kiosk token -> 200 within quota (2), 403 on external URLs, 429 on 3rd paid call
r_kiosk_ext = await client.post(
"/api/v1/situational-awareness/investigate",
headers={"X-Planet-Kiosk-Token": "kiosk-demo-secret-2026"},
json={"domain": "bgp", "question": "调查事件", "external_urls": ["https://example.org/rfo"]},
)
assert r_kiosk_ext.status_code == 403
for _ in range(2):
rk = await client.post(
"/api/v1/situational-awareness/investigate",
headers={"X-Planet-Kiosk-Token": "kiosk-demo-secret-2026"},
json={"domain": "compute", "question": "看中美算力"},
)
assert rk.status_code == 200
rk_exceeded = await client.post(
"/api/v1/situational-awareness/investigate",
headers={"X-Planet-Kiosk-Token": "kiosk-demo-secret-2026"},
json={"domain": "compute", "question": "看中美算力"},
)
assert rk_exceeded.status_code == 429
# Tier 3: Analyst (viewer + research group) & Admin -> 200 + hygiene audit access
r_analyst = await client.post(
"/api/v1/situational-awareness/investigate",
headers={"Authorization": f"Bearer {analyst_jwt}"},
json={"domain": "compute", "question": "解释中美智算剪刀差"},
)
assert r_analyst.status_code == 200
r_audit = await client.get(
"/api/v1/situational-awareness/audit/hygiene",
headers={"Authorization": f"Bearer {admin_jwt}"},
)
assert r_audit.status_code == 200
assert r_audit.json()["read_only_audit"] is True
@pytest.mark.asyncio
async def test_r5_r6_r7_time_basis_question_routing_and_frozen_revision_export():
reset_memory_quota_ledger()
raw, session = _make_sqlite_async_session()
raw.add(
BGPIncident(
source="ripe_ris",
incident_key="route_leak:192.0.2.0/24:65100",
incident_type="route_leak",
title="AS65100 route leak on 192.0.2.0/24",
summary="Control-plane route leak across European peers.",
severity="high",
status="resolved",
confidence=0.95,
started_at=datetime(2026, 5, 10, 10, 0, 0, tzinfo=UTC),
ended_at=datetime(2026, 5, 10, 12, 30, 0, tzinfo=UTC),
affected_prefixes=["192.0.2.0/24"],
affected_asns=[65100, 65101],
affected_collectors=["rrc00", "rrc04"],
affected_regions=[{"country": "荷兰", "city": "Amsterdam", "latitude": 52.37, "longitude": 4.90}],
related_cables=[],
)
)
raw.commit()
app = _make_app(session)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
sess = (await client.post("/api/v1/situational-awareness/sessions", json={"domain": "compute"})).json()
sid = sess["session_id"]
# R5 Compute: `as_known_then` cutoff 2022-12-31 withholds 2024/2025 reports (`caict-2025` etc.)
t_2022 = await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
json={
"question": "站在2022当时的视角看中美算力对比",
"time_basis": "as_known_then",
"cutoff_at": "2022-12-31",
},
)
assert t_2022.status_code == 200
body_2022 = t_2022.json()
assert "caict-2025" not in body_2022["evidence"]
matrix_blk = next(b for b in body_2022["view_spec"]["blocks"] if b["block_type"] == "comparison_matrix")
assert matrix_blk["data"]["year"] <= 2022
# R5 BGP: `as_known_then` at 10:30 UTC masks 12:30 UTC recovery (`status == active`)
t_bgp_known = await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
json={
"domain": "bgp",
"entity_id": "route_leak:192.0.2.0/24:65100",
"question": "当时可知视角下恢复了吗?",
"time_basis": "as_known_then",
"cutoff_at": "2026-05-10T10:30:00Z",
},
)
assert t_bgp_known.status_code == 200
topo_blk = next(b for b in t_bgp_known.json()["view_spec"]["blocks"] if b["block_type"] == "impact_topology")
assert topo_blk["data"]["status"] == "active"
# R6: Four distinct BGP questions yield distinct analysis_focus & distinct statements
focuses = set()
first_claims = set()
for q in (
"这次事件影响谁、哪些前缀?",
"为什么会发生这次异常?",
"这次异常和附近海底光缆有关吗?",
"这次路由泄漏恢复了吗?",
):
r_q = await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
json={
"domain": "bgp",
"entity_id": "route_leak:192.0.2.0/24:65100",
"time_basis": "retrospective",
"question": q,
},
)
assert r_q.status_code == 200
d_q = r_q.json()
focuses.add(d_q["view_spec"]["subtitle"])
first_claims.add(d_q["reply"]["answer_markdown"])
assert len(focuses) == 4
assert len(first_claims) == 4
# R7: Frozen revision read & export (`v1` preserved verbatim even after `v2` exists, 0 new rows)
raw.add(
SituationalAssessment(
subject_type="compute_posture",
subject_key="compute:CN,US:global_share",
input_fingerprint="fp-v1-historical",
revision=1,
generation_mode="deterministic_baseline",
payload={
"headline": "历史冻结版本 V1 专属标题",
"headline_en": "Frozen V1 Headline",
"executive_assertion": "这是 V1 版本的冻结断言内容。",
"executive_assertion_en": "Frozen V1 assertion.",
"causes": [{"label": "V1成因", "label_en": "C1", "detail": "V1明细", "detail_en": "D1", "evidence_ids": ["caict-2024"]}],
"breakthrough_levers": [],
"blind_spots": [],
"evidence_ids": ["caict-2024"],
},
evidence_ids=["caict-2024"],
audit_meta={"time_basis": "retrospective"},
)
)
raw.add(
SituationalAssessment(
subject_type="compute_posture",
subject_key="compute:CN,US:global_share",
input_fingerprint="fp-v2-latest",
revision=2,
generation_mode="deterministic_baseline",
payload={
"headline": "最新演进版本 V2 标题",
"headline_en": "Latest V2 Headline",
"executive_assertion": "这是 V2 版本的最新断言内容。",
"executive_assertion_en": "Latest V2 assertion.",
"causes": [{"label": "V2成因", "label_en": "C2", "detail": "V2明细", "detail_en": "D2", "evidence_ids": ["caict-2025"]}],
"breakthrough_levers": [],
"blind_spots": [],
"evidence_ids": ["caict-2025"],
},
evidence_ids=["caict-2025"],
audit_meta={"time_basis": "retrospective"},
)
)
raw.commit()
before_rows = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
rev1_res = await client.get(
"/api/v1/situational-awareness/results/compute:CN,US:global_share/revisions/1?format=markdown"
)
assert rev1_res.status_code == 200
rev1_json = rev1_res.json()
assert rev1_json["revision"] == 1
assert rev1_json["is_historical_revision"] is True
assert "历史冻结版本 V1 专属标题" in rev1_json["markdown"]
assert "最新演进版本 V2 标题" not in rev1_json["markdown"]
assert "v2" in rev1_json["newer_revision_notice"]
exp_v1 = await client.post(
"/api/v1/situational-awareness/reports/export",
json={"subject_type": "compute_posture", "countries": ["CN", "US"], "expected_revision": 1},
)
assert exp_v1.status_code == 200
assert "历史冻结版本 V1 专属标题" in exp_v1.json()["markdown"]
after_rows = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
assert after_rows == before_rows
@pytest.mark.parametrize(
("utterance", "prior_ctx", "expected_domain", "expected_workflow_prefix", "expected_preset_or_mode"),
[
# 1–8: Compute comparisons & multilateral natural phrasing
("看看中美算力对比", {}, "compute", "compute.compare", "global_share"),
("对比一下中国、美国和日本的算力格局", {}, "compute", "compute.compare", "global_share"),
("中欧算力份额情况怎么样", {}, "compute", "compute.compare", "global_share"),
("把德国和英国也拉进来比比算力", {}, "compute", "compute.compare", "global_share"),
("Compare China and United States compute share", {}, "compute", "compute.compare", "global_share"),
("印度和新加坡的算力有公开同口径数据吗", {}, "compute", "compute.compare", "global_share"),
("看看亚太与北美算力态势", {}, "compute", "compute.compare", "global_share"),
("两国历年算力走势走廊", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.compare", "trend"),
# 9–14: Metric switches inheriting participant context
("只看智算份额", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "global_share"}, "compute", "compute.switch_metric", "intelligent_share"),
("切到绝对规模 EFlops", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "intelligent_share"}, "compute", "compute.switch_metric", "absolute"),
("改看前沿集群样本占比", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "global_share"}, "compute", "compute.switch_metric", "frontier_cluster_share"),
("看看高端芯片流向占比", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "global_share"}, "compute", "compute.switch_metric", "chip_inflow_share"),
("切回全球总份额", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "absolute"}, "compute", "compute.switch_metric", "global_share"),
("Switch to AI compute share", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "global_share"}, "compute", "compute.switch_metric", "intelligent_share"),
# 15–20: Causal & gap attribution + hubs + innovation
("为什么中国算力绝对规模翻倍,份额反而回落了?", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.explain_gap", "causal"),
("解释一下中美算力剪刀差成因", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.explain_gap", "causal"),
("为什么 AI 创新指数分差连续三年收窄?", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.explain_gap", "causal"),
("东数西算八大国家算力枢纽怎么用绿电协同?", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.explain_gap", "causal"),
("分母稀释效应具体是怎么算出来的?", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.explain_gap", "causal"),
("Why did the compute gap widen in 2024?", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.explain_gap", "causal"),
# 21–25: Zero-token view projections inheriting session domain
("换成表格看看", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.project_view", "table"),
("做成一页领导简报", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.project_view", "onepager"),
("换成柱状图对比", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.project_view", "comparison"),
("换成对比图", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.project_view", "comparison"),
("换成走势图", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.project_view", "trend"),
("只看证据清单和出处", {"domain": "bgp", "entity_id": "inc-1"}, "bgp", "incident.project_view", "evidence"),
("做成一页复盘汇报版", {"domain": "bgp", "entity_id": "inc-1"}, "bgp", "incident.project_view", "onepager"),
# 26–32: BGP forensics, cable boundary, historical cutoff, drill & globe tour
("这次 BGP 路由异动影响谁、波及哪些前缀?", {"domain": "bgp"}, "bgp", "incident.impact", "topology"),
("为什么会出现这次 BGP 路径抖动?", {"domain": "bgp"}, "bgp", "incident.cause", "causal"),
("这次事件和附近海底光缆断缆有关吗?", {"domain": "bgp"}, "bgp", "incident.cable_correlation", "topology"),
("故障现在恢复收敛了吗?", {"domain": "bgp"}, "bgp", "incident.recovery", "default"),
("按当时可知模式回放,屏蔽事后报告", {"domain": "bgp"}, "bgp", "incident.replay_as_known", "default"),
("打开演练档案 AS13335 路由泄漏样板", {}, "bgp", "incident.", "default"),
("在地球上演示给我看四幕导览", {"domain": "compute", "selected_codes": ["CN", "US"]}, "compute", "compute.present_on_globe", "default"),
],
)
def test_gate3_thirty_two_natural_expression_routing_cases(
utterance: str,
prior_ctx: dict,
expected_domain: str,
expected_workflow_prefix: str,
expected_preset_or_mode: str,
):
routed = classify_turn_intent(utterance, prior_ctx)
assert routed["domain"] == expected_domain
assert routed["workflow_id"].startswith(expected_workflow_prefix)
assert expected_preset_or_mode in {routed["view_preset"], routed["mode"]}
@pytest.mark.asyncio
async def test_p1_session_and_run_idor_isolation():
reset_memory_quota_ledger()
raw, session = _make_sqlite_async_session()
user_a = User(
id=21,
username="owner_a",
email="owner_a@planet.local",
password_hash="x",
role="admin",
gatekeeper_groups=[],
is_active=True,
)
user_b = User(
id=22,
username="attacker_b",
email="attacker_b@planet.local",
password_hash="x",
role="admin",
gatekeeper_groups=[],
is_active=True,
)
raw.add_all([user_a, user_b])
raw.commit()
jwt_a = create_access_token({"sub": str(user_a.id)})
jwt_b = create_access_token({"sub": str(user_b.id)})
app = _make_app(session)
transport = ASGITransport(app=app)
# 1. Anonymous session & run: another bare anonymous client CANNOT read, modify, append, or cancel
async with (
AsyncClient(transport=transport, base_url="http://test") as anon_owner,
AsyncClient(transport=transport, base_url="http://test") as anon_stranger,
):
sess_res = await anon_owner.post("/api/v1/situational-awareness/sessions", json={"domain": "compute"})
assert sess_res.status_code == 200
sid_anon = sess_res.json()["session_id"]
turn_res = await anon_owner.post(
f"/api/v1/situational-awareness/sessions/{sid_anon}/turns",
json={"question": "看看中美算力对比"},
)
assert turn_res.status_code == 200
run_anon = turn_res.json()["run_id"]
for method, path, kwargs in (
("GET", f"/api/v1/situational-awareness/sessions/{sid_anon}", {}),
("PATCH", f"/api/v1/situational-awareness/sessions/{sid_anon}/pins", {"json": {"pinned_items": [{"id": "evil"}]}}),
("POST", f"/api/v1/situational-awareness/sessions/{sid_anon}/turns", {"json": {"question": "越权提问"}}),
("GET", f"/api/v1/situational-awareness/sessions/{sid_anon}/runs", {}),
("GET", f"/api/v1/situational-awareness/runs/{run_anon}", {}),
("POST", f"/api/v1/situational-awareness/runs/{run_anon}/cancel", {}),
("GET", "/api/v1/situational-awareness/sessions/nonexistent-sess-999", {}),
("POST", "/api/v1/situational-awareness/runs/nonexistent-run-999/cancel", {}),
):
r = await anon_stranger.request(method, path, **kwargs)
assert r.status_code == 401, f"Expected 401 for anonymous stranger on {method} {path}, got {r.status_code}"
# Original owner retains intact state & full access
owner_sess = await anon_owner.get(f"/api/v1/situational-awareness/sessions/{sid_anon}")
assert owner_sess.status_code == 200
assert owner_sess.json()["pinned_items"] == []
owner_runs = await anon_owner.get(f"/api/v1/situational-awareness/sessions/{sid_anon}/runs")
assert owner_runs.status_code == 200
assert owner_runs.json()["items"][0]["status"] == "completed"
# 2. Cross-user isolation: authenticated user B gets 403 on user A's session & run
async with AsyncClient(transport=transport, base_url="http://test") as client:
headers_a = {"Authorization": f"Bearer {jwt_a}"}
headers_b = {"Authorization": f"Bearer {jwt_b}"}
sess_a = (
await client.post("/api/v1/situational-awareness/sessions", headers=headers_a, json={"domain": "compute"})
).json()["session_id"]
run_a = (
await client.post(
f"/api/v1/situational-awareness/sessions/{sess_a}/turns",
headers=headers_a,
json={"question": "看看中美算力对比"},
)
).json()["run_id"]
for method, path, kwargs in (
("GET", f"/api/v1/situational-awareness/sessions/{sess_a}", {}),
("PATCH", f"/api/v1/situational-awareness/sessions/{sess_a}/pins", {"json": {"pinned_items": [{"id": "hijack"}]}}),
("POST", f"/api/v1/situational-awareness/sessions/{sess_a}/turns", {"json": {"question": "跨用户提问"}}),
("GET", f"/api/v1/situational-awareness/sessions/{sess_a}/runs", {}),
("GET", f"/api/v1/situational-awareness/runs/{run_a}", {}),
("POST", f"/api/v1/situational-awareness/runs/{run_a}/cancel", {}),
):
r = await client.request(method, path, headers=headers_b, **kwargs)
assert r.status_code == 403, f"Expected 403 for cross-user on {method} {path}, got {r.status_code}"
assert (await client.get(f"/api/v1/situational-awareness/sessions/{sess_a}", headers=headers_a)).status_code == 200
assert (await client.get(f"/api/v1/situational-awareness/runs/{run_a}", headers=headers_a)).status_code == 200
assert (await client.get("/api/v1/situational-awareness/sessions/missing-id", headers=headers_b)).status_code == 404
assert (await client.get("/api/v1/situational-awareness/runs/missing-run", headers=headers_b)).status_code == 404
@pytest.mark.asyncio
async def test_p1_frozen_revision_chart_and_rpi_immutability_after_config_mutation():
reset_memory_quota_ledger()
raw, session = _make_sqlite_async_session()
app = _make_app(session)
def _cn_matrix_value(view_spec: dict) -> tuple[float, float]:
matrix = next(b for b in view_spec["blocks"] if b["block_type"] == "comparison_matrix")
cn_row = next(r for r in matrix["data"]["rows"] if r["code"] == "CN")
return float(cn_row["value"]), float(matrix["data"]["rpi_percent"])
def _cn_trend_2024(view_spec: dict) -> float:
trend = next(b for b in view_spec["blocks"] if b["block_type"] == "trend_corridor")
cn_series = next(s for s in trend["data"]["series"] if s["code"] == "CN")
pt_2024 = next(p for p in cn_series["points"] if p["year"] == 2024)
return float(pt_2024["value"])
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
sess1 = (await client.post("/api/v1/situational-awareness/sessions", json={"domain": "compute"})).json()["session_id"]
turn1 = await client.post(
f"/api/v1/situational-awareness/sessions/{sess1}/turns",
json={"question": "看看中美算力对比"},
)
assert turn1.status_code == 200
t1 = turn1.json()
assert t1["revision"] == 1
assert _cn_matrix_value(t1["view_spec"]) == (23.0, 52.3)
# Mutate live configuration: CN 2024 global_share 23 -> 12
mutated = copy.deepcopy(default_envelope())
for obs in mutated["config"]["observations"]:
if obs["metric"] == "global_share" and obs["year"] == 2024:
obs["values"]["CN"] = 12
mutated["revision"] = "mutated-cn-12-rev2"
mutated["is_default"] = False
raw.add(SystemSetting(category=COMPUTE_CONFIG_CATEGORY, payload=mutated))
raw.commit()
# Generate Rev.2 against the mutated config
sess2 = (await client.post("/api/v1/situational-awareness/sessions", json={"domain": "compute"})).json()["session_id"]
turn2 = await client.post(
f"/api/v1/situational-awareness/sessions/{sess2}/turns",
json={"question": "看看中美算力对比"},
)
assert turn2.status_code == 200
t2 = turn2.json()
assert t2["revision"] == 2
assert _cn_matrix_value(t2["view_spec"]) == (12.0, 27.3)
rows_before_reads = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
# Reading Rev.1 after config mutation MUST keep CN=23, RPI=52.3 across default, comparison, table, trend, onepager, markdown
for view_name in ("default", "comparison", "table"):
r1 = await client.get(
f"/api/v1/situational-awareness/results/compute:CN,US:global_share/revisions/1?view={view_name}"
)
assert r1.status_code == 200
d1 = r1.json()
assert d1["is_historical_revision"] is True
assert _cn_matrix_value(d1["view_spec"]) == (23.0, 52.3)
if view_name == "default":
assert _cn_trend_2024(d1["view_spec"]) == 23.0
r1_trend = await client.get(
"/api/v1/situational-awareness/results/compute:CN,US:global_share/revisions/1?view=trend"
)
assert r1_trend.status_code == 200
d1_trend = r1_trend.json()
assert d1_trend["is_historical_revision"] is True
assert _cn_trend_2024(d1_trend["view_spec"]) == 23.0
r1_one = await client.get(
"/api/v1/situational-awareness/results/compute:CN,US:global_share/revisions/1?view=onepager"
)
assert r1_one.status_code == 200
one_blk = next(b for b in r1_one.json()["view_spec"]["blocks"] if b["block_type"] == "executive_onepager")
assert any("23" in m for m in one_blk["data"]["key_metrics"])
r1_md = await client.get(
"/api/v1/situational-awareness/results/compute:CN,US:global_share/revisions/1?format=markdown"
)
assert r1_md.status_code == 200
assert "52.3%" in r1_md.json()["markdown"]
assert "27.3%" not in r1_md.json()["markdown"]
exp1 = await client.post(
"/api/v1/situational-awareness/reports/export",
json={"subject_type": "compute_posture", "countries": ["CN", "US"], "expected_revision": 1},
)
assert exp1.status_code == 200
assert "52.3%" in exp1.json()["markdown"]
# Pure view projection in session 1 (bound to Rev.1) also preserves Rev.1 values!
proj = await client.post(
f"/api/v1/situational-awareness/sessions/{sess1}/turns",
json={"question": "换成表格看看"},
)
assert proj.status_code == 200
assert proj.json()["revision"] == 1
assert _cn_matrix_value(proj.json()["view_spec"]) == (23.0, 52.3)
rows_after_reads = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
assert rows_after_reads == rows_before_reads
@pytest.mark.asyncio
async def test_p1_existing_bgp_incident_severity_escalation_triggers_proactive_pipeline():
raw, session = _make_sqlite_async_session()
t0 = datetime.now(UTC) - timedelta(minutes=10)
existing = BGPIncident(
source="ris_live_bgp",
incident_key="path_flap:198.51.100.0/24:64512",
incident_type="path_flap",
title="Path Flap incident on 198.51.100.0/24",
summary="1 anomaly signal(s) grouped into one path_flap incident.",
severity="medium",
status="active",
confidence=0.6,
started_at=t0,
affected_prefixes=["198.51.100.0/24"],
affected_asns=[64512],
affected_collectors=["rrc00"],
affected_regions=[{"collector": "rrc00", "country": "NL", "city": "Amsterdam", "latitude": 52.37, "longitude": 4.9}],
related_cables=[],
related_ixps=[],
evidence_refs=["path_flap:198.51.100.0/24:64512"],
)
watch = TopicWatch(
watch_key="bgp:global",
domain="bgp",
target_scope={},
trigger_policy={"severities": ["high", "critical"]},
owner_key="user:analyst1",
status="active",
)
raw.add_all([existing, watch])
raw.commit()
baseline = await process_bgp_snapshot_proactive_delta(
session, source="ris_live_bgp", record_count=10, status="success", completed_at=t0
)
raw.commit()
assert baseline["delta"]["verdict"] == "new_change"
assert len(baseline["emitted_presentations"]) == 1
escalated_anomaly = BGPAnomaly(
source="ris_live_bgp",
anomaly_type="path_flap",
severity="high",
status="active",
entity_key="path_flap:198.51.100.0/24:64512",
prefix="198.51.100.0/24",
origin_asn=64512,
new_origin_asn=64512,
summary="Path flap escalated to high severity",
confidence=0.92,
peer_scope=["rrc00"],
evidence={
"impacted_regions": [
{"collector": "rrc00", "country": "NL", "city": "Amsterdam", "latitude": 52.37, "longitude": 4.9}
]
},
)
spy_calls: list[dict] = []
orig_delta_fn = proactive_pipeline.process_bgp_snapshot_proactive_delta
async def _wrapped_proactive_delta(*args, **kwargs):
res = await orig_delta_fn(*args, **kwargs)
spy_calls.append(res)
return res
with patch(
"app.services.ai.proactive_pipeline.process_bgp_snapshot_proactive_delta",
new=AsyncMock(side_effect=_wrapped_proactive_delta),
) as mock_delta:
created = await create_bgp_incidents_for_anomalies(
session,
source="ris_live_bgp",
snapshot_id=2,
task_id=3,
anomalies=[escalated_anomaly],
)
assert created == 0
assert mock_delta.call_count == 1
assert len(spy_calls) == 1
assert spy_calls[0]["delta"]["verdict"] == "new_change"
assert "path_flap:198.51.100.0/24:64512" in spy_calls[0]["delta"]["updated_keys"]
assert len(spy_calls[0]["emitted_presentations"]) == 1
assert len(spy_calls[0]["triggered_watches"]) == 1
assessments = raw.execute(
select(SituationalAssessment)
.where(SituationalAssessment.subject_key == "path_flap:198.51.100.0/24:64512")
.order_by(SituationalAssessment.revision.asc())
).scalars().all()
assert len(assessments) == 2
assert assessments[-1].revision == 2
assert assessments[-1].audit_meta["frozen_incident"]["severity"] == "high"
assert raw.execute(select(TopicWatch).where(TopicWatch.watch_key == "bgp:global")).scalar_one().last_triggered_revision == 2
@pytest.mark.asyncio
async def test_p2_in_flight_workflow_run_visibility_and_real_cancellation():
reset_memory_quota_ledger()
raw, session = _make_sqlite_async_session()
analyst = User(
id=23,
username="slow_analyst",
email="slow@planet.local",
password_hash="x",
role="admin",
gatekeeper_groups=[],
is_active=True,
)
raw.add(analyst)
raw.commit()
jwt_a = create_access_token({"sub": str(analyst.id)})
headers_a = {"Authorization": f"Bearer {jwt_a}"}
step1_started = asyncio.Event()
step1_release = asyncio.Event()
step1_cancelled = asyncio.Event()
copilot_call_count = 0
async def fake_analyze(req):
nonlocal copilot_call_count
if "算力态势" in str(req.title):
step1_started.set()
try:
await step1_release.wait()
except asyncio.CancelledError:
step1_cancelled.set()
raise
else:
copilot_call_count += 1
return SimpleNamespace(provider="mock", model="mock", content="{}", thinking_blocks=[])
app = _make_app(session, ai_client_override=SimpleNamespace(service_url="http://mock-ai", analyze=fake_analyze))
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
sid = (
await client.post("/api/v1/situational-awareness/sessions", headers=headers_a, json={"domain": "compute"})
).json()["session_id"]
client_run_id = "run-inflight-test-1"
turn_task = asyncio.create_task(
client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
headers=headers_a,
json={"run_id": client_run_id, "question": "深度分析中美算力", "prefer_llm": True},
)
)
await asyncio.wait_for(step1_started.wait(), timeout=2.0)
# While step 1 is still executing in the background, session & run endpoints expose the running run_id
sess_mid = await client.get(f"/api/v1/situational-awareness/sessions/{sid}", headers=headers_a)
assert sess_mid.status_code == 200
assert sess_mid.json()["active_run_id"] == client_run_id
runs_mid = await client.get(f"/api/v1/situational-awareness/sessions/{sid}/runs", headers=headers_a)
assert runs_mid.status_code == 200
assert runs_mid.json()["items"][0]["run_id"] == client_run_id
assert runs_mid.json()["items"][0]["status"] == "running"
run_mid = await client.get(f"/api/v1/situational-awareness/runs/{client_run_id}", headers=headers_a)
assert run_mid.status_code == 200
assert run_mid.json()["status"] == "running"
# Stop/Cancel cancels the underlying coroutine immediately and skips step 2 & DB assessment write
cancel_res = await client.post(
f"/api/v1/situational-awareness/runs/{client_run_id}/cancel",
headers=headers_a,
)
assert cancel_res.status_code == 200
assert cancel_res.json()["status"] == "cancelled"
turn_res = await asyncio.wait_for(turn_task, timeout=2.0)
assert turn_res.status_code == 200
assert turn_res.json()["status"] == "cancelled"
assert step1_cancelled.is_set() is True
assert copilot_call_count == 0
assert raw.execute(select(func.count(SituationalAssessment.id))).scalar_one() == 0
run_after = await client.get(f"/api/v1/situational-awareness/runs/{client_run_id}", headers=headers_a)
assert run_after.status_code == 200
assert run_after.json()["status"] == "cancelled"
sess_after = await client.get(f"/api/v1/situational-awareness/sessions/{sid}", headers=headers_a)
assert sess_after.json()["active_run_id"] is None
@pytest.mark.asyncio
async def test_p1_legacy_assessment_without_snapshot_never_reads_live_config_and_marks_unsupported():
reset_memory_quota_ledger()
raw, session = _make_sqlite_async_session()
# Legacy compute assessment missing `frozen_series` AND legacy BGP assessment missing `frozen_incident`
raw.add(
SituationalAssessment(
subject_type="compute_posture",
subject_key="compute:CN,US:global_share",
input_fingerprint="legacy-fp-no-snapshot",
revision=1,
generation_mode="deterministic_baseline",
payload={
"headline": "旧格式冻结版本 V1 标题(23%)",
"headline_en": "Legacy Frozen V1 (23%)",
"executive_assertion": "旧版本正文断言保持中国 23%。",
"executive_assertion_en": "Legacy text assertion CN 23%.",
"causes": [
{
"label": "旧版成因",
"label_en": "Legacy Cause",
"detail": "旧版明细(23%)",
"detail_en": "Legacy detail (23%)",
"evidence_ids": ["caict-2024"],
}
],
"breakthrough_levers": [],
"blind_spots": [],
"evidence_ids": ["caict-2024"],
},
evidence_ids=["caict-2024"],
audit_meta={"time_basis": "retrospective"},
)
)
raw.add(
SituationalAssessment(
subject_type="bgp_incident",
subject_key="path_flap:203.0.113.0/24:65099",
input_fingerprint="legacy-bgp-no-snapshot",
revision=1,
generation_mode="deterministic_baseline",
payload={
"incident_key": "path_flap:203.0.113.0/24:65099",
"title": "旧版 BGP 异常取证结论(medium)",
"analysis_focus": "overview",
"is_drill": False,
"time_basis": "retrospective",
"cutoff_at": None,
"confirmed_facts": [{"statement": "旧版已核验事实正文", "evidence_ids": ["ev-legacy-1"]}],
"candidate_explanations": [{"statement": "旧版候选解释正文", "evidence_ids": ["ev-legacy-1"]}],
"counter_evidence": [],
"open_gaps": ["旧版待查缺口"],
"cable_correlation_policy": "物理状态保持未知",
"evidence_ids": ["ev-legacy-1"],
},
evidence_ids=["ev-legacy-1"],
audit_meta={"time_basis": "retrospective"},
)
)
# Mutate live compute config (CN 2024: 23 -> 12) and insert live BGP incident with different fields
mutated = copy.deepcopy(default_envelope())
for obs in mutated["config"]["observations"]:
if obs["metric"] == "global_share" and obs["year"] == 2024:
obs["values"]["CN"] = 12
mutated["revision"] = "mutated-cn-12-legacy-test"
mutated["is_default"] = False
raw.add(SystemSetting(category=COMPUTE_CONFIG_CATEGORY, payload=mutated))
raw.add(
BGPIncident(
source="ris_live_bgp",
incident_key="path_flap:203.0.113.0/24:65099",
incident_type="path_flap",
title="LIVE_MUTATED_BGP_TITLE",
summary="LIVE_MUTATED_BGP_SUMMARY",
severity="critical",
status="resolved",
confidence=0.99,
started_at=datetime.now(UTC) - timedelta(hours=1),
ended_at=datetime.now(UTC),
affected_prefixes=["203.0.113.0/24"],
affected_asns=[65099],
affected_collectors=["rrc00"],
affected_regions=[],
related_cables=[],
)
)
raw.commit()
rows_before = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
app = _make_app(session)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
for view_name in ("default", "comparison", "table", "trend", "onepager"):
res = await client.get(
f"/api/v1/situational-awareness/results/compute:CN,US:global_share/revisions/1?view={view_name}&format=markdown"
)
assert res.status_code == 200
body = res.json()
assert body["replay_supported"] is False
assert body["replay_status"] == "incomplete_snapshot"
assert "不支持完整重放" in body["replay_notice"]
assert body["view_spec"]["replay_supported"] is False
assert body["view_spec"]["replay_status"] == "incomplete_snapshot"
assert "不支持完整重放" in body["view_spec"]["replay_notice"]
assert "旧版本正文断言保持中国 23%" in body["markdown"]
assert "不支持完整重放" in body["markdown"]
assert "27.3%" not in body["markdown"]
assert "12 %" not in body["markdown"]
matrix_blk = next(
(b for b in body["view_spec"]["blocks"] if b["block_type"] == "comparison_matrix"),
None,
)
if matrix_blk is not None:
assert matrix_blk["data"]["replay_supported"] is False
assert matrix_blk["data"]["rpi_percent"] is None
for row in matrix_blk["data"]["rows"]:
assert row["value"] is None
assert row["is_missing"] is True
assert "不支持完整重放" in row["formatted"]
exp_legacy = await client.post(
"/api/v1/situational-awareness/reports/export",
json={"subject_type": "compute_posture", "countries": ["CN", "US"], "expected_revision": 1},
)
assert exp_legacy.status_code == 200
assert exp_legacy.json()["replay_supported"] is False
assert "不支持完整重放" in exp_legacy.json()["markdown"]
assert "12 %" not in exp_legacy.json()["markdown"]
bgp_res = await client.get(
"/api/v1/situational-awareness/results/path_flap:203.0.113.0/24:65099/revisions/1?format=markdown"
)
assert bgp_res.status_code == 200
bgp_body = bgp_res.json()
assert bgp_body["replay_supported"] is False
assert bgp_body["replay_status"] == "incomplete_snapshot"
assert "不支持完整重放" in bgp_body["replay_notice"]
assert "旧版 BGP 异常取证结论(medium)" in bgp_body["markdown"]
assert "LIVE_MUTATED_BGP_TITLE" not in str(bgp_body)
assert "LIVE_MUTATED_BGP_SUMMARY" not in str(bgp_body)
rows_after = raw.execute(select(func.count(SituationalAssessment.id))).scalar_one()
assert rows_after == rows_before
@pytest.mark.asyncio
async def test_leadership_path_auto_llm_frozen_reuse_globe_tour_and_cost_ledger(monkeypatch):
import json as json_mod
reset_memory_quota_ledger()
monkeypatch.setattr("app.core.security.redis_client.sismember", lambda *_: False)
raw, session = _make_sqlite_async_session()
admin = User(
id=41,
username="leader_admin",
email="leader@planet.local",
password_hash="x",
role="admin",
gatekeeper_groups=[],
is_active=True,
)
raw.add(admin)
raw.commit()
admin_jwt = create_access_token({"sub": str(admin.id)})
headers_admin = {"Authorization": f"Bearer {admin_jwt}"}
# Verify exact leadership phrasing `"在地球上演示"` classifies as present_on_globe
routed_demo = classify_turn_intent("在地球上演示", {"domain": "compute", "selected_codes": ["CN", "US"], "mode": "intelligent_share"})
assert routed_demo["workflow_id"] == "compute.present_on_globe"
assert routed_demo["auto_play_presentation"] is True
llm_invocations: list[str] = []
async def fake_analyze(req):
llm_invocations.append(req.title)
usage = {
"input_tokens": 300,
"output_tokens": 120,
"output_tokens_details": {"thinking_tokens": 20},
}
if "态势研判" in str(req.title):
body = {
"headline": "LLM研判:2024中美智能算力差距(46% vs 21%)",
"headline_en": "LLM Brief: US-China Intelligent Compute Gap",
"executive_assertion": "已核验2024年智能算力份额中国21%对美国46%(RPI 45.7%)。",
"executive_assertion_en": "Verified 2024 intelligent share: China 21% vs US 46%.",
"causes": [
{
"label": "智算供给与出口管制约束",
"label_en": "AI Chip & Cluster Constraints",
"detail": "高端加速卡新增流向约72%集中美国。",
"detail_en": "High-end AI chip inflows concentrated in US.",
"evidence_ids": ["caict-2025", "epoch-chip-imports-2026"],
}
],
"breakthrough_levers": [
{
"label": "八大绿电枢纽",
"label_en": "Eight Green Hubs",
"detail": "东数西算依托低PUE绿电底座缓解能耗约束。",
"detail_en": "Green hubs mitigate power limits.",
"evidence_ids": ["ndrc-east-west-2022"],
}
],
"blind_spots": ["商业私有集群未全量公开"],
"evidence_ids": ["caict-2025", "epoch-chip-imports-2026", "ndrc-east-west-2022"],
}
else:
body = {
"answer_markdown": "### 中美智能算力差距归因(MiniMax实测)\n\n- 中国占 `21%` 对美国 `46%`。[caict-2025]",
"key_points": [{"claim": "智能算力份额差25个百分点", "evidence_ids": ["caict-2025"]}],
"highlight_entities": ["CN", "US"],
"suggested_followups": ["在地球上演示"],
"evidence_ids": ["caict-2025"],
}
return SimpleNamespace(
provider="minimax",
model="MiniMax-M3.1-Flash",
content=json_mod.dumps(body, ensure_ascii=False),
thinking_blocks=[],
raw_response={"usage": usage},
)
fake_ai_client = SimpleNamespace(
service_url="http://mock-ai",
service_token="",
timeout=30,
retry_attempts=1,
llm_config={"provider": "minimax", "model": "MiniMax-M3.1-Flash", "max_tokens": 1200},
analyze=fake_analyze,
)
app = _make_app(session, ai_client_override=fake_ai_client)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
# Anonymous caller with prefer_llm="auto" gracefully executes 0-token deterministic (never 401/403)
anon_sid = (await client.post("/api/v1/situational-awareness/sessions", json={"domain": "compute"})).json()["session_id"]
anon_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{anon_sid}/turns",
json={"question": "看中美算力对比", "prefer_llm": "auto"},
)
assert anon_turn.status_code == 200
assert anon_turn.json()["zero_token"] is True
assert len(llm_invocations) == 0
# Leader (admin) session flow: 看中美算力对比 -> 只看智算 -> 解释差距 -> 换成表格 -> 在地球上演示 -> 导出同版本简报
sid = (
await client.post("/api/v1/situational-awareness/sessions", headers=headers_admin, json={"domain": "compute"})
).json()["session_id"]
t1 = (
await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
headers=headers_admin,
json={"question": "看中美算力对比", "prefer_llm": "auto"},
)
).json()
assert t1["zero_token"] is False
assert t1["context_state"]["mode"] == "global_share"
calls_after_t1 = len(llm_invocations)
assert calls_after_t1 >= 1
# Metric switch: fast 0-token switch to intelligent_share
t2 = (
await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
headers=headers_admin,
json={"question": "只看智算", "prefer_llm": "auto"},
)
).json()
assert t2["zero_token"] is True
assert t2["context_state"]["mode"] == "intelligent_share"
assert len(llm_invocations) == calls_after_t1
# Reasoning request ("解释差距"): invokes configured LLM, upgrades cached deterministic intelligent_share posture to llm_verified Rev.2
t3 = (
await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
headers=headers_admin,
json={"question": "解释差距", "prefer_llm": "auto"},
)
).json()
assert t3["zero_token"] is False
assert t3["generation_mode"] == "llm_verified"
assert "MiniMax实测" in t3["reply"]["answer_markdown"]
assert len(llm_invocations) > calls_after_t1
intel_result_id = t3["result_id"]
intel_rev = t3["revision"]
assert intel_rev >= 2
calls_before_reuse = len(llm_invocations)
# Chart/view switch ("换成表格"): reuses frozen Rev.2 with 0 LLM calls!
t4 = (
await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
headers=headers_admin,
json={"question": "换成表格", "prefer_llm": "auto"},
)
).json()
assert t4["zero_token"] is True
assert t4["result_id"] == intel_result_id
assert t4["revision"] == intel_rev
assert t4["view_spec"]["active_preset"] == "table"
assert len(llm_invocations) == calls_before_reuse
# "在地球上演示": triggers auto_play_presentation with 0 LLM calls while keeping exact same frozen revision!
t5 = (
await client.post(
f"/api/v1/situational-awareness/sessions/{sid}/turns",
headers=headers_admin,
json={"question": "在地球上演示", "prefer_llm": "auto"},
)
).json()
assert t5["zero_token"] is True
assert t5["result_id"] == intel_result_id
assert t5["revision"] == intel_rev
assert t5["scene_spec"]["auto_play_presentation"] is True
assert len(llm_invocations) == calls_before_reuse
# Export same revision brief
exp = await client.get(
f"/api/v1/situational-awareness/results/{intel_result_id}/revisions/{intel_rev}?format=markdown",
headers=headers_admin,
)
assert exp.status_code == 200
md = exp.json()["markdown"]
assert f"v{intel_rev}" in md
assert "LLM研判:2024中美智能算力差距" in md
assert "caict-2025" in md
# Hygiene audit reports model token cost summary
hygiene = (
await client.get("/api/v1/situational-awareness/audit/hygiene", headers=headers_admin)
).json()
cost = hygiene["model_cost_summary"]
assert cost["llm_runs"] == 2
assert cost["zero_token_runs"] >= 3
assert cost["total_input_tokens"] > 0
assert cost["total_output_tokens"] > 0
assert cost["total_tokens"] == cost["total_input_tokens"] + cost["total_output_tokens"]
@pytest.mark.parametrize(
("utterance", "prior_ctx", "expected_domain", "expected_workflow"),
[
("当前全球多域态势整体怎么样?", {}, "global", "global.overview"),
("对比一下所有图层的当前规模", {}, "global", "global.compare"),
("看看全球海底光缆态势", {}, "cable", "cable.overview"),
("看看海底光缆与主要登陆枢纽", {}, "cable", "cable.landing_hubs"),
("哪些国家的海缆登陆站最集中?", {"domain": "cable"}, "cable", "cable.landing_hubs"),
("统计在轨卫星星座与轨道体制分布", {}, "satellite", "satellite.orbit_distribution"),
("近一小时 AIS 船舶在航和锚泊态势如何?", {}, "vessel", "vessel.traffic_status"),
("这次事件和附近海底光缆断缆有关吗?", {"domain": "bgp"}, "bgp", "incident.cable_correlation"),
],
)
def test_multi_domain_routing_and_cable_vs_bgp_disambiguation(
utterance: str,
prior_ctx: dict,
expected_domain: str,
expected_workflow: str,
):
routed = classify_turn_intent(utterance, prior_ctx)
assert routed["domain"] == expected_domain
assert routed["workflow_id"] == expected_workflow
@pytest.mark.asyncio
async def test_multi_domain_sessions_frozen_revision_and_spatial_tour(monkeypatch):
reset_memory_quota_ledger()
raw, session = _make_sqlite_async_session()
app = _make_app(session)
# Seed realistic built-in CollectedData for cable & landing point aggregation
raw.add(
CollectedData(
source="arcgis_cables",
source_id="cable-sea-me-we-5",
name="SEA-ME-WE 5",
data_type="cable",
is_current=True,
extra_data={
"value": 20000.0,
"status": "active",
"rfs": "2016",
"owners": "Consortium",
"route_coordinates": [[[103.8, 1.3], [80.2, 13.0], [32.5, 29.9]]],
},
)
)
raw.add(
CollectedData(
source="arcgis_landing_points",
source_id="lp-tuas",
name="Tuas",
data_type="landing_point",
is_current=True,
extra_data={
"country": "Singapore",
"city": "Singapore",
"latitude": 1.32,
"longitude": 103.65,
"city_id": 101,
},
)
)
raw.add(
CollectedData(
source="arcgis_cable_landing_relation",
source_id="rel-1",
name="SEA-ME-WE 5 - Tuas",
data_type="relation",
is_current=True,
extra_data={"cable_id": "cable-sea-me-we-5", "city_id": 101},
)
)
# Seed legacy incomplete cable_posture v99 missing `frozen_snapshot` (R7 guard)
raw.add(
SituationalAssessment(
subject_type="cable_posture",
subject_key="cable:legacy-test:retrospective",
input_fingerprint="fp-legacy-cable",
revision=99,
generation_mode="deterministic_baseline",
payload={
"domain": "cable",
"subject_key": "cable:legacy-test:retrospective",
"title": "历史海缆冻结测试标题",
"executive_assertion": "历史版本仅含原始断言。",
"confirmed_facts": [],
"analytical_notes": [],
"open_gaps": [],
"evidence_ids": ["ev-cable-legacy"],
},
evidence_ids=["ev-cable-legacy"],
audit_meta={"time_basis": "retrospective"},
)
)
raw.commit()
# Patch satellite & vessel helpers so spatial telemetry snapshots return deterministic counts without Redis
async def _fake_sat_geojson(*, limit=None, db=None):
return {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 99901,
"geometry": {"type": "Point", "coordinates": [0, 0, 0]},
"properties": {
"norad_cat_id": 99901,
"name": "STARLINK-TEST-1",
"constellation_group": "Starlink",
"inclination": 53.2,
"mean_motion": 15.1,
"epoch": "2026-11-18T00:00:00Z",
},
}
],
"count": 1,
}
async def _fake_vessel_snapshot(db, **kwargs):
return {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 412000111,
"geometry": {"type": "Point", "coordinates": [121.5, 31.2]},
"properties": {
"mmsi": 412000111,
"mmsi_display": "412000111",
"name": "COSCO DEMO",
"vessel_type_name": "Cargo",
"flag": "CN",
"sog": 14.5,
"nav_status": 0,
"received_at": "2026-11-18T12:00:00Z",
},
}
],
"count": 1,
"stats": {"total": 1, "by_type": {"Cargo": 1}, "underway": 1, "anchored_or_moored": 0},
}
monkeypatch.setattr("app.api.v1.visualization._build_satellites_geojson", _fake_sat_geojson)
monkeypatch.setattr("app.api.v1.visualization.build_vessel_snapshot_response", _fake_vessel_snapshot)
session.stream_scalars = AsyncMock()
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
# 1. Global session with visible_layers context
g_sess = await client.post(
"/api/v1/situational-awareness/sessions",
json={
"domain": "global",
"selected_codes": ["CN", "US"],
"visible_layers": {"cables": True, "compute": True, "bgp": True, "satellites": True, "vessels": True},
},
)
assert g_sess.status_code == 200
g_sid = g_sess.json()["session_id"]
g_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{g_sid}/turns",
json={"question": "汇报当前全球多域态势", "prefer_llm": "auto"},
)
assert g_turn.status_code == 200
g_data = g_turn.json()
assert g_data["domain"] == "global"
assert g_data["zero_token"] is True
g_types = {b["block_type"]: b for b in g_data["view_spec"]["blocks"]}
assert "comparison_matrix" in g_types
assert g_types["impact_topology"]["data"]["topology_kind"] == "global_summary"
# 2. Cable turn & frozen revision verification
c_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{g_sid}/turns",
json={"domain": "cable", "question": "看看海缆总量与登陆枢纽分布", "prefer_llm": "auto"},
)
assert c_turn.status_code == 200
c_data = c_turn.json()
assert c_data["domain"] == "cable"
assert c_data["result_id"].startswith("cable:")
assert c_data["revision"] >= 1
assert "SEA-ME-WE 5" in c_data["reply"]["answer_markdown"]
c_exp = await client.post(
"/api/v1/situational-awareness/reports/export",
json={"subject_type": "cable_posture", "result_id": c_data["result_id"], "expected_revision": c_data["revision"]},
)
assert c_exp.status_code == 200
assert "核心空间态势断言" in c_exp.json()["markdown"]
# 3. Incomplete legacy cable_posture snapshot blocks live fallback (R7)
legacy_res = await client.get(
"/api/v1/situational-awareness/results/cable:legacy-test:retrospective/revisions/99"
)
assert legacy_res.status_code == 200
assert legacy_res.json()["replay_supported"] is False
assert legacy_res.json()["replay_status"] == "incomplete_snapshot"
# 4. Satellite & Vessel turns
s_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{g_sid}/turns",
json={"domain": "satellite", "question": "统计目前卫星轨道体制与星座分布"},
)
assert s_turn.status_code == 200
assert s_turn.json()["domain"] == "satellite"
assert "LEO" in s_turn.json()["reply"]["answer_markdown"]
v_turn = await client.post(
f"/api/v1/situational-awareness/sessions/{g_sid}/turns",
json={"domain": "vessel", "question": "分析近一小时 AIS 船舶态势"},
)
assert v_turn.status_code == 200
assert v_turn.json()["domain"] == "vessel"
assert "在航" in v_turn.json()["reply"]["answer_markdown"]
# 5. On-demand Spatial Stage Presentation with 0 DB writes (R1)
rows_before_pres = raw.execute(select(func.count(StagePresentation.id))).scalar_one()
pres_res = await client.get("/api/v1/situational-awareness/presentations/spatial-cable-tour")
assert pres_res.status_code == 200
pres_json = pres_res.json()
assert pres_json["domain"] == "cable"
assert len(pres_json["contract"]["steps"]) == 3
assert raw.execute(select(func.count(StagePresentation.id))).scalar_one() == rows_before_pres
# 6. Default /presentations keeps strict Outbox contract without polluting non-compute/bgp cursors
default_pres = await client.get("/api/v1/situational-awareness/presentations")
assert default_pres.status_code == 200
assert all(it["domain"] in {"compute", "bgp"} for it in default_pres.json()["items"])