Repository navigation
Conversation
`.env.example` 里每个可选开关都写成 `KEY=value # 中文说明`,文件头却写着
"复制为 .env 并填入实际值"。用户照做并取消注释任意一行后,旧解析器只做
`strip()`,注释整段被当成值的一部分:
MEMORY_PIPELINE_TRACE_ENABLED=true # pipeline_logs 表(...)
-> "true # pipeline_logs 表(...)"
把模板整份取消注释后,91 个键里有 49 个带着注释,后果分三档:
* 静默失效:`_get_env_bool`/`_get_env_float` 落回默认值,或
`.lower() in ("true", ...)` 恒为 False —— 用户刚打开的开关其实没开;
* 静默降级:`getattr(logging, "DEBUG # ...", logging.INFO)` → INFO;
* 直接报错:`cross_domain_sweeper` 模块级的 `float(os.getenv(...))` 抛
ValueError,`client.__init__` 的 `mode not in VALID_MODES` 抛
ValueError("Invalid mode 'pro # lite | pro | ultra'")。
按 python-dotenv 的约定解析:整行 `#` 忽略;空白后的 `#` 起注释作用,
`value#x` 里的 `#` 仍是值;引号内的 `#` 原样保留;`KEY= # 说明` 视为空值
(`.env.example` 里空默认值就是这么写的),但 `KEY=#secret` 紧贴等号的
`#` 仍算值。
tests/conftest.py 里那份手写的 `.env` 解析是同一段逻辑的副本,一并改成调用
`parse_dotenv`,避免两边以后再次漂移。
This was referenced Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
这个 PR 修什么
_load_dotenv()只做strip(),不剥行尾注释。而.env.example是行尾注释写法,文件头又写着"复制为 .env 并填入实际值",于是取消注释的每一行都把注释带进了值里。把模板整份取消注释后解析,91 个键里 49 个被污染。Fixes #502
三类后果(前两档静默)
MEMORY_PIPELINE_TRACE_ENABLED=true"true # pipeline_logs 表(...)".lower() in ("true",...)为False—— trace 静默关闭,Inspector 恒空MEMORY_LOG_LEVEL=DEBUG"DEBUG # DEBUG | INFO | ..."getattr(logging, ..., logging.INFO)落回INFOMEMORY_DATA_DIR=~/.hy_memory"~/.hy_memory # 数据存储根目录"MEMORY_MODE=pro"pro # lite | pro | ultra"client.py的mode not in VALID_MODES→ValueErrorMEMORY_CROSS_DOMAIN_BEH_THRESHOLD=0.70"0.70 # 行为相似度碰撞阈值"float()→ValueError改法
hy_memory/config.py新增parse_dotenv(text),按python-dotenv的约定解析:#注释忽略;#起注释作用 ——MEMORY_MYSQL_PASSWORD=#p@ss这种紧贴等号的#仍是值的一部分;#原样保留:MEMORY_LLM_API_KEY="sk-abc # def"→sk-abc # def;KEY= # 说明视为空值(.env.example里空默认值正是这么写的),但KEY=#secret仍算值;.env.example在仓库里是 CRLF)。_load_dotenv()改为读全文 →parse_dotenv→ 只写入尚未存在的变量(保持原来"不覆盖已存在的环境变量"的语义)。tests/conftest.py里有一份同一逻辑的手写副本_load_env(),同样只strip();改成调用parse_dotenv,避免这两处以后再次漂移。测试
新增
tests/test_dotenv_parsing.py(14 个用例):#、引号内的#、只有注释的空值、整行注释/空行、CRLF;.env.example整份当输入:取消注释后每个值都不许残留#或中文说明;*_ENABLED/_ENABLE必须是可识别的开关字面量;MEMORY_PIPELINE_TRACE_ENABLED/MEMORY_ENABLE_GRAPH/MEMORY_LLM_MAX_TOKENS/MEMORY_CROSS_DOMAIN_*_THRESHOLD等要能被_get_env_bool/_get_env_int/_get_env_float读回模板上写的值。开关用例故意把 default 设成相反值,只用 default 兜底是蒙不过去的。红/绿对照(真实仓库,
uv run pytest tests/test_dotenv_parsing.py):旧语义下仍通过的那 4 个用例是刻意留的:
value#x的#不能被误切、整行注释/空行必须跳过、以及模板里两行本来就没有行尾注释的键(MEMORY_LLM_MAX_TOKENS、MEMORY_EMBEDDING_DIMS)。CI 的
sdk作业跑的是uv run pytest tests/ -q(整个根仓测试集),已通过。兼容性
parse_dotenv是新加的公开函数。.env复制出去、开关"打开了却不生效"的用户,升级后不用改文件即生效。KEY= #secret(等号后先一个空格再#)现在解析为空值。这是"值为空、后面跟了说明"的写法,.env.example有 7 处是这种;真正以#开头的密码要保持KEY=#secret紧贴等号,或加引号。