mpfaffenberger / mpfaffenberger/code_puppy

Sub-agent session history persisted via pickle: silent corruption fallback, version fragility, and avoidable pickle.load risk

Open
#439 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
814
Forks
278
Avg merge
2d 5h
Merged PRs (30d)
76

Description

Files:

  • code_puppy/tools/agent_tools.py (~lines 120-185): _save_session_history / _load_session_history
  • code_puppy/tools/subagent_invocation.py uses the same .pkl session files

Severity: Medium (robustness; low-grade security footnote)

Sub-agent session history is persisted with pickle:

with open(tmp_pkl, "wb") as f:
    pickle.dump(message_history, f)
...
with open(pkl_path, "rb") as f:
    return pickle.load(f)

Problems:

  1. Errors pass silently: _load_session_history catches bare Exception and returns [], so a corrupted/incompatible session silently restarts the sub-agent with no memory and no warning (refusing-to-guess violation). At minimum emit_warning the load failure.
  2. Version fragility: pydantic-ai message classes change between releases; pickled histories break on upgrade with no migration path. JSON serialization via pydantic_core.to_jsonable_python / ModelMessagesTypeAdapter (which pydantic-ai provides specifically for message history persistence) is stable and human-inspectable.
  3. pickle.load on a shared-location file (~/.code_puppy/...) executes arbitrary code if anything else writes those files. Low practical risk (local user dir) but trivially avoided by using the JSON adapters.
  4. The .tmp rename pattern here is fine, but the companion .txt metadata uses a read-modify-write without locking - concurrent parallel sub-agents on the same session can interleave.

Suggested fix: switch to ModelMessagesTypeAdapter.dump_json() / validate_json() for session history, log load failures, and keep a one-release fallback that reads legacy .pkl files.

Filed by Zen Reviewer B (code-puppy-60635a)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with _save_session_history and _load_session_history in code_puppy/tools/agent_tools.py, then inspect how code_puppy/tools/subagent_invocation.py uses the .pkl and .txt session files. Use the pydantic-ai message adapters named in the issue, preserve a legacy .pkl fallback, and ensure load failures emit a warning; done means session history remains recoverable across versions without pickle as the primary format.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.