@@ -190,7 +190,7 @@ function makeFakeCodexAdapter(
190190 Effect . void ,
191191 ) ;
192192
193- const compactThread = vi . fn ( ( threadId : ThreadId ) =>
193+ const compactThread = vi . fn ( ( threadId : ThreadId ) : Effect . Effect < void , ProviderAdapterError > =>
194194 Effect . sync ( ( ) =>
195195 emit ( {
196196 type : "thread.state.changed" ,
@@ -278,7 +278,13 @@ function makeFakeCodexAdapter(
278278 } ,
279279 startSession,
280280 sendTurn,
281- ...( provider === CODEX_DRIVER ? { compactThread } : { } ) ,
281+ ...( provider === CODEX_DRIVER
282+ ? { compaction : { type : "native" , start : compactThread } }
283+ : provider === CURSOR_DRIVER
284+ ? { compaction : { type : "slash-command" , command : "/compress" } }
285+ : provider === CLAUDE_AGENT_DRIVER
286+ ? { compaction : { type : "slash-command" , command : "/compact" } }
287+ : { } ) ,
282288 interruptTurn,
283289 respondToRequest,
284290 respondToUserInput,
@@ -981,6 +987,125 @@ it.effect("ProviderServiceLive rejects new sessions for disabled custom instance
981987
982988const routing = makeProviderServiceLayer ( ) ;
983989
990+ const customCompactionDriver = ProviderDriverKind . make ( "custom-compaction-provider" ) ;
991+ const nativeCompactionInstanceId = ProviderInstanceId . make ( "native-compaction" ) ;
992+ const slashCompactionInstanceId = ProviderInstanceId . make ( "slash-compaction" ) ;
993+ const unsupportedCompactionInstanceId = ProviderInstanceId . make ( "unsupported-compaction" ) ;
994+ const customNativeCompaction = makeFakeCodexAdapter ( customCompactionDriver ) ;
995+ const customSlashCompaction = makeFakeCodexAdapter ( customCompactionDriver ) ;
996+ const unsupportedCompaction = makeFakeCodexAdapter ( customCompactionDriver ) ;
997+ const declaredCompaction = makeProviderServiceLayer ( {
998+ registry : makeStaticInstanceRegistry ( [
999+ [
1000+ nativeCompactionInstanceId ,
1001+ {
1002+ ...customNativeCompaction . adapter ,
1003+ compaction : { type : "native" , start : customNativeCompaction . compactThread } ,
1004+ } ,
1005+ ] ,
1006+ [
1007+ slashCompactionInstanceId ,
1008+ {
1009+ ...customSlashCompaction . adapter ,
1010+ compaction : { type : "slash-command" , command : "/reduce-context" } ,
1011+ } ,
1012+ ] ,
1013+ [ unsupportedCompactionInstanceId , unsupportedCompaction . adapter ] ,
1014+ ] ) ,
1015+ } ) ;
1016+
1017+ declaredCompaction . layer ( "ProviderService declared compaction" , ( it ) => {
1018+ it . effect ( "starts declared native compaction instead of sending a prompt" , ( ) =>
1019+ Effect . gen ( function * ( ) {
1020+ const provider = yield * ProviderService . ProviderService ;
1021+ const threadId = asThreadId ( "custom-native-compaction" ) ;
1022+ const requestId = MessageId . make ( "custom-native-request" ) ;
1023+ yield * provider . startSession ( threadId , {
1024+ providerInstanceId : nativeCompactionInstanceId ,
1025+ threadId,
1026+ runtimeMode : "full-access" ,
1027+ } ) ;
1028+ const compactedEventFiber = yield * provider . streamEvents . pipe (
1029+ Stream . filter (
1030+ ( event ) => event . threadId === threadId && event . type === "thread.state.changed" ,
1031+ ) ,
1032+ Stream . runHead ,
1033+ Effect . forkChild ( { startImmediately : true } ) ,
1034+ ) ;
1035+ yield * advanceTestClock ( 50 ) ;
1036+ yield * provider . compactThread ( threadId , undefined , requestId ) ;
1037+ const compacted = Option . getOrThrow ( yield * Fiber . join ( compactedEventFiber ) ) ;
1038+ assert . equal ( compacted . requestId , String ( requestId ) ) ;
1039+ assert . equal ( customNativeCompaction . compactThread . mock . calls . length , 1 ) ;
1040+ assert . equal ( customNativeCompaction . sendTurn . mock . calls . length , 0 ) ;
1041+ yield * provider . stopSession ( { threadId } ) ;
1042+ } ) ,
1043+ ) ;
1044+
1045+ it . effect ( "sends the declared slash command as the compaction turn" , ( ) =>
1046+ Effect . gen ( function * ( ) {
1047+ const provider = yield * ProviderService . ProviderService ;
1048+ const threadId = asThreadId ( "custom-slash-compaction" ) ;
1049+ const requestId = MessageId . make ( "custom-slash-request" ) ;
1050+ const modelSelection = createModelSelection ( slashCompactionInstanceId , "custom-model" ) ;
1051+ yield * provider . startSession ( threadId , {
1052+ providerInstanceId : slashCompactionInstanceId ,
1053+ threadId,
1054+ runtimeMode : "full-access" ,
1055+ } ) ;
1056+ const compactedEventFiber = yield * provider . streamEvents . pipe (
1057+ Stream . filter (
1058+ ( event ) => event . threadId === threadId && event . type === "thread.state.changed" ,
1059+ ) ,
1060+ Stream . runHead ,
1061+ Effect . forkChild ( { startImmediately : true } ) ,
1062+ ) ;
1063+ const compactFiber = yield * provider
1064+ . compactThread ( threadId , modelSelection , requestId )
1065+ . pipe ( Effect . forkChild ) ;
1066+ yield * advanceTestClock ( 50 ) ;
1067+ customSlashCompaction . emit ( {
1068+ type : "turn.completed" ,
1069+ eventId : asEventId ( "custom-slash-completed" ) ,
1070+ provider : customCompactionDriver ,
1071+ createdAt : "2026-01-01T00:00:01.000Z" ,
1072+ threadId,
1073+ turnId : asTurnId ( `turn-${ threadId } ` ) ,
1074+ payload : { state : "completed" } ,
1075+ } ) ;
1076+ yield * Fiber . join ( compactFiber ) ;
1077+ const compacted = Option . getOrThrow ( yield * Fiber . join ( compactedEventFiber ) ) ;
1078+ assert . equal ( compacted . requestId , String ( requestId ) ) ;
1079+ assert . equal ( customSlashCompaction . compactThread . mock . calls . length , 0 ) ;
1080+ assert . equal ( customSlashCompaction . sendTurn . mock . calls . length , 1 ) ;
1081+ assert . equal ( customSlashCompaction . sendTurn . mock . calls [ 0 ] ?. [ 0 ] . input , "/reduce-context" ) ;
1082+ assert . deepEqual (
1083+ customSlashCompaction . sendTurn . mock . calls [ 0 ] ?. [ 0 ] . modelSelection ,
1084+ modelSelection ,
1085+ ) ;
1086+ yield * provider . stopSession ( { threadId } ) ;
1087+ } ) ,
1088+ ) ;
1089+
1090+ it . effect ( "rejects compaction for adapters without a declared strategy" , ( ) =>
1091+ Effect . gen ( function * ( ) {
1092+ const provider = yield * ProviderService . ProviderService ;
1093+ const threadId = asThreadId ( "custom-unsupported-compaction" ) ;
1094+ yield * provider . startSession ( threadId , {
1095+ providerInstanceId : unsupportedCompactionInstanceId ,
1096+ threadId,
1097+ runtimeMode : "full-access" ,
1098+ } ) ;
1099+ const failure = yield * provider . compactThread ( threadId ) . pipe ( Effect . flip ) ;
1100+ assert . instanceOf ( failure , ProviderValidationError ) ;
1101+ assert . include ( failure . message , "does not support context compaction" ) ;
1102+ assert . equal ( unsupportedCompaction . sendTurn . mock . calls . length , 0 ) ;
1103+ assert . equal ( unsupportedCompaction . compactThread . mock . calls . length , 0 ) ;
1104+ yield * provider . stopSession ( { threadId } ) ;
1105+ } ) ,
1106+ ) ;
1107+ } ) ;
1108+
9841109const antigravityDriver = ProviderDriverKind . make ( "antigravity" ) ;
9851110const replacementAntigravity = makeFakeCodexAdapter ( antigravityDriver ) ;
9861111const originalAntigravityInstanceId = ProviderInstanceId . make ( "antigravity-personal" ) ;
0 commit comments