Files
sentinel-home-ai/fam-edge/src/fam_edge/model_adapters/circuit_breaker.py
ericwyuan 61db82cb9b refactor(fam-edge): 重构第一阶段 - 人物图片零额外调用 + 运行时稳定性 + 工程质量
人物图片功能重做: bbox 随核心视频分析那一次 Gemini 调用一并产出(prompts.py 加
person_appearances.bbox 字段, [ymin,xmin,ymax,xmax] 0-1000 归一化), frame_service
直接用存好的 bbox 裁剪头像/事件缩略图, 删除原来"展示时额外调用 Gemini 定位人物"的
整套逻辑(locate_person_bbox/VLM 校验/熔断), 从架构上消除与核心视频分析共抢配额的
问题; 用真实数据验证裁剪结果正确框住人物本体。

NVIDIA 模型修复: 实测原配置的 3 个模型均不可用(asset_id 引用 500/400, 不支持视频),
改用 nemotron-3-nano-omni 的 base64 内嵌视频方式(唯一实测打通), 加 max_base64_mb
防止对大文件做注定失败的编码。

Gemini 多 Key 轮换: 支持 extra_api_keys 配置多个独立项目的 key, 配额用尽时依次
换 key 重试(每换 key 需重新上传, Files API 按项目隔离)。

稳定性加固: CircuitBreaker HALF_OPEN 清空旧失败计数(修复探测一失败就重新 OPEN 的
bug); chat() 统一接入熔断器(原来只有视频分析路径检查); NVIDIA 适配器改用共享
json_parser(原来自己重复实现且不做 schema 校验); Gemini Files API 上传超时也尝试
清理远程孤儿文件; video_processor/video_queue 里直接操作 OracleDB._conn 的裸 SQL
改走新增的 set_event_start_time/mark_video_invalid/reset_video_to_pending 方法;
/health 加入队列线程存活状态; 密钥改用 ${ENV_VAR} 引用(.env 已支持自动加载),
不再明文写入 config.yaml。

工程质量: 新增 fam-edge/tests(32 个单元测试, 覆盖熔断器状态机/JSON 解析容错/
时间戳解析/bbox 坐标换算/多 key 解析), 新增 scripts/smoke_test.py(发版前接口
稳定性检查); 清理死代码(OllamaAdapter.analyze_frames、get_sync_delta 死分支、
未使用的 vision_timeout/max_concurrent_tasks 配置项); 修正 get_events_for_label
排序(改最近优先 + 过滤畸形历史时间戳)。

已部署 Oracle 并跑通 smoke test 全部 6 项检查。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-22 00:22:57 +08:00

54 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
熔断器 - 每个云端模型独立实例
状态机: CLOSED -> OPEN -> HALF_OPEN -> CLOSED/OPEN
- 连续 threshold 次失败 -> OPEN
- 冷却 cooldown 秒后 -> HALF_OPEN允许一次探测
- 探测成功 -> CLOSED探测失败 -> 重新 OPEN
"""
from collections import deque
import time
import threading
class CircuitBreaker:
def __init__(self, threshold: int = 5, cooldown: int = 900, enabled: bool = True):
self.enabled = enabled
if enabled:
self.failures = deque(maxlen=threshold)
else:
self.failures = None
self.threshold = threshold
self.cooldown = cooldown
self.state = 'CLOSED'
self.last_failure = None
self._lock = threading.Lock() # 状态转换加锁,防多线程竞争
def record_failure(self):
if not self.enabled:
return
with self._lock:
self.failures.append(time.time())
if len(self.failures) >= self.threshold:
self.state = 'OPEN'
self.last_failure = time.time()
def record_success(self):
if not self.enabled:
return
with self._lock:
self.failures.clear()
self.state = 'CLOSED'
def is_open(self):
if not self.enabled:
return False
with self._lock:
if self.state == 'OPEN' and self.last_failure and time.time() - self.last_failure > self.cooldown:
self.state = 'HALF_OPEN'
self.failures.clear() # 清空旧失败记录,避免探测调用一失败就被旧记录凑数重新 OPEN
return self.state == 'OPEN'
def __repr__(self):
return f"CircuitBreaker(state={self.state}, enabled={self.enabled})"