Skip to content

Commit 1530bec

Browse files
fix(opencode): a context overflow that OpenCode compacts no longer fails the turn (pingdotgg#26743)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 36f4e07 commit 1530bec

2 files changed

Lines changed: 182 additions & 2 deletions

File tree

‎packages/provider-opencode/src/server/adapter.test.ts‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,159 @@ describe("OpenCodeAdapterV2", () => {
660660
),
661661
);
662662

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+
663816
it.effect("preserves native multi-select and custom-answer flags through a live reply", () =>
664817
Effect.gen(function* () {
665818
const nativeEvents = asyncEventStream();

‎packages/provider-opencode/src/server/adapter.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ interface ActiveOpenCodeTurn {
281281
admissionMessageId: string | null;
282282
interrupted: boolean;
283283
finalized: boolean;
284+
contextOverflow: OrchestrationV2ProviderFailure | null;
284285
planId: PlanId | null;
285286
admissionGeneration: number;
286287
admissionReconciliationGeneration: number | null;
@@ -2045,6 +2046,12 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
20452046
status = "failed";
20462047
terminal = { failure: nativeStreamFailure, threadDisposition: "broken" };
20472048
}
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+
}
20482055
turn.finalized = true;
20492056
const completedAt = yield* DateTime.now;
20502057
for (const part of turn.parts.values()) {
@@ -2099,7 +2106,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
20992106
);
21002107
yield* updateProviderSession(
21012108
anotherTurnIsActive ? "running" : status === "failed" ? "error" : "ready",
2102-
status === "failed" ? sessionEntity.lastError : null,
2109+
status === "failed" ? lastError : null,
21032110
);
21042111
yield* emitProviderEvent(
21052112
status === "failed"
@@ -2258,6 +2265,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
22582265
admissionMessageId: null,
22592266
interrupted: false,
22602267
finalized: false,
2268+
contextOverflow: null,
22612269
planId: null,
22622270
admissionGeneration: 0,
22632271
admissionReconciliationGeneration: null,
@@ -2555,6 +2563,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
25552563
case "session.compacted": {
25562564
const state = threads.get(event.properties.sessionID);
25572565
if (state?.activeTurn !== undefined && state.activeTurn !== null) {
2566+
state.activeTurn.contextOverflow = null;
25582567
yield* emitCompactionItem(state, state.activeTurn);
25592568
}
25602569
return;
@@ -2657,6 +2666,20 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
26572666
return;
26582667
}
26592668
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+
}
26602683
const states =
26612684
event.properties.sessionID === undefined
26622685
? Array.from(threads.values()).filter((state) => state.activeTurn !== null)
@@ -3244,6 +3267,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
32443267
admissionMessageId,
32453268
interrupted: false,
32463269
finalized: false,
3270+
contextOverflow: null,
32473271
planId: null,
32483272
admissionGeneration: state.nextAdmissionGeneration++,
32493273
admissionReconciliationGeneration: null,
@@ -3280,7 +3304,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
32803304
),
32813305
).pipe(
32823306
Effect.tap(() =>
3283-
turn.interrupted || turn.finalized || nativeStreamFailure !== null
3307+
turn.interrupted ||
3308+
turn.finalized ||
3309+
turn.contextOverflow !== null ||
3310+
nativeStreamFailure !== null
32843311
? Effect.void
32853312
: emitCompactionItem(state, turn),
32863313
),

0 commit comments

Comments
 (0)