diff --git a/docs/notifications-quant-sentinel.zh-CN.md b/docs/notifications-quant-sentinel.zh-CN.md index 438e2fab..53af27a0 100644 --- a/docs/notifications-quant-sentinel.zh-CN.md +++ b/docs/notifications-quant-sentinel.zh-CN.md @@ -51,7 +51,7 @@ Chat 目标仍只通过运行时注入:`QSL_GLOBAL_TELEGRAM_CHAT_ID`(首选 - **只收录当日实际运行**的平台/策略:加密小时级有周期才出现;月末 DCA 仅在运行日出现;未运行的省略。 - **无运行**:真空虚心跳(短文案)。**有运行但无成交**:心跳标题;若候选证据带有观察字段则按旧版分平台 bot 风格展开策略块。 - **有成交**:收盘日报标题;同样优先展开观察块。 -- **观察字段(有证据才写,禁止编造)**:`strategy_label`、`equity`、`holdings[]`(symbol/value/qty)、`signal_summary`、`rebalance_kind` / `rebalance_conclusion`、可选 `tips` / `account_hint`。GitHub workflow 成功结论只证明「跑过」,不填金额/持仓。 +- **观察字段(有证据才写,禁止编造)**:`strategy_label`、`equity`、`holdings[]`(symbol/value/qty)、`signal_summary`、`rebalance_kind` / `rebalance_conclusion`、可选 `tips` / `account_hint`。每个观察块标题前缀 `[account]`:`account_hint` → `target_id` → 短 `opaque_account_uid`。GitHub workflow 成功结论只证明「跑过」,不填金额/持仓。 - **不改交易/风控判定**;纯聚合展示。金额与账户标识不得写入公开 CI 日志。 ### 调度(合同默认) @@ -88,7 +88,7 @@ Chat 目标仍只通过运行时注入:`QSL_GLOBAL_TELEGRAM_CHAT_ID`(首选 ``` 📡 量化哨兵 · 心跳 业务日: 2026-10-08 -💓 【心跳检测】 +[schwab-primary] 💓 【心跳检测】 🧭 策略: 半导体趋势收益 💰 账户总权益: USD 990.06 💼 持仓 @@ -104,7 +104,7 @@ Chat 目标仍只通过运行时注入:`QSL_GLOBAL_TELEGRAM_CHAT_ID`(首选 ``` 📡 量化哨兵 · 心跳 业务日: 2026-10-09 -💓 【心跳检测】 +[schwab-primary] 💓 【心跳检测】 🧭 策略: 半导体趋势收益 💰 账户总权益: USD 989.34 ✅ 无需调仓 diff --git a/python/scripts/daily_digest_notify.py b/python/scripts/daily_digest_notify.py index 010cb100..775ee933 100644 --- a/python/scripts/daily_digest_notify.py +++ b/python/scripts/daily_digest_notify.py @@ -552,6 +552,31 @@ def _block_title(entry: DigestRunEntry, locale: DigestLocale) -> str: return _t(locale, "block_heartbeat") +def account_block_tag(entry: DigestRunEntry) -> str: + """Short account label for ``[tag]`` block titles. + + Preference order (product convention): + 1. ``account_hint`` (IBKR-style ids such as ``U16608560``) + 2. ``target_id`` (e.g. ``schwab-primary``) when not the unknown sentinel + 3. short stable form of ``opaque_account_uid`` (first 8 chars when long) + Returns empty string when nothing usable is present. + """ + + hint = str(entry.account_hint or "").strip() + if hint: + return hint + target = str(entry.target_id or "").strip() + if target and target.lower() != UNKNOWN_TARGET_ID: + return target + uid = str(entry.opaque_account_uid or "").strip() + if uid and uid.lower() != UNKNOWN_ACCOUNT_UID: + # Binding hashes are 64 hex; keep Telegram titles short and stable. + if len(uid) > 12: + return uid[:8] + return uid + return "" + + def _display_strategy(entry: DigestRunEntry) -> str: label = entry.strategy_label.strip() return label or entry.strategy_profile @@ -584,9 +609,9 @@ def _default_conclusion(entry: DigestRunEntry, locale: DigestLocale) -> str: def _render_observation_block(entry: DigestRunEntry, locale: DigestLocale) -> list[str]: lines: list[str] = [] title = _block_title(entry, locale) - hint = entry.account_hint.strip() - if hint: - lines.append(f"[{hint}] {title}") + tag = account_block_tag(entry) + if tag: + lines.append(f"[{tag}] {title}") else: lines.append(title) lines.append(f"{_t(locale, 'strategy_label')}: {_display_strategy(entry)}") @@ -863,6 +888,7 @@ def filter_runs_for_digest( "EvidenceCoverage", "UNKNOWN_ACCOUNT_UID", "UNKNOWN_TARGET_ID", + "account_block_tag", "filter_runs_for_digest", "fills_are_verified_zero", "has_observation", diff --git a/python/tests/test_daily_digest_notify.py b/python/tests/test_daily_digest_notify.py index fe86772b..11b91d7b 100644 --- a/python/tests/test_daily_digest_notify.py +++ b/python/tests/test_daily_digest_notify.py @@ -148,6 +148,7 @@ def test_rich_heartbeat_matches_legacy_schwab_fields(self): tips=( "净值 $990 低于建议 $1,000;整数股和最小仓位限制可能导致实盘无法完全复现回测", ), + target_id="schwab-primary", ), ) text = daily_digest_notify.render_daily_digest( @@ -164,7 +165,7 @@ def test_rich_heartbeat_matches_legacy_schwab_fields(self): self.assertNotIn("Asia/Shanghai", text) self.assertNotIn("通道", text) self.assertNotIn("QuantSentinel", text) - self.assertIn("💓 【心跳检测】", text) + self.assertIn("[schwab-primary] 💓 【心跳检测】", text) self.assertIn("🧭 策略: 半导体趋势收益", text) self.assertIn("💰 账户总权益: USD 990.06", text) self.assertIn("💼 持仓", text) @@ -217,6 +218,7 @@ def test_no_order_reason_renders(self): platform_id="ibkr", strategy_profile="tqqq_growth_income", strategy_label="纳斯达克增长收益", + account_hint="U16608560", equity=572.93, holdings=( daily_digest_notify.DigestHolding( @@ -232,7 +234,7 @@ def test_no_order_reason_renders(self): business_day="2026-10-08", locale="zh", runs=runs ) ) - self.assertIn("⚠️ 【未下单】", text) + self.assertIn("[U16608560] ⚠️ 【未下单】", text) self.assertIn("低于最小订单金额:TQQQ", text) def test_english_observation_labels(self): @@ -249,6 +251,7 @@ def test_english_observation_labels(self): ), signal_summary="SOXX above 140d threshold", rebalance_kind="no_rebalance", + target_id="schwab-primary", ), ) text = daily_digest_notify.render_daily_digest( @@ -257,6 +260,7 @@ def test_english_observation_labels(self): ) ) self.assertIn("Heartbeat", text) + self.assertIn("[schwab-primary] 💓 [Heartbeat]", text) self.assertIn("Account equity: USD 990.06", text) self.assertIn("4sh", text) self.assertIn("No rebalance needed", text) @@ -373,6 +377,7 @@ def test_omits_missing_observation_fields(self): strategy_profile="soxl_soxx_trend_income", strategy_label="半导体趋势收益", rebalance_kind="no_rebalance", + target_id="schwab-primary", ), ) text = daily_digest_notify.render_daily_digest( @@ -380,6 +385,7 @@ def test_omits_missing_observation_fields(self): business_day="2026-10-08", locale="zh", runs=runs ) ) + self.assertIn("[schwab-primary] 💓 【心跳检测】", text) self.assertIn("半导体趋势收益", text) self.assertIn("✅ 无需调仓", text) self.assertNotIn("账户总权益", text) @@ -396,6 +402,10 @@ def test_single_schwab_omits_window_and_channel(self): fill_count_status="unknown", equity=989.34, rebalance_kind="no_rebalance", + opaque_account_uid=( + "bf2e669106f029a41e6f36dc18f704906ad6078d1f16a171ec0001e992d39b7d" + ), + target_id="schwab-primary", ), ) text = daily_digest_notify.render_daily_digest( @@ -409,7 +419,7 @@ def test_single_schwab_omits_window_and_channel(self): expected = ( "📡 量化哨兵 · 心跳\n" "业务日: 2026-10-09\n" - "💓 【心跳检测】\n" + "[schwab-primary] 💓 【心跳检测】\n" "🧭 策略: 半导体趋势收益\n" "💰 账户总权益: USD 989.34\n" "✅ 无需调仓\n" @@ -427,6 +437,7 @@ def test_multi_platform_observation_blocks_separated(self): fill_count_status="unknown", equity=989.34, rebalance_kind="no_rebalance", + target_id="schwab-primary", ), daily_digest_notify.DigestRunEntry( platform_id="ibkr", @@ -462,17 +473,18 @@ def test_multi_platform_observation_blocks_separated(self): self.assertIn("🧭 策略: 纳斯达克增长收益", text) self.assertIn("💰 账户总权益: USD 989.34", text) self.assertIn("💰 账户总权益: USD 569.16", text) + self.assertIn("[schwab-primary] 💓 【心跳检测】", text) self.assertIn("[U16608560] 💓 【心跳检测】", text) # Blank line between observation blocks. schwab_idx = text.index("平台: schwab") ibkr_idx = text.index("[U16608560]") between = text[schwab_idx:ibkr_idx] self.assertIn("\n\n", between) - # Exact multi-platform shape for zh locale. + # Exact multi-platform shape for zh locale — every block has [account] tag. expected = ( "📡 量化哨兵 · 心跳\n" "业务日: 2026-10-09\n" - "💓 【心跳检测】\n" + "[schwab-primary] 💓 【心跳检测】\n" "🧭 策略: 半导体趋势收益\n" "💰 账户总权益: USD 989.34\n" "✅ 无需调仓\n" @@ -488,6 +500,41 @@ def test_multi_platform_observation_blocks_separated(self): ) self.assertEqual(text, expected) + def test_account_block_tag_preference_order(self): + hint_wins = daily_digest_notify.DigestRunEntry( + platform_id="ibkr", + strategy_profile="tqqq_growth_income", + account_hint="U16608560", + opaque_account_uid="aaaaaaaaaaaaaaaa", + target_id="ibkr-primary", + ) + self.assertEqual( + daily_digest_notify.account_block_tag(hint_wins), "U16608560" + ) + target_wins = daily_digest_notify.DigestRunEntry( + platform_id="schwab", + strategy_profile="soxl_soxx_trend_income", + opaque_account_uid=( + "bf2e669106f029a41e6f36dc18f704906ad6078d1f16a171ec0001e992d39b7d" + ), + target_id="schwab-primary", + ) + self.assertEqual( + daily_digest_notify.account_block_tag(target_wins), "schwab-primary" + ) + short_uid = daily_digest_notify.DigestRunEntry( + platform_id="schwab", + strategy_profile="soxl_soxx_trend_income", + opaque_account_uid=( + "bf2e669106f029a41e6f36dc18f704906ad6078d1f16a171ec0001e992d39b7d" + ), + ) + self.assertEqual(daily_digest_notify.account_block_tag(short_uid), "bf2e6691") + empty = daily_digest_notify.DigestRunEntry( + platform_id="schwab", strategy_profile="soxl_soxx_trend_income" + ) + self.assertEqual(daily_digest_notify.account_block_tag(empty), "") + def test_identity_key_includes_account_and_target(self): a = daily_digest_notify.identity_key(