"""本地:仅离线测试。桥机容器:python run.py plan-refresh [--date 数据日]。 统一调度每五分钟触发独立 plan-refresh 步骤;北京时间 07:20—15:10 的 交易日才工作。只替换评析/区制,重放其余输入,绝不调用模型或写日频状态表。 """ from __future__ import annotations import copy import datetime as dt from zoneinfo import ZoneInfo import config import plan_inputs as pi import plan_versions as pv BJ = ZoneInfo('Asia/Shanghai') def in_window(now): from chinese_calendar import is_holiday local = now.astimezone(BJ) return (local.weekday() < 5 and not is_holiday(local.date()) and dt.time(7, 20) <= local.time().replace(tzinfo=None) <= dt.time(15, 10)) def refresh(day=None, *, now=None, regime_value=None): import plan import sources now = now or dt.datetime.now(dt.timezone.utc) if not in_window(now): return {'status': 'outside_window'} root = config.PLAN_SNAPSHOT_DIR with pv.writer(root): try: old = pv.load(root, day) morning = pv.load(root, old['date'], morning=True) except (FileNotFoundError, ValueError): return {'status': 'no_morning'} # 行情日常是昨天;晨版截止时刻才界定属于哪一个决策日。 if dt.datetime.fromisoformat(morning['decision']['cutoff_at']).astimezone(BJ).date() != now.astimezone(BJ).date(): return {'status': 'no_morning_today'} day = old['date'] pending = set(pv.health(root, day).get('pending_codes') or []) try: env = pv.envelope(root, old['decision']['id']) params = copy.deepcopy(env['params']) # 只允许本次明确批准的吸筹角色参数随真实修订改变。 # 其余冻结参数原样重放,不顺带采用当前环境的排序或资格变化。 previous_confirm = params.get('CARD_ACCUM_CONFIRM_REQUIRED', True) desired_confirm = bool(config.CARD_ACCUM_CONFIRM_REQUIRED) policy_changed = previous_confirm != desired_confirm if policy_changed: params['CARD_ACCUM_CONFIRM_REQUIRED'] = desired_confirm pending.update(r['code'] for b in ('main', 'observe') for r in old.get(b, [])) rating_changed = params.get('PLAN_RATING_PRIORITY_ENABLED', False) != config.PLAN_RATING_PRIORITY_ENABLED params['PLAN_RATING_PRIORITY_ENABLED'] = config.PLAN_RATING_PRIORITY_ENABLED params['PLAN_RATING_PRIORITY_DAYS'] = config.PLAN_RATING_PRIORITY_DAYS inputs = copy.deepcopy(env['inputs']) import user_candidates as uc selected = uc.active(root, now) candidates_changed = selected != inputs.get('user_candidates', []) if candidates_changed: pending.update(r['code'] for r in selected + inputs.get('user_candidates', [])) inputs['user_candidates'] = selected pv.health(root, day, pending_codes=sorted(pending), error=None) if selected and not any(r['name'] == 'user_candidates.catalog' for r in inputs['records'].values()): # 旧发布版尚未录制完整证券名单时,作为本次真实修订的新输入补齐。 pending.update(r['code'] for r in selected) pv.health(root, day, pending_codes=sorted(pending), error=None) catalog_bundle = pi.Bundle(cutoff_at=now.isoformat()) with catalog_bundle.use(): pi.read('user_candidates.catalog', uc.catalog) inputs['records'].update(catalog_bundle.records) for rec in inputs['records'].values(): if rec['name'] != 'sources.company_reviews': continue args, kw = pi.unpack(rec['args']) before = pi.unpack(rec['value']) if 'value' in rec else {} # 读取失败时无法确定哪些股票变化,保守暂停整个本次观察范围。 pending.update(args[0]) pv.health(root, day, pending_codes=sorted(pending), error=None) after = sources.company_reviews(*args, **dict(kw, cutoff_at=now.isoformat(), strict=True, include_rating=params['PLAN_RATING_PRIORITY_ENABLED'], rating_days=params['PLAN_RATING_PRIORITY_DAYS'])) pending.difference_update(args[0]) pending.update(c for c in set(before) | set(after) if before.get(c) != after.get(c)) rec.pop('error', None) rec['value'] = pi.pack(after) if regime_value is not None: # 抓取时刻不构成区制变化;保留最后一次语义变化的抓取时刻。 def meaningful(x): return {k: v for k, v in (x or {}).items() if k != 'fetched_at'} if meaningful(inputs.get('regime')) != meaningful(regime_value): inputs['regime'] = regime_value if policy_changed: pending.update(r['code'] for b in ('main', 'observe') for r in old.get(b, [])) pv.health(root, day, pending_codes=sorted(pending), error=None) if pv.digest(inputs) == old['decision']['input_hash'] and not policy_changed and not rating_changed: pv.health(root, day, pending_codes=[], error=None, decision_id=old['decision']['id']) return {'status': 'unchanged', 'decision': old['decision']} bundle = pi.Bundle(records=None if candidates_changed else inputs['records'], params=params, cutoff_at=now.isoformat()) if candidates_changed: # 原有调用继续读冻结输入;新增股票带来的新取数在本次真实修订中录制。 bundle.records = copy.deepcopy(inputs['records']) shown = old.get('shown_params') or {'top': 20, 'obs_top': 10, 'theme_cap': 5} with bundle.use(): data = plan.collect(day, **shown, **({'user_candidates': selected} if selected else {})) inputs['records'] = bundle.records full = data.pop('_full') data.update(main_shown=data['main'], observe_shown=data['observe'], shown_params=shown, main=full['main'], observe=full['observe'], market=inputs.get('market'), regime=inputs.get('regime')) reason = ('恢复吸筹确认规则' if desired_confirm else '吸筹改为三源合议参考') if policy_changed else ( '用户候选更新' if candidates_changed else '评析更新' if pending else '市场区制更新') if rating_changed: reason = '启用评级变动三十日排序' if config.PLAN_RATING_PRIORITY_ENABLED else '关闭评级变动排序' elif config.PLAN_RATING_PRIORITY_ENABLED and pending: reason = '评析或评级排序期限更新' published = pv.publish(root, data, inputs, params, cutoff_at=now.isoformat(), reason=reason) plan.write_published_md(published) return {'status': 'published', 'decision': published['decision']} except Exception as e: pv.health(root, day, pending_codes=sorted(pending), error=f'{type(e).__name__}: {e}') raise