全栈安全加固、性能提速与架构演进:连接池与沙箱止血、双路混合检索、多版本归档及前端分包治理

- 第一批(安全加固与生命周期止血):隔离测试宿主机密钥与 .env 泄漏;修复 SQLite 取消查询挂死、:memory: 连接池报错与 EngineCache 世代竞态;补齐运行与评测删除时的 Checkpointer 线程、Artifact 磁盘文件与无主附件回收;加固三套沙箱路径越界、进程组清理与 Seatbelt 数据目录隔离,封堵 SSRF IPv4-mapped IPv6 元数据绕过与响应体 OOM;修复推理模型多轮 reasoning_content、结构化输出 Token 漏计与缓存折扣计费
- 第二批(数据库/集群/问数提速与首屏分包):补齐核心表索引与 SQLite WAL 调优,列表接口按需 defer 大列,收敛启动扫描并打通跨进程 slot 槽位唤醒;优化跨源全局选表、哈希桶数字绑定、流式 CSV 导入及多方言分析算子;剥离首屏静态引入的 @xyflow/react 与全局样式(首屏主包降至 461.59 kB),补充流式 rAF 攒批、Store 精细订阅与表单无障碍绑定
- 第三批(架构演进与工程自动化闭环):记录库超挂接阈值历史版本自动增量归档至单库多表(解除 SQLite 10 库 ATTACH 硬上限);重构知识库为词面 BM25 与全集合归一化向量矩阵真·双路独立召回并支持游标分批重建;升级多方言感知 SQL 模板词法分析(E'...' / $$...$$ / q'[...]' / # / 嵌套注释)与 Decimal / bool / NaN 参数校验,消除 ledger 层对 api 路由层的反向依赖;拆分 3101 行 CaliberDrafts 页面为 7 个领域子模块,消除检查脚本本机路径硬编码并新增 GitHub Actions CI 门禁与三批回归测试套件
This commit is contained in:
ThearchyHelios
2026-11-13 14:51:36 +08:00
parent 09949fc8ac
commit eddeded2a4
106 changed files with 7178 additions and 3913 deletions
+11
View File
@@ -19,6 +19,17 @@ from pathlib import Path
_TMP = Path(tempfile.mkdtemp(prefix="agentlab-tests-"))
os.environ["AGENTLAB_DATA_DIR"] = str(_TMP)
os.environ["AGENTLAB_DISABLE_DOTENV"] = "1"
# 剥离宿主机环境里可能携带的真实模型密钥与地址,防止测试或 seed 误调线上付费接口;需要测环境变量回退的用例用 monkeypatch.setenv 单独设
for _env_key in (
"ANTHROPIC_API_KEY",
"ANTHROPIC_BASE_URL",
"ANTHROPIC_AUTH_TOKEN",
"OPENAI_API_KEY",
"OPENAI_BASE_URL",
"AGENTLAB_USE_OPENAI_EMBEDDINGS",
):
os.environ.pop(_env_key, None)
# 下面这几项也都要在任何 app.* 导入之前设(两种库同一个顺序):PostgreSQL 分支要先导入 app.db.base 装连接池,settings 在那一刻
# 就按当时的环境定下来了——以前这两项写在那之后,PostgreSQL 上 AGENTLAB_SCHEDULER 没生效(scheduler_on 成了 True)
# 别把真实的 key 带进测试环境
+370
View File
@@ -0,0 +1,370 @@
"""第一批 P0 止血修复、安全加固与多模型适配回归测试。"""
from __future__ import annotations
import asyncio
import types
import uuid
import httpx
import pytest
from httpx import ASGITransport, AsyncClient
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from sqlalchemy import select
from app.core import artifact_store
from app.core.config import settings
from app.data import engine as data_engine
from app.db.base import SessionLocal
from app.db.models import Artifact, DataSource, Provider, Run
from app.db.models_blueprints import StoredFile
from app.engine import checkpoints
from app.engine.context import NodeContext, NodeError, RunContext
from app.engine.expressions import ExpressionError, eval_expression, resolve_path
from app.engine.nodes.llm import _invoke_streaming, run_llm
from app.engine.runner import run_manager
from app.engine.schema import GraphNode, GraphSpec
from app.files import store as files_store
from app.main import app
from app.providers import catalog, factory
from app.providers.factory import ModelSpec, _ReasoningAwareChatOpenAI, build_chat_model
from app.sandbox.local_sandbox import LocalSandbox
from app.sandbox.seatbelt_sandbox import SeatbeltSandbox, build_policy
from app.tools import net
async def test_memory_sqlite_engine_cache_and_epoch_invalidation(tmp_path):
cache = data_engine.EngineCache()
try:
mem_source = DataSource(
id=uuid.uuid4().hex,
name="mem-src",
kind="sqlite",
database=":memory:",
readonly=False,
)
eng = await cache.get(mem_source)
async with eng.connect() as conn:
val = await conn.scalar(data_engine.text("SELECT 42"))
assert val == 42
# 模拟 get() 在锁外核对期间并发触发 invalidate(),旧世代不得写回缓存
db_file = tmp_path / "snap.db"
conn0 = data_engine.open_checked_sqlite(str(db_file), readonly=False)
try:
conn0.execute("CREATE TABLE t (x INT)")
conn0.commit()
finally:
conn0.close()
digest = data_engine._file_sha256(str(db_file))
snap_view = types.SimpleNamespace(
id="src-epoch",
snapshot_id="s1",
kind="sqlite",
database=str(db_file),
readonly=True,
immutable=True,
expected_sha256=digest,
options={},
)
orig_verify = cache._verify_once
invalidated_once = False
async def slow_verify(key: str, source: object, expected: str):
nonlocal invalidated_once
fp = await orig_verify(key, source, expected)
if not invalidated_once:
invalidated_once = True
await cache.invalidate("src-epoch", broadcast=False)
return fp
cache._verify_once = slow_verify # type: ignore[method-assign]
eng2 = await cache.get(snap_view)
assert eng2 is not None
assert cache._epochs.get("src-epoch", 0) == 1
finally:
await cache.close()
async def test_sqlite_cancelled_query_interrupts_immediately(tmp_path):
db_file = tmp_path / "slow.db"
conn0 = data_engine.open_checked_sqlite(str(db_file), readonly=False)
try:
conn0.execute("CREATE TABLE t (x INT)")
conn0.commit()
finally:
conn0.close()
src = DataSource(
id=uuid.uuid4().hex,
name="slow-sqlite",
kind="sqlite",
database=str(db_file),
readonly=True,
)
infinite_cte = (
"WITH RECURSIVE r(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM r WHERE n < 100000000) "
"SELECT SUM(n) FROM r"
)
task = asyncio.create_task(
data_engine.run_query(src, infinite_cte, limits=data_engine.QueryLimits(timeout_seconds=30))
)
await asyncio.sleep(0.08)
task.cancel()
# 修复前 finally 先把 deadline.over 置 True,这里会挂满整条递归 CTE 甚至永久卡死
with pytest.raises(asyncio.CancelledError):
await asyncio.wait_for(task, timeout=2.0)
await data_engine.engines.invalidate(src.id, broadcast=False)
async def test_delete_run_cleans_artifacts_files_and_checkpoints():
await run_manager.setup()
try:
graph = {
"nodes": [
{"id": "in", "type": "input", "data": {"config": {}}},
{"id": "out", "type": "output", "data": {"config": {"fields": [{"name": "ok", "value": "1"}]}}},
],
"edges": [{"source": "in", "target": "out"}],
}
started = await run_manager.start(graph=graph, input_payload={})
for _ in range(100):
await asyncio.sleep(0.03)
async with SessionLocal() as session:
row = await session.get(Run, started.id)
if row and row.status in ("succeeded", "failed"):
break
assert row is not None and row.status == "succeeded"
art_id = await artifact_store.put_json({"unique_for_delete_run": started.id}, kind="test", run_id=started.id)
async with SessionLocal() as session:
saved_file = await files_store.save(
session,
f"run-bound-{started.id}".encode(),
"note.txt",
uploaded_by="tester",
context={"blueprint_id": "b1"},
)
saved_file.bound_run_id = started.id
await session.commit()
file_id, file_sha = saved_file.id, saved_file.sha256
ckpt_before, _ = await checkpoints.count(run_manager.checkpointer, started.id)
assert ckpt_before >= 1
assert artifact_store.load(art_id) is not None
assert files_store.blob_path(file_sha).exists()
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
resp = await client.delete(f"/api/runs/{started.id}")
assert resp.status_code == 204, resp.text
assert artifact_store.load(art_id) is None
assert not files_store.blob_path(file_sha).exists()
async with SessionLocal() as session:
assert await session.get(StoredFile, file_id) is None
rem_arts = list((await session.execute(select(Artifact).where(Artifact.run_id == started.id))).scalars())
assert rem_arts == []
ckpt_after, _ = await checkpoints.count(run_manager.checkpointer, started.id)
assert ckpt_after == 0
finally:
await run_manager.shutdown()
async def test_sandbox_path_prefix_and_ephemeral_cleanup():
box = LocalSandbox()
# workdir 同前缀兄弟目录逃逸(如 tmp-xxx -> ../tmp-xxx2/evil.txt)必须被 is_relative_to 拦下
escaped = await box.run("print(1)", session_id="sess1", files={"../sess1_sibling/evil.txt": "bad"})
assert not escaped.ok and "非法文件路径" in (escaped.error or "")
await box.cleanup("sess1")
before_dirs = {p.name for p in box._root.iterdir()} if box._root.exists() else set()
ok = await box.run("print('hi')")
assert ok.ok and "hi" in ok.stdout
after_dirs = {p.name for p in box._root.iterdir()} if box._root.exists() else set()
assert after_dirs - before_dirs == set(), "无 session_id 的临时工作目录执行完必须回收"
def test_seatbelt_policy_denies_data_dir_outside_home(tmp_path, monkeypatch):
ext_data = tmp_path / "external_agentlab_data"
work = ext_data / "workspace" / "seatbelt" / "s1"
work.mkdir(parents=True)
monkeypatch.setattr(settings, "data_dir", ext_data)
policy = build_policy(work, language="python", network=False)
real_data = str(ext_data.resolve())
real_work = str(work.resolve())
assert f'(deny file-read* (subpath "{real_data}"))' in policy
assert policy.index(f'(deny file-read* (subpath "{real_data}"))') < policy.index(
f'(allow file-read* (subpath "{real_work}"))'
)
assert SeatbeltSandbox is not None
async def test_net_ipv4_mapped_metadata_and_streaming_redirect_sanitization(monkeypatch):
assert net._is_metadata("::ffff:169.254.169.254") is True
assert net._is_metadata("::ffff:100.100.100.200") is True
assert net._is_private("::ffff:127.0.0.1") is True
async def fake_assert_safe(url: str, *, allow_intranet: bool = False) -> str:
return url
monkeypatch.setattr(net, "assert_safe_url", fake_assert_safe)
seen_requests: list[httpx.Request] = []
async def handler(request: httpx.Request) -> httpx.Response:
seen_requests.append(request)
if request.url.host == "origin-a.example":
return httpx.Response(302, headers={"location": "https://origin-b.example/final?x=1"})
return httpx.Response(200, content=b"0123456789abcdef", headers={"content-type": "text/plain"})
orig_client = httpx.AsyncClient
def patched_client(**kwargs):
return orig_client(transport=httpx.MockTransport(handler), **{k: v for k, v in kwargs.items() if k != "trust_env"})
monkeypatch.setattr(net.httpx, "AsyncClient", patched_client)
res = await net.safe_request(
"POST",
"https://origin-a.example/start",
headers={"Authorization": "Bearer secret", "Cookie": "sid=1", "X-Custom": "keep"},
params={"init": "1"},
json_body={"hello": "world"},
max_bytes=10,
)
assert res["status"] == 200
assert res["body"] == "0123456789"
assert res["truncated"] is True
assert len(seen_requests) == 2
hop2 = seen_requests[1]
assert hop2.method == "GET"
assert "authorization" not in hop2.headers
assert "cookie" not in hop2.headers
assert hop2.headers.get("x-custom") == "keep"
assert str(hop2.url) == "https://origin-b.example/final?x=1"
def test_expressions_private_attr_and_seq_repeat_cap():
class Dummy:
public = "ok"
_secret = "nope"
assert resolve_path({"obj": Dummy()}, "obj.public") == "ok"
assert resolve_path({"obj": Dummy()}, "obj._secret") is None
assert eval_expression("'ab' * 3", {}) == "ababab"
with pytest.raises(ExpressionError, match="序列重复长度超过上限"):
eval_expression("'x' * 2000000", {})
with pytest.raises(ExpressionError, match="序列重复长度超过上限"):
eval_expression("[1, 2] * 600000", {})
async def test_provider_reasoning_roundtrip_bearer_catalog_and_structured_usage(monkeypatch):
# 1. o1 / o3 / o4 禁采样 + 大小写不敏感匹配 + 自定义模型价格 + 缓存折扣
assert catalog.supports_sampling("o1") is False
assert catalog.supports_sampling("openai/o3-mini") is False
assert catalog.supports_sampling("O4-mini") is False
assert catalog.supports_sampling("ollama") is True
assert catalog.find_pricing("deepseek-ai/deepseek-v3") is not None
base_cost = catalog.estimate_cost("claude-sonnet-5", 10_000, 1_000)
cached_cost = catalog.estimate_cost("claude-sonnet-5", 10_000, 1_000, cache_read_tokens=9_000)
assert cached_cost < base_cost
# 2. Anthropic bearer 保留 timeout 与 default_headers
p_bearer = Provider(
name="anth-gw",
kind="anthropic",
base_url="https://gw.example.com",
api_key="tok-123",
default_model="claude-sonnet-5",
enabled=True,
extra={"auth_style": "bearer", "headers": {"X-Tenant": "t1"}},
)
chat_anth = build_chat_model(p_bearer, ModelSpec(timeout=42))
assert chat_anth._async_client.timeout == 42
assert chat_anth._async_client.default_headers.get("X-Tenant") == "t1"
# 3. ReasoningAwareChatOpenAI 在带 tool_calls 的多轮消息里回传 reasoning_content
llm = _ReasoningAwareChatOpenAI(model="deepseek-reasoner", api_key="k", base_url="https://api.deepseek.com/v1")
msgs = [
HumanMessage(content="查销售额"),
AIMessage(
content="",
tool_calls=[{"name": "sql_query", "args": {"sql": "SELECT 1"}, "id": "call_1", "type": "tool_call"}],
additional_kwargs={"reasoning_content": "先查表确认销售额"},
),
ToolMessage(content="100", tool_call_id="call_1"),
]
payload = llm._get_request_payload(msgs)
assistant_msg = next(m for m in payload["messages"] if m["role"] == "assistant")
assert assistant_msg["reasoning_content"] == "先查表确认销售额"
# 4. _NO_STREAM_USAGE 按 (endpoint, model) 粒度隔离
monkeypatch.setattr(factory, "_NO_STREAM_USAGE", {("https://gw.example.com/v1", "legacy-model")})
assert ("https://gw.example.com/v1", "modern-model") not in factory._NO_STREAM_USAGE
# 5. run_llm 结构化输出通过 include_raw=True 记录真实 token 与费用
class FakeStructuredModel:
def with_structured_output(self, schema, include_raw: bool = False):
assert include_raw is True
class _Runner:
async def ainvoke(self_inner, _messages):
return {
"raw": AIMessage(
content='{"answer": 42}',
usage_metadata={"input_tokens": 500, "output_tokens": 100, "total_tokens": 600},
),
"parsed": {"answer": 42},
"parsing_error": None,
}
return _Runner()
async def fake_get_chat_model(_session, _spec):
return FakeStructuredModel(), "gpt-5.2"
monkeypatch.setattr("app.engine.nodes.llm.get_chat_model", fake_get_chat_model)
node = GraphNode(
id="s1",
type="llm",
data={
"label": "结构化",
"config": {
"prompt": "回答",
"output_schema": {"type": "object", "properties": {"answer": {"type": "integer"}}},
},
},
)
events_seen: list[dict] = []
ctx = NodeContext(
node=node,
run=RunContext(
run_id="r-struct",
thread_id="t-struct",
spec=GraphSpec(nodes=[node], edges=[]),
extra={"stream_writer": lambda ev: events_seen.append(ev)},
),
)
out = await run_llm({"input": {}, "vars": {}, "nodes": {}, "messages": []}, ctx)
assert out["nodes"]["s1"]["data"] == {"answer": 42}
assert out["usage"]["input_tokens"] == 500 and out["usage"]["output_tokens"] == 100
assert out["usage"]["cost_usd"] > 0
# 6. _invoke_streaming 遇到 401/429 等不可重试错误直接抛出,不触发二次非流式请求
ainvoke_called = 0
class AuthFailModel:
async def astream(self, _messages):
if False:
yield None
err = RuntimeError("401 invalid api key")
err.status_code = 401 # type: ignore[attr-defined]
raise err
async def ainvoke(self, _messages):
nonlocal ainvoke_called
ainvoke_called += 1
return AIMessage(content="should not happen")
with pytest.raises(NodeError):
await _invoke_streaming(AuthFailModel(), [HumanMessage(content="hi")], ctx, "gpt-5.2")
assert ainvoke_called == 0
+256
View File
@@ -0,0 +1,256 @@
from __future__ import annotations
from datetime import date
from fractions import Fraction
import math
from pathlib import Path
from types import SimpleNamespace
import pytest
from sqlalchemy import event, text
from sqlalchemy.ext.asyncio import create_async_engine
from app.analysis import DATA_ERRORS, stats as analysis_stats
from app.analysis.contract import CallError
from app.analysis.decompose import lmdi
from app.analysis.differ import MAX_ROWS_MODE
from app.analysis.periods import plain_day
from app.analysis.stats import theil_sen
from app.chat.direct import build_direct_graph
from app.chat.direct_evidence import normalize_and_bind_fast_citations
from app.chat.grounding import GroundingBundle
from app.cluster.service import _Service
from app.core.bus import EventBus, _MAX_SEQ_ENTRIES
from app.data import coverage, csv_grid
from app.data.tabular import _detect_csv_encoding
from app.db.base import _INDEX_MIGRATIONS, _set_sqlite_pragma, create_schema
from app.engine.schema import GraphSpec, validate_graph
from app.ledger.match import Caliber, Fetch, Query, compare_queries
from tests.analysis_samples import analyze, snap as make_snap
@pytest.mark.asyncio
async def test_sqlite_pragmas_and_index_migrations(tmp_path: Path) -> None:
db_path = tmp_path / "idx_check.db"
eng = create_async_engine(f"sqlite+aiosqlite:///{db_path}")
event.listen(eng.sync_engine, "connect", _set_sqlite_pragma)
try:
await create_schema(eng)
async with eng.connect() as conn:
sync_mode = (await conn.execute(text("PRAGMA synchronous"))).scalar_one()
# SQLite NORMAL = 1
assert sync_mode == 1
rows = (await conn.execute(text("SELECT name FROM sqlite_master WHERE type='index'"))).scalars().all()
existing = set(rows)
for idx_name, _, _ in _INDEX_MIGRATIONS:
assert idx_name in existing
finally:
await eng.dispose()
@pytest.mark.asyncio
async def test_event_bus_seq_lru_and_close_eviction() -> None:
bus = EventBus()
seq1 = bus.next_seq("run-1")
assert seq1 == 1
assert "run-1" in bus._seq
await bus.close("run-1")
assert "run-1" not in bus._seq
for i in range(_MAX_SEQ_ENTRIES + 25):
bus.next_seq(f"r-{i}")
assert len(bus._seq) <= _MAX_SEQ_ENTRIES
assert "r-0" not in bus._seq
def test_cluster_slot_signal_wakes_local_waiters(monkeypatch: pytest.MonkeyPatch) -> None:
woken: list[bool] = []
monkeypatch.setattr("app.cluster.service.slots.wake_local", lambda: woken.append(True))
svc = _Service()
svc._on_slot(["w1"])
assert woken == [True]
def test_coverage_parses_two_digit_tz_offset_and_lru_eviction() -> None:
d1 = coverage.as_day("2026-08-31 23:30:00+08")
d2 = coverage.as_day("2026-08-31T00:00:00+00")
d3 = coverage.as_day("2026-08-31 12:00:00-0530")
assert d1 is not None and d1.day == date(2026, 8, 31) and d1.timed is True
assert d2 is not None and d2.day == date(2026, 8, 31) and d2.timed is False
assert d3 is not None and d3.day == date(2026, 8, 31) and d3.timed is True
coverage._LIVE.clear()
try:
for i in range(coverage._LIVE_MAX + 10):
coverage._LIVE[("src", f"t_{i}")] = (1e12, i) # type: ignore[assignment]
if len(coverage._LIVE) > coverage._LIVE_MAX:
coverage._LIVE.popitem(last=False)
assert len(coverage._LIVE) == coverage._LIVE_MAX
assert ("src", "t_0") not in coverage._LIVE
assert ("src", f"t_{coverage._LIVE_MAX + 9}") in coverage._LIVE
finally:
coverage._LIVE.clear()
def test_compare_queries_prioritizes_table_overlap_before_max_candidates() -> None:
calibers: list[Caliber] = []
for i in range(95):
calibers.append(
Caliber(
workflow_id=f"wf_{i}",
version=1,
level="certified",
graph_hash=f"h_{i}",
fetches=(
Fetch(
node_id="q1",
source="sales",
sql=f"SELECT COUNT(*) AS cnt_{i} FROM orders",
tables=frozenset({"orders"}),
),
),
)
)
calibers.append(
Caliber(
workflow_id="wf_exact",
version=1,
level="certified",
graph_hash="h_exact",
fetches=(
Fetch(
node_id="q1",
source="sales",
sql="SELECT SUM(o.amount - COALESCE(r.refund_amt, 0)) AS net_sales "
"FROM orders o LEFT JOIN refunds r ON r.order_id = o.id",
tables=frozenset({"orders", "refunds"}),
),
),
)
)
q = Query(
artifact="sha_q1",
node_id="fetch_1",
source="sales",
sql="SELECT SUM(o.amount - COALESCE(r.refund_amt, 0)) AS net_sales "
"FROM orders o LEFT JOIN refunds r ON r.order_id = o.id",
columns=["net_sales"],
tables=frozenset({"orders", "refunds"}),
dialect="sqlite",
)
res = compare_queries([q], calibers)
assert len(res) == 1
assert res[0]["status"] == "proven"
assert res[0]["candidates"][0]["workflow_id"] == "wf_exact"
def test_direct_evidence_indexed_auto_bind_preserves_semantics() -> None:
snap = {
"kind": "tool_output",
"tool": "db_query__mes",
"columns": ["line_code", "good_qty", "yield_rate", "output_amt"],
"column_types": ["text", "integer", "real", "real"],
"rows": [["L01", 69133, 0.985, 125000.0]],
"truncated": False,
}
catalog = {
"Q1": {
"kind": "query",
"artifact": "art_q1",
"tool": "db_query__mes",
"source": "mes",
"columns": ["line_code", "good_qty", "yield_rate", "output_amt"],
"row_count": 1,
}
}
loader = lambda art: snap if art == "art_q1" else None
raw = "产线 L01 良品数为 69,133 件,良率达到 98.5%,产值 12.5万 元。"
bound = normalize_and_bind_fast_citations(raw, catalog, loader=loader)
assert "[[v:Q1.r0.good_qty]]" in bound
assert "[[v:Q1.r0.yield_rate|pct]]" in bound
assert "[[v:Q1.r0.output_amt|万]]" in bound
def test_plain_day_recognizes_cast_and_pg_double_colon() -> None:
assert plain_day("SELECT CAST(stat_date AS DATE) AS dt, SUM(x) FROM t GROUP BY 1", "dt", "sqlite") is True
assert plain_day("SELECT stat_date::date AS dt, SUM(x) FROM t GROUP BY 1", "dt", "postgres") is True
assert plain_day("SELECT DATE_TRUNC('month', stat_date) AS dt, SUM(x) FROM t GROUP BY 1", "dt", "postgres") is False
@pytest.mark.asyncio
async def test_differ_rows_mode_enforces_max_rows_ceiling() -> None:
assert "too_many_rows" in DATA_ERRORS
rows = [["A" if i % 2 == 0 else "B", float(i)] for i in range(MAX_ROWS_MODE + 5)]
s = make_snap("Q1", "SELECT grp, val FROM t", ["grp", "val"], rows)
with pytest.raises(CallError) as exc_info:
await analyze("differ", "rows", snaps=[s], query="Q1", by="grp", value="val")
assert exc_info.value.code == "too_many_rows"
refused = await analyze(
"differ", "rows", snaps=[s], refuse_data_errors=True, query="Q1", by="grp", value="val"
)
assert refused["status"] == "refused"
assert refused["reason_code"] == "input_unavailable"
assert str(MAX_ROWS_MODE) in refused["reason"]
def test_theil_sen_handles_large_denominator_lcm_and_extreme_floats() -> None:
primes = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
239, 241, 251, 257, 263, 269, 271, 277, 281,
]
xs = [Fraction(i + 1, p) for i, p in enumerate(primes)]
ys = [x * 3 + Fraction(1, 7) for x in xs]
slope = theil_sen(xs, ys)
assert math.isclose(slope, 3.0, rel_tol=1e-9)
huge_xs = [Fraction(0), Fraction(1, 10**200), Fraction(2, 10**200)]
huge_ys = [Fraction(0), Fraction(10**200, 1), Fraction(2 * 10**200, 1)]
huge_slope = theil_sen(huge_xs, huge_ys)
assert huge_slope == Fraction(10**400, 1)
assert math.isinf(analysis_stats._safe_float(huge_slope))
def test_lmdi_handles_many_factors_without_float_overflow() -> None:
base = [10.0] * 300
target = [20.0] * 300
contribs = lmdi(base, target, 100.0, 250.0)
assert len(contribs) == 300
assert math.isclose(sum(contribs), 150.0, rel_tol=1e-9)
def test_streaming_csv_encoding_detection_and_grid_scan() -> None:
gbk_bytes = "产线,良品数,备注\nL01,1200,正常生产\nL02,980,换型调试\n".encode("gb18030")
assert _detect_csv_encoding(gbk_bytes) == ("gb18030", "strict")
utf8_bom = b"\xef\xbb\xbfcol_a,col_b\n1,2\n"
assert _detect_csv_encoding(utf8_bom) == ("utf-8-sig", "strict")
scanned = csv_grid.scan(gbk_bytes)
assert len(scanned.sheets) == 1
assert scanned.sheets[0].bounds is not None
assert scanned.sheets[0].bounds.max_row == 3
assert scanned.sheets[0].bounds.max_col == 3
parsed = csv_grid.parse(gbk_bytes)
assert len(parsed.records) == 3
assert parsed.records[0] == ["产线", "良品数", "备注"]
assert parsed.records[1] == ["L01", "1200", "正常生产"]
streamed = list(csv_grid.rows(parsed, min_row=1, max_row=3, min_col=1, max_col=3))
assert len(streamed) == 3
def test_build_direct_graph_with_and_without_metrics_passes_validation() -> None:
b = GroundingBundle(narrowed_sources=[SimpleNamespace(name="demo_db")]) # type: ignore[list-item]
assert b.has_enabled_metrics is False
g_no_metrics = build_direct_graph(bundle=b, has_metrics=False)
res_no = validate_graph(GraphSpec.model_validate(g_no_metrics))
assert res_no.ok is True, res_no.issues
g_with_metrics = build_direct_graph(bundle=b, has_metrics=True)
res_yes = validate_graph(GraphSpec.model_validate(g_with_metrics))
assert res_yes.ok is True, res_yes.issues
+388
View File
@@ -0,0 +1,388 @@
"""第三批架构级演进回归测试(Step 3.1 – 3.4)。
覆盖:
1. 记录库 `records` 多版本单库归档演进(突破 SQLite 10 库 `ATTACH` 上限、增量同步、WAL 与 `records_archive` 越权拦截);
2. 知识库真·双路混合检索(零词面重合高语义命中不被 BM25 前置截断、向量缓存失效、>1000 块倒排分批更新);
3. 方言感知 SQL 模板词法分析(MySQL `\\'`/`#`、PostgreSQL `E'...'`/`$$...$$`/嵌套注释/数组下标、Oracle `q'[...]'`、SQLite `[id]`)与 `Decimal`/`bool`/`NaN` 参数完整性;
4. 分层依赖守卫(`app/ledger/*` 零依赖 `app.api.settings` / `app.api.ledger` / `app.api.evidence`)与前端组件拆分门禁。
"""
from __future__ import annotations
import ast
import sqlite3
import uuid
from decimal import Decimal
from pathlib import Path
import numpy as np
import pytest
from sqlalchemy import select
from app.core.config import settings
from app.data.engine import run_query
from app.data.guard import SqlRejected
from app.data.sql_semantics import _template_slots
from app.db.base import SessionLocal
from app.db.models import Chunk, DataSource, Document
from app.db.models_apps import App
from app.db.models_blueprints import RecordsTable
from app.engine.expressions import render_template
from app.ledger import params
from app.memory import embeddings as emb
from app.memory import inverted, kb
from app.records import store
from app.records.tables import columns_of, ensure_table, path_of
from app.records.views import ARCHIVE_SCHEMA, resolve
@pytest.fixture
def isolated_data_dir(monkeypatch, tmp_path):
data = tmp_path / "data"
(data / "uploads").mkdir(parents=True)
monkeypatch.setattr(settings, "data_dir", data)
kb.invalidate_vector_cache()
yield data
kb.invalidate_vector_cache()
# ---------------------------------------------------------------------------
# Step 3.1:记录库 >10 版本单库归档、增量同步与安全边界
# ---------------------------------------------------------------------------
async def test_records_scale_beyond_sqlite_attach_limit_with_incremental_archive(isolated_data_dir):
async with SessionLocal() as session:
owner = App(name="多版本报损台账", created_by="admin")
session.add(owner)
await session.commit()
app_id = owner.id
versions: list[RecordsTable] = []
inserted_row_ids: dict[int, str] = {}
# 连续演进 14 个不兼容版本(每次新增一个必填列),远超 SQLite 默认 SQLITE_MAX_ATTACHED=10 上限
for idx in range(1, 15):
cols = [
{"name": "门店", "label": "门店", "type": "string", "required": True},
{"name": "数量", "label": "数量", "type": "integer", "required": True},
{"name": f"必填项_{idx}", "label": f"必填项_{idx}", "type": "string", "required": True},
]
async with SessionLocal() as session:
owner = await session.get(App, app_id)
tbl = await ensure_table(session, owner, cols)
await session.commit()
assert tbl.version == idx
rec = store.append(
path_of(tbl),
columns_of(tbl),
{"门店": f"门店_{idx:02d}", "数量": idx * 10, f"必填项_{idx}": f"v{idx}"},
version=tbl.version,
run_id=None,
actor="amy",
action_key=uuid.uuid4().hex,
)
inserted_row_ids[idx] = rec.row_id
versions.append(tbl)
# 验证单表库开启了 WAL 模式
with sqlite3.connect(path_of(versions[-1])) as conn:
mode = conn.execute("PRAGMA journal_mode").fetchone()[0]
assert str(mode).lower() == "wal"
# 在将被归档的第 2 版中做一次更正(supersedes),确保归档表保留 _supersedes 演进语义
store.append(
path_of(versions[1]),
columns_of(versions[1]),
{"门店": "门店_02", "数量": 25, "必填项_2": "v2_fixed"},
version=2,
run_id=None,
actor="amy",
action_key=uuid.uuid4().hex,
supersedes=inserted_row_ids[2],
)
latest = versions[-1]
async with SessionLocal() as session:
source = await session.get(DataSource, latest.source_id)
resolved = await resolve(session, source)
# 13 个历史版本若逐库 ATTACH 会触发 OperationalError: too many attached databases (max 10)
# 归档收敛后挂接数 <= 8(7 个近期直挂库 + 1 个归档库)
assert len(resolved.attach) <= 8
assert ARCHIVE_SCHEMA in dict(resolved.attach)
assert store.archive_path(latest.table_key).exists()
dbs = await run_query(resolved, "PRAGMA database_list")
assert len(dbs.rows) <= 10
summary = await run_query(resolved, 'SELECT count(*) AS n, sum("数量") AS total FROM records')
expected_total = sum(i * 10 for i in range(1, 15)) - 20 + 25
assert summary.rows == [[14, expected_total]]
# 模拟停用前已在途的动作向第 1 版追加一行,验证 sync_archive 按 _seq > since_seq 增量同步
store.append(
path_of(versions[0]),
columns_of(versions[0]),
{"门店": "门店_01_补单", "数量": 5, "必填项_1": "v1_late"},
version=1,
run_id=None,
actor="bob",
action_key=uuid.uuid4().hex,
)
async with SessionLocal() as session:
refreshed = await resolve(session, source)
summary_after = await run_query(refreshed, 'SELECT count(*) AS n, sum("数量") AS total FROM records')
assert summary_after.rows == [[15, expected_total + 5]]
# 直接点名归档 schema 必须被 SQL 守卫拦截
for forbidden_sql in (
f"SELECT count(*) FROM {ARCHIVE_SCHEMA}.rows_v1",
f'SELECT count(*) FROM "{ARCHIVE_SCHEMA}"."rows_v1"',
f"SELECT count(*) FROM [{ARCHIVE_SCHEMA.upper()}].rows_v1",
):
with pytest.raises(SqlRejected):
await run_query(refreshed, forbidden_sql)
# ---------------------------------------------------------------------------
# Step 3.2:真·双路混合检索、缓存失效与超千块倒排分批处理
# ---------------------------------------------------------------------------
async def test_kb_true_dual_path_hybrid_search_finds_zero_lexical_overlap_hit(isolated_data_dir, monkeypatch):
target_text = "超级管理员可调整他人角色与访问范围。"
distractors = [f"审批规范第 {i} 条:普通报销单需部门主管签字归档。" for i in range(65)]
async def fake_embed_texts(texts: list[str]) -> list[np.ndarray]:
vecs: list[np.ndarray] = []
for t in texts:
v = np.zeros(emb.embedder_dim(), dtype=np.float32)
if "超级管理员" in t:
v[0] = 1.0
else:
v[1] = 1.0
vecs.append(v)
return vecs
async def fake_embed_text(query: str) -> np.ndarray:
v = np.zeros(emb.embedder_dim(), dtype=np.float32)
v[0] = 1.0
return v
monkeypatch.setattr(kb, "embed_texts", fake_embed_texts)
monkeypatch.setattr(kb, "embed_text", fake_embed_text)
collection = f"dual_path_{uuid.uuid4().hex[:8]}"
async with SessionLocal() as session:
# 65 条词面强命中「审批规范」的干扰块(超过原 BM25 单路候选池上限 50)
for idx, text in enumerate(distractors):
await kb.ingest_document(session, collection=collection, title=f"干扰_{idx}", content=text)
# 1 条零词面重合但语义完全一致的目标块
target_doc = await kb.ingest_document(session, collection=collection, title="权限说明", content=target_text)
degrade_reasons: list[str] = []
async with SessionLocal() as session:
hits = await kb.search(
session,
collection=collection,
query="审批规范里谁能改权限",
limit=5,
alpha=0.6,
on_degrade=degrade_reasons.append,
)
assert degrade_reasons == []
assert hits, "双路召回应当返回结果"
assert hits[0]["content"] == target_text, "零词面重合但余弦相似度为 1.0 的目标块在双路混合检索下应排第一"
# 删除目标文档后,向量矩阵缓存自动失效,不再返回已删块
async with SessionLocal() as session:
deleted = await kb.delete_document(session, target_doc.id)
assert deleted is True
after_delete = await kb.search(
session,
collection=collection,
query="审批规范里谁能改权限",
limit=5,
alpha=0.6,
)
assert all(h["content"] != target_text for h in after_delete)
async def test_inverted_index_handles_over_thousand_chunks_without_bind_limit(isolated_data_dir):
collection = f"batch_inv_{uuid.uuid4().hex[:8]}"
doc_id = f"doc_{uuid.uuid4().hex[:12]}"
chunk_ids: list[str] = []
made: list[Chunk] = []
async with SessionLocal() as session:
session.add(Document(id=doc_id, collection=collection, title="bulk", content="bulk"))
await session.flush()
for i in range(1100):
cid = uuid.uuid4().hex
chunk_ids.append(cid)
chunk = Chunk(
id=cid,
document_id=doc_id,
collection=collection,
ordinal=i,
content=f"统一结算批次规范条目 {i}",
embedding=b"",
embed_model="",
embed_dim=0,
meta={"title": "bulk"},
)
session.add(chunk)
made.append(chunk)
await session.flush()
for chunk in made:
await inverted.index_chunk(session, chunk)
await session.commit()
async with SessionLocal() as session:
scored, all_scores = await inverted.candidates_with_scores(
session, collection=collection, query="统一结算批次规范", limit=25,
)
assert len(scored) == 25
assert len(all_scores) == 1100
assert scored[0][1] > 0
# 超 1000 个 chunk_id 删除时自动分批,不触发 SQLite 999 / PG 32767 绑定参数上限
await inverted.drop_chunk_terms(session, chunk_ids)
await session.commit()
after, after_all = await inverted.candidates_with_scores(
session, collection=collection, query="统一结算批次规范", limit=25,
)
assert after == []
assert after_all == {}
# ---------------------------------------------------------------------------
# Step 3.3:方言感知 SQL 模板词法分析与 Decimal / bool / NaN 参数完整性
# ---------------------------------------------------------------------------
def test_dialect_aware_sql_template_slots():
# MySQL 反斜杠转义单引号与 # 行注释:注释里的模板识别为 comment,真参数仍在 WHERE 后作为 bare
mysql_sql = r"SELECT 'it\'s fine' AS tag # 忽略 {{ input.ignored }}" + "\nFROM orders WHERE id = {{ input.order_id }}"
mysql_spans, _ = _template_slots(mysql_sql, dialect="mysql")
assert [(s.field, s.context) for _, _, s in mysql_spans] == [
("ignored", "comment"),
("order_id", "bare"),
]
# PostgreSQL $$...$$ 美元引号、E'...' 转义串、嵌套块注释与数组下标 arr[{{ input.idx }}]
pg_sql = (
"/* 外层 /* 内层 {{ input.in_comment }} */ 结束 */\n"
"SELECT $$raw {{ input.in_dollar }}$$ AS d, E'escaped \\' {{ input.in_estr }}' AS e, "
"tags[{{ input.idx }}] AS picked FROM items"
)
pg_spans, _ = _template_slots(pg_sql, dialect="postgres")
assert [(s.field, s.context) for _, _, s in pg_spans] == [
("in_comment", "comment"),
("in_dollar", "quoted"),
("in_estr", "quoted"),
("idx", "bare"),
]
# Oracle q'[...]' 替代引号字面量识别为 quoted 上下文
oracle_sql = "SELECT q'[ Department's {{ input.dept }} ]' FROM dual"
oracle_spans, _ = _template_slots(oracle_sql, dialect="oracle")
assert [(s.field, s.context) for _, _, s in oracle_spans] == [("dept", "quoted")]
# SQLite 方括号标识符里的占位符属于不支持的标识符注入位,必须拒绝
sqlite_sql = "SELECT * FROM [{{ input.table_name }}]"
with pytest.raises(params.ParameterRefused) as exc_info:
params.slots_of(sqlite_sql, dialect="sqlite")
assert exc_info.value.code == params.UNSUPPORTED
def test_decimal_boolean_and_non_finite_parameter_integrity():
# 极小 Decimal 在表达式求值与参数写出时不退化为科学计数法 1E-7
rendered = render_template("{{ input.rate }}", {"input": {"rate": Decimal("1E-7")}})
assert rendered == "0.0000001"
fields = [
{"name": "amount", "type": "decimal", "required": True},
{"name": "active", "type": "boolean", "required": True},
]
template = "SELECT * FROM ledger_tx WHERE amount >= {{ input.amount }} AND is_active = {{ input.active }}"
assert params.check_template(template, fields=params.declared(fields), dialect="postgres") == []
stmt = params.plan(
template,
fields=fields,
values={"amount": Decimal("19.95"), "active": True},
dialect="postgres",
)
assert "amount >= 19.95" in stmt.statement
assert "is_active = True" in stmt.statement
by_name = {entry["name"]: entry for entry in stmt.material["parameters"]}
assert by_name["amount"]["kind"] == "number" and by_name["amount"]["declared"] == "number"
assert by_name["active"]["kind"] == "boolean" and by_name["active"]["declared"] == "boolean"
# NaN / Inf 绝不能当作合法裸数拼入 SQL(否则会变成列名 NaN / Inf)
for bad_number in (float("nan"), float("inf"), float("-inf"), Decimal("NaN"), Decimal("Infinity")):
with pytest.raises(params.ParameterRefused) as exc_info:
params.plan(
"SELECT * FROM ledger_tx WHERE amount >= {{ input.amount }}",
fields=[{"name": "amount", "type": "decimal", "required": True}],
values={"amount": bad_number},
dialect="postgres",
)
assert exc_info.value.code == params.LITERAL
# ---------------------------------------------------------------------------
# Step 3.3 & 3.4:分层依赖边界与前端超大文件拆分门禁
# ---------------------------------------------------------------------------
def test_ledger_layer_has_no_upward_api_domain_imports():
repo_root = Path(__file__).resolve().parents[2]
ledger_dir = repo_root / "backend" / "app" / "ledger"
forbidden_modules = {"app.api.settings", "app.api.ledger", "app.api.evidence"}
violations: list[str] = []
for py_file in sorted(ledger_dir.rglob("*.py")):
tree = ast.parse(py_file.read_text(encoding="utf-8"), filename=str(py_file))
rel = py_file.relative_to(repo_root)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
if alias.name in forbidden_modules:
violations.append(f"{rel}:{node.lineno} import {alias.name}")
elif isinstance(node, ast.ImportFrom) and node.module:
if node.module in forbidden_modules:
violations.append(f"{rel}:{node.lineno} from {node.module} import ...")
elif node.module == "app.api":
for alias in node.names:
full = f"app.api.{alias.name}"
if full in forbidden_modules:
violations.append(f"{rel}:{node.lineno} from app.api import {alias.name}")
assert violations == [], f"ledger 领域层出现了对 api 路由层的反向依赖:{violations}"
listing_py = repo_root / "backend" / "app" / "records" / "listing.py"
listing_src = listing_py.read_text(encoding="utf-8")
assert "app.api.evidence" not in listing_src
assert "app.engine.evidence_reader" in listing_src
def test_caliber_drafts_page_is_modularized():
repo_root = Path(__file__).resolve().parents[2]
drafts_page = repo_root / "frontend" / "src" / "pages" / "ledger" / "CaliberDrafts.tsx"
drafts_dir = repo_root / "frontend" / "src" / "pages" / "ledger" / "drafts"
line_count = len(drafts_page.read_text(encoding="utf-8").splitlines())
assert line_count < 900, f"CaliberDrafts.tsx 仍超过 900 行(当前 {line_count} 行)"
expected_modules = {
"helpers.ts",
"DraftSharedBlocks.tsx",
"DraftTrialSection.tsx",
"DraftRulesSection.tsx",
"DraftImportsSection.tsx",
"DraftNlSection.tsx",
"DraftValidationSection.tsx",
}
actual_modules = {p.name for p in drafts_dir.glob("*.ts*")}
assert expected_modules.issubset(actual_modules)
+1 -4
View File
@@ -63,13 +63,10 @@ def test_empty_reasoning_is_not_reported() -> None:
def _anthropic(model: str, **spec_kw):
import os
from app.db.models import Provider
from app.providers.factory import ModelSpec, build_chat_model
os.environ.setdefault("ANTHROPIC_API_KEY", "test-only")
provider = Provider(name="t", kind="anthropic", api_key=None, models=[],
provider = Provider(name="t", kind="anthropic", api_key="test-only", models=[],
extra=spec_kw.pop("extra", {}) or {})
return build_chat_model(provider, ModelSpec(model=model, **spec_kw))