MCPcopy Create free account
hub / github.com/Noumena-Network/code / shouldAutoCompact

Function shouldAutoCompact

src/services/compact/autoCompact.ts:168–247  ·  view source on GitHub ↗
(
  messages: Message[],
  model: string,
  querySource?: QuerySource,
  // Snip removes messages but the surviving assistant's usage still reflects
  // pre-snip context, so tokenCountWithEstimation can't see the savings.
  // Subtract the rough-delta that snip already computed.
  snipTokensFreed = 0,
)

Source from the content-addressed store, hash-verified

166}
167
168export async function shouldAutoCompact(
169 messages: Message[],
170 model: string,
171 querySource?: QuerySource,
172 // Snip removes messages but the surviving assistant's usage still reflects
173 // pre-snip context, so tokenCountWithEstimation can't see the savings.
174 // Subtract the rough-delta that snip already computed.
175 snipTokensFreed = 0,
176): Promise<boolean> {
177 // Recursion guards. session_memory and compact are forked agents that
178 // would deadlock.
179 if (querySource === 'session_memory' || querySource === 'compact') {
180 return false
181 }
182 // marble_origami is the ctx-agent — if ITS context blows up and
183 // autocompact fires, runPostCompactCleanup calls resetContextCollapse()
184 // which destroys the MAIN thread's committed log (module-level state
185 // shared across forks). Inside feature() so the string DCEs from
186 // external builds (it's in excluded-strings.txt).
187 if (feature('CONTEXT_COLLAPSE')) {
188 if (querySource === 'marble_origami') {
189 return false
190 }
191 }
192
193 if (!isAutoCompactEnabled()) {
194 return false
195 }
196
197 // Reactive-only mode: suppress proactive autocompact, let reactive compact
198 // catch the API's prompt-too-long. feature() wrapper keeps the flag string
199 // out of external builds (REACTIVE_COMPACT is ant-only).
200 // Note: returning false here also means autoCompactIfNeeded never reaches
201 // trySessionMemoryCompaction in the query loop — the /compact call site
202 // still tries session memory first. Revisit if reactive-only graduates.
203 if (feature('REACTIVE_COMPACT')) {
204 if (getFeatureValue_CACHED_MAY_BE_STALE('ncode_cobalt_raccoon', false)) {
205 return false
206 }
207 }
208
209 // Context-collapse mode: same suppression. Collapse IS the context
210 // management system when it's on — the 90% commit / 95% blocking-spawn
211 // flow owns the headroom problem. Autocompact firing at effective-13k
212 // (~93% of effective) sits right between collapse's commit-start (90%)
213 // and blocking (95%), so it would race collapse and usually win, nuking
214 // granular context that collapse was about to save. Gating here rather
215 // than in isAutoCompactEnabled() keeps reactiveCompact alive as the 413
216 // fallback (it consults isAutoCompactEnabled directly) and leaves
217 // sessionMemory + manual /compact working.
218 //
219 // Consult isContextCollapseEnabled (not the raw gate) so the
220 // CLAUDE_CONTEXT_COLLAPSE env override is honored here too. require()
221 // inside the block breaks the init-time cycle (this file exports
222 // getEffectiveContextWindowSize which collapse's index imports).
223 if (feature('CONTEXT_COLLAPSE')) {
224 /* eslint-disable @typescript-eslint/no-require-imports */
225 const { isContextCollapseEnabled } =

Callers 1

autoCompactIfNeededFunction · 0.85

Calls 8

isAutoCompactEnabledFunction · 0.85
isContextCollapseEnabledFunction · 0.85
tokenCountWithEstimationFunction · 0.85
getAutoCompactThresholdFunction · 0.85
logForDebuggingFunction · 0.50

Tested by

no test coverage detected