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:
@@ -41,6 +41,13 @@ from dataclasses import dataclass
|
||||
from enum import IntEnum
|
||||
|
||||
from app.trace.decorators import trace_service
|
||||
from app.services.runtime_context import (
|
||||
LIVE_INSTANCE_ID,
|
||||
MODE_LIVE,
|
||||
MODE_SANDBOX,
|
||||
get_runtime_context,
|
||||
task_runtime_filter,
|
||||
)
|
||||
|
||||
|
||||
class TaskPriority(IntEnum):
|
||||
@@ -189,6 +196,14 @@ import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _runtime_values(conn, site_id: int):
|
||||
"""返回当前门店任务写入所需的运行上下文值。"""
|
||||
ctx = get_runtime_context(site_id, conn=conn)
|
||||
mode = MODE_SANDBOX if ctx.is_sandbox else MODE_LIVE
|
||||
instance_id = ctx.sandbox_instance_id if ctx.is_sandbox else LIVE_INSTANCE_ID
|
||||
return ctx, mode, instance_id, ctx.business_now
|
||||
|
||||
|
||||
def _get_connection():
|
||||
"""延迟导入 get_connection,避免纯函数测试时触发模块级导入失败。"""
|
||||
from app.database import get_connection
|
||||
@@ -210,7 +225,10 @@ def run() -> dict:
|
||||
|
||||
返回: {"created": int, "replaced": int, "skipped": int, "transferred": int}
|
||||
"""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
stats = {"created": 0, "replaced": 0, "skipped": 0, "transferred": 0}
|
||||
run_started_at = datetime.now(timezone.utc)
|
||||
|
||||
conn = _get_connection()
|
||||
try:
|
||||
@@ -265,6 +283,14 @@ def run() -> dict:
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
# ── 6. 触发 AI 消费事件 — 对本次 run 新建的任务逐个触发 ai_consumption_settled
|
||||
# 仅按 created_at >= run_started_at 过滤(精确锁定本次新建),避免误触发历史任务。
|
||||
# dispatcher 内部按 (event, member_id, site_id, date) 去重,重复触发无害。
|
||||
try:
|
||||
_fire_ai_consumption_events(conn, run_started_at)
|
||||
except Exception:
|
||||
logger.exception("ai_consumption_settled 事件触发失败(不影响任务生成主流程)")
|
||||
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@@ -278,6 +304,54 @@ def run() -> dict:
|
||||
return stats
|
||||
|
||||
|
||||
def _fire_ai_consumption_events(conn, run_started_at) -> None:
|
||||
"""查询本次 run 新建的任务,对每条 (site_id, member_id, assistant_id) 触发 ai_consumption_settled。
|
||||
|
||||
has_assistant 恒为 True(任务必然绑定助教)。
|
||||
dispatcher 去重确保每 member 每天 AI 链路至多跑一次。
|
||||
"""
|
||||
from app.services.trigger_scheduler import fire_event
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT DISTINCT site_id, member_id, assistant_id
|
||||
FROM biz.coach_tasks
|
||||
WHERE created_at >= %s
|
||||
AND member_id IS NOT NULL
|
||||
AND assistant_id IS NOT NULL
|
||||
""",
|
||||
(run_started_at,),
|
||||
)
|
||||
pairs = cur.fetchall()
|
||||
conn.commit()
|
||||
|
||||
triggered = 0
|
||||
for row in pairs:
|
||||
site_id, member_id, assistant_id = row[0], row[1], row[2]
|
||||
try:
|
||||
fire_event(
|
||||
"ai_consumption_settled",
|
||||
{
|
||||
"site_id": site_id,
|
||||
"member_id": member_id,
|
||||
"assistant_id": assistant_id,
|
||||
"has_assistant": True,
|
||||
},
|
||||
)
|
||||
triggered += 1
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"触发 ai_consumption_settled 失败: site_id=%s member_id=%s",
|
||||
site_id, member_id,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"ai_consumption_settled 触发完成: 新建任务去重后 %d 个 member,成功触发 %d 次",
|
||||
len(pairs), triggered,
|
||||
)
|
||||
|
||||
|
||||
def _run_for_site(conn, site_id: int, stats: dict) -> None:
|
||||
"""
|
||||
单门店处理流程。
|
||||
@@ -766,9 +840,10 @@ def _run_transfer_check(
|
||||
w_ms = params["transfer_score_w_ms"]
|
||||
w_ml = params["transfer_score_w_ml"]
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from app.services.runtime_context import as_runtime_now_param
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
# 业务时间锚:sandbox 模式下用 business_now,避免按真实时间把已转移很久的任务再算成候选
|
||||
now = as_runtime_now_param(site_id, conn=conn)
|
||||
|
||||
for task_id, from_assistant_id, member_id, task_type, transfer_count, created_at in candidates:
|
||||
# CHANGE 2026-03-29 | 用升级倍数判定是否触发转移
|
||||
@@ -805,9 +880,7 @@ def _run_transfer_check(
|
||||
)
|
||||
entry_dates = {r[0]: r[1] for r in cur.fetchall()}
|
||||
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
# 沿用上方 business_now,避免「真实今天」的入驻时间保护
|
||||
eligible = []
|
||||
for a in pool:
|
||||
aid = a["assistant_id"]
|
||||
|
||||
Reference in New Issue
Block a user