Skip to content

Commit b53ba50

Browse files
committed
fix(session): cap auto-compaction continue at one turn (anomalyco#15533)
Port of upstream PR anomalyco#15532 adapted for the pwrcode fork's plugin-trigger structure. Without this, claude-via-copilot sessions enter an infinite loop: agent ends turn (finish='stop') -> context overflow -> auto-compaction -> synthetic 'Continue if you have next steps' injected -> agent generates same summary block -> overflow -> repeat The synthetic continue is now only injected when both hold: 1. The last non-summary assistant was mid-tool-use (finish in 'tool-calls' / 'unknown' / unset). 2. We haven't already injected a synthetic continue since the user's last real text message — caps the loop at exactly one continuation per real user turn. Hit reproducibly with github-copilot/claude-opus-4.7 + oh-my-opencode-slim orchestrator, where the orchestrator's templated Goal/Done/Next-Steps output reads as an active task to the post-compaction continue prompt.
1 parent 168f2bb commit b53ba50

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,40 @@ export const layer = Layer.effect(
506506
}
507507

508508
if (!replay) {
509-
const info = yield* provider.getProvider(userMessage.model.providerID)
509+
// Guard against the auto-compaction infinite loop reported upstream as
510+
// anomalyco/opencode#15533: if the assistant naturally ended its turn
511+
// (finish === "stop") and we still inject a synthetic "Continue…",
512+
// the agent responds, the context overflows again, compaction fires
513+
// again, and we loop. Affects Claude via Copilot especially.
514+
//
515+
// Only inject when ALL hold:
516+
// 1. The last non-summary assistant was mid-tool-use
517+
// (finish in "tool-calls" / "unknown" / unset).
518+
// 2. We haven't already injected a synthetic continue since the
519+
// user's last real text message — caps the loop at one turn.
520+
const lastNonSummaryAssistant = [...input.messages]
521+
.reverse()
522+
.find((m) => m.info.role === "assistant" && !(m.info as MessageV2.Assistant).summary)?.info as
523+
| MessageV2.Assistant
524+
| undefined
525+
526+
const wasUsingTools =
527+
!lastNonSummaryAssistant?.finish ||
528+
lastNonSummaryAssistant.finish === "tool-calls" ||
529+
lastNonSummaryAssistant.finish === "unknown"
530+
531+
const lastUserWithText = [...input.messages]
532+
.reverse()
533+
.find((m) => m.info.role === "user" && m.parts.some((p) => p.type === "text"))
534+
const alreadyContinued = lastUserWithText?.parts.some(
535+
(p) => p.type === "text" && "synthetic" in p && (p as MessageV2.TextPart).synthetic === true,
536+
)
537+
538+
if (!wasUsingTools || alreadyContinued) {
539+
// Skip the synthetic continue — let the session loop exit and wait
540+
// for real user input.
541+
} else {
542+
const info = yield* provider.getProvider(userMessage.model.providerID)
510543
if (
511544
(yield* plugin.trigger(
512545
"experimental.compaction.autocontinue",
@@ -557,6 +590,7 @@ export const layer = Layer.effect(
557590
},
558591
})
559592
}
593+
}
560594
}
561595
}
562596

0 commit comments

Comments
 (0)