Skip to content

Compaction persists a length-truncated summary, and the resumed turn degenerates into a closing-tag loop #78

Closed

Description

@ernestrc

Summary

When the compaction summary stream ends with FinishReasonLength, agent.Summarize
accepts the partial output anyway. cleanSummary cannot match the <summary>...</summary>
pair because the closing tag never arrived, so the raw fragment, including the dangling
opening tag, is written verbatim into the post-compaction resume message and the original
conversation is archived and replaced. On the next turn the model is handed an
unterminated XML document cut mid-sentence, and it tries to repair it instead of resuming
the task.

Observed failure

Seen on a long auto-compacted session. The archived dialogue contains no compact tool
call and its last message is a plain tool result, confirming the AutoCompactRatio path.

  • Persisted resume message: 7022 chars, contains <summary>, does not contain
    </summary>, and ends mid-sentence inside an unclosed fenced code block.
  • The following assistant turn opened with (continued from summary), closed the code
    fence, wrote the remaining summary sections instead of doing any work, and then emitted
    </parameter>\n</invoke> 22 times before finally closing with </summary>.

The visible symptom is a wall of stray closing tags in the chat transcript. The real
damage is that a turn is wasted continuing a document rather than executing the task, and
the compacted dialogue permanently carries a malformed prompt.

Where

cmd/rune-agent/agent/agent.go:1862-1882. The stream loop only consumes EventTextDelta
and EventStreamError. llmapi.Event.DoneData.FinishReason is ignored, and the only
guard is:

summary := cleanSummary(sb.String())
if summary == "" {
	return "", fmt.Errorf("LLM returned an empty summary")
}

cleanSummary (agent.go:1832) uses reSummary = (?s)<summary>(.*?)</summary>. With no
closing tag there is no match, so the text, opening tag included, passes through as-is.

CompactDialogue (agent.go:1893) then calls store.ArchiveAndReplace, so the truncated
summary replaces the real conversation.

Note that Rune already recognises this finish reason elsewhere: agent.go:933,
cmd/rune-agent/extension/handler.go:1961 and handler.go:2391, which surfaces
truncatedTurnHint (handler.go:1918) for truncated chat turns. The compaction path has
no equivalent.

Proposed fix

  1. Detect it. In Summarize, capture ev.DoneData.FinishReason and treat
    llmapi.FinishReasonLength as a failure rather than a successful summary.
  2. Do not destroy the conversation on failure. Summarize returning an error already
    makes CompactDialogue bail before ArchiveAndReplace (agent.go:1921-1924), and
    Agent.compact (agent.go:1633) emits EventError and falls through. Confirm this
    leaves the dialogue intact and the user informed, with a message pointing at
    /max_tokens, matching the guidance in truncatedTurnHint.
  3. Harden cleanSummary. Reject or repair an unbalanced <summary> block, and strip a
    trailing run of stray tool-call XML (</parameter> and </invoke>) so a partially
    degenerate response cannot leak into the resume prompt.
  4. Optional follow-up: retry once with a larger budget before surfacing the error.

This is complementary to #77, which removes the most common trigger. This issue is the safety
net for when the summary exceeds whatever budget is configured.

Tests

Per the repo bug-fix policy, start with failing tests in
cmd/rune-agent/agent/agent_test.go:

  • A fake stream that emits partial text then DoneData{FinishReason: llmapi.FinishReasonLength}.
    Summarize must return an error, and CompactDialogue must not call
    ArchiveAndReplace (dialogue unchanged, no archived ID).
  • Table-driven cleanSummary cases: an unbalanced <summary> with no closing tag, a valid
    summary followed by a </parameter></invoke> run, and a well-formed summary (unchanged).

Existing fakes are in place. See TestCompactDialogueNormalizesMessagesBeforeSummarizing
(agent_test.go:4360) and TestSummarizeEmptySummaryReturnsError (agent_test.go:4334).

Verification

go test -race -run 'TestSummarize|TestCompact|TestCleanSummary' ./cmd/rune-agent/agent/
make generate && make lint && make test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions