11import type { SessionState , WithParts } from "../state"
2- import type { Logger } from "../logger"
32import type { PluginConfig } from "../config"
43import type { UserMessage } from "@opencode-ai/sdk/v2"
54import { renderNudge } from "../prompts"
@@ -12,6 +11,7 @@ import {
1211import { getFilePathsFromParameters , isProtected } from "../protected-file-patterns"
1312import { getLastUserMessage , isMessageCompacted } from "../shared-utils"
1413import { getCurrentTokenUsage } from "../strategies/utils"
14+ import { clog , C } from "../compress-logger"
1515
1616function parsePercentageString ( value : string , total : number ) : number | undefined {
1717 if ( ! value . endsWith ( "%" ) ) return undefined
@@ -149,14 +149,15 @@ const buildContextPressureTools = (state: SessionState, config: PluginConfig): s
149149export const insertCompressToolContext = (
150150 state : SessionState ,
151151 config : PluginConfig ,
152- logger : Logger ,
153152 messages : WithParts [ ] ,
154153) : void => {
155154 if ( state . manualMode || state . pendingManualTrigger ) {
155+ clog . debug ( C . INJECT , "Skipping compress context injection (manual mode or pending trigger)" )
156156 return
157157 }
158158
159159 if ( config . tools . compress . permission === "deny" ) {
160+ clog . debug ( C . INJECT , "Skipping compress context injection (compress denied)" )
160161 return
161162 }
162163
@@ -170,38 +171,48 @@ export const insertCompressToolContext = (
170171 : undefined
171172
172173 if ( state . lastToolPrune ) {
173- logger . debug ( "Last context operation was compress - injecting cooldown")
174+ clog . info ( C . INJECT , "Injecting cooldown (lastToolPrune=true) ")
174175 contentParts . push ( wrapCooldownMessage ( ) )
175176 } else {
176177 if ( config . tools . settings . contextPressureEnabled ) {
177178 const contextPressureTools = buildContextPressureTools ( state , config )
178179 if ( contextPressureTools ) {
179180 contentParts . push ( contextPressureTools )
181+ clog . debug ( C . INJECT , "Added context pressure tools list" )
180182 }
181183 }
182184
183185 if ( config . tools . settings . compressContextEnabled ) {
184186 contentParts . push ( buildCompressContext ( state , messages ) )
187+ clog . debug ( C . INJECT , "Added compress context block" )
185188 }
186189
187190 if ( shouldInjectLimitNudge ( config , state , messages , providerId , modelId ) ) {
188- logger . info ( "Injecting context-limit nudge" )
191+ clog . info ( C . INJECT , "Injecting context-limit nudge" , {
192+ nudgeCounter : state . nudgeCounter ,
193+ currentTokens : getCurrentTokenUsage ( messages ) ,
194+ } )
189195 contentParts . push ( renderNudge ( "context-limit" ) )
190196 } else if (
191197 config . tools . settings . nudgeEnabled &&
192198 state . nudgeCounter >= config . tools . settings . nudgeFrequency
193199 ) {
194- logger . info ( "Injecting frequency nudge" )
200+ clog . info ( C . INJECT , "Injecting frequency nudge" , {
201+ nudgeCounter : state . nudgeCounter ,
202+ nudgeFrequency : config . tools . settings . nudgeFrequency ,
203+ } )
195204 contentParts . push ( renderNudge ( "frequency" ) )
196205 }
197206 }
198207
199208 if ( contentParts . length === 0 ) {
209+ clog . debug ( C . INJECT , "No content parts to inject" )
200210 return
201211 }
202212
203213 const combinedContent = contentParts . join ( "\n" )
204214 if ( ! lastUserMessage ) {
215+ clog . warn ( C . INJECT , "No last user message found, cannot inject" )
205216 return
206217 }
207218
@@ -210,16 +221,25 @@ export const insertCompressToolContext = (
210221 ( message ) => ! ( message . info . role === "user" && isIgnoredUserMessage ( message ) ) ,
211222 )
212223 if ( ! lastNonIgnoredMessage ) {
224+ clog . warn ( C . INJECT , "No non-ignored message found, cannot inject" )
213225 return
214226 }
215227
216228 if ( lastNonIgnoredMessage . info . role === "user" ) {
217229 const textPart = createSyntheticTextPart ( lastNonIgnoredMessage , combinedContent )
218230 lastNonIgnoredMessage . parts . push ( textPart )
231+ clog . debug ( C . INJECT , "Injected as synthetic text part on user message" , {
232+ msgId : lastNonIgnoredMessage . info . id ,
233+ contentLen : combinedContent . length ,
234+ } )
219235 return
220236 }
221237
222238 const modelID = userInfo . model ?. modelID || ""
223239 const toolPart = createSyntheticToolPart ( lastNonIgnoredMessage , combinedContent , modelID )
224240 lastNonIgnoredMessage . parts . push ( toolPart )
241+ clog . debug ( C . INJECT , "Injected as synthetic tool part on assistant message" , {
242+ msgId : lastNonIgnoredMessage . info . id ,
243+ contentLen : combinedContent . length ,
244+ } )
225245}
0 commit comments