[Bug] read_core_file has no size cap while write_core_file caps at 1 MiB
English is recommended so global developers can help. 推荐使用英文提交,谢谢 ❤️
- I'm on the latest version and searched existing issues (incl. closed) — no duplicate.
Environment
Version: current master (commit 08f64f7)
OS: any — platform-independent
Python: 3.11 / 3.13 (CI matrix)
Install: any
What happened?
AgentAdminService.read_core_file() reads an Agent's core file with no size limit, while write_core_file() refuses anything over 1 MiB.
MAX_CORE_FILE_BYTES = 1024 * 1024 # 1 MiB
def write_core_file(...):
raw = content.encode("utf-8")
if len(raw) > MAX_CORE_FILE_BYTES:
raise AgentAdminError("core file exceeds 1 MiB") # write side: capped
def read_core_file(self, agent_id, filename):
path = self._core_path(agent_id, filename)
raw = path.read_bytes() if path.exists() else b"" # read side: uncapped
return {..., "content": raw.decode("utf-8"), ...}So the API had a write limit and no read limit. The cap is not what keeps the file small: the Agent writes AGENT.md / MEMORY.md / USER.md / RULE.md itself through its own write and edit tools, which have no such limit, and MEMORY.md grows on its own as conversations are summarised into it.
Steps to reproduce
- Let the Agent accumulate a large
MEMORY.md(or write one directly):$ python -c "open('cow/memory/MEMORY.md','a').write('x' * (2*1024*1024))" - Open the Agent's core file in the web console / desktop app, which calls
GET /api/agents/core-file.
Expected: the same core file exceeds 1 MiB refusal the write side gives, so the
console can explain the problem.
Actual: the whole 2 MiB (or larger) file is read into memory and returned as
content inside a JSON response.
Impact
- A multi-megabyte string is materialised per request, and the response is held in memory end to end. A core file in the tens or hundreds of megabytes turns every console open of that file into a large allocation, repeated per request.
- The read succeeds where the write would have been refused, so the console can display a file the API itself considers too large to manage.
- Related, same line:
raw.decode("utf-8")is unguarded, so a core file that has been replaced with binary content raises an uncaughtUnicodeDecodeErrorinstead of a 4xx. Left out of the suggested fix to keep it focused, but worth handling together.
Suggested fix
Enforce the same constant on the read side and raise AgentAdminError, so it
surfaces through the existing error path rather than as an OOM or a 500:
if path.exists() and path.stat().st_size > MAX_CORE_FILE_BYTES:
raise AgentAdminError("core file exceeds 1 MiB")Using stat().st_size rather than reading first keeps the check cheap.
Logs
Not applicable — reproducible with a large core file and the read_core_file API path.
Source: zhayujie/CowAgent