fix: resolve PrePrompt Hook env var crash and increase Mem0 pool size (#24)

1. Fix "all environment values must be bytes or str" error in hook execution
   by ensuring all env values are converted to str (getattr may return None
   when attribute exists but is None). Also sanitize shell_env values.

2. Increase MEM0_POOL_SIZE default from 20 to 50 to address "connection pool
   exhausted" errors under high concurrency.

Fixes: sparticleinc/felo-mygpt#2519

Co-authored-by: zhuchao <zhuchaowe@163.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
autobee-sparticle 2026-04-01 22:04:31 +09:00 committed by GitHub
parent 89a9d81892
commit 213e541697
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 10 deletions

View File

@ -165,27 +165,30 @@ async def _execute_command(skill_path: str, command: str, hook_type: str, config
"""
try:
# 设置环境变量,传递给子进程
# 注意subprocess 要求所有 env 值必须是 str 类型,
# getattr 可能返回 None属性存在但值为 None需要确保转换为 str
env = os.environ.copy()
env['ASSISTANT_ID'] = getattr(config, 'bot_id', '')
env['USER_IDENTIFIER'] = getattr(config, 'user_identifier', '')
env['TRACE_ID'] = getattr(config, 'trace_id', '')
env['SESSION_ID'] = getattr(config, 'session_id', '')
env['LANGUAGE'] = getattr(config, 'language', '')
env['ASSISTANT_ID'] = str(getattr(config, 'bot_id', '') or '')
env['USER_IDENTIFIER'] = str(getattr(config, 'user_identifier', '') or '')
env['TRACE_ID'] = str(getattr(config, 'trace_id', '') or '')
env['SESSION_ID'] = str(getattr(config, 'session_id', '') or '')
env['LANGUAGE'] = str(getattr(config, 'language', '') or '')
env['HOOK_TYPE'] = hook_type
# 合并 config 中的自定义 shell 环境变量
shell_env = getattr(config, 'shell_env', None)
if shell_env:
env.update(shell_env)
# 确保所有自定义环境变量值也是字符串
env.update({k: str(v) if v is not None else '' for k, v in shell_env.items()})
# 对于 PreSave传递 content
if hook_type == 'PreSave':
env['CONTENT'] = kwargs.get('content', '')
env['ROLE'] = kwargs.get('role', '')
env['CONTENT'] = str(kwargs.get('content', '') or '')
env['ROLE'] = str(kwargs.get('role', '') or '')
# 对于 PostAgent传递 response
if hook_type == 'PostAgent':
env['RESPONSE'] = kwargs.get('response', '')
env['RESPONSE'] = str(kwargs.get('response', '') or '')
metadata = kwargs.get('metadata', {})
env['METADATA'] = json.dumps(metadata) if metadata else ''

View File

@ -57,7 +57,7 @@ CHECKPOINT_DB_URL = os.getenv("CHECKPOINT_DB_URL", "postgresql://moshui:@localho
# 连接池大小
# 同时可以持有的最大连接数
CHECKPOINT_POOL_SIZE = int(os.getenv("CHECKPOINT_POOL_SIZE", "20"))
MEM0_POOL_SIZE = int(os.getenv("MEM0_POOL_SIZE", "20"))
MEM0_POOL_SIZE = int(os.getenv("MEM0_POOL_SIZE", "50"))
# Checkpoint 自动清理配置
# 是否启用自动清理旧 session