refactor(Dockerfile): 使用requirements.txt替代硬编码依赖

将Dockerfile中的硬编码pip包列表替换为通过requirements.txt文件安装,
提高依赖管理的灵活性和可维护性。

feat(scheduling): 移除内置APScheduler,采用独立调度系统

移除app/core/background/scheduler.py中原来的APScheduler实现,
改为使用新的应用级调度系统app.application.scheduling。

refactor(task_queue): 调整任务队列模块结构和导入路径

将任务队列相关常量从app.core.background.task_queue.constants迁移至
app.tasks.constants,并更新所有相关导入路径和引用。

refactor(events): 将事件服务重构为独立的应用层组件

将事件分发逻辑从核心层迁移到应用层,使用app.application.events.EventDispatchService
替代原有的app.modules.events.services.EventService。

feat(ai_memory): 增强AI记忆自动写入的安全策略

新增ai_memory_blocked_content_terms配置项用于阻止敏感内容,
添加TTL过期机制控制自动写入条目的生命周期。

fix(security): 强化生产环境安全验证机制

增加model_validator确保生产环境中数据库连接、API密钥、CORS设置等
关键安全配置符合要求。

feat(risks): 优化风险事件操作动作的外键约束

为RiskEventAction模型的风险事件ID字段添加外键约束,
防止孤立记录并增强数据完整性。

refactor(audit): 优化审计服务方法命名和事务处理

将AuditService的log方法重命名为record以反映其阶段行为,
并调整事务提交时机以提高性能。

feat(events): 增强领域事件并发处理和响应模型

添加事件锁定机制防止重复处理,更新API响应模型以提供
更准确的数据类型定义。
```
This commit is contained in:
2026-07-15 16:36:42 +08:00
parent 267b01b9f4
commit db751f03b4
73 changed files with 1615 additions and 933 deletions

View File

@@ -1,342 +0,0 @@
from collections.abc import Callable
from datetime import date
from socket import gethostname
from typing import Any
from fastapi import FastAPI
from app.core.config import get_settings
from app.core.constants import ActorValue
from app.modules.feishu.constants import FeishuReceiveIdType
from app.modules.observability.constants import HeartbeatComponent
def attach_scheduler(app: FastAPI) -> None:
"""Attach optional APScheduler jobs to the FastAPI application."""
settings = get_settings()
if not settings.scheduler_enabled:
return
scheduler = create_scheduler(app)
@app.on_event("startup")
def start_scheduler() -> None:
scheduler.start()
@app.on_event("shutdown")
def stop_scheduler() -> None:
scheduler.shutdown(wait=False)
def create_scheduler(app: FastAPI | None = None) -> Any:
"""Create the V2/V3 scheduler without requiring a FastAPI process."""
from apscheduler.schedulers.background import BackgroundScheduler
from app.core.database import SessionLocal
from app.core.background.task_queue import (
enqueue_attendance_summary_push,
enqueue_daily_brief_push,
enqueue_event_dispatch,
enqueue_legacy_project_sync,
enqueue_legacy_task_sync,
enqueue_lifecycle_report,
enqueue_market_report,
enqueue_project_weekly_push,
enqueue_risk_progress_push,
enqueue_work_daily_push,
enqueue_work_weekly_push,
)
from app.modules.observability.service import ObservabilityService
from app.modules.reports.services import ReportService
settings = get_settings()
scheduler = BackgroundScheduler(timezone="Asia/Shanghai")
def deliver_report(
state_key: str,
build_report: Callable[[Any], dict[str, Any]],
enqueue_push: Callable[..., dict[str, Any]],
) -> None:
db = SessionLocal()
try:
service = ReportService(db)
report = build_report(service)
_set_state(app, state_key, report)
if not _feishu_delivery_configured(settings):
return
if settings.task_queue_enabled:
dispatch = enqueue_push(
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
_set_state(app, f"{state_key}_dispatch", dispatch)
return
service.push_report(
report,
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
finally:
db.close()
def run_daily_brief() -> None:
deliver_report(
"last_daily_brief",
lambda service: service.daily_brief(),
enqueue_daily_brief_push,
)
def run_project_weekly() -> None:
deliver_report(
"last_project_weekly",
lambda service: service.project_weekly(),
enqueue_project_weekly_push,
)
def run_attendance_summary() -> None:
deliver_report(
"last_attendance_summary",
lambda service: service.attendance_summary(),
enqueue_attendance_summary_push,
)
def run_risk_progress() -> None:
deliver_report(
"last_risk_progress",
lambda service: service.risk_progress(),
enqueue_risk_progress_push,
)
def run_work_daily() -> None:
deliver_report(
"last_work_daily",
lambda service: service.work_daily_report(
reporter=ActorValue.SCHEDULER,
actor=ActorValue.SCHEDULER,
),
enqueue_work_daily_push,
)
def run_work_weekly() -> None:
deliver_report(
"last_work_weekly",
lambda service: service.work_weekly_report(
reporter=ActorValue.SCHEDULER,
actor=ActorValue.SCHEDULER,
),
enqueue_work_weekly_push,
)
def run_legacy_project_sync() -> None:
dispatch = enqueue_legacy_project_sync(actor=ActorValue.SCHEDULER)
_set_state(app, "last_legacy_project_sync_dispatch", dispatch)
def run_legacy_task_sync() -> None:
dispatch = enqueue_legacy_task_sync(actor=ActorValue.SCHEDULER)
_set_state(app, "last_legacy_task_sync_dispatch", dispatch)
def run_daily_lifecycle() -> None:
dispatch = enqueue_lifecycle_report(
report_type="daily",
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
_set_state(app, "last_daily_lifecycle_dispatch", dispatch)
def run_weekly_lifecycle() -> None:
dispatch = enqueue_lifecycle_report(
report_type="weekly",
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
_set_state(app, "last_weekly_lifecycle_dispatch", dispatch)
def run_market_premarket() -> None:
dispatch = enqueue_market_report("premarket")
_set_state(app, "last_market_premarket_dispatch", dispatch)
def run_market_close() -> None:
dispatch = enqueue_market_report("close")
_set_state(app, "last_market_close_dispatch", dispatch)
def run_market_weekly() -> None:
dispatch = enqueue_market_report("weekly", date.today())
_set_state(app, "last_market_weekly_dispatch", dispatch)
def run_event_dispatch() -> None:
dispatch = enqueue_event_dispatch(
limit=settings.event_dispatch_batch_size,
actor=ActorValue.SCHEDULER,
)
_set_state(app, "last_event_dispatch", dispatch)
def record_scheduler_heartbeat() -> None:
db = SessionLocal()
try:
heartbeat = ObservabilityService(db).record_heartbeat(
component=HeartbeatComponent.SCHEDULER,
instance_id=gethostname(),
actor=ActorValue.SCHEDULER,
)
_set_state(app, "last_scheduler_heartbeat", heartbeat)
finally:
db.close()
if settings.lifecycle_pipeline_enabled:
scheduler.add_job(
run_daily_lifecycle,
trigger="cron",
hour=settings.daily_brief_cron_hour,
minute=settings.daily_brief_cron_minute,
id="daily_lifecycle_pipeline",
replace_existing=True,
)
scheduler.add_job(
run_weekly_lifecycle,
trigger="cron",
day_of_week=settings.weekly_project_report_day_of_week,
hour=settings.weekly_project_report_cron_hour,
minute=settings.weekly_project_report_cron_minute,
id="weekly_lifecycle_pipeline",
replace_existing=True,
)
else:
scheduler.add_job(
run_daily_brief,
trigger="cron",
hour=settings.daily_brief_cron_hour,
minute=settings.daily_brief_cron_minute,
id="daily_brief_push",
replace_existing=True,
)
scheduler.add_job(
run_project_weekly,
trigger="cron",
day_of_week=settings.weekly_project_report_day_of_week,
hour=settings.weekly_project_report_cron_hour,
minute=settings.weekly_project_report_cron_minute,
id="project_weekly_push",
replace_existing=True,
)
scheduler.add_job(
run_attendance_summary,
trigger="cron",
hour=settings.attendance_summary_cron_hour,
minute=settings.attendance_summary_cron_minute,
id="attendance_summary_push",
replace_existing=True,
)
scheduler.add_job(
run_risk_progress,
trigger="cron",
hour=settings.risk_progress_cron_hour,
minute=settings.risk_progress_cron_minute,
id="risk_progress_push",
replace_existing=True,
)
scheduler.add_job(
run_work_daily,
trigger="cron",
hour=settings.work_daily_cron_hour,
minute=settings.work_daily_cron_minute,
id="work_daily_push",
replace_existing=True,
)
scheduler.add_job(
run_work_weekly,
trigger="cron",
day_of_week=settings.work_weekly_report_day_of_week,
hour=settings.work_weekly_cron_hour,
minute=settings.work_weekly_cron_minute,
id="work_weekly_push",
replace_existing=True,
)
if settings.event_dispatch_enabled:
scheduler.add_job(
run_event_dispatch,
trigger="cron",
minute=settings.event_dispatch_cron_minute,
id="event_dispatch",
replace_existing=True,
)
if settings.market_analysis_enabled:
scheduler.add_job(
run_market_premarket,
trigger="cron",
day_of_week="mon-fri",
hour=settings.market_premarket_cron_hour,
minute=settings.market_premarket_cron_minute,
id="market_premarket_analysis",
replace_existing=True,
)
scheduler.add_job(
run_market_close,
trigger="cron",
day_of_week="mon-fri",
hour=settings.market_close_cron_hour,
minute=settings.market_close_cron_minute,
id="market_close_analysis",
replace_existing=True,
)
scheduler.add_job(
run_market_weekly,
trigger="cron",
day_of_week=settings.market_weekly_day_of_week,
hour=settings.market_weekly_cron_hour,
minute=settings.market_weekly_cron_minute,
id="market_weekly_analysis",
replace_existing=True,
)
scheduler.add_job(
record_scheduler_heartbeat,
trigger="interval",
seconds=settings.heartbeat_interval_seconds,
id="scheduler_heartbeat",
replace_existing=True,
)
if (
not settings.lifecycle_pipeline_enabled
and settings.legacy_sync_enabled
and settings.legacy_project_query
):
scheduler.add_job(
run_legacy_project_sync,
trigger="cron",
hour=settings.legacy_project_sync_cron_hour,
minute=settings.legacy_project_sync_cron_minute,
id="legacy_project_sync",
replace_existing=True,
)
if (
not settings.lifecycle_pipeline_enabled
and settings.legacy_sync_enabled
and settings.legacy_task_query
):
scheduler.add_job(
run_legacy_task_sync,
trigger="cron",
hour=settings.legacy_task_sync_cron_hour,
minute=settings.legacy_task_sync_cron_minute,
id="legacy_task_sync",
replace_existing=True,
)
return scheduler
def _set_state(app: FastAPI | None, key: str, value: Any) -> None:
if app is not None:
setattr(app.state, key, value)
def _feishu_delivery_configured(settings: Any) -> bool:
return bool(
settings.feishu_app_id
and settings.feishu_app_secret
and settings.feishu_default_chat_id
)

View File

@@ -1,4 +1,4 @@
from app.core.background.task_queue.constants import (
from app.tasks.constants import (
TASK_DISPATCH_PENDING_EVENTS,
TASK_GENERATE_RISK_EVENTS,
TASK_PUSH_ATTENDANCE_SUMMARY,

View File

@@ -1,13 +0,0 @@
TASK_PUSH_DAILY_BRIEF = "reports.push_daily_brief"
TASK_PUSH_PROJECT_WEEKLY = "reports.push_project_weekly"
TASK_PUSH_ATTENDANCE_SUMMARY = "reports.push_attendance_summary"
TASK_PUSH_RISK_PROGRESS = "reports.push_risk_progress"
TASK_PUSH_WORK_DAILY = "reports.push_work_daily"
TASK_PUSH_WORK_WEEKLY = "reports.push_work_weekly"
TASK_GENERATE_RISK_EVENTS = "risks.generate_events"
TASK_SYNC_LEGACY_PROJECTS = "legacy.sync_projects"
TASK_SYNC_LEGACY_TASKS = "legacy.sync_tasks"
TASK_DISPATCH_PENDING_EVENTS = "events.dispatch_pending"
TASK_RUN_LIFECYCLE = "reports.run_lifecycle"
TASK_RUN_MARKET_REPORT = "market.report.run"
TASK_RUN_MARKET_CLOSE = TASK_RUN_MARKET_REPORT

View File

@@ -1,7 +1,8 @@
from typing import Any
from app.application.events import EventDispatchService
from app.core.config import get_settings
from app.core.background.task_queue.constants import (
from app.tasks.constants import (
TASK_DISPATCH_PENDING_EVENTS,
)
from app.core.background.task_queue.dispatcher import dispatch_task
@@ -13,11 +14,10 @@ def enqueue_event_dispatch(
) -> dict[str, Any]:
def inline() -> Any:
from app.core.database import SessionLocal
from app.modules.events.services import EventService
db = SessionLocal()
try:
return EventService(db).dispatch_pending(
return EventDispatchService(db).dispatch_pending(
limit=limit or get_settings().event_dispatch_batch_size,
worker_id=actor,
)

View File

@@ -1,6 +1,6 @@
from typing import Any
from app.core.background.task_queue.constants import (
from app.tasks.constants import (
TASK_SYNC_LEGACY_PROJECTS,
TASK_SYNC_LEGACY_TASKS,
)

View File

@@ -1,10 +1,10 @@
from typing import Any
from app.core.background.task_queue.constants import TASK_RUN_LIFECYCLE
from app.tasks.constants import TASK_RUN_LIFECYCLE
from app.core.config import get_settings
from app.core.constants import ActorValue
from app.core.database import SessionLocal
from app.modules.reports.lifecycle_pipeline import LifecyclePipelineService
from app.application.pipelines import LifecyclePipelineService
def enqueue_lifecycle_report(

View File

@@ -1,7 +1,7 @@
from datetime import date
from typing import Any
from app.core.background.task_queue.constants import TASK_RUN_MARKET_REPORT
from app.tasks.constants import TASK_RUN_MARKET_REPORT
from app.core.config import get_settings

View File

@@ -1,7 +1,7 @@
from collections.abc import Callable
from typing import Any
from app.core.background.task_queue.constants import (
from app.tasks.constants import (
TASK_PUSH_ATTENDANCE_SUMMARY,
TASK_PUSH_DAILY_BRIEF,
TASK_PUSH_PROJECT_WEEKLY,
@@ -135,6 +135,7 @@ def _enqueue_report_push(
)
def inline() -> Any:
from app.application.delivery import ReportDeliveryService
from app.core.database import SessionLocal
from app.modules.reports.services import ReportService
@@ -142,7 +143,9 @@ def _enqueue_report_push(
try:
service = ReportService(db)
report = build_report(service, actor)
return service.push_report(report, receive_id, receive_id_type, actor)
return ReportDeliveryService(db).push_report(
report, receive_id, receive_id_type, actor
)
finally:
db.close()

View File

@@ -1,6 +1,6 @@
from typing import Any
from app.core.background.task_queue.constants import (
from app.tasks.constants import (
TASK_GENERATE_RISK_EVENTS,
)
from app.core.background.task_queue.dispatcher import dispatch_task

View File

@@ -2,7 +2,7 @@ import json
from functools import lru_cache
from typing import Annotated, Any
from pydantic import Field, field_validator
from pydantic import Field, field_validator, model_validator
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
from app.core.constants import (
@@ -112,6 +112,25 @@ class Settings(BaseSettings):
ai_memory_enabled: bool = True
ai_memory_auto_write_enabled: bool = True
ai_memory_recall_limit: int = 5
ai_memory_auto_write_ttl_days: int = Field(default=90, ge=1, le=3650)
ai_memory_blocked_content_terms: list[str] = Field(
default_factory=lambda: [
"bank account",
"budget",
"cash flow",
"financial statement",
"id card",
"payment",
"salary",
"成本",
"付款",
"工资",
"收款",
"现金流",
"财务",
"预算",
]
)
ai_analysis_max_attempts: int = 3
ai_memory_forbidden_keys: list[str] = Field(
default_factory=lambda: [
@@ -151,6 +170,7 @@ class Settings(BaseSettings):
"openclaw_allowed_tools",
"openclaw_allowed_actions",
"ai_memory_forbidden_keys",
"ai_memory_blocked_content_terms",
mode="before",
)
@classmethod
@@ -216,6 +236,27 @@ class Settings(BaseSettings):
return {str(key): str(item) for key, item in data.items()}
raise ValueError(ConfigErrorDetail.LEGACY_ALLOWED_QUERIES_FORMAT)
@model_validator(mode="after")
def validate_production_safety(self) -> "Settings":
if self.app_env.lower() not in {"prod", "production"}:
return self
errors: list[str] = []
if self.database_url.startswith("sqlite"):
errors.append("DATABASE_URL must use PostgreSQL in production")
if not self.api_key and not any(item.get("key") for item in self.api_keys):
errors.append("API_KEY or API_KEYS is required in production")
if not self.audit_api_key and not any(
item.get("key") for item in self.audit_api_keys
):
errors.append("AUDIT_API_KEY or AUDIT_API_KEYS is required in production")
if "*" in self.cors_origins:
errors.append("CORS_ORIGINS cannot contain '*' in production")
if self.debug:
errors.append("DEBUG must be false in production")
if errors:
raise ValueError("; ".join(errors))
return self
@lru_cache
def get_settings() -> Settings:

View File

@@ -0,0 +1,12 @@
from typing import Any
from fastapi.responses import JSONResponse
from app.core.http.masking import mask_configured
class MaskedJSONResponse(JSONResponse):
"""Apply baseline sensitive-field masking to every JSON response."""
def render(self, content: Any) -> bytes:
return super().render(mask_configured(content))

View File

@@ -3,10 +3,18 @@ from app.core.security.operation_guard import (
READ_ONLY_OPERATION_DISABLED,
require_operations_enabled,
)
from app.core.security.operation_policy import (
OperationsDisabledError,
business_mutations_enabled,
ensure_business_mutations_enabled,
)
__all__ = [
"ApiPrincipal",
"OperationsDisabledError",
"READ_ONLY_OPERATION_DISABLED",
"business_mutations_enabled",
"ensure_business_mutations_enabled",
"require_api_key",
"require_audit_api_key",
"require_operations_enabled",

View File

@@ -1,6 +1,6 @@
from fastapi import HTTPException, status
from app.core.config import get_settings
from app.core.security.operation_policy import business_mutations_enabled
READ_ONLY_OPERATION_DISABLED = "This service is read-only; data mutation operations are disabled"
@@ -9,7 +9,7 @@ READ_ONLY_OPERATION_DISABLED = "This service is read-only; data mutation operati
def require_operations_enabled(detail: str = READ_ONLY_OPERATION_DISABLED) -> None:
"""Reject mutation-oriented endpoints when the product is running read-only."""
if get_settings().read_only_mode:
if not business_mutations_enabled():
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail=detail,

View File

@@ -0,0 +1,20 @@
from app.core.config import get_settings
class OperationsDisabledError(RuntimeError):
"""Raised when a business mutation is attempted in read-only mode."""
def business_mutations_enabled() -> bool:
"""Return whether platform-owned business ledgers may be mutated."""
return not get_settings().read_only_mode
def ensure_business_mutations_enabled() -> None:
"""Enforce read-only policy outside the HTTP transport layer."""
if not business_mutations_enabled():
raise OperationsDisabledError(
"Business mutations are disabled while READ_ONLY_MODE is enabled"
)