Explorar el Código

fix: improve booking notifications and bound proxy traffic

hujiarui hace 3 semanas
padre
commit
9a616a8ff4

+ 1 - 0
.dockerignore

@@ -25,6 +25,7 @@ env/
 secrets.json
 **/secrets.json
 data/booking_outbox.sqlite3*
+data/delivery.sqlite3*
 
 # —— 运行时日志、会话与临时数据 ——
 logs/

+ 1 - 0
.gitignore

@@ -32,6 +32,7 @@ env/
 *.local
 secrets.json
 data/booking_outbox.sqlite3*
+data/delivery.sqlite3*
 config/ita_query_account.local.json
 config/test_conf.local.json
 

+ 13 - 5
booker.py

@@ -1,4 +1,5 @@
 import os
+from copy import deepcopy
 from utils.resource_lifecycle import init_lifecycle, start_once, serialized_stop, join_threads
 import time
 import json
@@ -90,6 +91,8 @@ class BaseBookerGCO:
         class_name = "".join(part.title() for part in plugin_name.split('_'))
         plugin_path = os.path.join(self.m_cfg.plugin_config.lib_path, self.m_cfg.plugin_config.plugin_bin)
         self.m_factory.register_plugin(plugin_name, plugin_path, class_name)
+        if plugin_name:
+            self.m_factory.ensure_booking_supported(plugin_name)
 
         for target in (self._booking_trigger_loop, self._creator_loop,
                        self._maintain_loop, self._heartbeat_loop, self._success_sync_loop):
@@ -490,6 +493,10 @@ class BaseBookerGCO:
                 result.apt_type = AppointmentType.model_validate(data['apt_type'])
                 if result.apt_type.routing_key != apt_type.routing_key:
                     raise ValueError('signal routing key mismatch')
+                if result.availability_status not in (AvailabilityStatus.Available, AvailabilityStatus.Waitlist):
+                    continue
+                if data.get('expires_at', float('inf')) <= time.time():
+                    continue
             except Exception as exc:
                 # A producer may have replaced this value since MGET. Do not
                 # delete a newer signal based on an old malformed payload.
