[PR #12970] fix: auto-resume agent loop after context compaction #14457

Open
opened 2026-02-16 18:19:14 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12970

State: open
Merged: No


Summary

Fixes #12780

After context compaction triggers, the agent sometimes stops and waits for the user to say "continue" instead of automatically resuming. The behavior is inconsistent because it depends on the processor's return value during compaction, which can vary based on response size and model behavior.

Root cause (two issues):

  1. SessionCompaction.process() gated the synthetic "Continue" user message on result === "continue". If the processor returned anything else (e.g. "compact" when the compaction response itself was large enough to trigger overflow detection), the synthetic message was skipped. Without this message, the main loop's exit condition (lastUser.id < lastAssistant.id) would see the compaction assistant as newer than the last user message and break out of the loop, stopping the agent.

  2. The main agent loop exit condition did not exclude compaction summary messages. A compaction assistant message with a non-tool-call finish reason (e.g. "stop", "end_turn") would trigger the exit even though the agent should continue processing after compaction.

Fix:

  • Move the error check (processor.message.error) before the synthetic message creation, and remove the result === "continue" gate. The synthetic "Continue" user message is now always created when auto=true and there is no error.
  • Add !lastAssistant.summary to the loop exit condition so the loop never breaks on a compaction summary message.

Related: #11301, #3053, #6068, #8394

Test plan

  • Trigger context compaction by running a long session that exceeds the model's context window
  • Verify the agent automatically resumes after compaction without requiring user to type "continue"
  • Verify the agent still correctly stops when compaction encounters an error
  • Verify manual compaction (auto: false) still works as before (no synthetic continue message)
  • Existing compaction tests pass
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12970 **State:** open **Merged:** No --- ## Summary Fixes #12780 After context compaction triggers, the agent sometimes stops and waits for the user to say "continue" instead of automatically resuming. The behavior is inconsistent because it depends on the processor's return value during compaction, which can vary based on response size and model behavior. **Root cause (two issues):** 1. `SessionCompaction.process()` gated the synthetic "Continue" user message on `result === "continue"`. If the processor returned anything else (e.g. `"compact"` when the compaction response itself was large enough to trigger overflow detection), the synthetic message was skipped. Without this message, the main loop's exit condition (`lastUser.id < lastAssistant.id`) would see the compaction assistant as newer than the last user message and break out of the loop, stopping the agent. 2. The main agent loop exit condition did not exclude compaction summary messages. A compaction assistant message with a non-tool-call finish reason (e.g. `"stop"`, `"end_turn"`) would trigger the exit even though the agent should continue processing after compaction. **Fix:** - Move the error check (`processor.message.error`) before the synthetic message creation, and remove the `result === "continue"` gate. The synthetic "Continue" user message is now always created when `auto=true` and there is no error. - Add `!lastAssistant.summary` to the loop exit condition so the loop never breaks on a compaction summary message. Related: #11301, #3053, #6068, #8394 ## Test plan - [ ] Trigger context compaction by running a long session that exceeds the model's context window - [ ] Verify the agent automatically resumes after compaction without requiring user to type "continue" - [ ] Verify the agent still correctly stops when compaction encounters an error - [ ] Verify manual compaction (`auto: false`) still works as before (no synthetic continue message) - [ ] Existing compaction tests pass
yindo added the pull-request label 2026-02-16 18:19:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14457