Context compaction loses inline scripts executed via Bash heredocs

Resolved 💬 3 comments Opened Jan 27, 2026 by manuel-j-glynias Closed Jan 31, 2026

Description

During context compaction/summarization, inline Python scripts executed via Bash heredocs (python3 << 'EOF') are lost. The summary references the script's existence and purpose but doesn't preserve the actual code.

Steps to Reproduce

  1. In a long session, create and execute a substantial Python script via Bash heredoc:

``bash
python3 << 'EOF'
import pandas as pd
# ... 150+ lines of parsing logic ...
df.to_csv('output.tsv')
EOF
``

  1. Continue the session until context compaction occurs
  1. After compaction, the summary mentions "Built improved parser using capital-letter city detection" but the actual code is gone

Expected Behavior

The compaction summary should either:

  • Preserve substantial code blocks (especially those that took significant iteration to develop)
  • Write them to a file before compaction
  • Include them in the summary in a recoverable format

Actual Behavior

  • Summary says things like "Generated tyler_customers_v2.tsv with new format" but the script that generated it is lost
  • Had to manually recover the code by parsing the raw JSONL transcript file at ~/.claude/projects/.../[session-id].jsonl

Impact

  • Significant work lost (150+ line parser with multiple iterations/refinements)
  • User had to interrupt the session to recover code
  • Recovery required parsing internal JSONL format which users shouldn't need to do

Workaround

The code was recoverable from the JSONL transcript by searching for tool_use blocks containing the function definitions:

import json
with open('session.jsonl', 'r') as f:
    for line in f:
        obj = json.loads(line)
        # Search for Bash tool calls containing the script

Suggestion

For Bash tool calls containing heredocs with substantial code (e.g., > 20 lines), consider:

  1. Auto-saving to a file in the working directory before compaction
  2. Including the full code in the compaction summary
  3. Flagging these as "preserve" during summarization

Environment

  • Claude Code CLI
  • Model: claude-opus-4-5-20250101
  • Session involved iterative development of a customer data parser

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