sycommon-python-lib 0.2.7a31__py3-none-any.whl → 0.2.7a33__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sycommon/agent/deep_agent.py +9 -7
- sycommon/agent/middleware/sensitive_guard.py +102 -15
- sycommon/agent/multi_agent_team.py +17 -4
- sycommon/agent/sandbox/minio_sync.py +4 -4
- sycommon/agent/summarization_utils.py +9 -4
- sycommon/config/Config.py +2 -1
- sycommon/tests/test_upgrade_aio_pika.py +153 -0
- sycommon/tests/test_upgrade_langgraph_stream.py +131 -0
- sycommon/tests/test_upgrade_verification.py +229 -0
- {sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/METADATA +7 -7
- {sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/RECORD +14 -11
- {sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/top_level.txt +0 -0
sycommon/agent/deep_agent.py
CHANGED
|
@@ -98,12 +98,13 @@ def _patch_agent_streaming(agent):
|
|
|
98
98
|
删除此函数及调用即可。
|
|
99
99
|
"""
|
|
100
100
|
# 已验证兼容的版本范围(超出仅警告, 不阻断 —— 闭包强校验是真正的安全网)
|
|
101
|
-
# 1.3.10/1.3.11/1.3.12 + langchain_core 1.4.8/1.4.9
|
|
102
|
-
# (factory.py 在 1.3.
|
|
103
|
-
# _execute_model_async, 内层 freevars 含
|
|
104
|
-
# patch 正常生效。
|
|
105
|
-
#
|
|
106
|
-
|
|
101
|
+
# 1.3.10/1.3.11/1.3.12/1.3.13 + langchain_core 1.4.8/1.4.9 确认闭包结构兼容
|
|
102
|
+
# (factory.py 在 1.3.12→1.3.13 逐字节无变化 —— PyPI sdist diff 实证;
|
|
103
|
+
# amodel_node freevars 含 _execute_model_async, 内层 freevars 含
|
|
104
|
+
# _get_bound_model/_handle_model_output), patch 正常生效。
|
|
105
|
+
# 1.3.13 的 _execute_model_async 仍为 ainvoke + 不传 config (factory.py:1467),
|
|
106
|
+
# 官方仍未修复, 补丁仍需保留。
|
|
107
|
+
_VERIFIED = {"langchain": "1.3.13", "langchain_core": "1.4.9"}
|
|
107
108
|
try:
|
|
108
109
|
import langchain as _lc
|
|
109
110
|
import langchain_core as _lcc
|
|
@@ -377,7 +378,8 @@ class AgentConfig(BaseModel):
|
|
|
377
378
|
internal_model_name: Optional[str] = None
|
|
378
379
|
# 敏感检测提示词(应用层可覆盖);未传(None)用基础库 DEFAULT_DETECT_PROMPT
|
|
379
380
|
sensitive_detect_prompt: Optional[str] = None
|
|
380
|
-
# 命中敏感后的动作:"allow"=不检测放行;"downgrade"=降级内部模型继续;
|
|
381
|
+
# 命中敏感后的动作:"allow"=不检测放行;"downgrade"=降级内部模型继续;
|
|
382
|
+
# "block"=直接阻断不执行(检测整段上下文);"block_user_input"=阻断(仅检测最后一条 HumanMessage)。
|
|
381
383
|
sensitive_action: str = "allow"
|
|
382
384
|
|
|
383
385
|
# 系统提示词
|
|
@@ -72,6 +72,19 @@ class DowngradeState:
|
|
|
72
72
|
self.last_source = source
|
|
73
73
|
return True
|
|
74
74
|
|
|
75
|
+
async def reset(self, source: str = "user_input") -> None:
|
|
76
|
+
"""清除降级标记(带锁,CAS 契约一致)。
|
|
77
|
+
|
|
78
|
+
应用层在「命中敏感后想放行下一轮重检」场景(如 block 模式阻断后,
|
|
79
|
+
用户切走/换 thread 重建 agent)应调本方法,而不是直接写
|
|
80
|
+
`state.downgraded = False`(无锁、绕过 CAS)。单 agent 下两者等价,
|
|
81
|
+
但 multi-agent 共享状态或未来跨 task 重置时,无锁直写会与
|
|
82
|
+
mark_downgraded 的 CAS 真竞态——本方法是唯一安全的重置入口。
|
|
83
|
+
"""
|
|
84
|
+
async with self._lock:
|
|
85
|
+
self.downgraded = False
|
|
86
|
+
self.last_source = source
|
|
87
|
+
|
|
75
88
|
|
|
76
89
|
DEFAULT_DETECT_PROMPT = """你是数据脱敏分类器。判断以下「即将发送给外部云模型」的对话上下文是否包含不宜外流的敏感数据,只回答结构化字段(is_sensitive + reason + source)。
|
|
77
90
|
|
|
@@ -237,13 +250,19 @@ class SensitiveGuardMiddleware(AgentMiddleware):
|
|
|
237
250
|
state: Optional[DowngradeState] = None,
|
|
238
251
|
# 应用层可覆盖检测提示词;未传(None)用 DEFAULT_DETECT_PROMPT
|
|
239
252
|
detect_prompt: Optional[str] = None,
|
|
240
|
-
# 命中敏感后的动作:"allow"=不检测放行;"downgrade"=降级内部模型继续;
|
|
253
|
+
# 命中敏感后的动作:"allow"=不检测放行;"downgrade"=降级内部模型继续;
|
|
254
|
+
# "block"=直接阻断(不调任何模型),检测整段上下文;
|
|
255
|
+
# "block_user_input"=直接阻断,但只检测最后一条 HumanMessage(用户本轮输入)。
|
|
241
256
|
action: str = "allow",
|
|
242
257
|
# 阻断模式下的异步提示回调(与 on_downgrade 区分文案);downgrade 模式不用
|
|
243
258
|
on_block: Optional[Callable[[], Awaitable[None]]] = None,
|
|
244
259
|
# 测试注入:绕过懒加载的真实 get_llm
|
|
245
260
|
_detector: Any = None,
|
|
246
261
|
_internal_raw: Any = None,
|
|
262
|
+
# 是否执行检测。多 agent 子 agent 传 False:它看到的「最后一条 HumanMessage」
|
|
263
|
+
# 多是协调者委派指令而非用户原文,重复判易误命中;改由协调者检测、子 agent 只靠
|
|
264
|
+
# 共享 DowngradeState 短路阻断。单 agent 始终 True(默认)。
|
|
265
|
+
detect: bool = True,
|
|
247
266
|
):
|
|
248
267
|
self._model_tier = model_tier
|
|
249
268
|
# None 时检测器/降级目标都用 get_llm() 读 nacos default 模型,不写死模型名
|
|
@@ -251,7 +270,13 @@ class SensitiveGuardMiddleware(AgentMiddleware):
|
|
|
251
270
|
self._on_downgrade = on_downgrade
|
|
252
271
|
self._on_block = on_block
|
|
253
272
|
# 默认 allow(不检测):应用层未显式指定时按"放行"处理,避免误伤。
|
|
254
|
-
self._action = action if action in ("downgrade", "block", "allow") else "allow"
|
|
273
|
+
self._action = action if action in ("downgrade", "block", "block_user_input", "allow") else "allow"
|
|
274
|
+
self._detect_enabled = detect
|
|
275
|
+
# 单轮「有附件降 block」标志:仅 block_user_input 模式下,应用层每轮 chat 前按
|
|
276
|
+
# has_file 刷新。True → 本轮按 block 行为检测(整段上下文,含工具结果/附件正文);
|
|
277
|
+
# False → 按 block_user_input(仅最后一条 HumanMessage)。绝不改 _action,避免
|
|
278
|
+
# 与用户配的真 block 混淆(set_file_turn_degrade 在非 block_user_input 上为 no-op)。
|
|
279
|
+
self._file_turn_degrade = False
|
|
255
280
|
self._enabled = enabled
|
|
256
281
|
self._state = state or DowngradeState()
|
|
257
282
|
self._detect_prompt = detect_prompt or DEFAULT_DETECT_PROMPT
|
|
@@ -263,15 +288,50 @@ class SensitiveGuardMiddleware(AgentMiddleware):
|
|
|
263
288
|
def is_downgraded(self) -> bool:
|
|
264
289
|
return self._state.is_downgraded
|
|
265
290
|
|
|
291
|
+
@property
|
|
292
|
+
def _is_block_turn(self) -> bool:
|
|
293
|
+
"""本轮是否走阻断路径(命中即返空响应、不调任何模型)。
|
|
294
|
+
|
|
295
|
+
block 和 block_user_input 都是阻断型 action;降级标志只改「检测范围」
|
|
296
|
+
(effective_detect_messages: last_human vs all),不改「是否阻断」。
|
|
297
|
+
is_blocked 与 awrap_model_call 的 (b)(d) 共用本判定。
|
|
298
|
+
"""
|
|
299
|
+
return self._action in ("block", "block_user_input")
|
|
300
|
+
|
|
266
301
|
@property
|
|
267
302
|
def is_blocked(self) -> bool:
|
|
268
|
-
"""
|
|
269
|
-
|
|
303
|
+
"""本会话是否已阻断(命中过敏感且本轮 action 是阻断型)。
|
|
304
|
+
action ∈ {block, block_user_input} 时为 True(两者都是阻断型 action)。
|
|
305
|
+
降级标志只改检测范围(effective_detect_messages),不改是否阻断。"""
|
|
306
|
+
return self._state.is_downgraded and self._is_block_turn
|
|
270
307
|
|
|
271
308
|
@property
|
|
272
309
|
def action(self) -> str:
|
|
273
310
|
return self._action
|
|
274
311
|
|
|
312
|
+
def set_file_turn_degrade(self, on: bool) -> None:
|
|
313
|
+
"""单轮「有附件降 block」标志开关(仅对 base=block_user_input 生效)。
|
|
314
|
+
|
|
315
|
+
应用层每轮 agent.chat 开始前按本轮 has_file 调用:
|
|
316
|
+
- has_file=True → on=True 本轮按 block 行为检测(整段上下文)
|
|
317
|
+
- has_file=False → on=False 本轮按 block_user_input 行为检测(仅用户输入)
|
|
318
|
+
只在 self._action == "block_user_input" 时生效;其它 base(allow/downgrade/block)
|
|
319
|
+
调本方法为 no-op(且强制清标志防残留),绝不改 _action。
|
|
320
|
+
"""
|
|
321
|
+
if self._action == "block_user_input":
|
|
322
|
+
self._file_turn_degrade = bool(on)
|
|
323
|
+
else:
|
|
324
|
+
self._file_turn_degrade = False
|
|
325
|
+
|
|
326
|
+
@property
|
|
327
|
+
def effective_detect_messages(self) -> str:
|
|
328
|
+
"""本轮检测范围标识:'all'(整段上下文)/ 'last_human'(仅最后一条 HumanMessage)。
|
|
329
|
+
block_user_input + 降级标志 → 'all';block_user_input 无标志 → 'last_human';
|
|
330
|
+
block/downgrade → 'all';allow 由 (a) 短路不检测。"""
|
|
331
|
+
if self._action == "block_user_input" and not self._file_turn_degrade:
|
|
332
|
+
return "last_human"
|
|
333
|
+
return "all"
|
|
334
|
+
|
|
275
335
|
@property
|
|
276
336
|
def last_notice_source(self) -> str:
|
|
277
337
|
"""首次命中时的敏感来源(user_input/tool_result/history),供应用层选文案。"""
|
|
@@ -281,6 +341,18 @@ class SensitiveGuardMiddleware(AgentMiddleware):
|
|
|
281
341
|
def state(self) -> DowngradeState:
|
|
282
342
|
return self._state
|
|
283
343
|
|
|
344
|
+
@staticmethod
|
|
345
|
+
def _last_human(messages: List[BaseMessage]) -> Optional[List[BaseMessage]]:
|
|
346
|
+
"""取最后一条 HumanMessage,包装成单元素列表供 _detect 使用。
|
|
347
|
+
|
|
348
|
+
block_user_input 模式只检测用户本轮输入(含附件正文),工具结果/历史不进检测。
|
|
349
|
+
无 HumanMessage 返回 None(调用方放行)。
|
|
350
|
+
"""
|
|
351
|
+
for m in reversed(messages):
|
|
352
|
+
if isinstance(m, HumanMessage):
|
|
353
|
+
return [m]
|
|
354
|
+
return None
|
|
355
|
+
|
|
284
356
|
async def _get_detector(self):
|
|
285
357
|
if self._detector is None:
|
|
286
358
|
async with self._init_lock:
|
|
@@ -364,27 +436,42 @@ class SensitiveGuardMiddleware(AgentMiddleware):
|
|
|
364
436
|
if not self._enabled or self._model_tier == "internal" or self._action == "allow":
|
|
365
437
|
return await handler(request)
|
|
366
438
|
|
|
367
|
-
# (b)
|
|
439
|
+
# (b) 本会话已命中敏感(共享 state 已 downgraded)
|
|
368
440
|
if self._state.is_downgraded:
|
|
369
|
-
#
|
|
370
|
-
|
|
371
|
-
# 不进工具循环(无 tool_calls),干净结束。
|
|
372
|
-
if self._action == "block":
|
|
441
|
+
# 阻断路径(真 block 或 block_user_input+降级标志):返回空响应,不再调模型。
|
|
442
|
+
if self._is_block_turn:
|
|
373
443
|
return ModelResponse(result=[AIMessage(content="")])
|
|
374
|
-
#
|
|
444
|
+
# 降级模式(downgrade,或 block_user_input 非降级轮——后者理论不会到这因 (c) 只判用户输入):
|
|
445
|
+
# 路由内部模型继续;本次输入若非敏感(来自历史记忆)刷新 source=history
|
|
375
446
|
await self._refresh_source_if_history(request.messages)
|
|
376
447
|
return await handler(request.override(model=await self._get_internal_raw()))
|
|
377
448
|
|
|
378
|
-
# (
|
|
379
|
-
|
|
449
|
+
# (b') detect=False 且尚未命中:放行(多 agent 子 agent 不主动检测,
|
|
450
|
+
# 等协调者命中后由共享 state 在 (b) 短路阻断)。
|
|
451
|
+
if not self._detect_enabled:
|
|
452
|
+
return await handler(request)
|
|
453
|
+
|
|
454
|
+
# (c) 检测:按 effective_detect_messages 选喂给 detector 的消息范围
|
|
455
|
+
# block_user_input 无降级标志 → 仅最后一条 HumanMessage;其余(block/downgrade/
|
|
456
|
+
# block_user_input+降级;allow 已在 (a) 短路不会到这)→ 整段上下文
|
|
457
|
+
if self.effective_detect_messages == "last_human":
|
|
458
|
+
detect_messages = self._last_human(request.messages)
|
|
459
|
+
if detect_messages is None:
|
|
460
|
+
# 无用户输入(理论上不会发生)→ 无从检测,放行
|
|
461
|
+
return await handler(request)
|
|
462
|
+
else:
|
|
463
|
+
# block / downgrade / block_user_input+降级:整段上下文
|
|
464
|
+
detect_messages = request.messages
|
|
465
|
+
|
|
466
|
+
check = await self._detect(detect_messages)
|
|
380
467
|
if not check.is_sensitive:
|
|
381
468
|
return await handler(request)
|
|
382
469
|
|
|
383
|
-
# (d) 命中 → CAS
|
|
470
|
+
# (d) 命中 → CAS 抢占;按本轮是否阻断路径分流
|
|
384
471
|
is_first = await self._state.mark_downgraded(check.source)
|
|
385
|
-
if self.
|
|
472
|
+
if self._is_block_turn:
|
|
386
473
|
SYLogger.info(
|
|
387
|
-
f"[SensitiveGuard] 命中敏感信息,阻断执行: {check.reason} (source={check.source})")
|
|
474
|
+
f"[SensitiveGuard] 命中敏感信息,阻断执行: {check.reason} (source={check.source}, action={self._action})")
|
|
388
475
|
if is_first and self._on_block:
|
|
389
476
|
try:
|
|
390
477
|
await self._on_block()
|
|
@@ -134,7 +134,7 @@ class TeamConfig(BaseModel):
|
|
|
134
134
|
sensitive_guard: bool = True # 是否启用敏感信息检测守卫
|
|
135
135
|
internal_model_name: Optional[str] = None # 敏感降级目标内部模型名(应用层透传)
|
|
136
136
|
sensitive_detect_prompt: Optional[str] = None # 敏感检测提示词(应用层覆盖)
|
|
137
|
-
sensitive_action: str = "allow" # "allow"=不检测放行;"downgrade"=降级继续;"block"=阻断
|
|
137
|
+
sensitive_action: str = "allow" # "allow"=不检测放行;"downgrade"=降级继续;"block"=阻断(整段上下文);"block_user_input"=阻断(仅检测用户输入)
|
|
138
138
|
|
|
139
139
|
coordinator_prompt: str = ""
|
|
140
140
|
coordinator_name: str = "项目经理"
|
|
@@ -200,6 +200,7 @@ class MultiAgentTeam:
|
|
|
200
200
|
coordinator_name: str = "项目经理",
|
|
201
201
|
recovery_manager: Optional[SandboxRecoveryManager] = None,
|
|
202
202
|
downgrade_state: Optional[DowngradeState] = None,
|
|
203
|
+
sensitive_guard: Optional["SensitiveGuardMiddleware"] = None,
|
|
203
204
|
):
|
|
204
205
|
self.user_id = user_id
|
|
205
206
|
self.agent = agent
|
|
@@ -210,6 +211,10 @@ class MultiAgentTeam:
|
|
|
210
211
|
self.sandbox_backend = sandbox_backend
|
|
211
212
|
self.recovery_manager = recovery_manager
|
|
212
213
|
self.downgrade_state = downgrade_state
|
|
214
|
+
# 协调者的敏感检测 guard 实例(build 时传入)。暴露给应用层读 guard 状态
|
|
215
|
+
# (is_downgraded / last_notice_source / set_file_turn_degrade 等),
|
|
216
|
+
# 让单 agent 与 multi-agent 的应用层代码统一走 `getattr(agent, "sensitive_guard", None)`。
|
|
217
|
+
self.sensitive_guard = sensitive_guard
|
|
213
218
|
|
|
214
219
|
@property
|
|
215
220
|
def is_downgraded(self) -> bool:
|
|
@@ -628,7 +633,9 @@ async def create_multi_agent_team(
|
|
|
628
633
|
# 任一命中 → 全员降级(敏感数据会经委派链流动,独立 state 会漏)。
|
|
629
634
|
shared_downgrade_state = DowngradeState()
|
|
630
635
|
|
|
631
|
-
def _make_guard():
|
|
636
|
+
def _make_guard(detect: bool = True):
|
|
637
|
+
"""协调者 detect=True(检测用户输入);子 agent detect=False
|
|
638
|
+
(只靠共享 state 短路阻断,不把委派指令当用户输入重复判)。"""
|
|
632
639
|
return SensitiveGuardMiddleware(
|
|
633
640
|
model_tier=config.model_tier,
|
|
634
641
|
enabled=config.sensitive_guard,
|
|
@@ -636,10 +643,11 @@ async def create_multi_agent_team(
|
|
|
636
643
|
internal_model_name=config.internal_model_name,
|
|
637
644
|
detect_prompt=config.sensitive_detect_prompt,
|
|
638
645
|
action=config.sensitive_action,
|
|
646
|
+
detect=detect,
|
|
639
647
|
)
|
|
640
648
|
|
|
641
649
|
middleware = [
|
|
642
|
-
_make_guard(),
|
|
650
|
+
_make_guard(detect=False),
|
|
643
651
|
ToolResultTruncationMiddleware(),
|
|
644
652
|
TokenTrackingMiddleware(model_name=config.model_name, user_id=user_id),
|
|
645
653
|
summarization_mw,
|
|
@@ -686,6 +694,10 @@ async def create_multi_agent_team(
|
|
|
686
694
|
# 创建协调者 Agent
|
|
687
695
|
coord_name = config.coordinator_name
|
|
688
696
|
coord_summarization_mw = build_summarization_middleware(model, config.model_name, sandbox_backend)
|
|
697
|
+
# 协调者 guard 实例:单独构造,传给 MultiAgentTeam.sensitive_guard 供应用层读取
|
|
698
|
+
# (与单 agent 的 agent.sensitive_guard 对齐)。
|
|
699
|
+
coord_guard = _make_guard(detect=True)
|
|
700
|
+
|
|
689
701
|
coordinator_agent = create_deep_agent(
|
|
690
702
|
model=model,
|
|
691
703
|
tools=config.shared_tools or [get_current_date],
|
|
@@ -698,7 +710,7 @@ async def create_multi_agent_team(
|
|
|
698
710
|
debug=config.debug,
|
|
699
711
|
name=coord_name,
|
|
700
712
|
middleware=[
|
|
701
|
-
|
|
713
|
+
coord_guard,
|
|
702
714
|
ToolResultTruncationMiddleware(),
|
|
703
715
|
TokenTrackingMiddleware(model_name=config.model_name, user_id=user_id),
|
|
704
716
|
coord_summarization_mw,
|
|
@@ -718,6 +730,7 @@ async def create_multi_agent_team(
|
|
|
718
730
|
coordinator_name=coord_name,
|
|
719
731
|
recovery_manager=recovery_manager,
|
|
720
732
|
downgrade_state=shared_downgrade_state,
|
|
733
|
+
sensitive_guard=coord_guard,
|
|
721
734
|
)
|
|
722
735
|
|
|
723
736
|
|
|
@@ -27,11 +27,11 @@ _SNAPSHOT_ENABLED = False
|
|
|
27
27
|
# restore_from_minio 的 MinIO 下载并发度。
|
|
28
28
|
# 两层信号量:
|
|
29
29
|
# - 单用户层 _RESTORE_PER_USER:每个用户恢复时最多 5 路并发下载(单用户恢复够快);
|
|
30
|
-
# - 全局层 _RESTORE_GLOBAL_CAP:全进程最多
|
|
31
|
-
#
|
|
32
|
-
# 单用户独享 5 槽 + 全局封顶
|
|
30
|
+
# - 全局层 _RESTORE_GLOBAL_CAP:全进程最多 100 路下载,多用户同时恢复时全局封顶,
|
|
31
|
+
# 避免无限放大压垮 MinIO/网络,又留足并发让多用户恢复不互相拖慢。
|
|
32
|
+
# 单用户独享 5 槽 + 全局封顶 100。
|
|
33
33
|
_RESTORE_PER_USER = 5
|
|
34
|
-
_RESTORE_GLOBAL_CAP =
|
|
34
|
+
_RESTORE_GLOBAL_CAP = 100
|
|
35
35
|
# 全局信号量惰性创建(绑定到首次调用时的 event loop;单 loop 部署下不变)。
|
|
36
36
|
_RESTORE_DOWNLOAD_SEM: "asyncio.Semaphore | None" = None
|
|
37
37
|
|
|
@@ -42,6 +42,13 @@ try:
|
|
|
42
42
|
from langchain_core.exceptions import ContextOverflowError
|
|
43
43
|
|
|
44
44
|
_orig_handle_bad_request = _lc_oai_base._handle_openai_bad_request
|
|
45
|
+
_orig_handle_api_error = _lc_oai_base._handle_openai_api_error
|
|
46
|
+
|
|
47
|
+
# langchain_openai 原生识别的上下文溢出模式(1.3.5 起 bad_request/api_error 各有):
|
|
48
|
+
# bad_request: "context_length_exceeded" / "Input tokens exceed the configured
|
|
49
|
+
# limit" / "prompt is too long"
|
|
50
|
+
# api_error : "exceeds the context window" ← 1.3.5 新增
|
|
51
|
+
# 这些都不覆盖 MiniMax/vLLM 的 "maximum context length is ..." 句式, 下方扩展补上。
|
|
45
52
|
|
|
46
53
|
def _patched_handle_openai_bad_request(e: openai.BadRequestError) -> None:
|
|
47
54
|
"""扩展版 _handle_openai_bad_request,识别更多上下文溢出错误格式。"""
|
|
@@ -58,14 +65,11 @@ try:
|
|
|
58
65
|
message=e.message, response=e.response, body=e.body
|
|
59
66
|
) from e
|
|
60
67
|
|
|
61
|
-
#
|
|
68
|
+
# 回退到原始处理逻辑(含官方的 context_length_exceeded 等识别)
|
|
62
69
|
_orig_handle_bad_request(e)
|
|
63
70
|
|
|
64
71
|
_lc_oai_base._handle_openai_bad_request = _patched_handle_openai_bad_request
|
|
65
72
|
|
|
66
|
-
# 同步 patch _handle_openai_api_error
|
|
67
|
-
_orig_handle_api_error = _lc_oai_base._handle_openai_api_error
|
|
68
|
-
|
|
69
73
|
def _patched_handle_openai_api_error(e: openai.APIError) -> None:
|
|
70
74
|
"""扩展版 _handle_openai_api_error,识别更多上下文溢出错误格式。"""
|
|
71
75
|
error_str = str(e)
|
|
@@ -73,6 +77,7 @@ try:
|
|
|
73
77
|
raise _lc_oai_base.OpenAIAPIContextOverflowError(
|
|
74
78
|
message=e.message, request=e.request, body=e.body
|
|
75
79
|
) from e
|
|
80
|
+
# 回退到原始处理逻辑(含官方 1.3.5 新增的 "exceeds the context window" 识别)
|
|
76
81
|
_orig_handle_api_error(e)
|
|
77
82
|
|
|
78
83
|
_lc_oai_base._handle_openai_api_error = _patched_handle_openai_api_error
|
sycommon/config/Config.py
CHANGED
|
@@ -186,7 +186,8 @@ class Config(metaclass=SingletonMeta):
|
|
|
186
186
|
# 但消费方按顶层 key 读取(LLMConfig 等)——提升到顶层,免得每个调用方都得知道嵌套路径。
|
|
187
187
|
override_keys = ['LLMConfig', 'EmbeddingConfig', 'RerankerConfig',
|
|
188
188
|
'ACPAgentConfig',
|
|
189
|
-
'SkillApiWhitelist'
|
|
189
|
+
'SkillApiWhitelist',
|
|
190
|
+
'SensitiveConfig']
|
|
190
191
|
for k in override_keys:
|
|
191
192
|
if k in project_cfg:
|
|
192
193
|
self.config[k] = project_cfg[k]
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""aio-pika 10 升级端到端验证(真实 RabbitMQ)。
|
|
2
|
+
|
|
3
|
+
验证 aio-pika 9.6.2 → 10.0.1(aiormq 6→7)升级的核心风险点:
|
|
4
|
+
1. 普通连接 connect() 的 connect+channel+publish+consume 全链路
|
|
5
|
+
2. connect_robust() + RobustConnection(robust 模块在 v10 仍保留)
|
|
6
|
+
3. Connection.Close 帧解析能力(用于诊断 vhost 权限等问题)
|
|
7
|
+
|
|
8
|
+
连接配置取自 shengye-platform-digital-work 的 nacos-data 快照(mq.yml)。
|
|
9
|
+
rabbit 用户对 /uat 无权限(broker ACL),对 / 有权限;本测试用 / 验证连通性,
|
|
10
|
+
/uat 的权限问题与本次升级无关,仅在 e2e 验证时记录为已知差异。
|
|
11
|
+
|
|
12
|
+
运行方式(pytest 或直接 python):
|
|
13
|
+
uv run python -m pytest src/sycommon/tests/test_upgrade_aio_pika.py -v -s
|
|
14
|
+
python src/sycommon/tests/test_upgrade_aio_pika.py
|
|
15
|
+
"""
|
|
16
|
+
import asyncio
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
|
|
20
|
+
import pytest
|
|
21
|
+
|
|
22
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
23
|
+
|
|
24
|
+
import aio_pika
|
|
25
|
+
from importlib.metadata import version
|
|
26
|
+
|
|
27
|
+
# nacos mq.yml 配置
|
|
28
|
+
MQ_HOST = "rabbitmq.test1.svc.k8s.syf.com"
|
|
29
|
+
MQ_PORT = 5672
|
|
30
|
+
MQ_USER = "rabbit"
|
|
31
|
+
MQ_PASS = "SU7BrhDfbGbH"
|
|
32
|
+
# rabbit 用户对 / 有权限;mq.yml 里的 /uat 是另一套授权用户才能访问的 vhost
|
|
33
|
+
MQ_VHOST_OK = "/"
|
|
34
|
+
MQ_VHOST_UAT = "uat"
|
|
35
|
+
|
|
36
|
+
TEST_QUEUE = "sycommon_upgrade_test"
|
|
37
|
+
TEST_MSG = b"upgrade-test"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _url(vhost: str) -> str:
|
|
41
|
+
return f"amqp://{MQ_USER}:{MQ_PASS}@{MQ_HOST}:{MQ_PORT}/{vhost}"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_aio_pika_versions_pinned():
|
|
45
|
+
"""v10 依赖链:aio-pika 10 / aiormq 7 / pamqp 4。"""
|
|
46
|
+
assert aio_pika.__version__ == "10.0.1", (
|
|
47
|
+
f"aio-pika 应为 10.0.1, 实际 {aio_pika.__version__}")
|
|
48
|
+
assert version("aiormq").split(".")[0] == "7", (
|
|
49
|
+
f"aiormq 主版本应为 7, 实际 {version('aiormq')}")
|
|
50
|
+
assert version("pamqp").split(".")[0] == "4", (
|
|
51
|
+
f"pamqp 主版本应为 4, 实际 {version('pamqp')}")
|
|
52
|
+
print(f" [OK] aio-pika={aio_pika.__version__} "
|
|
53
|
+
f"aiormq={version('aiormq')} pamqp={version('pamqp')}")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_roundtrip_plain_connection():
|
|
57
|
+
"""普通连接:connect + channel + declare_queue + publish + get 全链路。"""
|
|
58
|
+
async def go():
|
|
59
|
+
conn = await aio_pika.connect(_url(MQ_VHOST_OK), timeout=10)
|
|
60
|
+
async with conn:
|
|
61
|
+
ch = await conn.channel()
|
|
62
|
+
q = await ch.declare_queue(TEST_QUEUE, auto_delete=True)
|
|
63
|
+
await ch.default_exchange.publish(
|
|
64
|
+
aio_pika.Message(body=TEST_MSG), routing_key=TEST_QUEUE)
|
|
65
|
+
msg = await q.get(timeout=5, no_ack=True)
|
|
66
|
+
return msg.body
|
|
67
|
+
|
|
68
|
+
body = asyncio.run(go())
|
|
69
|
+
assert body == TEST_MSG, f"收发不一致: {body!r}"
|
|
70
|
+
print(f" [OK] 普通连接 roundtrip: body={body!r}")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_robust_connection_roundtrip():
|
|
74
|
+
"""connect_robust 返回 RobustConnection,robust 模块在 v10 仍存在且工作。"""
|
|
75
|
+
async def go():
|
|
76
|
+
conn: aio_pika.RobustConnection = await aio_pika.connect_robust(
|
|
77
|
+
_url(MQ_VHOST_OK), timeout=10)
|
|
78
|
+
info = {
|
|
79
|
+
"type": type(conn).__name__,
|
|
80
|
+
"is_robust": issubclass(type(conn), aio_pika.RobustConnection),
|
|
81
|
+
"reconnect_interval": conn.reconnect_interval,
|
|
82
|
+
}
|
|
83
|
+
async with conn:
|
|
84
|
+
ch = await conn.channel()
|
|
85
|
+
info["channel_type"] = type(ch).__name__
|
|
86
|
+
q = await ch.declare_queue(TEST_QUEUE, auto_delete=True)
|
|
87
|
+
got = asyncio.Event()
|
|
88
|
+
received = []
|
|
89
|
+
|
|
90
|
+
async def consumer(message: aio_pika.IncomingMessage):
|
|
91
|
+
received.append(message.body)
|
|
92
|
+
got.set()
|
|
93
|
+
|
|
94
|
+
await q.consume(consumer, no_ack=True)
|
|
95
|
+
await ch.default_exchange.publish(
|
|
96
|
+
aio_pika.Message(body=TEST_MSG), routing_key=TEST_QUEUE)
|
|
97
|
+
await asyncio.wait_for(got.wait(), timeout=5)
|
|
98
|
+
info["body"] = received[0]
|
|
99
|
+
return info
|
|
100
|
+
|
|
101
|
+
info = asyncio.run(go())
|
|
102
|
+
assert info["is_robust"], "connect_robust 未返回 RobustConnection 子类"
|
|
103
|
+
assert info["type"] == "RobustConnection", f"连接类型异常: {info['type']}"
|
|
104
|
+
assert info["channel_type"] == "RobustChannel", (
|
|
105
|
+
f"channel 非鲁棒类型: {info['channel_type']}")
|
|
106
|
+
assert info["body"] == TEST_MSG, f"robust 收发不一致: {info['body']!r}"
|
|
107
|
+
print(f" [OK] RobustConnection roundtrip: "
|
|
108
|
+
f"type={info['type']} channel={info['channel_type']} "
|
|
109
|
+
f"reconnect_interval={info['reconnect_interval']} body={info['body']!r}")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_uat_vhost_permission_known_issue():
|
|
113
|
+
"""记录已知差异:rabbit 用户对 /uat 无权限(broker ACL,与升级无关)。
|
|
114
|
+
|
|
115
|
+
断言它确实被 broker 拒绝(reply_text 含 NOT_ALLOWED),以便未来
|
|
116
|
+
broker 授权变更时本测试会主动失败提醒复核。
|
|
117
|
+
"""
|
|
118
|
+
async def go():
|
|
119
|
+
conn = await aio_pika.connect(_url(MQ_VHOST_UAT), timeout=10)
|
|
120
|
+
async with conn:
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
try:
|
|
124
|
+
asyncio.run(go())
|
|
125
|
+
# 如果将来 /uat 对 rabbit 开放了权限,连接会成功 —— 提示复核
|
|
126
|
+
pytest.fail(
|
|
127
|
+
"/uat 现在可连了:broker 可能已给 rabbit 授权,请复核 mq.yml 配置")
|
|
128
|
+
except Exception as e: # noqa: BLE001
|
|
129
|
+
cl = e.args[1] if len(getattr(e, "args", ())) > 1 else None
|
|
130
|
+
detail = ""
|
|
131
|
+
if cl is not None and hasattr(cl, "marshal"):
|
|
132
|
+
detail = cl.marshal().decode("latin1", "replace")
|
|
133
|
+
assert "NOT_ALLOWED" in detail, (
|
|
134
|
+
f"/uat 被拒但原因非权限问题,请复核: {detail!r}")
|
|
135
|
+
print(f" [OK] /uat 确实因权限被拒(已知差异,与升级无关): "
|
|
136
|
+
f"{detail[:60]!r}")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
if __name__ == "__main__":
|
|
140
|
+
# 直接运行模式(与 test_consumer_recovery.py 一致的 __main__ 风格)
|
|
141
|
+
async def main():
|
|
142
|
+
print("=" * 70)
|
|
143
|
+
print(" aio-pika 10 升级端到端验证")
|
|
144
|
+
print("=" * 70)
|
|
145
|
+
test_aio_pika_versions_pinned()
|
|
146
|
+
test_roundtrip_plain_connection()
|
|
147
|
+
test_robust_connection_roundtrip()
|
|
148
|
+
test_uat_vhost_permission_known_issue()
|
|
149
|
+
print("=" * 70)
|
|
150
|
+
print(" 全部通过")
|
|
151
|
+
print("=" * 70)
|
|
152
|
+
|
|
153
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""langgraph agent 流式升级端到端验证(真实 LLM)。
|
|
2
|
+
|
|
3
|
+
验证 langchain 1.2.12→1.3.13 + langgraph 1.2.8→1.2.9 + streaming 补丁
|
|
4
|
+
在真实 LLM 网关下,stream_mode="messages" 逐 token 触发 on_llm_new_token。
|
|
5
|
+
|
|
6
|
+
覆盖升级核心风险点:
|
|
7
|
+
1. 底层 ChatOpenAI.astream() 能逐 token 触发 on_llm_new_token
|
|
8
|
+
2. deepagents 的 model node(_execute_model_async)经补丁替换为 astream+config 后,
|
|
9
|
+
agent.astream(stream_mode="messages") 正常产出消息片段
|
|
10
|
+
|
|
11
|
+
连接配置取自 nacos-data llm 配置(10.10.6.132:3000 网关)。
|
|
12
|
+
|
|
13
|
+
CI/无内网环境默认跳过;连接到内网网关时显式开启:
|
|
14
|
+
SYCOMMON_E2E_LLM=1 uv run python -m pytest src/sycommon/tests/test_upgrade_langgraph_stream.py -v -s
|
|
15
|
+
python src/sycommon/tests/test_upgrade_langgraph_stream.py
|
|
16
|
+
"""
|
|
17
|
+
import asyncio
|
|
18
|
+
import os
|
|
19
|
+
import sys
|
|
20
|
+
|
|
21
|
+
import pytest
|
|
22
|
+
|
|
23
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
24
|
+
|
|
25
|
+
from langchain_openai import ChatOpenAI
|
|
26
|
+
from langchain_core.messages import HumanMessage
|
|
27
|
+
from langchain_core.callbacks import BaseCallbackHandler
|
|
28
|
+
|
|
29
|
+
# nacos llm 配置
|
|
30
|
+
BASE_URL = "http://10.10.6.132:3000/v1"
|
|
31
|
+
API_KEY = "sk-gbDkgZb3JD6i2Mrwf0RfYJf8YuEiby7nozQmv99E2fxEdUIh"
|
|
32
|
+
MODEL = "glm-5-turbo"
|
|
33
|
+
|
|
34
|
+
# 无内网网关访问时跳过真实集成(pytest 收集仍可用)
|
|
35
|
+
_E2E_ENABLED = os.environ.get("SYCOMMON_E2E_LLM") == "1"
|
|
36
|
+
_skip_no_intranet = pytest.mark.skipif(
|
|
37
|
+
not _E2E_ENABLED,
|
|
38
|
+
reason="需要内网 LLM 网关访问;设置 SYCOMMON_E2E_LLM=1 启用",
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class _TokenCollector(BaseCallbackHandler):
|
|
43
|
+
"""统计 on_llm_new_token 触发次数。"""
|
|
44
|
+
|
|
45
|
+
def __init__(self):
|
|
46
|
+
self.tokens = 0
|
|
47
|
+
|
|
48
|
+
def on_llm_new_token(self, token, **kwargs): # noqa: D401
|
|
49
|
+
self.tokens += 1
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _make_model(max_tokens: int = 50) -> ChatOpenAI:
|
|
53
|
+
return ChatOpenAI(
|
|
54
|
+
model=MODEL, base_url=BASE_URL, api_key=API_KEY,
|
|
55
|
+
streaming=True, temperature=0.0, max_tokens=max_tokens,
|
|
56
|
+
extra_body={"stream_options": {"include_usage": True}},
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@_skip_no_intranet
|
|
61
|
+
def test_underlying_astream_triggers_token_callbacks():
|
|
62
|
+
"""底层 model.astream() 逐 token 触发 on_llm_new_token。"""
|
|
63
|
+
async def go():
|
|
64
|
+
model = _make_model()
|
|
65
|
+
collector = _TokenCollector()
|
|
66
|
+
chunks = 0
|
|
67
|
+
async for _chunk in model.astream(
|
|
68
|
+
[HumanMessage(content="用一句话介绍你自己")],
|
|
69
|
+
config={"callbacks": [collector]},
|
|
70
|
+
):
|
|
71
|
+
chunks += 1
|
|
72
|
+
return chunks, collector.tokens
|
|
73
|
+
|
|
74
|
+
chunks, tokens = asyncio.run(go())
|
|
75
|
+
assert chunks > 0, "astream 无产出"
|
|
76
|
+
assert tokens > 0, "on_llm_new_token 未触发 —— 流式回调链路断了"
|
|
77
|
+
print(f" [OK] astream chunks={chunks}, on_llm_new_token 触发 {tokens} 次")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@_skip_no_intranet
|
|
81
|
+
def test_agent_stream_mode_messages(monkeypatch):
|
|
82
|
+
"""端到端:deepagents agent + streaming 补丁 → stream_mode='messages' 产出。
|
|
83
|
+
|
|
84
|
+
这是 streaming 补丁(deep_agent.py:_patch_agent_streaming)要修复的核心场景:
|
|
85
|
+
官方 factory._execute_model_async 用 ainvoke 且不传 config,导致回调不触发;
|
|
86
|
+
补丁替换为 astream + 从 ContextVar 读 config,使 on_llm_new_token 透传到
|
|
87
|
+
stream_mode="messages" 消费者。
|
|
88
|
+
"""
|
|
89
|
+
from deepagents import create_deep_agent
|
|
90
|
+
from langgraph.checkpoint.memory import MemorySaver
|
|
91
|
+
|
|
92
|
+
agent = create_deep_agent(
|
|
93
|
+
model=_make_model(), tools=[], middleware=(), checkpointer=MemorySaver(),
|
|
94
|
+
)
|
|
95
|
+
from sycommon.agent.deep_agent import _patch_agent_streaming
|
|
96
|
+
_patch_agent_streaming(agent)
|
|
97
|
+
|
|
98
|
+
async def go():
|
|
99
|
+
config = {"configurable": {"thread_id": "upgrade-stream-test"}}
|
|
100
|
+
message_chunks = 0
|
|
101
|
+
async for _stream_mode, _chunk in agent.astream(
|
|
102
|
+
{"messages": [HumanMessage(content="用一个词回答:你好")]},
|
|
103
|
+
config=config,
|
|
104
|
+
stream_mode="messages",
|
|
105
|
+
):
|
|
106
|
+
message_chunks += 1
|
|
107
|
+
if message_chunks > 200: # 防御性截断
|
|
108
|
+
break
|
|
109
|
+
return message_chunks
|
|
110
|
+
|
|
111
|
+
message_chunks = asyncio.run(go())
|
|
112
|
+
assert message_chunks > 0, (
|
|
113
|
+
"stream_mode='messages' 无产出 —— 补丁未生效或流式链路断了")
|
|
114
|
+
print(f" [OK] agent stream_mode='messages' 产出 {message_chunks} 个消息片段")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
if __name__ == "__main__":
|
|
118
|
+
async def main():
|
|
119
|
+
print("=" * 70)
|
|
120
|
+
print(" langgraph agent 流式升级端到端验证")
|
|
121
|
+
print("=" * 70)
|
|
122
|
+
if not _E2E_ENABLED:
|
|
123
|
+
print(" [SKIP] 未设置 SYCOMMON_E2E_LLM=1,跳过(需内网 LLM 网关)")
|
|
124
|
+
return
|
|
125
|
+
test_underlying_astream_triggers_token_callbacks()
|
|
126
|
+
test_agent_stream_mode_messages(None)
|
|
127
|
+
print("=" * 70)
|
|
128
|
+
print(" 全部通过")
|
|
129
|
+
print("=" * 70)
|
|
130
|
+
|
|
131
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""验证本次依赖升级 + 补丁适配的正确性(纯离线,不依赖 RabbitMQ/真实 LLM)。
|
|
2
|
+
|
|
3
|
+
覆盖本次三处改动:
|
|
4
|
+
A. deep_agent.py 的 streaming 补丁白名单:_VERIFIED 已更新到 langchain==1.3.13,
|
|
5
|
+
且在 1.3.13 的真实 langchain.agents.factory 上能定位到 _execute_model_async
|
|
6
|
+
闭包(验证补丁在新版上仍生效、不跳过)。
|
|
7
|
+
B. summarization_utils.py 的 ContextOverflow 补丁:在 1.3.5 上成功替换
|
|
8
|
+
_handle_openai_bad_request / _handle_openai_api_error,且:
|
|
9
|
+
- MiniMax/vLLM 句式 "maximum context length is ..." 仍被正确转成
|
|
10
|
+
OpenAIContextOverflowError / OpenAIAPIContextOverflowError;
|
|
11
|
+
- 官方原生句式(context_length_exceeded / exceeds the context window)
|
|
12
|
+
通过回退 _orig 仍被识别(不被补丁截断)。
|
|
13
|
+
C. 依赖版本落地:langchain 1.3.13 / langchain_openai 1.3.5 / langchain_core 1.4.9。
|
|
14
|
+
"""
|
|
15
|
+
import sys
|
|
16
|
+
import pytest
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# ============================================================
|
|
20
|
+
# C. 依赖版本落地
|
|
21
|
+
# ============================================================
|
|
22
|
+
def test_installed_versions():
|
|
23
|
+
import langchain
|
|
24
|
+
import langchain_openai
|
|
25
|
+
import langchain_core
|
|
26
|
+
|
|
27
|
+
assert langchain.__version__ == "1.3.13", (
|
|
28
|
+
f"langchain 应为 1.3.13, 实际 {langchain.__version__}")
|
|
29
|
+
assert langchain_openai.__version__ == "1.3.5", (
|
|
30
|
+
f"langchain_openai 应为 1.3.5, 实际 {langchain_openai.__version__}")
|
|
31
|
+
assert langchain_core.__version__ == "1.4.9", (
|
|
32
|
+
f"langchain_core 应为 1.4.9, 实际 {langchain_core.__version__}")
|
|
33
|
+
print(f" [OK] langchain={langchain.__version__} "
|
|
34
|
+
f"langchain_openai={langchain_openai.__version__} "
|
|
35
|
+
f"langchain_core={langchain_core.__version__}")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# ============================================================
|
|
39
|
+
# A. streaming 补丁白名单 + 闭包定位(不创建真实 agent,直接验 langchain 源码结构)
|
|
40
|
+
# ============================================================
|
|
41
|
+
def test_streaming_patch_whitelist_updated():
|
|
42
|
+
"""_VERIFIED 白名单已更新到 1.3.13。"""
|
|
43
|
+
import inspect
|
|
44
|
+
from sycommon.agent import deep_agent
|
|
45
|
+
|
|
46
|
+
src = inspect.getsource(deep_agent._patch_agent_streaming)
|
|
47
|
+
assert '"langchain": "1.3.13"' in src, (
|
|
48
|
+
"_VERIFIED 白名单未更新到 langchain==1.3.13")
|
|
49
|
+
assert '"1.3.12"' not in src.split("_VERIFIED")[0].split("#")[-1] or \
|
|
50
|
+
True # 仅作存在性提示,真正断言在下一行
|
|
51
|
+
# 精确:_VERIFIED 字典行内应含 1.3.13
|
|
52
|
+
ver_line = [l for l in src.splitlines() if "_VERIFIED = " in l][0]
|
|
53
|
+
assert "1.3.13" in ver_line, f"_VERIFIED 行未指向 1.3.13: {ver_line.strip()}"
|
|
54
|
+
print(f" [OK] 白名单已更新: {ver_line.strip()}")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_factory_closure_locatable_on_1_3_13():
|
|
58
|
+
"""直接验证 langchain 1.3.13 的 factory.py 闭包结构,
|
|
59
|
+
确认 _patch_agent_streaming 的 cell 定位逻辑能成功命中。
|
|
60
|
+
|
|
61
|
+
复刻补丁里的 _cell_by_name 定位逻辑,对真实源码运行。
|
|
62
|
+
"""
|
|
63
|
+
# deepagents 的 create_deep_agent 在构建 agent 时才创建闭包,
|
|
64
|
+
# 这里直接 import factory 模块确认关键符号存在且未被官方改动。
|
|
65
|
+
from langchain.agents import factory as fac
|
|
66
|
+
|
|
67
|
+
src = inspect_getsource(fac)
|
|
68
|
+
# 1.3.13 官方仍是 ainvoke + 不传 config(子代理已用 sdiff 实证)
|
|
69
|
+
assert "output = await model_.ainvoke(messages)" in src, (
|
|
70
|
+
"1.3.13 的 _execute_model_async 未改用 astream —— 与补丁白名单注释一致, "
|
|
71
|
+
"补丁仍需保留")
|
|
72
|
+
assert "_execute_model_async" in src, "_execute_model_async 符号仍在"
|
|
73
|
+
assert "async def amodel_node" in src, "amodel_node 闭包仍在"
|
|
74
|
+
print(" [OK] langchain 1.3.13 factory.py 闭包结构确认: "
|
|
75
|
+
"_execute_model_async 仍存在、仍为 ainvoke+无config, 补丁替换点有效")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def inspect_getsource(mod):
|
|
79
|
+
import inspect
|
|
80
|
+
return inspect.getsource(mod)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_cell_by_name_logic_on_real_amodel_node(monkeypatch):
|
|
84
|
+
"""实际构建一个 deep agent,触发 _patch_agent_streaming,断言它成功命中
|
|
85
|
+
闭包(而非打印 skip 警告)。这是补丁在 1.3.13 上是否真正生效的端到端验证。
|
|
86
|
+
"""
|
|
87
|
+
from sycommon.logging.kafka_log import SYLogger
|
|
88
|
+
|
|
89
|
+
# 收集补丁内部发出的日志(SYLogger 是自定义类, 静态方法 warning/info, monkeypatch 拦截)
|
|
90
|
+
captured: list[str] = []
|
|
91
|
+
monkeypatch.setattr(SYLogger, "warning",
|
|
92
|
+
staticmethod(lambda *a, **k: captured.append(" ".join(str(x) for x in a))))
|
|
93
|
+
monkeypatch.setattr(SYLogger, "info",
|
|
94
|
+
staticmethod(lambda *a, **k: captured.append(" ".join(str(x) for x in a))))
|
|
95
|
+
|
|
96
|
+
from langchain.chat_models import init_chat_model
|
|
97
|
+
from langchain_core.tools import tool as lc_tool
|
|
98
|
+
|
|
99
|
+
from deepagents import create_deep_agent
|
|
100
|
+
|
|
101
|
+
model = init_chat_model(
|
|
102
|
+
model="test", model_provider="openai",
|
|
103
|
+
base_url="http://localhost:1/v1",
|
|
104
|
+
api_key="sk-test", temperature=0.0)
|
|
105
|
+
|
|
106
|
+
@lc_tool
|
|
107
|
+
def dummy_tool() -> str:
|
|
108
|
+
"""dummy"""
|
|
109
|
+
return "ok"
|
|
110
|
+
|
|
111
|
+
agent = create_deep_agent(
|
|
112
|
+
model=model, tools=[dummy_tool],
|
|
113
|
+
middleware=(), checkpointer=None,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
from sycommon.agent.deep_agent import _patch_agent_streaming
|
|
117
|
+
_patch_agent_streaming(agent)
|
|
118
|
+
|
|
119
|
+
# 断言没有打出 "skip streaming patch"(即命中失败)警告
|
|
120
|
+
skip_warnings = [m for m in captured if "skip streaming patch" in m]
|
|
121
|
+
assert not skip_warnings, (
|
|
122
|
+
f"补丁在 1.3.13 上未命中闭包, 发出 skip 警告: {skip_warnings}")
|
|
123
|
+
# 应打出 patched 成功日志
|
|
124
|
+
patched_logs = [m for m in captured if "patched (astream + config)" in m]
|
|
125
|
+
assert patched_logs, (
|
|
126
|
+
"补丁未打印 patched 成功日志, 可能未替换 _execute_model_async")
|
|
127
|
+
print(f" [OK] 补丁在 langchain 1.3.13 上成功替换 _execute_model_async")
|
|
128
|
+
print(f" 成功日志: {patched_logs[0][:120]}")
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# ============================================================
|
|
132
|
+
# B. ContextOverflow 补丁
|
|
133
|
+
# ============================================================
|
|
134
|
+
def _make_bad_request(msg: str):
|
|
135
|
+
"""构造一个真实的 openai.BadRequestError(带 httpx.Response)。"""
|
|
136
|
+
import openai
|
|
137
|
+
import httpx
|
|
138
|
+
resp = httpx.Response(400, request=httpx.Request("POST", "http://localhost/v1"))
|
|
139
|
+
return openai.BadRequestError(msg, response=resp, body={})
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _make_api_error(msg: str):
|
|
143
|
+
"""构造一个真实的 openai.APIError(带 request)。"""
|
|
144
|
+
import openai
|
|
145
|
+
import httpx
|
|
146
|
+
req = httpx.Request("POST", "http://localhost/v1")
|
|
147
|
+
# APIError 签名: (message, *, request, body=None)
|
|
148
|
+
return openai.APIError(msg, request=req, body={})
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_overflow_patch_replaces_handlers():
|
|
152
|
+
"""import summarization_utils 后, 两个 handler 已被替换为 patched 版本。"""
|
|
153
|
+
import sycommon.agent.summarization_utils # noqa: F401 触发补丁
|
|
154
|
+
import langchain_openai.chat_models.base as base
|
|
155
|
+
|
|
156
|
+
assert base._handle_openai_bad_request.__name__ == \
|
|
157
|
+
"_patched_handle_openai_bad_request", "bad_request handler 未被替换"
|
|
158
|
+
assert base._handle_openai_api_error.__name__ == \
|
|
159
|
+
"_patched_handle_openai_api_error", "api_error handler 未被替换"
|
|
160
|
+
print(" [OK] 两个 handler 均已被 patched 版本替换")
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_overflow_minimax_bad_request_recognized():
|
|
164
|
+
"""MiniMax/vLLM 句式 → OpenAIContextOverflowError。"""
|
|
165
|
+
import sycommon.agent.summarization_utils # noqa: F401 确保补丁已应用
|
|
166
|
+
import langchain_openai.chat_models.base as base
|
|
167
|
+
|
|
168
|
+
for msg in [
|
|
169
|
+
"This model's maximum context length is 8192 tokens. "
|
|
170
|
+
"However, your messages resulted in 9000 tokens.",
|
|
171
|
+
"Please reduce the length of the input prompt.",
|
|
172
|
+
]:
|
|
173
|
+
e = _make_bad_request(msg)
|
|
174
|
+
with pytest.raises(base.OpenAIContextOverflowError):
|
|
175
|
+
base._handle_openai_bad_request(e)
|
|
176
|
+
print(" [OK] MiniMax/vLLM bad_request 句式被正确识别")
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_overflow_minimax_api_error_recognized():
|
|
180
|
+
"""MiniMax/vLLM 句式经 APIError 路径 → OpenAIAPIContextOverflowError。"""
|
|
181
|
+
import sycommon.agent.summarization_utils # noqa: F401 确保补丁已应用
|
|
182
|
+
import langchain_openai.chat_models.base as base
|
|
183
|
+
|
|
184
|
+
e = _make_api_error(
|
|
185
|
+
"This model's maximum context length is 8192 tokens.")
|
|
186
|
+
with pytest.raises(base.OpenAIAPIContextOverflowError):
|
|
187
|
+
base._handle_openai_api_error(e)
|
|
188
|
+
print(" [OK] MiniMax/vLLM api_error 句式被正确识别")
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_overflow_native_unchanged_via_fallback():
|
|
192
|
+
"""官方原生句式通过 _orig 回退仍被识别(补丁没有截断官方逻辑)。
|
|
193
|
+
|
|
194
|
+
1.3.5 新增的 _handle_openai_api_error 识别 'exceeds the context window';
|
|
195
|
+
补丁对该句式应回退到官方 _orig 从而抛出 OpenAIAPIContextOverflowError。
|
|
196
|
+
"""
|
|
197
|
+
import sycommon.agent.summarization_utils # noqa: F401 确保补丁已应用
|
|
198
|
+
import langchain_openai.chat_models.base as base
|
|
199
|
+
|
|
200
|
+
# bad_request 原生: context_length_exceeded
|
|
201
|
+
e1 = _make_bad_request(
|
|
202
|
+
"This model's context_length_exceeded, too long")
|
|
203
|
+
with pytest.raises(base.OpenAIContextOverflowError):
|
|
204
|
+
base._handle_openai_bad_request(e1)
|
|
205
|
+
|
|
206
|
+
# api_error 1.3.5 原生: exceeds the context window
|
|
207
|
+
e2 = _make_api_error("input exceeds the context window")
|
|
208
|
+
with pytest.raises(base.OpenAIAPIContextOverflowError):
|
|
209
|
+
base._handle_openai_api_error(e2)
|
|
210
|
+
print(" [OK] 官方原生句式(context_length_exceeded / exceeds the context window)"
|
|
211
|
+
" 通过回退仍被识别")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def test_overflow_unrelated_error_reraised():
|
|
215
|
+
"""非上下文溢出的错误应原样 re-raise,不被误转。"""
|
|
216
|
+
import sycommon.agent.summarization_utils # noqa: F401 确保补丁已应用
|
|
217
|
+
import openai
|
|
218
|
+
import langchain_openai.chat_models.base as base
|
|
219
|
+
|
|
220
|
+
# 一个与上下文无关的 BadRequestError(会落入官方源码末尾的 raise, re-raise)
|
|
221
|
+
# 官方 _handle_openai_bad_request 末尾是裸 `raise`, 需在 except 块内才有 active
|
|
222
|
+
# exception。这里用 try/except 包一层模拟 langchain 的真实调用上下文。
|
|
223
|
+
e = _make_bad_request("invalid api key format")
|
|
224
|
+
try:
|
|
225
|
+
raise e
|
|
226
|
+
except openai.BadRequestError:
|
|
227
|
+
with pytest.raises(openai.BadRequestError):
|
|
228
|
+
base._handle_openai_bad_request(e)
|
|
229
|
+
print(" [OK] 无关错误原样 re-raise, 未被误转")
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sycommon-python-lib
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7a33
|
|
4
4
|
Summary: Add your description here
|
|
5
5
|
Requires-Python: >=3.11
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
7
|
-
Requires-Dist: aio-pika==
|
|
7
|
+
Requires-Dist: aio-pika==10.0.1
|
|
8
8
|
Requires-Dist: aiohttp==3.14.1
|
|
9
9
|
Requires-Dist: aiomysql==0.3.2
|
|
10
10
|
Requires-Dist: anyio==4.14.1
|
|
@@ -13,12 +13,12 @@ Requires-Dist: deepagents==0.6.12
|
|
|
13
13
|
Requires-Dist: elasticsearch==9.4.1
|
|
14
14
|
Requires-Dist: fastapi==0.139.0
|
|
15
15
|
Requires-Dist: jinja2==3.1.6
|
|
16
|
-
Requires-Dist: kafka-python==3.0.
|
|
17
|
-
Requires-Dist: langchain==1.3.
|
|
16
|
+
Requires-Dist: kafka-python==3.0.8
|
|
17
|
+
Requires-Dist: langchain==1.3.13
|
|
18
18
|
Requires-Dist: langchain-core==1.4.9
|
|
19
|
-
Requires-Dist: langchain-openai==1.3.
|
|
20
|
-
Requires-Dist: langfuse==4.
|
|
21
|
-
Requires-Dist: langgraph==1.2.
|
|
19
|
+
Requires-Dist: langchain-openai==1.3.5
|
|
20
|
+
Requires-Dist: langfuse==4.14.0
|
|
21
|
+
Requires-Dist: langgraph==1.2.9
|
|
22
22
|
Requires-Dist: langgraph-checkpoint-postgres==3.1.0
|
|
23
23
|
Requires-Dist: langgraph-checkpoint-redis==0.5.0
|
|
24
24
|
Requires-Dist: ldap3==2.9.1
|
|
@@ -136,9 +136,9 @@ sycommon/services.py,sha256=iC73h5d5bpNqW2qjRUGODZFhUhcwvKVnKUFLKJlBSq0,25322
|
|
|
136
136
|
sycommon/agent/__init__.py,sha256=mxceAeUifQ-DKvWp7ZEJIFlmOCb5wpYHPGQw3rwEN8I,4378
|
|
137
137
|
sycommon/agent/agent_manager.py,sha256=UhhaekEumT7g4v_Z1UB4jTp13X0n8M8erYaQdkGGWkA,13620
|
|
138
138
|
sycommon/agent/chat_events.py,sha256=t7qWa6OrIWLfqtd1AnqaVP67QWkn1JxpCBTZYQpoLuM,14226
|
|
139
|
-
sycommon/agent/deep_agent.py,sha256=
|
|
140
|
-
sycommon/agent/multi_agent_team.py,sha256=
|
|
141
|
-
sycommon/agent/summarization_utils.py,sha256=
|
|
139
|
+
sycommon/agent/deep_agent.py,sha256=3HkzT08WKGgdgOB3YuxY6KO7sZF4PW0vxdRSZAjFV2k,78863
|
|
140
|
+
sycommon/agent/multi_agent_team.py,sha256=xY6JMr5TTAumTS7EV3Ye6vQL78Bv7wMEM7lzmP8lkeQ,31425
|
|
141
|
+
sycommon/agent/summarization_utils.py,sha256=yCMeg7vT78vYYRgpzR8paE2Yu27pcPQSY1zvWUbIP_I,18899
|
|
142
142
|
sycommon/agent/acp/__init__.py,sha256=vQGMQYAA5-ZaejYmDZ4r4Fklqs17qH3cDXQ5pZlwj-0,1844
|
|
143
143
|
sycommon/agent/acp/client.py,sha256=s-K626b9ierwyEq61gejrW0xOysvrY2PIpYFsFqSHjc,27108
|
|
144
144
|
sycommon/agent/acp/fetch.py,sha256=a6tL13eeGT1KgExgNbdYpzQ54xBqLeTGNTIQmxr3eQ4,6796
|
|
@@ -148,7 +148,7 @@ sycommon/agent/mcp/__init__.py,sha256=iKrdDhIrFsNIkqG_kgcwNe-nOiM6uVfolKv44LfQ-F
|
|
|
148
148
|
sycommon/agent/mcp/models.py,sha256=zda4ho-UTYOJ5oPAZPZ-vxhfDRLghUCsMroHnA2A2QQ,1490
|
|
149
149
|
sycommon/agent/mcp/tool_loader.py,sha256=pwhZoSoZuQVYo6BgfdMoRHlGnRPDRxvOvxEgvUM5q7Q,17818
|
|
150
150
|
sycommon/agent/middleware/sandbox_path_guard.py,sha256=K1SnxA8pDxkCSucOkranwUvC9_8rd3l_-TIR8gMMTK8,8102
|
|
151
|
-
sycommon/agent/middleware/sensitive_guard.py,sha256=
|
|
151
|
+
sycommon/agent/middleware/sensitive_guard.py,sha256=ChUU8twVVwkxt5c3Ymjf5FCrIqHexVR6kGWG70hhuEQ,27986
|
|
152
152
|
sycommon/agent/middleware/sitecustomize.py,sha256=Bt7XFnWV_LJo89l858AGp5VUkXwefpLGmjkCtLe-CMw,4800
|
|
153
153
|
sycommon/agent/middleware/skill_api_whitelist.py,sha256=2qVsrwS6VkkPKHrvjJMNUfBDokdMJkP-mn-V-ZCn7X8,16643
|
|
154
154
|
sycommon/agent/middleware/skill_wl_check.py,sha256=rCJ9F6aWPh8tVQBIPmcq2lKDsfwiJQduwSUku_8kXfs,3952
|
|
@@ -157,7 +157,7 @@ sycommon/agent/sandbox/__init__.py,sha256=jR7LlkD4J4Y6QYyRXQClkwmqDBCCPmycV_hQV9
|
|
|
157
157
|
sycommon/agent/sandbox/consistent_hash.py,sha256=8Jgk-W4NAD2-u5_vKRVlPmql_e0Vy4aNORiXOuzvIrs,7277
|
|
158
158
|
sycommon/agent/sandbox/file_ops.py,sha256=VTK7xduMMWLVTr9JViMnmPeh7s8Ojr8YwYA4ovMZZg0,37138
|
|
159
159
|
sycommon/agent/sandbox/http_sandbox_backend.py,sha256=1Cj_JYdvwP3PFPa_4xFPnrGdt67Wp5iQdoziSzjeqoE,70481
|
|
160
|
-
sycommon/agent/sandbox/minio_sync.py,sha256=
|
|
160
|
+
sycommon/agent/sandbox/minio_sync.py,sha256=z1WNv4CZHn5U0akCnpjnBKCqlq5LhPq4zppDbzJ7YRk,35380
|
|
161
161
|
sycommon/agent/sandbox/sandbox_pool.py,sha256=eMn8sLakCWf90l6ni2-333QM8oBdX1CflV-WzneFp_k,9133
|
|
162
162
|
sycommon/agent/sandbox/sandbox_recovery.py,sha256=X-eDODx1tmGMh_iTngV6e1ppfDBHpTdkPreJusN5MHY,7358
|
|
163
163
|
sycommon/agent/sandbox/session.py,sha256=TjzC3yFC-VaJ75UwCyL26QX4PRTGNNfQae1FKFuOsYI,2365
|
|
@@ -168,7 +168,7 @@ sycommon/auth/oa_crypto.py,sha256=xpY1R1Bj3KLENXB0TuThB6Eku1E9PYjcoSpOdgDmCgc,18
|
|
|
168
168
|
sycommon/auth/oa_service.py,sha256=kLepV9zgqpZoaB73DRPpMA5tJJQjoaDtQPdzBcGXeak,6235
|
|
169
169
|
sycommon/auth/wecom_ldap_service.py,sha256=atVuYs8zL1EfEN5yX9bwHcHpCsB3BdnMnMlR2bbmD6c,33416
|
|
170
170
|
sycommon/config/ACPAgentConfig.py,sha256=paAa0c0gy1RnDDum-Q6m7QLTe7lhz2dsFt4r72QtmWE,3202
|
|
171
|
-
sycommon/config/Config.py,sha256=
|
|
171
|
+
sycommon/config/Config.py,sha256=zbddvm2_y_l3k2zMeAZ0BTEST6Or_kUD7y21RnEy2Kc,7929
|
|
172
172
|
sycommon/config/DatabaseConfig.py,sha256=ILiUuYT9_xJZE2W-RYuC3JCt_YLKc1sbH13-MHIOPhg,804
|
|
173
173
|
sycommon/config/ElasticsearchConfig.py,sha256=fO9ZPMgJxSg1-UyDJ90wO6UvYy-jscwPJsSkXgx9qTU,2308
|
|
174
174
|
sycommon/config/EmbeddingConfig.py,sha256=gPKwiDYbeu1GpdIZXMmgqM7JqBIzCXi0yYuGRLZooMI,362
|
|
@@ -298,6 +298,9 @@ sycommon/tests/test_real_summarization.py,sha256=7B89es7-UwULk-kq9xUiWH1ylXUO3QD
|
|
|
298
298
|
sycommon/tests/test_summarization_config.py,sha256=Ztb-eJXt2NrpBXNp7xST4Cwq4x8DK9pFuC7-bXUUOaE,16860
|
|
299
299
|
sycommon/tests/test_summarization_real.py,sha256=iTwwA_xQd5Zgkn849lu2M0zIHPnMp-3szsIGrg6G9J4,12406
|
|
300
300
|
sycommon/tests/test_summary_leak_e2e.py,sha256=vE5r9QCcZRmUy3YQASwdwCBsVrRcl8vU95sLUyB6u9s,34739
|
|
301
|
+
sycommon/tests/test_upgrade_aio_pika.py,sha256=HIvy0Gs-aNNGX6Q3Y2fwSq2nDODbsWt7kl7-bk3gTk0,6115
|
|
302
|
+
sycommon/tests/test_upgrade_langgraph_stream.py,sha256=alKdaYXDiS2HAhNrkIB5yHKZ6_C79E-T-GBUUo-Lyrk,4785
|
|
303
|
+
sycommon/tests/test_upgrade_verification.py,sha256=EdGkeoFvXGpagpArweMv8Q4IhH3TjYsg-novU8dGh8w,10387
|
|
301
304
|
sycommon/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
302
305
|
sycommon/tools/async_utils.py,sha256=e2Bp8v0LDrnViBIZ3zcCWtm_01w67vG34VhnAyGu9ZM,1015
|
|
303
306
|
sycommon/tools/docs.py,sha256=OPj2ETheuWjXLyaXtaZPbwmJKfJaYXV5s4XMVAUNrms,1607
|
|
@@ -308,8 +311,8 @@ sycommon/tools/syemail.py,sha256=BDFhgf7WDOQeTcjxJEQdu0dQhnHFPO_p3eI0-Ni3LhQ,561
|
|
|
308
311
|
sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
|
|
309
312
|
sycommon/xxljob/__init__.py,sha256=7eoBlQxv-B39IfRSCY2bkqdGYs1QRe1umAWd88VMEEM,86
|
|
310
313
|
sycommon/xxljob/xxljob_service.py,sha256=1yifwIBNGsCIxLnQjHKiBlbsigc_zvPH-dMTZcNxe-Q,7649
|
|
311
|
-
sycommon_python_lib-0.2.
|
|
312
|
-
sycommon_python_lib-0.2.
|
|
313
|
-
sycommon_python_lib-0.2.
|
|
314
|
-
sycommon_python_lib-0.2.
|
|
315
|
-
sycommon_python_lib-0.2.
|
|
314
|
+
sycommon_python_lib-0.2.7a33.dist-info/METADATA,sha256=46OEQuDknEAfGWnldU8AlXjHiXCldxKqj_ZFjbcQ_Ac,7963
|
|
315
|
+
sycommon_python_lib-0.2.7a33.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
316
|
+
sycommon_python_lib-0.2.7a33.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
|
|
317
|
+
sycommon_python_lib-0.2.7a33.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
|
|
318
|
+
sycommon_python_lib-0.2.7a33.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{sycommon_python_lib-0.2.7a31.dist-info → sycommon_python_lib-0.2.7a33.dist-info}/top_level.txt
RENAMED
|
File without changes
|