#3343·CowAgent

env_config writes a multi-line value as several .env entries, so the key is truncated and extra variables appear

Author: LesereingrapeCreated Sep 29, 2026Updated Sep 29, 2026

OS / 系统版本

Windows 11 (also reproducible on Linux; the format assumption is platform-independent)

CowAgent 版本 / Branch

master @ 08f64f70

问题描述 / Description

env_config stores one entry per line in ~/.cow/.env, but set accepts a value containing a line break and writes it unchanged. The file that comes back up is not the entry that went in: the key is truncated at the break, and every remaining line is parsed as its own assignment — so setting one variable creates two, and the stored API key is no longer the key that was set.

Three readers make the same one-line assumption independently: EnvConfig._read_env_file, and python-dotenv via bridge/agent_initializer.py:818 (load_dotenv) and agent/tools/bash/bash.py:205 (dotenv_values, whose values are also what _redact_progress strips out of tool output). _write_env_file (line 124) quotes nothing and checks nothing.

相关日志 / Logs

[EnvConfig] Set OPENAI_API_KEY=sk-liv***evil      # status: success

重现示例 / Repro

python
import os, tempfile
from agent.tools.env_config.env_config import EnvConfig

d = tempfile.mkdtemp()
t = EnvConfig(config={})
t.env_dir = d
t.env_path = os.path.join(d, ".env")
t._ensure_env_file()

print(t.execute({"action": "set", "key": "OPENAI_API_KEY",
                 "value": "sk-live-secret\nPROVIDER=evil"}).status)   # success
print(open(t.env_path, encoding="utf-8").read())
# # Environment variables for agent skills
# # Auto-managed by env_config tool
#
# OPENAI_API_KEY=sk-live-secret
# PROVIDER=evil
print(t._read_env_file())          # {'OPENAI_API_KEY': 'sk-live-secret', 'PROVIDER': 'evil'}
print(os.environ.get("PROVIDER"))  # 'evil' after _reload_env()

PROVIDER is never removed again unless the caller happens to notice it: a later list shows it as a configured variable, and bridge/agent_initializer.py:1246 rewrites every entry it read back out of the file, so the extra variable survives a restart.

The same holds for a key that is not a single field: set(key="A=INJECTED", value="1") writes A=INJECTED=1, which reads back as {"A": "INJECTED=1"} — and on the platform measured, the call still reports failure (ValueError: illegal environment variable name from os.environ[key] = value at line 187) after _write_env_file has already rewritten the file.

Nothing in the tool is the right place to put a multi-line secret, so the fix is not "learn to quote it" — it is to stop writing an entry that the format cannot represent, and say so in the tool result.