Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,25 @@ export const layer: Layer.Layer<
model,
})

// Handle overflow with recovery attempt
// See: https://github.com/XiaomiMiMo/MiMo-Code/issues/1221
// Ported from OpenCode commit 820c984d
if (result === "overflow") {
log.warn("context.overflow.detected", {
sessionID: input.sessionID,
messageCount: messages.length,
attemptingRecovery: !replay, // Only attempt recovery on first overflow
})

// If we haven't tried replay yet, attempt to recover by stripping more content
if (!replay) {
log.info("context.overflow.attempting.recovery", { sessionID: input.sessionID })
// Return "text-repeat" to trigger retry with overflow flag
// This allows the caller to strip media and try again
return "text-repeat"
}

// If we already tried replay (stripped media) and still overflow, it's truly too large
processor.message.error = new MessageV2.ContextOverflowError({
message: replay
? "Conversation history too large to compact - exceeds model context limit"
Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,21 @@ export const layer: Layer.Layer<
const halt = Effect.fn("SessionProcessor.halt")(function* (e: unknown) {
slog.error("process", { error: errorMessage(e), stack: e instanceof Error ? e.stack : undefined })
const error = parse(e)

// Enhanced context overflow handling with recovery
// See: https://github.com/XiaomiMiMo/MiMo-Code/issues/1221
// Ported from OpenCode commit 820c984d
if (MessageV2.ContextOverflowError.isInstance(error)) {
slog.warn("context.overflow.detected", {
sessionID: ctx.sessionID,
attemptingRecovery: true,
message: error.message,
})
ctx.needsOverflowHandling = true
yield* bus.publish(Session.Event.Error, { sessionID: ctx.sessionID, error })
return
}

ctx.assistantMessage.error = error
yield* bus.publish(Session.Event.Error, {
sessionID: ctx.assistantMessage.sessionID,
Expand Down