@@ -524,6 +531,7 @@ class BaseBookerGCO:
     def _push_success_notification(self, task_id, order_id, book_res):
         """Shared helper to sync booking success state to cloud and dispatch notifications."""
         grab_info = {
+            "order_id": str(order_id) if order_id else None,
             "account": book_res.account,
             "session_id": book_res.session_id,
             "urn": book_res.urn,
@@ -539,12 +547,12 @@ class BaseBookerGCO:
             self._pending_results[str(task_id)] = payload
         self._sync_successes()
         try:
-            notify_book_result(book_res, order_id=str(order_id) if order_id else None, logger=self._log)
+            notify_book_result(book_res, order_id=str(order_id) if order_id else None,
+                               logger=self._log, event_id='booking:order:' + str(task_id))
         except Exception as exc:
             self._log(f"Booking succeeded, but notification could not be queued: {exc}")
 
 
-
 class BuiltinBookerGCO(BaseBookerGCO):
     """
     Unbound mode (shared built-in account pool):
@@ -655,7 +663,7 @@ class BuiltinBookerGCO(BaseBookerGCO):
                     
         try:
             user_input = task_data.get('user_inputs', {})
-            book_res = task.instance.book(query_result, user_input)
+            book_res = task.instance.book(query_result, deepcopy(user_input))
             
             if book_res.success:
                 self._log(f"✅ BOOK SUCCESS! Order: {order_id}")
@@ -674,7 +682,7 @@ class BuiltinBookerGCO(BaseBookerGCO):
             with self.m_lock:
                 if str(task_id) not in self._pending_results:
                     self._leased_orders.discard(str(task_id))
-            if book_res is None or not book_res.success:
+            if (book_res is None or not book_res.success):
                 self.redis_client.zadd(self.m_tracker_key, {str(task_id): time.time()})
 
 
@@ -768,7 +776,7 @@ class OrderBookerGCO(BaseBookerGCO):
             
             order_id = task_data.get('order_id')
             user_input = task_data.get('user_inputs', {})
-            book_res = task.instance.book(query_result, user_input)
+            book_res = task.instance.book(query_result, deepcopy(user_input))
 
             if book_res.success:
                 self._log(f"✅ BOOK SUCCESS! Order: {order_id}. Destroying instance.")

+ 3 - 2
core_types.py

@@ -179,7 +179,7 @@ class SentinelConfig(BaseModel):
     account_pool_id: str = ""
     target_instances: int = 1
     account_cd: int = 180*60        # unit: seconds
-    signal_ttl: int = 180           # unit: seconds
+    signal_ttl: int = Field(default=180, ge=1)  # seconds
     query_wait: QueryWaitConfig = Field(default_factory=QueryWaitConfig)
 
 # === Booker Config ===
@@ -191,7 +191,7 @@ class BookerConfig(BaseModel):
     account_cd: int = 180*60         # Account cooldown in seconds (used in built-in mode only)
     target_instances: int = 1        # Global limit in built-in mode; per-queue limit in order mode
     keep_alive: int = 60             # Keepalive interval in seconds
-    booking_cooldown: float = 10.0   # unit: seconds
+    booking_cooldown: float = Field(default=10.0, ge=1)  # seconds
     max_bookings_per_account: int = 1
 
 class GroupConfig(BaseModel):
@@ -291,6 +291,7 @@ class QueryResult(BaseModel):
             return data
         
         return {
+            "success": self.success,
             "routing_key": self.apt_type.routing_key,
             "country": self.apt_type.country,
             "city": self.apt_type.city,

+ 3 - 3
docker-compose.yml

@@ -19,9 +19,9 @@ services:
       - ALLOW_LAN=true
     ports:
       # 全局混杂端口 (Mixed-port)
-      - "16000:16000"
+      - "127.0.0.1:16000:16000"
       # 多节点独立端口池 (覆盖 proxies.yaml 中的所有独立出口端口)
-      - "16001-16350:16001-16350"
+      - "127.0.0.1:16001-16350:16001-16350"
     healthcheck:
       test: ["CMD", "python3", "start_proxy.py", "--check-health"]
       interval: 10s
@@ -138,4 +138,4 @@ services:
       resources:
         limits:
           cpus: '0.2'
-          memory: 256M
+          memory: 256M

+ 48 - 0
docs/audits/2026-10-25/reliability-fixes.md

@@ -0,0 +1,48 @@
+# 可靠性修复及运行说明
+
+## 当前存储方式
+
+新增的通用本地存储模块及依赖它的人工核对脚本已移除。当前只保留原有 `BookingOutbox` 用于订单结果同步,不新增通知、slot 或预约保护的 SQLite 数据库。
+
+- 通知待发送队列和日期/时段去重状态只放在进程内存中。slot 信号不再使用待发送缓存,查询完成后直接写入 Redis。
+- 通知队列独立于遥测执行器,最多 256 项,失败后退避 5~300 秒重试;满载返回失败并记录日志,不提前更新 slot 去重状态。
+- 进程退出会丢失这些内存队列和去重状态;不保证重启后的通知补发。已清空的订单 outbox 也不能恢复尚未发送的内存通知。
+- Standalone 只在当前进程记住成功状态,并停止当前任务的重复预订;重启会忘记完成状态。
+- 订单模式保留原有订单 outbox 及成功结果的 Redis 同步标记。最终提交前的 Redis 占位、提交边界标记和结果未知自动暂停策略已移除。
+- 原有数据库 `data/booking_outbox.sqlite3` 继续通过 `BOOKING_OUTBOX_PATH` 配置并持久保存。
+- 历史存储文件未自动销毁,但运行代码不再读取或写入它们;Git/Docker 中保留历史文件的忽略项,防止误提交或打入镜像。
+
+## 保留的修复
+
+| 问题 | 当前行为 |
+|---|---|
+| 代理流量被他人消耗 | Compose 只发布宿主机 127.0.0.1;本机启动默认将主监听及所有独立监听绑定回环地址。原始配置不改写,运行配置位于 logs/mihomo.runtime.yaml。容器内部网络仍可访问。 |
+| VFS 扫描数百个月 | 单次最多扫描有效起始月份后的 6 个月;即使结束日期为 2100 年也不能绕过。 |
+| 提交调用与异常 | Booker/Standalone 直接调用插件 book 方法。提交边界与结果未知的专用处理已移除,异常交由现有错误处理和重试流程;GRC 仍不自动重放 POST。 |
+| Standalone 成功后继续预订 | 在当前进程记录完成状态,释放浏览器并停止该任务;此状态不跨重启保存。 |
+| 通知满载或发送失败 | 独立内存队列,检查容量,进程存活期间重试。无法承诺跨重启恢复。 |
+| slot 恢复、Waitlist 漏通知 | 比较状态、日期及时段;同日恢复、时段变化、Waitlist 都可通知。队列拒绝时不更新去重缓存。 |
+| Redis 信号发送 | 查询完成后直接 setex 写入 Redis,无后台补发队列。SafeRedisClient 原有的单次失败重试保留;无号结果仍发布不可用状态,消费者仍忽略过期/不可用信号。 |
+| USA 点击提交即错报成功 | 等待确认路径及匹配的预约日期。无权威确认则抛出普通业务异常,不冒报成功。实际站点页面仍需部署验收。 |
+
+新增的 slot 快照上报、信号重试后台循环及待发送缓存已移除;Sentinel 只启动原有的检测和实例创建线程,不再额外上报云端快照。BEL/POL/PRT 的空预订实现仍在启动阶段报错,避免领取账号/订单后空转。检测间隔至少 1 秒,booking_cooldown 和 signal_ttl 必须为正数。
+
+## 配置和结果核对
+
+本次没有启用真实下单组或更改云端订单。消费者需要启用、匹配 routing_key,并配置正确的账号/订单来源。无启用组时启动日志会明确提示不会触发预订;Italy 的最终提交开关保持原配置。
+
+最终提交的结果跟踪辅助模块已删除,不再创建或消费专门的结果未知 Redis 标记。最终提交响应超时会走现有失败处理流程;成功记录仍由 BookingOutbox 同步。
+
+Standalone 的 booking_run_id 可区分当前进程中明确授权的新任务;单纯修改日期不会跳过进程内的完成保护,重启则不保留该保护。
+
+改动需部署重启后生效。跨主机访问代理仍需显式配置受控入口;同一 Compose 内访问 proxy 服务名不受宿主机回环发布的影响。真实官网预约、云端接口及微信最终送达尚未执行端到端验收。
+
+## 验证
+
+```powershell
+.\venv\Scripts\python.exe -X utf8 -m unittest discover -s test -p 'test_*.py'
+.\venv\Scripts\python.exe -X utf8 docs/audits/2026-10-25/reproduce.py
+docker compose config --quiet
+```
+
+回归测试已移除新增本地存储的持久化场景,覆盖保留的流量限制、内存通知重试与拒绝、slot 状态变更、直接信号发布、订单 outbox 和进程内 Standalone 完成保护。

+ 109 - 0
docs/audits/2026-10-25/report.md

@@ -0,0 +1,109 @@
+# 流量、预订结果及通知可靠性审查
+
+> 本文为修复前的历史审查结论。后续修复及运行说明见 [reliability-fixes.md](reliability-fixes.md);reproduce.py 现已运行修复后的回归测试。
+
+审查日期:2026-10-25(Asia/Shanghai)。范围:本地 coordinate 代码、Compose 和配置文件。
+
+结论:存在能造成流量放大、成功状态遗漏、通知丢失、slot 信号丢失及错误成功上报的路径。以下区分代码缺陷和部署条件;没有证据证明已经发生实际扣费、重复预约或漏单。
+
+本次仅新增报告和离线复现脚本,没有修改生产代码、配置或启用服务。没有调用真实预约、通知、代理或云端接口;复现脚本阻止 socket.connect。
+
+## 发现及修复方向
+
+### 1. [P1] 代理端口对宿主机外部发布,配置无认证
+
+- 位置:`docker-compose.yml:19-24`、`config/mihomo_config.yaml:1-2`。
+- Compose 发布 `16000` 和 `16001-16350`,未指定宿主机绑定地址;配置 `allow-lan: true`,未设置全局 authentication;306 个 listener 均未设置 users 或显式 listen 地址。
+- 如果部署主机的网络和防火墙允许其他人接入,这些端口可成为消耗付费出口流量的入口。仅凭代码不能判断公网可达性,本次未做网络扫描。
+- 建议:业务容器优先通过内部网络连接;宿主机需要访问时绑定回环或明确的内网地址,跨主机连接配置认证和来源限制。
+- 验证:静态检查 Compose、解析实际 YAML;未验证外部可达性。
+
+### 2. [P1] VFS 默认结束日期为 2100 年,单次预订可能查询数百个月
+
+- 位置:`plugins/vfs_plugin.py:991-992,1064-1077,1550-1576`。
+- 当 user_inputs 未提供 expected_end_date,默认到 `2100-01-01`。`_get_filtered_covered_months` 生成整个范围;每个月都调用 `_query_slot_calendar`,直到找到可用时间或结束。
+- 离线输入 earliest=`2026-10-25`、未限制结束日期,对应 **880 个月**。若日历持续返回空数据而不是错误,单次 book 可产生 880 次日历请求,另加配置、申请人和日期时间查询;遇到限流异常会提前退出,并非每次必定达到上限。
+- 旧信号重复触发及多个实例会进一步放大请求量。
+- 建议:缺省限制为合理预约窗口,增加单次最大查询月数、请求总量和耗时上限;无有效窗口应拒绝预订或要求明确配置。
+- 复现:`test_vfs_default_range_scans_hundreds_of_months`。
+
+### 3. [P1] 最终提交响应不确定时,会被当作可重试失败;GRC 还自动重放 POST
+
+- 位置:`booker.py:658-678`、`main_sweeper.py:_rescue_task`、`plugins/vfs_plugin.py:1125,1453-1475`、`plugins/grc_plugin.py:627-642`。
+- 网站已接受最终预订、但客户端读响应超时,插件无法返回 BookResult(success=True),因此不会写成功 outbox。Builtin Booker 将 tracker 的 deadline 直接设为当前时间,Sweeper 见云端仍为 processing 时会归还队列;没有针对网站预约状态的确认步骤。Order/Standalone 也没有明确的“提交结果不确定,暂停重试”状态。
+- GRC 的通用请求方法对所有方法捕获异常后重试,包含最终提交 POST,最多发送三次相同请求。服务端是否幂等未知,客户端没有幂等键或查单保护,存在重复提交风险,不能直接断言网站一定产生多个预约。
+- 建议:提交前持久化意图;区分“明确拒绝”和“结果未知”。未知状态先查预约记录或进入人工核对,禁止自动回队列;最终提交不套用通用 POST 重试。
+- 复现:`test_uncertain_builtin_booking_becomes_immediately_rescuable`、`test_grc_replays_mutating_post_after_uncertain_response`。模拟服务端已提交而响应丢失,没有执行真实预订。
+
+### 4. [P1] Standalone 成功结果没有持久化,而且成功后继续循环预订
+
+- 位置:`standalone.py:411-432`。
+- book 返回成功后只调用异步通知和日志,等待 5 秒,随后重新安排下一次查询。没有写 BookingOutbox、本地成功记录或持久化完成标志,也不停止该账号的预订。
+- 通知失败或进程退出可能只留下日志;相同 slot 再次被查到时仍调用 book。网站若拒绝重复预约,只会产生无效请求;若允许,存在重复预约风险。
+- 建议:Standalone 也保存成功结果和通知事件,按任务/账号记录完成状态;完成后停止或明确消费下一项授权任务,支持重启恢复。
+- 复现:`test_standalone_continues_booking_after_success`,连续两次 book 均成功,第一次成功后仍触发第二次调用。
+
+### 5. [P1] 通知队列拒绝或接口失败后,没有可靠补发
+
+- 位置:`toolkit/notifier.py:25,37-43,74-81`、`toolkit/task_executor.py:TaskExecutor.submit`、`booker.py:536-544`、`utils/booking_outbox.py:flush`。
+- TaskExecutor 满载会返回 False,两个 notify 函数都忽略返回值并返回 True。通知与代理探测、信誉同步、指标上报共用后台执行器,非通知任务也会占用容量。
+- 微信接口失败只记日志,没有持久化重试。后台线程是 daemon,进程退出时未发送完的事件也没有恢复机制。
+- Booker 的成功 outbox 能重试云端订单更新,但没有通知待发/已发送字段;重启后的 flush 不会重放成功通知。因此“订单最终记录成功”并不保证“通知最终送达”。
+- 建议:持久化通知事件、接收对象和幂等标识,独立重试并保存送达状态;至少应显式检查队列拒绝,不能把排队失败当作成功。
+- 复现:`test_full_queue_silently_drops_both_notification_types`。
+
+### 6. [P2] slot 通知去重提前生效,恢复同日档期和 Waitlist 会漏通知
+
+- 位置:`toolkit/notifier.py:16-25,37-43`、`plugins/vfs_plugin.py:597-603`。
+- 入队前就缓存 earliest_date。第一次发送失败后,相同日期一直被抑制;这是第五项故障在 slot 通知上的持续影响。
+- 无 slot 时直接 return,不清除“已通知日期”。同一日期从有号 → 无号 → 恢复,后一次不会再通知;相同最早日期下新增时间或其他日期也不触发通知。
+- VFS Waitlist 的 success=True 但没有 earliest_date,notify_date_changes 会直接忽略。这是实际可预订状态与“仅通知日期变化”的覆盖差异。
+- 建议:比较可用状态和 slot 集合,区分事件去重与送达去重;恢复可用时形成新事件;Waitlist 使用独立通知内容。
+- 复现:`test_push_failure_is_not_retried_for_same_date`、`test_closed_then_reopened_same_date_is_not_notified`、`test_waitlist_without_date_is_not_notified`。
+
+### 7. [P1] Redis 信号发布失败被忽略;无号结果也不会撤销旧信号
+
+- 位置:`sentinel.py:340-356`、`utils/safe_redis_cli.py:setex/_safe_execute`、`booker.py:473-516`。
+- SafeRedisClient 在两次写失败后返回 False,Sentinel 不检查返回值,正常安排下一次查询。可能出现“已检测/已排通知,但 Booker 完全没收到信号”;下一次查询前没有保存并重试这次 slot。
+- 一次有号信号发布后,后续无号结果既不覆盖也不删除旧值。Booker 按 cooldown 重复读取旧信号,没有按 slot 事件去重或检查外层 timestamp;旧值会持续到 TTL 到期。失败后持续请求已消失的档期,尤其会放大 VFS 扫月开销。
+- 当前本地 Sentinel TTL 为 30 秒;代码默认 TTL 为 180 秒。真实运行值取决于云端配置。TTL 内重试次数还受 book 耗时、cooldown 和实例数影响。
+- 建议:保存有版本和有效期的信号,发布失败重试并告警;发布明确的无号状态,按新鲜度校验后消费;多 Sentinel 时不能直接无条件删除其他实例刚发布的新信号。
+- 复现:`test_failed_signal_publish_is_treated_as_completed_query`、`test_no_slot_does_not_retract_previous_signal`。
+
+### 8. [P1] USA 插件点击提交即返回成功,没有网站确认
+
+- 位置:`plugins/usa_plugin.py:698-712`。
+- 调用鼠标点击后直接 success=True,没有验证响应、确认页或预约编号。页面校验不通过、网站拒绝、提交还未完成时,也可能上报 grabbed,通知用户并退役实例,造成漏订和错误记录。
+- 建议:点击后等待并验证网站的权威确认结果;拒绝返回失败,超时进入结果不确定状态;保存确认凭据后再完成订单。
+- 复现:`test_usa_reports_success_without_server_confirmation`。
+
+## 另外核对到的配置与功能边界
+
+1. **本地配置没有启用预订消费者。** `config_booker.yaml` 三组、`config_standalone.yaml` 五组均 enable=0;Sentinel 启用的是 vfs.ie.at、vfs.ie.nl、vfs.ie.hu。使用这些本地配置时不会自动预订;即使启用现有 Booker,也没有覆盖 AT/HU 的路由配置。**Compose 启动命令未传 --config,main.py:285-287 默认使用云端配置**,因此这不是对当前线上运行状态的结论,需核对云端实际下发的组与路由。
+2. **部分插件尚未实现预订。** `plugins/bel_plugin.py:270-272`、`plugins/pol_plugin.py:369-372` 的 book 直接返回默认失败;`plugins/prt_plugin.py:40-44` 的 query/book 均为空实现。启用这些插件不会完成真实预订。
+3. **Italy 最终提交有显式开关。** `plugins/ita_plugin.py:1987-1991` 仅在 final_submit_enabled is True 时提交;本地 Italy 组禁用,且该开关为 False。这属于配置行为,不当作默认自动修复项。
+4. **已有保护并非完全缺失。** Booker 有 SQLite 成功 outbox、Redis 成功 marker、独立订单心跳及并发控制;现有测试验证了部分云端/Redis故障恢复。上述第三、四、五项分别发生在成功结果返回前、Standalone 分支、通知交付阶段,绕过或超出了这些保护。
+5. **没有调用 slot_snapshot_report。** 仓库仅找到 CloudApi 中的方法定义,检测主链路目前不调用。如果还要求云端/UI 保存每次 slot 快照,需要补齐生产端及确认服务端消费约定;本次未检查服务端,不能据此断言其 UI 一定不更新。
+
+## 验证及复现
+
+现有测试:69 项,68 通过,1 项 POSIX 信号测试在 Windows 跳过。执行命令:
+
+```powershell
+.\venv\Scripts\python.exe -X utf8 -m unittest discover -s test -p 'test_*.py'
+```
+
+新增离线审查复现:11 项全部复现当前缺陷。**这里的 OK 表示已观察到报告中的问题,不表示缺陷已修复。**
+
+```powershell
+.\venv\Scripts\python.exe -X utf8 docs/audits/2026-10-25/reproduce.py
+```
+
+## 建议顺序
+
+1. 核对代理端口的实际可达性并限制入口;限制 VFS 扫描范围。
+2. 引入最终提交的结果不确定状态和查单流程,移除最终提交的盲目 POST 重试;修正 USA 成功判定。
+3. Standalone 成功持久化并停止重复预订;为通知添加独立持久化补发机制。
+4. 修复通知去重、信号发布确认与失效语义;核对云端启用的消费者及路由覆盖。
+
+本次未测量实际出入站字节量、代理计费、真实预约接口或微信最终送达;11 项复现证明客户端控制流缺陷,不能替代部署和第三方接口的端到端验收。

+ 8 - 0
docs/audits/2026-10-25/reproduce.py

@@ -0,0 +1,8 @@
+"""Run the repaired failure scenarios as offline regression tests."""
+import sys
+import unittest
+from pathlib import Path
+sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
+from test.test_booking_reliability import BookingReliabilityTests
+if __name__ == '__main__':
+    unittest.main(verbosity=2)

+ 4 - 0
main.py

@@ -16,6 +16,7 @@ from configure import REDIS_CFG
 from sentinel import SentinelGCO
 from booker import BuiltinBookerGCO, OrderBookerGCO
 from standalone import StandaloneGCO
+from toolkit.notifier import start_notification_worker
 from utils.safe_redis_cli import SafeRedisClient
 from utils.cloud_config import (
     load_remote_or_cache,
@@ -219,6 +220,8 @@ def sync_groups(
 ):
     """Synchronizes GCOWrapper lifecycle (stop, restart, update, start) according to latest config."""
     new_groups = parse_group_configs(role, raw_conf)
+    if role in ('booker', 'standalone') and not any(cfg.enable for cfg in new_groups.values()):
+        logger.warning('No enabled booking groups: slot detection will not trigger bookings on this role.')
 
     # 1. Stop removed or disabled groups
     for gid in list(wrappers.keys()):
@@ -285,6 +288,7 @@ def main():
     args = parser.parse_args()
     role = args.role
     use_cloud = args.config is None
+    start_notification_worker()
     config_path = args.config or f"config/config_{role}.yaml"
     node_id = getattr(configure, "NODE_ID", None) or os.getenv("NODE_ID", "node01")
     status_key = f"vs:status:{role}:{node_id}"

+ 4 - 3
main_sweeper.py

@@ -30,6 +30,8 @@ class OrphanTaskSweeper:
             self._running = False
 
     def run_forever(self, interval: float = 3.0):
+        from toolkit.notifier import start_notification_worker
+        start_notification_worker()
         self.logger.info("🛡️ Task Sweeper started. Monitoring dead tasks (Heartbeat timeout)...")
         while self._running:
             try:
@@ -88,9 +90,8 @@ class OrphanTaskSweeper:
             success = self.redis_client.get(self._outbox.marker(task_id))
             if success:
                 import json
-                CloudApi.Instance().update_vas_task(str(task_id), json.loads(success))
-                self.redis_client.zrem(tracker_key, task_id)
-                self.redis_client.delete(self._outbox.marker(task_id))
+                self._outbox.put(task_id, tracker_key, json.loads(success))
+                self._outbox.flush(CloudApi.Instance(), self.redis_client, self.logger.warning)
                 return
             task_data = None
             try:

+ 2 - 1
plugin.py

@@ -13,6 +13,7 @@ class IPlugin(ABC):
     name: str = ""
     version: str = "1.0.0"
     description: str = ""
+    booking_supported: bool = True
 
     @classmethod
     def get_plugin_info(cls) -> Dict[str, str]:
@@ -92,4 +93,4 @@ class IPlugin(ABC):
         @brief Gets snapshot of plugin instance runtime status.
         @return PluginStatus
         """
-        return PluginStatus()
+        return PluginStatus()

+ 13 - 1
plugin_factory.py

@@ -60,4 +60,16 @@ class PluginFactory:
 
         except Exception as e:
             VSC_ERROR("factory", "Failed to create plugin instance '%s': %s", plugin_name, str(e))
-            raise
+            raise
+
+    def ensure_booking_supported(self, plugin_name):
+        """Reject incomplete adapters before popping accounts/orders or opening a browser."""
+        path, class_name = self._registered_plugins[plugin_name]
+        spec = importlib.util.spec_from_file_location(plugin_name, path)
+        if spec is None or spec.loader is None:
+            raise ImportError(f'Cannot load booking adapter {plugin_name}')
+        module = importlib.util.module_from_spec(spec)
+        sys.modules[plugin_name] = module
+        spec.loader.exec_module(module)
+        if getattr(getattr(module, class_name), 'booking_supported', True) is False:
+            raise NotImplementedError(f'{plugin_name} does not implement booking; use sentinel only')

+ 2 - 1
plugins/bel_plugin.py

@@ -36,6 +36,7 @@ from utils.plugin_utils import (
 
 
 class BelPlugin(IPlugin):
+    booking_supported = False
     """
     Belgium (https://visaonweb.diplomatie.be/en) 签证预约插件 (Browser + Tunnel Mode)
     """
@@ -319,4 +320,4 @@ class BelPlugin(IPlugin):
         """
         析构函数:当对象被垃圾回收时自动调用
         """
-        self.cleanup()
+        self.cleanup()

+ 1 - 1
plugins/bls_plugin.py

@@ -1113,4 +1113,4 @@ class BlsPlugin(IPlugin):
         """
         析构函数:当对象被垃圾回收时自动调用
         """
-        self.cleanup()
+        self.cleanup()

+ 5 - 2
plugins/de_plugin.py

@@ -421,6 +421,7 @@ class DePlugin(IPlugin):
 
     def book(self, slot_info: QueryResult, user_inputs: Dict = None) -> BookResult:
         res = BookResult()
+        res.account = self.config.account.username
         user_inputs = user_inputs or {}
         
         # 1. 动态检测日历模式
@@ -491,6 +492,8 @@ class DePlugin(IPlugin):
             raise BizLogicError("Incomplete fields response")
             
         match = re.search(r'https:\/\/checkout\.stripe\.com\/c\/pay\/[^\s"]+', book_res_html)
+        if not match:
+            raise BizLogicError('Germany final response has no payment/confirmation receipt')
         
         res.success = True
         res.book_date = target_date
@@ -520,7 +523,7 @@ class DePlugin(IPlugin):
         if resp.status_code == 200:
             return resp
         elif resp.status_code == 403:
-            if "Just a moment" in resp.text and retry_count < 2:
+            if method.upper() in ('GET', 'HEAD') and "Just a moment" in resp.text and retry_count < 2:
                 self._log("Cloudflare 403. Refreshing...")
                 if self._refresh_firewall_session():
                     return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
@@ -778,4 +781,4 @@ class DePlugin(IPlugin):
         """
         析构函数:当对象被垃圾回收时自动调用
         """
-        self.cleanup()
+        self.cleanup()

+ 6 - 7
plugins/grc_plugin.py

@@ -427,6 +427,7 @@ class GrcPlugin(IPlugin):
             
         res = BookResult()
         res.success = False
+        res.account = self.config.account.username
 
         # 重新登录一次,避免session expired
         self.create_session()
@@ -564,14 +565,12 @@ class GrcPlugin(IPlugin):
                         err_reason = err_match.group(1) if err_match else "Form validation error"
                         self._log(f"Submission failed for {start_time_str} ({err_reason}). Trying next...")
                     else:
-                        self._log(f"Submission failed for {start_time_str}: Unknown error. Trying next...")
-                        if self.config.debug:
-                            self._save_debug_html(resp_book_stp_2.text, prefix='book_step2_fail')
+                        raise BizLogicError('GRC final response has no known success or rejection')
                     continue
 
             except Exception as e:
-                self._log(f"Exception trying slot {start_time_str}: {e}")
-                continue
+                self._log(f'Booking attempt failed: {e}')
+                raise
 
         self._log("All candidate slots failed.")
         return res
@@ -628,7 +627,7 @@ class GrcPlugin(IPlugin):
         try:
             resp = self.session.request(method, url, headers=headers, data=data, json=json_data, params=params, timeout=30)
         except Exception as e:
-            if retry_count < 2:
+            if method.upper() in ('GET', 'HEAD', 'OPTIONS') and retry_count < 2:
                 self._log(f"⚠️ [Network/TLS Error] {str(e)[:120]}. Reconnecting transport socket (Try {retry_count+1}/2)...")
                 time.sleep(1.0)
                 old_cookies = None
@@ -682,4 +681,4 @@ class GrcPlugin(IPlugin):
         )
 
     def __del__(self):
-        self.cleanup()
+        self.cleanup()

+ 2 - 1
plugins/pol_plugin.py

@@ -32,6 +32,7 @@ from utils.plugin_utils import filter_dates_in_range
 
 
 class PolPlugin(IPlugin):
+    booking_supported = False
     """
     Poland (e-konsulat) 签证预约插件 (Pure HTTP REST API Mode)
     基于 official e-konsulat REST API (https://api.e-konsulat.gov.pl) 与大模型视觉验证码识别
@@ -399,4 +400,4 @@ class PolPlugin(IPlugin):
         )
 
     def __del__(self) -> None:
-        self.cleanup()
+        self.cleanup()

+ 1 - 0
plugins/prt_plugin.py

@@ -16,6 +16,7 @@ from core_types import (
 )
 
 class PrtPlugin(IPlugin):
+    booking_supported = False
     """
     葡萄牙 (E-Visa / MAE) 签证预约插件
     """

+ 2 - 1
plugins/tls_plugin.py

@@ -579,6 +579,7 @@ class TlsPlugin(IPlugin):
     def book(self, slot_info: QueryResult, user_inputs: Dict = None) -> BookResult:
         res = BookResult()
         res.success = False
+        res.account = self.config.account.username
         
         exp_start = user_inputs.get('expected_start_date', '')
         exp_end = user_inputs.get('expected_end_date', '')
@@ -917,4 +918,4 @@ class TlsPlugin(IPlugin):
         """
         析构函数:当对象被垃圾回收时自动调用
         """
-        self.cleanup()
+        self.cleanup()

+ 19 - 2
plugins/usa_plugin.py

@@ -701,7 +701,23 @@ class UsaPlugin(IPlugin):
         submit_appointment_selector = "#submitbtn"
         submit_button = self.page.ele(submit_appointment_selector, timeout=10)
         self.mouse.human_click_ele(submit_button)
-        self._log("Booking submitted successfully!")
+        # A click is not a receipt. Require the site's confirmation page and
+        # visible appointment details; a timeout must be reconciled.
+        confirmed = False
+        for _ in range(30):
+            current_url = str(self.page.url).lower()
+            if ('appointment-confirmation' in current_url
+                    or 'appointment_confirmation' in current_url):
+                details = self.page.ele('tag:body', timeout=0.5)
+                if details and selected_slot_date in str(details.text):
+                    confirmed = True
+                    break
+            error = self.page.ele('css:.validation-summary-errors', timeout=0.2)
+            if error and str(error.text).strip():
+                return res
+            time.sleep(0.5)
+        if not confirmed:
+            raise BizLogicError('USA submission has no verified appointment confirmation')
 
         # 构造返回结果
         res = BookResult()
@@ -709,6 +725,7 @@ class UsaPlugin(IPlugin):
         res.book_date = selected_slot_date
         res.book_time = selected_slot_time
         res.account = self.config.account.username
+        res.label = current_url
         return res
 
     def cleanup(self):
@@ -748,4 +765,4 @@ class UsaPlugin(IPlugin):
         """
         析构函数:当对象被垃圾回收时自动调用
         """
-        self.cleanup()
+        self.cleanup()

+ 17 - 8
plugins/vfs_plugin.py

@@ -656,7 +656,7 @@ class VfsPlugin(IPlugin):
             if "Just a moment" in resp.text or "cloudflare" in resp.text.lower():
                 self._log(f"[TRAFFIC] HTTP 403 (Cloudflare) detected. Re-verifying (Try {retry_count+1}/3)...")
                 
-                if retry_count < 3:
+                if retry_count < 3 and method.upper() in ('GET', 'HEAD'):
                     new_token = self._refresh_turnstile_token()
                     
                     if new_token:
@@ -989,8 +989,8 @@ class VfsPlugin(IPlugin):
         from_date = slot_info.earliest_date.strftime("%Y-%m-%d") if slot_info.earliest_date else datetime.now().strftime("%Y-%m-%d")
         
         expected_start = user_inputs.get("expected_start_date", "2000-01-01")
-        expected_end = user_inputs.get("expected_end_date", "2100-01-01")
-        if from_date > expected_end:
+        expected_end = user_inputs.get("expected_end_date") or ''
+        if expected_end and from_date > expected_end:
             self._log("No valid slots in date preference.") 
             res.success = False
             return res
@@ -1138,7 +1138,11 @@ class VfsPlugin(IPlugin):
         lowered_schedule = {str(k).lower(): v for k, v in schedule_res.items()} if isinstance(schedule_res, dict) else {}
         if lowered_schedule.get("ispaymentrequired", False):
             self._log("Processing payment link...")
-            payment_url = self._pay_request(schedule_res)
+            try:
+                payment_url = self._pay_request(schedule_res)
+            except Exception as exc:
+                self._log(f'Booking confirmed; payment URL recovery needed: {exc}')
+                payment_url = ''
             if payment_url:
                 res.payment_link = payment_url
                     
@@ -1546,13 +1550,14 @@ class VfsPlugin(IPlugin):
             "CanVFSReachoutToApplicant": True
         }
         resp = self._perform_request("POST", url, headers=headers, json_data=data)
-        return resp.json().get("isConfirmed", False)
+        confirmation = resp.json().get("isConfirmed")
+        return confirmation is True
 
     def _get_filtered_covered_months(self, start_date, end_date, from_date) -> List[str]:
         fmt = "%Y-%m-%d"
         try:
             dt_start = datetime.strptime(start_date, fmt) if start_date else datetime.now()
-            dt_end = datetime.strptime(end_date, fmt) if end_date else datetime.now().replace(year=datetime.now().year + 1)
+            dt_end = datetime.strptime(end_date, fmt) if end_date else datetime.max
             try:
                 dt_from = datetime.strptime(from_date, fmt)
             except:
@@ -1566,8 +1571,12 @@ class VfsPlugin(IPlugin):
         curr = max(dt_start, dt_from)
         
         months = []
-        while curr <= dt_end:
+        # A hard request budget also applies when callers explicitly ask for
+        # a distant end date. Subsequent attempts recheck a bounded window.
+        while curr <= dt_end and len(months) < 6:
             months.append(curr.strftime(fmt))
+            if len(months) == 6 or (curr.year == 9999 and curr.month == 12):
+                break
             if curr.month == 12:
                 curr = curr.replace(year=curr.year + 1, month=1)
             else:
@@ -1611,4 +1620,4 @@ class VfsPlugin(IPlugin):
         """
         析构函数:当对象被垃圾回收时自动调用
         """
-        self.cleanup()
+        self.cleanup()

+ 14 - 10
sentinel.py

@@ -48,9 +48,9 @@ class SentinelGCO:
         if mode == QueryWaitMode.Loop:
             return 1.0
         elif mode == QueryWaitMode.Fixed:
-            return float(self.m_cfg.sentinel.query_wait.fixed_wait)
+            return max(1.0, float(self.m_cfg.sentinel.query_wait.fixed_wait))
         elif mode == QueryWaitMode.Random:
-            return (self.m_cfg.sentinel.query_wait.random_min + self.m_cfg.sentinel.query_wait.random_max) / 2.0
+            return max(1.0, (self.m_cfg.sentinel.query_wait.random_min + self.m_cfg.sentinel.query_wait.random_max) / 2.0)
         return 30.0
     
     def update_config(self, new_cfg: GroupConfig):
@@ -341,14 +341,18 @@ class SentinelGCO:
                 result.apt_type = apt_type
                 with self.m_lock:
                     notify_date_changes(result, cache=self.m_current_dates, logger=self._log)
-                if result.success:
-                    payload = {"group_id": self.m_cfg.identifier,
-                               "apt_type": apt_type.model_dump(),
-                               "query_result": result.to_snapshot_payload(),
-                               "timestamp": time.time()}
-                    self.redis_client.setex(self._get_redis_key(apt_type.routing_key),
-                                            self.m_cfg.sentinel.signal_ttl, json.dumps(payload))
-                task.next_run = time.time() + max(0.05, interval)
+                # Also publish no-slot transitions; never leave a stale positive
+                # signal until TTL after the site has already withdrawn it.
+                published_at = time.time()
+                payload = {"group_id": self.m_cfg.identifier,
+                           "apt_type": apt_type.model_dump(),
+                           "query_result": result.to_snapshot_payload(),
+                           "timestamp": published_at,
+                           "expires_at": published_at + self.m_cfg.sentinel.signal_ttl}
+                if not self.redis_client.setex(self._get_redis_key(apt_type.routing_key),
+                                               self.m_cfg.sentinel.signal_ttl, json.dumps(payload)):
+                    self._log(f'Slot signal publication failed: {apt_type.routing_key}')
+                task.next_run = time.time() + max(1.0, interval)
                 if task.instance.config.proxy.port > 0:
                     ProxyManager.Instance().mark_proxy_result(
                         proxy=task.instance.config.proxy.model_dump(),

+ 18 - 4
standalone.py

@@ -1,4 +1,5 @@
 import os
+from copy import deepcopy
 from utils.resource_lifecycle import init_lifecycle, start_once, serialized_stop, join_threads
 import time
 import random
@@ -56,6 +57,7 @@ class StandaloneGCO:
         self.m_current_dates = {}
         self._worker_thread = None
         self.redis_client = SafeRedisClient(redis_conf, self.m_logger)
+        self._booking_outcome = None
         init_lifecycle(self)
 
     def _log(self, message: str):
@@ -71,9 +73,9 @@ class StandaloneGCO:
         if mode == QueryWaitMode.Loop:
             return 1.0
         elif mode == QueryWaitMode.Fixed:
-            return float(self.m_cfg.query_wait.fixed_wait)
+            return max(1.0, float(self.m_cfg.query_wait.fixed_wait))
         elif mode == QueryWaitMode.Random:
-            return random.uniform(self.m_cfg.query_wait.random_min, self.m_cfg.query_wait.random_max)
+            return max(1.0, random.uniform(self.m_cfg.query_wait.random_min, self.m_cfg.query_wait.random_max))
         return 30.0
 
     def _is_within_active_hours(self) -> bool:
@@ -139,6 +141,8 @@ class StandaloneGCO:
         class_name = "".join(part.title() for part in plugin_name.split('_'))
         plugin_path = os.path.join(self.m_cfg.plugin_config.lib_path, self.m_cfg.plugin_config.plugin_bin)
         self.m_factory.register_plugin(plugin_name, plugin_path, class_name)
+        if plugin_name:
+            self.m_factory.ensure_booking_supported(plugin_name)
 
         worker_name = f"Standalone-{self.m_cfg.identifier}"
         self._worker_thread = threading.Thread(target=self._worker_loop, daemon=True, name=worker_name)
@@ -337,6 +341,15 @@ class StandaloneGCO:
 
         while not self.m_stop_event.is_set():
             try:
+                key = (self.m_cfg.identifier, self.m_cfg.account.username,
+                       self.m_cfg.user_preferences.get('booking_run_id', 'default'))
+                event_id = 'standalone:' + repr(key)
+                if self._booking_outcome and self._booking_outcome[0] == key:
+                    receipt = self._booking_outcome[1]
+                    notify_book_result(receipt, event_id='booking:' + event_id, logger=self._log)
+                    self._cleanup_instance('booking completed')
+                    self.m_stop_event.wait(5)
+                    continue
                 if not self.m_cfg.enable:
                     self.m_stop_event.wait(1.0)
                     continue
@@ -414,9 +427,10 @@ class StandaloneGCO:
                         if self.m_stop_event.is_set():
                             break
                         with self._resources.operation(self.m_instance):
-                            book_result = self.m_instance.book(query_result, self.m_cfg.user_preferences)
-                        notify_book_result(book_result, logger=self._log)
+                            book_result = self.m_instance.book(query_result, deepcopy(self.m_cfg.user_preferences))
+                        notify_book_result(book_result, logger=self._log, event_id='booking:' + event_id)
                         if book_result.success:
+                            self._booking_outcome = (key, book_result)
                             self._log(f"🎉 BOOKING SUCCESSFUL for {apt_type.routing_key}!")
                             self.m_stop_event.wait(5.0)
                     except Exception as e:

+ 19 - 12
start_proxy.py

@@ -271,6 +271,14 @@ def check_health(config_path):
         sys.exit(1)
 
 
+def secure_listener_config(config, external_access=False):
+    config['allow-lan'] = bool(external_access)
+    config['bind-address'] = '*' if external_access else '127.0.0.1'
+    for listener in config.get('listeners', []):
+        listener['listen'] = '0.0.0.0' if external_access else '127.0.0.1'
+    return config
+
+
 def start_service(args):
     """启动代理服务"""
     bin_path = find_mihomo_bin(args.bin)
@@ -288,20 +296,19 @@ def start_service(args):
     print(f"[*] Mihomo 程序: {bin_path}", flush=True)
     print(f"[*] 配置文件路径: {config_path}", flush=True)
 
-    # 自动适配 Docker / 外部连接:确保 allow-lan 开启
+    # Host launches default to loopback. Container access remains on its
+    # internal network; Compose publishes host ports on loopback as well.
     allow_lan_env = os.getenv("ALLOW_LAN", "").lower() in ("1", "true", "yes")
     in_docker = os.path.exists("/.dockerenv") or os.getenv("DOCKER_CONTAINER") == "true"
-    if getattr(args, "allow_lan", False) or allow_lan_env or in_docker:
-        try:
-            with open(config_path, "r", encoding="utf-8") as f:
-                content = f.read()
-            if "allow-lan: false" in content:
-                print("[*] 正在为容器/局域网环境自动修正 allow-lan: true ...", flush=True)
-                content = content.replace("allow-lan: false", "allow-lan: true")
-                with open(config_path, "w", encoding="utf-8") as f:
-                    f.write(content)
-        except Exception as e:
-            print(f"⚠️ 自动配置 allow-lan 提示: {e}", flush=True)
+    external_access = getattr(args, "allow_lan", False) or allow_lan_env or in_docker
+    with open(config_path, "r", encoding="utf-8") as f:
+        secured_config = yaml.safe_load(f) or {}
+    secured_config = secure_listener_config(secured_config, external_access)
+    # Fail closed on a write error; never fall back to an exposed config.
+    os.makedirs(LOG_DIR, exist_ok=True)
+    config_path = os.path.join(LOG_DIR, 'mihomo.runtime.yaml')
+    with open(config_path, 'w', encoding='utf-8') as f:
+        yaml.safe_dump(secured_config, f, allow_unicode=True, sort_keys=False)
 
     # 读取配置概要
     try:

+ 257 - 0
test/test_booking_reliability.py

@@ -0,0 +1,257 @@
+"""Offline regressions for notification, booking and traffic controls."""
+import json
+import os
+import tempfile
+import threading
+import types
+import unittest
+from datetime import datetime
+from pathlib import Path
+from unittest.mock import MagicMock, patch
+
+from booker import BuiltinBookerGCO
+from core_types import (AppointmentType, AvailabilityStatus, BookResult, BookerStandaloneConfig,
+                        DateAvailability, GroupConfig, PluginConfig, QueryResult, Task, BizLogicError)
+from plugins.grc_plugin import GrcPlugin
+from plugins.usa_plugin import UsaPlugin
+from plugins.vfs_plugin import VfsPlugin
+from sentinel import SentinelGCO
+from standalone import StandaloneGCO
+from toolkit.notifier import notify_book_result, notify_date_changes
+from utils.booking_outbox import BookingOutbox
+from toolkit import notifier
+from collections import OrderedDict
+from plugin_factory import PluginFactory
+
+
+class BookingReliabilityTests(unittest.TestCase):
+    def setUp(self):
+        self.tmp = tempfile.TemporaryDirectory()
+        self.addCleanup(self.tmp.cleanup)
+        env = patch.dict(os.environ, {'BOOKING_OUTBOX_PATH': str(Path(self.tmp.name) / 'outbox.sqlite3')})
+        env.start()
+        self.addCleanup(env.stop)
+        network = patch('socket.socket.connect', side_effect=AssertionError('Network forbidden in tests'))
+        network.start()
+        self.addCleanup(network.stop)
+        for name, value in (('_pending', {}), ('_sent', OrderedDict()), ('_slot_states', {})):
+            state = patch.object(notifier, name, value)
+            state.start()
+            self.addCleanup(state.stop)
+        self.cloud = MagicMock()
+        cloud = patch('toolkit.cloud_api.CloudApi.Instance', return_value=self.cloud)
+        cloud.start()
+        self.addCleanup(cloud.stop)
+        self.apt = AppointmentType(routing_key='audit.slot')
+        self.available = QueryResult(success=True, apt_type=self.apt,
+                                     availability_status=AvailabilityStatus.Available,
+                                     earliest_date=datetime(2026, 11, 1),
+                                     availability=[DateAvailability(date=datetime(2026, 11, 1))])
+
+    def pending(self):
+        return len(notifier._pending)
+
+    def test_notifications_do_not_use_telemetry_executor(self):
+        with patch('toolkit.task_executor.submit_background', return_value=False):
+            self.assertTrue(notify_date_changes(self.available))
+            self.assertTrue(notify_book_result(BookResult(success=True, urn='confirmed')))
+        self.assertEqual(self.pending(), 2)
+        notifier._flush_pending(self.cloud, lambda _: None)
+        self.assertEqual(self.cloud.push_weixin_text.call_count, 2)
+        self.assertEqual(self.pending(), 0)
+
+    def test_full_memory_queue_does_not_advance_slot_cache(self):
+        cache = {}
+        with patch.object(notifier, '_MAX_PENDING', 0):
+            self.assertFalse(notify_date_changes(self.available, cache, logger=lambda _: None))
+        self.assertEqual(cache, {})
+        self.assertTrue(notify_date_changes(self.available, cache))
+
+
+    def test_plugin_preferences_mutation_does_not_change_configuration(self):
+        preferences = {'email': 'audit@example.invalid'}
+        g = BuiltinBookerGCO(GroupConfig(identifier='audit', enable=True), {}, logger=lambda _: None)
+        self.addCleanup(g.redis_client.close)
+        g.redis_client = MagicMock()
+        self.cloud.get_vas_task_pop.return_value = {'id': 42, 'user_inputs': preferences}
+        def book(_, inputs):
+            inputs['alias_email'] = 'generated@example.invalid'
+            return BookResult(success=False)
+        plugin = MagicMock(config=PluginConfig())
+        plugin.book.side_effect = book
+        g._execute_book_job(Task(instance=plugin), self.available)
+        plugin.book.assert_called_once()
+        self.assertNotIn('alias_email', preferences)
+
+    def test_failed_delivery_retries_without_duplicate_enqueue(self):
+        self.cloud.push_weixin_text.side_effect = RuntimeError('offline')
+        notify_date_changes(self.available)
+        notifier._flush_pending(self.cloud, lambda _: None)
+        self.assertFalse(notify_date_changes(self.available))
+        self.assertEqual(self.pending(), 1)
+        self.cloud.push_weixin_text.side_effect = None
+        with patch('toolkit.notifier.time.time', return_value=9999999999):
+            notifier._flush_pending(self.cloud, lambda _: None)
+        self.assertEqual(self.pending(), 0)
+
+    def test_reopened_same_date_and_new_time_and_waitlist_notify(self):
+        notify_date_changes(self.available)
+        notify_date_changes(QueryResult(apt_type=self.apt))
+        notify_date_changes(self.available)
+        from core_types import TimeSlot
+        modified = self.available.model_copy(deep=True)
+        modified.availability[0].times.append(TimeSlot(time='10:00'))
+        notify_date_changes(modified)
+        notify_date_changes(QueryResult(success=True, apt_type=self.apt,
+                                        availability_status=AvailabilityStatus.Waitlist))
+        self.assertEqual(self.pending(), 4)
+
+
+    def sentinel(self):
+        g = SentinelGCO(GroupConfig(identifier='audit', enable=True), {}, logger=lambda _: None)
+        self.addCleanup(g.redis_client.close)
+        g.redis_client = MagicMock()
+        g.redis_client.get.return_value = None
+        plugin = MagicMock(config=PluginConfig())
+        return g, plugin, Task(instance=plugin, next_health_check=float('inf'))
+
+    def test_unavailable_result_replaces_positive_signal(self):
+        g, plugin, task = self.sentinel()
+        plugin.query.side_effect = [self.available, QueryResult()]
+        with patch('sentinel.notify_date_changes'):
+            g._monitor_task(task, self.apt)
+            g._monitor_task(task, self.apt)
+        self.assertEqual(g.redis_client.setex.call_count, 2)
+        last = json.loads(g.redis_client.setex.call_args.args[2])
+        self.assertFalse(last['query_result']['success'])
+        self.assertEqual(last['query_result']['availability_status'], AvailabilityStatus.NoneAvailable.value)
+
+
+    def test_vfs_window_has_hard_six_month_budget(self):
+        self.assertEqual(len(VfsPlugin._get_filtered_covered_months(None, '2000-01-01', '2100-01-01', '2026-10-25')), 6)
+
+    def test_grc_does_not_replay_post_on_read_timeout(self):
+        session = MagicMock()
+        session.request.side_effect = TimeoutError('response lost after commit')
+        plugin = types.SimpleNamespace(session=session, config=PluginConfig(), _log=lambda _: None,
+                                       _init_session=lambda **_: session)
+        plugin._perform_request = types.MethodType(GrcPlugin._perform_request, plugin)
+        with self.assertRaises(TimeoutError), patch('plugins.grc_plugin.time.sleep'):
+            plugin._perform_request('POST', 'https://example.invalid/reservation')
+        session.request.assert_called_once()
+
+
+    def test_booking_outbox_queues_success_notification(self):
+        outbox = BookingOutbox()
+        outbox.put(42, 'tracker', {'status': 'grabbed', 'grabbed_history': {'urn': 'receipt'}})
+        BookingOutbox(outbox.path).flush(self.cloud, MagicMock(), lambda _: None)
+        self.assertEqual(self.pending(), 1)
+        self.assertIsNone(outbox.get(42))
+
+    def test_standalone_success_stops_further_booking_in_current_process(self):
+        cfg = BookerStandaloneConfig(identifier='audit', appointment_types=[self.apt])
+        g = StandaloneGCO(cfg, {}, logger=lambda _: None)
+        self.addCleanup(g.redis_client.close)
+        plugin = MagicMock()
+        plugin.query.return_value = self.available
+        plugin.book.return_value = BookResult(success=True, urn='receipt')
+        g.m_instance = plugin
+        stopped = threading.Event()
+        g.m_stop_event = MagicMock()
+        g.m_stop_event.is_set.side_effect = stopped.is_set
+        waits = []
+        def wait(*_):
+            waits.append(1)
+            if len(waits) >= 2:
+                stopped.set()
+            return stopped.is_set()
+        g.m_stop_event.wait.side_effect = wait
+        with patch.object(g, '_get_wait_interval', return_value=0):
+            g._worker_loop()
+        plugin.book.assert_called_once()
+        self.assertEqual(g._booking_outcome[1].urn, 'receipt')
+
+    def test_usa_rejects_click_as_success_and_accepts_confirmation(self):
+        element = MagicMock()
+        element.parent.return_value.text = '09:00'
+        element.text = ''
+        page = MagicMock()
+        page.url = 'https://example.invalid/schedule'
+        page.ele.return_value = element
+        plugin = types.SimpleNamespace(page=page, mouse=MagicMock(), config=PluginConfig(),
+                                       _random_sleep=lambda *_: None, _log=lambda _: None)
+        with patch('plugins.usa_plugin.time.sleep'), self.assertRaises(BizLogicError):
+            UsaPlugin.book(plugin, self.available, {})
+        page.url = 'https://example.invalid/appointment-confirmation/'
+        element.text = 'Confirmed: 2026-11-01'
+        self.assertTrue(UsaPlugin.book(plugin, self.available, {}).success)
+
+    def test_vfs_payment_resolution_error_preserves_confirmed_booking(self):
+        route = {'subcategory_code': 'sub'}
+        plugin = types.SimpleNamespace(
+            _log=lambda _: None, config=PluginConfig(), free_config={'apt_configs': {self.apt.routing_key: route}},
+            subcategory_conf={'sub': {}}, booking_wait_applied=True,
+            _fetch_configurations=MagicMock(), _add_primary_applicant=MagicMock(return_value='receipt'),
+            _get_filtered_covered_months=MagicMock(return_value=['2026-11-01']),
+            _query_slot_calendar=MagicMock(return_value=['2026-11-01']),
+            _saveuseractionaudit=MagicMock(return_value=True),
+            _query_slot_time=MagicMock(return_value=[{'allocationId': '1', 'slot': '09:00'}]),
+            _submit_no_addition_service=MagicMock(), _query_fee=MagicMock(return_value=(1, 'EUR')),
+            _schedule=MagicMock(return_value={'IsAppointmentBooked': True, 'isPaymentRequired': True}),
+            _pay_request=MagicMock(side_effect=TimeoutError('payment redirect lost')))
+        plugin.book = types.MethodType(VfsPlugin.book, plugin)
+        receipt = plugin.book(self.available,
+                                  {'email': 'audit@example.invalid', 'expected_start_date': '2026-11-01',
+                                   'expected_end_date': '2026-12-01'})
+        self.assertTrue(receipt.success)
+        self.assertEqual(receipt.urn, 'receipt')
+
+
+    def test_unsupported_adapters_fail_before_browser_or_order_consumption(self):
+        root = Path(__file__).resolve().parent.parent
+        for name in ('bel', 'pol', 'prt'):
+            factory = PluginFactory()
+            factory.register_plugin(name + '_plugin', str(root / 'plugins' / (name + '_plugin.py')),
+                                    name.title() + 'Plugin')
+            with self.assertRaises(NotImplementedError):
+                factory.ensure_booking_supported(name + '_plugin')
+
+    def test_proxy_host_ports_and_native_listeners_default_to_loopback(self):
+        import yaml
+        from start_proxy import secure_listener_config
+        root = Path(__file__).resolve().parent.parent
+        compose = yaml.safe_load((root / 'docker-compose.yml').read_text(encoding='utf-8'))
+        self.assertTrue(all(str(port).startswith('127.0.0.1:')
+                            for port in compose['services']['proxy']['ports']))
+        cfg = secure_listener_config({'allow-lan': True, 'listeners': [{'listen': '0.0.0.0', 'port': 16001}]})
+        self.assertFalse(cfg['allow-lan'])
+        self.assertEqual(cfg['bind-address'], '127.0.0.1')
+        self.assertEqual(cfg['listeners'][0]['listen'], '127.0.0.1')
+
+    def test_expired_and_unavailable_signals_never_start_booking(self):
+        g = BuiltinBookerGCO(GroupConfig(identifier='audit', enable=True,
+                                        appointment_types=[self.apt]), {}, logger=lambda _: None)
+        self.addCleanup(g.redis_client.close)
+        g.redis_client = MagicMock()
+        g.redis_client.get.return_value = None
+        g.m_tasks = [Task(instance=MagicMock(), acceptable_routing_keys=[self.apt.routing_key])]
+        for payload in [
+            {'apt_type': self.apt.model_dump(), 'query_result': self.available.to_snapshot_payload(), 'expires_at': 1},
+            {'apt_type': self.apt.model_dump(), 'query_result': QueryResult().to_snapshot_payload()},
+        ]:
+            g.redis_client.mget.return_value = [json.dumps(payload)]
+            with patch.object(g._executor, 'submit') as submit:
+                g._poll_booking_signals()
+                submit.assert_not_called()
+
+
+    def test_confirmed_success_supersedes_pending_pause(self):
+        outbox = BookingOutbox()
+        outbox.put(42, 'tracker', {'status': 'pause'})
+        outbox.put(42, 'tracker', {'status': 'grabbed', 'grabbed_history': {'urn': 'receipt'}})
+        outbox.put(42, 'tracker', {'status': 'pause'})
+        self.assertEqual(outbox.get(42)['status'], 'grabbed')
+
+
+if __name__ == '__main__':
+    unittest.main()

+ 6 - 2
test/test_scheduler_reliability.py

@@ -29,13 +29,14 @@ class SchedulerTests(unittest.TestCase):
     def make(self, cls=BuiltinBookerGCO):
         g = cls(GroupConfig(identifier='audit', enable=True), {}, logger=lambda _: None)
         g.redis_client = MagicMock()
+        g.redis_client.get.return_value = None
         if hasattr(g, '_outbox'):
             g._outbox = BookingOutbox(str(Path(self.tmp.name) / 'outbox.sqlite3'))
         self.addCleanup(g.stop)
         return g
 
     def signal(self, key):
-        return json.dumps({'apt_type': {'routing_key': key}, 'query_result': {}})
+        return json.dumps({'apt_type': {'routing_key': key}, 'query_result': {'availability_status': 'Available'}})
 
     def test_long_builtin_booking_renews_order_lease_without_browser_ping(self):
         g = self.make()
@@ -281,6 +282,8 @@ class SchedulerTests(unittest.TestCase):
 
 class SweeperTests(unittest.TestCase):
     def setUp(self):
+        self.tmp = tempfile.TemporaryDirectory()
+        self.addCleanup(self.tmp.cleanup)
         self.sweeper = OrphanTaskSweeper({}, logger=MagicMock())
         self.sweeper.redis_client = MagicMock()
         self.sweeper._outbox = MagicMock()
@@ -311,7 +314,8 @@ class SweeperTests(unittest.TestCase):
     def test_remote_success_marker_is_synchronized(self):
         self.sweeper.redis_client.get.return_value = '{"status": "grabbed"}'
         self.sweeper._rescue_task('tracker', 42)
-        self.cloud.update_vas_task.assert_called_once_with('42', {'status': 'grabbed'})
+        self.sweeper._outbox.put.assert_called_once_with(42, 'tracker', {'status': 'grabbed'})
+        self.sweeper._outbox.flush.assert_called_once()
         self.cloud.return_vas_task_to_queue.assert_not_called()
 
     def test_lease_renewed_during_cloud_read_is_not_requeued(self):

+ 102 - 68
toolkit/notifier.py

@@ -1,81 +1,115 @@
-from typing import Dict, Callable, Optional
-from core_types import QueryResult, BookResult
+"""Bounded in-memory notification retries, isolated from telemetry workers."""
+import hashlib
+import json
+import logging
+import threading
+import time
+import uuid
+from collections import OrderedDict
+from core_types import QueryResult, BookResult, AvailabilityStatus
 from toolkit.cloud_api import CloudApi
-from toolkit.task_executor import submit_background
 
+_worker_lock = threading.Lock()
+_pending_lock = threading.RLock()
+_pending = {}
+_sent = OrderedDict()
+_slot_states = {}
+_MAX_PENDING = 256
+_MAX_SENT = 1024
+_worker = None
+_wakeup = threading.Event()
 
-def notify_date_changes(
-    query_result: QueryResult,
-    cache: Optional[Dict[str, str]] = None,
-    logger: Optional[Callable[[str], None]] = None
-) -> bool:
-    """
-    Slot availability change WeChat notification.
-    If a cache dict is provided (e.g. {routing_key: earliest_date}), only pushes and updates cache when earliest date changes.
-    """
-    current_earliest_date = query_result.earliest_date
-    apt_type = query_result.apt_type
-    if not current_earliest_date or not apt_type:
-        return False
 
-    if cache is not None:
-        last_date = cache.get(apt_type.routing_key)
-        if last_date == current_earliest_date:
-            return False
-        cache[apt_type.routing_key] = current_earliest_date
+def start_notification_worker():
+    global _worker
+    with _worker_lock:
+        if _worker is None or not _worker.is_alive():
+            def run():
+                while True:
+                    try:
+                        _flush_pending(CloudApi.Instance(), logging.getLogger(__name__).warning)
+                    except Exception:
+                        logging.getLogger(__name__).exception('Notification delivery retry failed')
+                    _wakeup.wait(5)
+                    _wakeup.clear()
+            _worker = threading.Thread(target=run, name='NotificationDelivery', daemon=True)
+            _worker.start()
+    _wakeup.set()
+
 
-    def _push_to_wx():
+def _flush_pending(cloud, logger):
+    with _pending_lock:
+        jobs = [(key, value) for key, value in _pending.items() if value[2] <= time.time()][:20]
+    for event_id, (content, attempts, due) in jobs:
         try:
-            push_content = (
-                f"📢【档期变化通知】\n"
-                f"最早日期: {query_result.earliest_date}\n"
-                f"目标国家: {apt_type.country}\n"
-                f"递交城市: {apt_type.city}\n"
-                f"签证类型: {apt_type.visa_type}\n"
-                f"Routing: {apt_type.routing_key}"
-            )
-            CloudApi.Instance().push_weixin_text(push_content)
-        except Exception as e:
-            if logger:
-                logger(f"Failed to notify date change to cloud: {e}")
+            cloud.push_weixin_text(content)
+        except Exception as exc:
+            with _pending_lock:
+                _pending[event_id] = (content, attempts + 1, time.time() + min(300, 5 * 2 ** min(attempts, 6)))
+            logger(f'Notification retry pending ({event_id}): {exc}')
+        else:
+            with _pending_lock:
+                _pending.pop(event_id, None)
+                _sent[event_id] = None
+                while len(_sent) > _MAX_SENT:
+                    _sent.popitem(last=False)
 
-    submit_background(_push_to_wx)
+
+def enqueue_text(event_id, content, logger=None):
+    with _pending_lock:
+        if event_id in _pending or event_id in _sent:
+            return True
+        if len(_pending) >= _MAX_PENDING:
+            (logger or logging.getLogger(__name__).error)('Notification queue full; message was not queued')
+            return False
+        _pending[event_id] = (content, 0, 0)
+    _wakeup.set()
     return True
 
 
-def notify_book_result(
-    book_result: BookResult,
-    order_id: Optional[str] = None,
-    logger: Optional[Callable[[str], None]] = None
-) -> bool:
-    """
-    Booking success WeChat notification.
-    Only sends push notification when book_result.success is True.
-    """
-    if not book_result.success:
+def notify_date_changes(query_result: QueryResult, cache=None, logger=None) -> bool:
+    apt = query_result.apt_type
+    if not apt or not apt.routing_key:
         return False
+    active = query_result.success or query_result.availability_status in (
+        AvailabilityStatus.Available, AvailabilityStatus.Waitlist)
+    snapshot = query_result.to_snapshot_payload()
+    state = {
+        'status': query_result.availability_status.value if active else AvailabilityStatus.NoneAvailable.value,
+        'earliest': snapshot['earliest_date'] if active else None,
+        'availability': sorted(snapshot['availability'], key=lambda item: json.dumps(item, sort_keys=True)) if active else [],
+    }
+    for item in state['availability']:
+        if 'times' in item:
+            item['times'] = sorted(item['times'], key=lambda slot: json.dumps(slot, sort_keys=True))
+    fingerprint = json.dumps(state, sort_keys=True)
+    content = None
+    if active:
+        content = (f"📢【档期变化通知】\n状态: {state['status']}\n"
+                   f"检测时间: {snapshot['snapshot_at']}\n"
+                   f"最早日期: {query_result.earliest_date or '候补/待确认'}\n"
+                   f"目标国家: {apt.country}\n递交城市: {apt.city}\n"
+                   f"签证类型: {apt.visa_type}\nRouting: {apt.routing_key}")
+    states = cache if cache is not None else _slot_states
+    with _pending_lock:
+        if states.get(apt.routing_key) == fingerprint:
+            return False
+        if not content:
+            states[apt.routing_key] = fingerprint
+            return False
+        queued = enqueue_text('slot:' + uuid.uuid4().hex, content, logger)
+        if queued:
+            states[apt.routing_key] = fingerprint
+        return queued
 
-    def _push_to_wx():
-        try:
-            lines = [
-                "🎉 【预定成功通知】",
-                "━━━━━━━━━━━━━━━",
-            ]
-            if order_id:
-                lines.append(f"订单编号: {order_id}")
-            lines.extend([
-                f"预约账号: {book_result.account}",
-                f"预约日期: {book_result.book_date}",
-                f"预约时间: {book_result.book_time}",
-                f"预约编号: {book_result.urn}",
-                f"支付链接: {book_result.payment_link if book_result.payment_link else '无需支付/暂无'}",
-                "━━━━━━━━━━━━━━━",
-            ])
-            push_content = "\n".join(lines)
-            CloudApi.Instance().push_weixin_text(push_content)
-        except Exception as e:
-            if logger:
-                logger(f"Failed to notify book result to cloud: {e}")
 
-    submit_background(_push_to_wx)
-    return True
+def notify_book_result(book_result: BookResult, order_id=None, logger=None, event_id=None) -> bool:
+    if not book_result.success:
+        return False
+    content = (f"🎉 【预定成功通知】\n订单编号: {order_id or '-'}\n"
+               f"预约账号: {book_result.account}\n预约日期: {book_result.book_date}\n"
+               f"预约时间: {book_result.book_time}\n预约编号: {book_result.urn}\n"
+               f"支付链接: {book_result.payment_link or '无需支付/暂无'}")
+    identity = json.dumps([order_id, book_result.model_dump()], sort_keys=True)
+    event_id = event_id or 'booking:' + hashlib.sha256(identity.encode()).hexdigest()
+    return enqueue_text(event_id, content, logger)

+ 18 - 0
utils/booking_outbox.py

@@ -30,6 +30,12 @@ class BookingOutbox:
 
     def put(self, task_id, tracker, payload):
         with closing(self._connect()) as db, db:
+            db.execute('BEGIN IMMEDIATE')
+            existing = db.execute('SELECT payload FROM booking_results WHERE task_id=?', (str(task_id),)).fetchone()
+            if existing and payload.get('status') == 'grabbed' and json.loads(existing[0]).get('status') != 'grabbed':
+                db.execute('UPDATE booking_results SET tracker=?,payload=?,cloud_synced=0,lease_until=0 WHERE task_id=?',
+                           (tracker, json.dumps(payload), str(task_id)))
+                return
             db.execute('INSERT INTO booking_results(task_id, tracker, payload) VALUES (?, ?, ?) '
                        'ON CONFLICT(task_id) DO NOTHING',
                        (str(task_id), tracker, json.dumps(payload)))
@@ -63,6 +69,18 @@ class BookingOutbox:
                     cloud.update_vas_task(task_id, json.loads(raw))
                     with closing(self._connect()) as db, db:
                         db.execute('UPDATE booking_results SET cloud_synced=1 WHERE task_id=?', (task_id,))
+                payload = json.loads(raw)
+                if payload.get('status') == 'grabbed':
+                    from core_types import BookResult
+                    from toolkit.notifier import notify_book_result
+                    info = payload.get('grabbed_history', {})
+                    receipt = BookResult(success=True, account=info.get('account', ''),
+                                         session_id=info.get('session_id', ''), urn=info.get('urn', ''),
+                                         book_date=info.get('slot_date', ''), book_time=info.get('slot_time', ''),
+                                         payment_link=info.get('payment_link', ''))
+                    if not notify_book_result(receipt, order_id=info.get('order_id'), logger=logger,
+                                              event_id='booking:order:' + str(task_id)):
+                        raise RuntimeError('Booking notification could not be queued')
                 # Keep the journal if Redis is unavailable; cleanup is retryable too.
                 redis_client.zrem(tracker_key, task_id)
                 redis_client.delete(self.marker(task_id))