feat: 2026-04-15~05-02 累积变更基线 — AI 重构 + Runtime Context + DWS 修复

涵盖(每条对应已存的审计记录):
- AI 模块拆分:apps/backend/app/ai/apps -> prompts/(8 个 APP + app2a 派生)
  audit: 2026-04-20__ai-module-complete.md
- admin-web AI 管理套件:AIDashboard / AIOperations / AIRunLogs / AITriggers / TriggerManager
  audit: 2026-04-21__admin-web-ai-management-suite.md
- App2 财务洞察 prompt v3 -> v5.1 + 小程序 AI 接入(chat / board-finance)
  audit: 2026-04-22__app2_prompt_v5_1_and_miniprogram_ai_insight.md
- App2 prewarm 全过滤器 + AI 触发器 cron reschedule
  audit: 2026-04-21__app2-finance-prewarm-all-filters.md
  migration: 20260420_ai_trigger_jobs_and_app2_prewarm.sql / 20260421_app2_prewarm_cron_reschedule.sql
- AppType 联合类型对齐 + adminAiAppTypes.test.ts
  audit: 2026-04-30__admin_web_ai_app_type_alignment.md
- DashScope tokens_used 提取修复
  audit: 2026-04-30__backend_dashscope_tokens_used_extraction.md
- App3 线索完整详情 prompt
  audit: 2026-05-01__backend_app3_full_detail_prompt.md
- Runtime Context 沙箱(5-1~5-2 主线):
  - 后端 schema/service + admin_runtime_context / xcx_runtime_clock 两个 router
  - admin-web RuntimeContext.tsx + miniprogram runtime-clock.ts
  - migration: 20260501__runtime_context_sandbox.sql
  - tools/db/verify_admin_web_sandbox.py + verify_sandbox_end_to_end.py
  - database/changes: 7 份 sandbox_* 验证报告
- 飞球 DWS 修复:finance_area_daily 区域汇总 + task_engine 调整
  + RLS 视图业务日上界(migration 20260502 + scripts/ops/gen_rls_business_date_migration.py)

合规:
- .gitignore 启用 tmp/ 排除
- 不入仓:apps/etl/connectors/feiqiu/.env(API_TOKEN secret,本地修改保留)

待验证清单:
- docs/audit/changes/2026-05-04__cumulative_baseline_pending_verification.md
  每个主题的功能完整性 / 上线验证几乎都未收口,按优先级 P0~P3 逐一处理
This commit is contained in:
Neo
2026-05-04 02:30:19 +08:00
parent 2010034840
commit caf179a5da
130 changed files with 14543 additions and 2717 deletions

View File

@@ -10,7 +10,10 @@
"""
from __future__ import annotations
import asyncio
import inspect
import logging
import threading
from datetime import datetime, timedelta, timezone
from typing import Any, Callable
@@ -19,6 +22,34 @@ from app.trace.decorators import trace_service
logger = logging.getLogger(__name__)
def _invoke_handler(handler: Callable, **kwargs: Any) -> Any:
"""统一调用 handler自动识别 sync / async。
- sync handler直接返回结果
- async handler
- 当前线程有 running loop → loop.create_task(coro),后台异步执行
- 当前线程无 running loop → 新起 daemon 线程跑 asyncio.run(coro),不阻塞调用方
说明fire_event / check_scheduled_jobs 是 sync 函数,但部分 handler
(如 dispatcher 注册的 AI 事件 handler是 async def本包装器保证正确调度。
"""
result = handler(**kwargs)
if not inspect.iscoroutine(result):
return result
try:
loop = asyncio.get_running_loop()
loop.create_task(result)
return None
except RuntimeError:
# 同步线程(无 running loop用后台线程异步执行 coroutine不阻塞调用方
threading.Thread(
target=lambda coro=result: asyncio.run(coro),
daemon=True,
).start()
return None
def _get_connection():
"""延迟导入 get_connection避免纯函数测试时触发模块级导入失败。"""
from app.database import get_connection
@@ -89,7 +120,8 @@ def fire_event(event_name: str, payload: dict[str, Any] | None = None) -> int:
continue
try:
# 将 job_id 传入 handlerhandler 在最终 commit 前更新 last_run_at
handler(payload=payload, job_id=job_id)
# async handler 经 _invoke_handler 自动调度
_invoke_handler(handler, payload=payload, job_id=job_id)
executed += 1
except Exception:
logger.exception("触发器 %s 执行失败", job_name)
@@ -136,7 +168,8 @@ def check_scheduled_jobs() -> int:
continue
try:
# cron/interval handler 接受 conn + job_id在最终 commit 前更新时间戳
handler(conn=conn, job_id=job_id)
# async handler 经 _invoke_handler 自动调度
_invoke_handler(handler, conn=conn, job_id=job_id)
# 计算 next_run_at 并更新(在 handler commit 后的新事务中)
next_run = _calculate_next_run(trigger_condition, trigger_config)
with conn.cursor() as cur:
@@ -276,7 +309,7 @@ def run_job_by_id(job_id: int) -> dict:
return {"success": False, "message": f"任务 {job_name} 未注册处理器"}
try:
handler()
_invoke_handler(handler)
# 更新 last_run_at 和 next_run_at
next_run = _calculate_next_run(trigger_condition, trigger_config)
with conn.cursor() as cur: