batch_context.py 910 B

123456789101112131415161718192021222324252627282930313233
  1. """宿主绑定的矿级执行上下文,不接受模型提供输出路径、凭证或取数范围。"""
  2. from contextvars import ContextVar
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. from typing import Any
  6. from extensions.main_fan.batch_models import ShaftPlan
  7. @dataclass
  8. class BatchScope:
  9. batch_id: str
  10. mine: dict
  11. user: str
  12. session: str
  13. output: Path
  14. cache_root: Path
  15. cutoff: str
  16. lookback_days: int
  17. report_client: Any
  18. shaft: ShaftPlan | None = None
  19. progress: Any = None
  20. active_workflow_id: str = ''
  21. current_scope: ContextVar[BatchScope | None] = ContextVar('main_fan_batch_scope', default=None)
  22. def scope_for(user, session):
  23. scope = current_scope.get()
  24. if scope is None or (scope.user, scope.session) != (user, session):
  25. raise ValueError('该工具仅供宿主已绑定的主通风机本地批次使用')
  26. return scope