@@ -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 ( ) ;
0 commit comments