fix: feat: agents flush context to scratch file before compaction (#262)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
openhands 2026-03-20 20:12:45 +00:00
parent edfdae9ad8
commit 7199bbf9b5
7 changed files with 138 additions and 16 deletions

View file

@ -126,6 +126,37 @@ formula_phase_callback() {
esac
}
# ── Scratch file helpers (compaction survival) ────────────────────────────
# build_scratch_instruction SCRATCH_FILE
# Returns a prompt block instructing Claude to periodically flush context
# to a scratch file so understanding survives context compaction.
build_scratch_instruction() {
local scratch_file="$1"
cat <<_SCRATCH_EOF_
## Context scratch file (compaction survival)
Periodically (every 10-15 tool calls), write a summary of:
- What you have discovered so far
- Decisions made and why
- What remains to do
to: ${scratch_file}
If you find this file exists when you start, read it first — it is your previous context.
This file is ephemeral — not evidence or permanent memory, just a compaction survival mechanism.
_SCRATCH_EOF_
}
# read_scratch_context SCRATCH_FILE
# If the scratch file exists, returns a context block for prompt injection.
# Returns empty string if the file does not exist.
read_scratch_context() {
local scratch_file="$1"
if [ -f "$scratch_file" ]; then
printf '## Previous context (from scratch file)\n%s\n' "$(cat "$scratch_file")"
fi
}
# ── Prompt + monitor helpers ──────────────────────────────────────────────
# build_prompt_footer [EXTRA_API_LINES]