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

Function shouldExtractMemory

src/services/SessionMemory/sessionMemory.ts:135–182  ·  view source on GitHub ↗
(messages: Message[])

Source from the content-addressed store, hash-verified

133}
134
135export function shouldExtractMemory(messages: Message[]): boolean {
136 // Check if we've met the initialization threshold
137 // Uses total context window tokens (same as autocompact) for consistent behavior
138 const currentTokenCount = tokenCountWithEstimation(messages)
139 if (!isSessionMemoryInitialized()) {
140 if (!hasMetInitializationThreshold(currentTokenCount)) {
141 return false
142 }
143 markSessionMemoryInitialized()
144 }
145
146 // Check if we've met the minimum tokens between updates threshold
147 // Uses context window growth since last extraction (same metric as init threshold)
148 const hasMetTokenThreshold = hasMetUpdateThreshold(currentTokenCount)
149
150 // Check if we've met the tool calls threshold
151 const toolCallsSinceLastUpdate = countToolCallsSince(
152 messages,
153 lastMemoryMessageUuid,
154 )
155 const hasMetToolCallThreshold =
156 toolCallsSinceLastUpdate >= getToolCallsBetweenUpdates()
157
158 // Check if the last assistant turn has no tool calls (safe to extract)
159 const hasToolCallsInLastTurn = hasToolCallsInLastAssistantTurn(messages)
160
161 // Trigger extraction when:
162 // 1. Both thresholds are met (tokens AND tool calls), OR
163 // 2. No tool calls in last turn AND token threshold is met
164 // (to ensure we extract at natural conversation breaks)
165 //
166 // IMPORTANT: The token threshold (minimumTokensBetweenUpdate) is ALWAYS required.
167 // Even if the tool call threshold is met, extraction won't happen until the
168 // token threshold is also satisfied. This prevents excessive extractions.
169 const shouldExtract =
170 (hasMetTokenThreshold && hasMetToolCallThreshold) ||
171 (hasMetTokenThreshold && !hasToolCallsInLastTurn)
172
173 if (shouldExtract) {
174 const lastMessage = messages[messages.length - 1]
175 if (lastMessage?.uuid) {
176 lastMemoryMessageUuid = lastMessage.uuid
177 }
178 return true
179 }
180
181 return false
182}
183
184async function setupSessionMemoryFile(
185 toolUseContext: ToolUseContext,

Callers 1

sessionMemory.tsFile · 0.85

Tested by

no test coverage detected