eca icon indicating copy to clipboard operation
eca copied to clipboard

Compaction injects a stale summary when the model does not call `compact_chat`

Open brdloush opened this issue 2 months ago • 1 comments

Summary

When the LLM answers the compact prompt with plain text instead of calling the compact_chat tool, ECA still completes the compaction and injects the chat's previous :last-summary as "The conversation was compacted/summarized, consider this summary:". The new summary, written as text, is discarded. The chat then continues from a summary of an older state, with no warning to the user or the model.

In a chat that was never compacted before, the injected summary is empty instead.

Environment

  • ECA 0.161.2 (also present on master at edca8608a)
  • Model: a local Qwen3.8-Flash-Next over an OpenAI-compatible endpoint (openai-chat API); probably reproducible with any model that sometimes ignores the tool instruction
  • Custom compact prompt (prompts.compact), which says "Call eca__compact_chat with the summary as its argument"; the default compact.md says the same
  • Auto-compaction during a running turn (autoCompactPercentage: 85)

What happened

One long chat, compacted three times by auto-compaction, same model and same compact prompt each time:

  1. Compactions 1 and 2: the model called compact_chat; the injected summary matched the tool argument. OK.
  2. Compaction 3 (mid-turn, right after a long subagent result): the model's reasoning ended with "Now, writing the summary." and it answered with the full summary (~12.8k chars) as a normal assistant message. No tool call, no malformed tool-call markup.
  3. ECA appended compact_marker and then injected the summary stored by compaction 2. That summary described a task from hours earlier, with obsolete state and instructions. The text answer from step 2 was not used.
  4. ECA resumed the turn ("Continue with the task. The previous user request was: …"), so the agent continued with the stale summary as its only memory of the session.

The persisted chat shows: assistant message with the new summary → compact_marker → user message whose text equals the chat's last-summary field (the old one).

Cause in the code

  • compact_chat is the only place that stores a summary: it sets :last-summary and :compact-done? true (src/eca/features/tools/chat.clj, compact-chat).
  • A mid-turn compaction finishes through compact-finished-side-effect! → lifecycle/complete-compact! → shared/compact-side-effect! (src/eca/features/chat.clj, trigger-compact!; src/eca/features/chat/lifecycle.clj, complete-compact!; src/eca/shared.clj, compact-side-effect!).
  • compact-side-effect! always reads (get-in db [:chats chat-id :last-summary]) and injects it. Nothing checks that compact_chat was called during this compaction; :compact-done? is only used to skip postRequest hooks (chat.clj, on-tools-called).
  • The finish side effect runs however the compact prompt ends, also when it ends with a text-only answer.

Steps to reproduce

  1. Use a model (or a fake OpenAI-compatible server) that answers the compact prompt with plain text and no tool call.
  2. In a chat that was compacted once before (so :last-summary holds summary A), trigger a second compaction (/compact or auto-compaction).
  3. The model answers with summary B as text.

Expected: the chat continues with summary B, or the compaction fails visibly. Actual: the chat continues with summary A (or an empty summary, if there was no earlier compaction). Nothing tells the user.

Suggested fixes

Any of these would prevent the silent stale summary:

  1. Detect it: clear :last-summary (or record the compaction's start) before sending the compact prompt, and in compact-side-effect! treat "no compact_chat call in this compaction" as a failure.
  2. Recover: when the compact turn ends without the tool call, re-prompt once ("call eca__compact_chat with the summary you just wrote"), or use the final assistant text as the summary.
  3. Fail loudly: if neither works, do not append the marker; keep the history, and show a system message such as "Compaction failed: the model did not call compact_chat".

A test with a fake LLM that answers the compact prompt with text only would cover this.

Workaround

None on the user side, except watching for the problem: after a compaction, check that the injected summary describes the current work. Hooks cannot fix it: postCompact runs after the summary was chosen. A postCompact hook can only add context, for example re-inject the current task state from a file.

brdloush avatar Oct 04 '26 16:10 brdloush

Verified a52293c9 on a local build: five auto-compactions inside one turn, from one typed prompt.

The rig

A custom agent that cannot take shortcuts:

"anvil": {
  "mode": "primary",
  "autoCompactPercentage": 25,
  "autoCompactMaxPerTurn": 5,
  "disabledTools": ["shell_command", "grep", "directory_tree", "bg_job",
                    "task", "spawn_agent", "web_search", "web_fetch"]
}

Corpus: 40 text files of ~38 KB, each seeded with a different number of FLAG lines. Task: read every file, count its FLAG lines, append the count to COUNTS.txt.

The file size is the whole trick. ECA caps a tool result at 2000 lines / 50 KB. At 38 KB a file fits under the cap, so one read_file returns all of it and line_offset/limit buy the agent nothing — each read costs ~9.5k tokens whether it wants it or not. Earlier attempts used 185 KB shards and produced zero compactions: the agent paged them with limit, or with head until I disabled the shell. Asking an agent in the prompt not to be efficient does not work. Taking the tools away does.

Result

All five inside the turn that started 08:54:05. Sampled every 30s, so the "before" figure is the last sample taken while the count still stood at the previous value:

# seen by context before context after
1 ~08:57 — —
2 ~08:59 — —
3 09:00:35 — 14,232
4 09:02:08 68,938 14,349
5 09:04:11 69,297 14,229

(Sampling started after #1 and #2 had already passed; the refill between samples is visible as 14,349 -> 32,623 -> 51,000 -> 69,297, about one 9.5k file read every 15 seconds.)

Ten minutes, one turn. The sawtooth is clean and repeatable: trigger at ~65.5k (25% of 262,144), floor at ~14.2k, five times over. The post-compaction guard releases reliably — this is not a one-off. The run stopped compacting at exactly five because that is what autoCompactMaxPerTurn was set to.

What follows the cap is the documented behaviour, and I report it only so the end of the run is not mistaken for a fault in the fix. auto-compact-suppressed? holds for the rest of the turn once the cap is reached, so the turn carried on with no further in-turn compaction and the context simply grew. It ran well past ECA's configured 262,144 and ended when the model server refused the next request at its own 409,600 limit. That is the cap working as written, not the compaction loop failing.

The work also stayed correct across all five compactions. Every count the agent wrote matched ground truth, including the ones taken after the fifth compaction, so the compacted context carried enough to keep answering correctly.

brdloush avatar Oct 25 '26 08:10 brdloush