@devries-cameron20 / t3code Mirror · read-only / commits / 1530bec29a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

fix(opencode): a context overflow that OpenCode compacts no longer fails the turn (#26743)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 11, 2026 at 03:44 UTC 1530bec29af48c73b7ab1a5bb19d68945ddd5ef5
2 files changed +182 -2
packages/provider-opencode/src/server/adapter.test.ts
+153
@@ -660,6 +660,159 @@ describe("OpenCodeAdapterV2", () => {
660 ),
661 );
662
663 + // OpenCode 1.18.18 reports a context overflow as session.error, then auto-compacts and continues.
664 + it.effect("keeps a turn running through a context overflow that OpenCode auto-compacts", () =>
665 + Effect.gen(function* () {
666 + const nativeEvents = asyncEventStream();
667 + let promptId = "";
668 + const harness = yield* makeOpenCodeRuntimeHarness("context-overflow", "root", {
669 + event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
670 + session: {
671 + create: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
672 + promptAsync: async (input: { messageID: string }) => {
673 + promptId = input.messageID;
674 + return { data: true };
675 + },
676 + abort: async () => ({ data: true }),
677 + children: async () => ({ data: [] }),
678 + },
679 + });
680 + yield* harness.startTurn();
681 + const received = yield* harness.runtime.events.pipe(
682 + Stream.takeUntil((event) => event.type === "turn.terminal"),
683 + Stream.runCollect,
684 + Effect.forkScoped,
685 + );
686 + const status = (type: "busy" | "idle") => ({
687 + type: "session.status",
688 + properties: { sessionID: "root", status: { type } },
689 + });
690 + yield* Effect.promise(() =>
691 + nativeEvents.push({
692 + type: "message.updated",
693 + properties: {
694 + sessionID: "root",
695 + info: { id: promptId, role: "user", time: { created: 1 } },
696 + },
697 + }),
698 + );
699 + yield* Effect.promise(() => nativeEvents.push(status("busy")));
700 + yield* Effect.promise(() =>
701 + nativeEvents.push({
702 + type: "session.error",
703 + properties: {
704 + sessionID: "root",
705 + error: {
706 + name: "ContextOverflowError",
707 + data: {
708 + message:
709 + "This model's maximum context length is 131070 tokens. However, your messages resulted in 140049 tokens.",
710 + },
711 + },
712 + },
713 + }),
714 + );
715 + yield* Effect.promise(() =>
716 + nativeEvents.push({ type: "session.compacted", properties: { sessionID: "root" } }),
717 + );
718 + yield* Effect.promise(() => nativeEvents.push(status("busy")));
719 + yield* Effect.promise(() =>
720 + nativeEvents.push({
721 + type: "message.part.updated",
722 + properties: {
723 + sessionID: "root",
724 + part: {
725 + id: "post-compaction-text",
726 + sessionID: "root",
727 + messageID: "post-compaction-assistant",
728 + type: "text",
729 + text: "Post-compaction step",
730 + time: { start: 2 },
731 + },
732 + },
733 + }),
734 + );
735 + yield* Effect.promise(() =>
736 + nativeEvents.push({
737 + type: "message.updated",
738 + properties: {
739 + sessionID: "root",
740 + info: {
741 + id: "post-compaction-assistant",
742 + sessionID: "root",
743 + role: "assistant",
744 + time: { created: 2, completed: 3 },
745 + parentID: promptId,
746 + },
747 + },
748 + }),
749 + );
750 + yield* Effect.promise(() => nativeEvents.push(status("idle")));
751 +
752 + const events = Array.from(yield* Fiber.join(received));
753 + const terminal = events.find((event) => event.type === "turn.terminal");
754 + assert.equal(terminal?.status, "completed");
755 + assert.isTrue(
756 + events.some(
757 + (event) =>
758 + event.type === "turn_item.updated" &&
759 + event.turnItem.type === "assistant_message" &&
760 + event.turnItem.text === "Post-compaction step",
761 + ),
762 + );
763 + }).pipe(
764 + Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
765 + Effect.scoped,
766 + ),
767 + );
768 +
769 + it.effect("fails an explicit compaction that OpenCode reports as a context overflow", () =>
770 + Effect.gen(function* () {
771 + const nativeEvents = asyncEventStream();
772 + const harness = yield* makeOpenCodeRuntimeHarness("compact-overflow", "root", {
773 + event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
774 + session: {
775 + create: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
776 + summarize: async () => {
777 + await nativeEvents.push({
778 + type: "session.error",
779 + properties: {
780 + sessionID: "root",
781 + error: {
782 + name: "ContextOverflowError",
783 + data: { message: "This model's maximum context length is 131070 tokens." },
784 + },
785 + },
786 + });
787 + return { data: true };
788 + },
789 + },
790 + });
791 + const received = yield* harness.runtime.events.pipe(
792 + Stream.takeUntil((event) => event.type === "turn.terminal"),
793 + Stream.runCollect,
794 + Effect.forkScoped,
795 + );
796 + yield* harness.startTurn("/compact");
797 +
798 + const events = Array.from(yield* Fiber.join(received));
799 + const terminal = events.find((event) => event.type === "turn.terminal");
800 + assert.equal(terminal?.status, "failed");
801 + assert.equal(
802 + terminal?.failure?.message,
803 + "This model's maximum context length is 131070 tokens.",
804 + );
805 + assert.isFalse(
806 + events.some(
807 + (event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction",
808 + ),
809 + );
810 + }).pipe(
811 + Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
812 + Effect.scoped,
813 + ),
814 + );
815 +
816 it.effect("preserves native multi-select and custom-answer flags through a live reply", () =>
817 Effect.gen(function* () {
818 const nativeEvents = asyncEventStream();
packages/provider-opencode/src/server/adapter.ts
+29 -2
@@ -281,6 +281,7 @@ interface ActiveOpenCodeTurn {
281 admissionMessageId: string | null;
282 interrupted: boolean;
283 finalized: boolean;
284 + contextOverflow: OrchestrationV2ProviderFailure | null;
285 planId: PlanId | null;
286 admissionGeneration: number;
287 admissionReconciliationGeneration: number | null;
@@ -2045,6 +2046,12 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2046 status = "failed";
2047 terminal = { failure: nativeStreamFailure, threadDisposition: "broken" };
2048 }
2049 + let lastError = sessionEntity.lastError;
2050 + if (status === "completed" && turn.contextOverflow !== null) {
2051 + status = "failed";
2052 + terminal = { failure: turn.contextOverflow };
2053 + lastError = turn.contextOverflow.message;
2054 + }
2055 turn.finalized = true;
2056 const completedAt = yield* DateTime.now;
2057 for (const part of turn.parts.values()) {
@@ -2099,7 +2106,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2106 );
2107 yield* updateProviderSession(
2108 anotherTurnIsActive ? "running" : status === "failed" ? "error" : "ready",
2102 - status === "failed" ? sessionEntity.lastError : null,
2109 + status === "failed" ? lastError : null,
2110 );
2111 yield* emitProviderEvent(
2112 status === "failed"
@@ -2258,6 +2265,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2265 admissionMessageId: null,
2266 interrupted: false,
2267 finalized: false,
2268 + contextOverflow: null,
2269 planId: null,
2270 admissionGeneration: 0,
2271 admissionReconciliationGeneration: null,
@@ -2555,6 +2563,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2563 case "session.compacted": {
2564 const state = threads.get(event.properties.sessionID);
2565 if (state?.activeTurn !== undefined && state.activeTurn !== null) {
2566 + state.activeTurn.contextOverflow = null;
2567 yield* emitCompactionItem(state, state.activeTurn);
2568 }
2569 return;
@@ -2657,6 +2666,20 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2666 return;
2667 }
2668 case "session.error": {
2669 + const overflowTurn =
2670 + event.properties.error?.name === "ContextOverflowError" &&
2671 + event.properties.sessionID !== undefined
2672 + ? threads.get(event.properties.sessionID)?.activeTurn
2673 + : undefined;
2674 + if (overflowTurn !== undefined && overflowTurn !== null) {
2675 + // OpenCode stays busy and auto-compacts; the turn fails only if it idles uncompacted.
2676 + overflowTurn.contextOverflow = makeProviderFailure({
2677 + message: openCodeErrorMessage(event),
2678 + code: "ContextOverflowError",
2679 + class: "provider_error",
2680 + });
2681 + return;
2682 + }
2683 const states =
2684 event.properties.sessionID === undefined
2685 ? Array.from(threads.values()).filter((state) => state.activeTurn !== null)
@@ -3244,6 +3267,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3267 admissionMessageId,
3268 interrupted: false,
3269 finalized: false,
3270 + contextOverflow: null,
3271 planId: null,
3272 admissionGeneration: state.nextAdmissionGeneration++,
3273 admissionReconciliationGeneration: null,
@@ -3280,7 +3304,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3304 ),
3305 ).pipe(
3306 Effect.tap(() =>
3283 - turn.interrupted || turn.finalized || nativeStreamFailure !== null
3307 + turn.interrupted ||
3308 + turn.finalized ||
3309 + turn.contextOverflow !== null ||
3310 + nativeStreamFailure !== null
3311 ? Effect.void
3312 : emitCompactionItem(state, turn),
3313 ),