MCPcopy Index your code
hub / github.com/codeaashu/claude-code / recordPromptState

Function recordPromptState

src/services/api/promptCacheBreakDetection.ts:247–430  ·  view source on GitHub ↗
(snapshot: PromptStateSnapshot)

Source from the content-addressed store, hash-verified

245 * Does NOT fire events — just stores pending changes for phase 2 to use.
246 */
247export function recordPromptState(snapshot: PromptStateSnapshot): void {
248 try {
249 const {
250 system,
251 toolSchemas,
252 querySource,
253 model,
254 agentId,
255 fastMode,
256 globalCacheStrategy = '',
257 betas = [],
258 autoModeActive = false,
259 isUsingOverage = false,
260 cachedMCEnabled = false,
261 effortValue,
262 extraBodyParams,
263 } = snapshot
264 const key = getTrackingKey(querySource, agentId)
265 if (!key) return
266
267 const strippedSystem = stripCacheControl(
268 system as unknown as ReadonlyArray<Record<string, unknown>>,
269 )
270 const strippedTools = stripCacheControl(
271 toolSchemas as unknown as ReadonlyArray<Record<string, unknown>>,
272 )
273
274 const systemHash = computeHash(strippedSystem)
275 const toolsHash = computeHash(strippedTools)
276 // Hash the full system array INCLUDING cache_control — this catches
277 // scope flips (global↔org/none) and TTL flips (1h↔5m) that the stripped
278 // hash can't see because the text content is identical.
279 const cacheControlHash = computeHash(
280 system.map(b => ('cache_control' in b ? b.cache_control : null)),
281 )
282 const toolNames = toolSchemas.map(t => ('name' in t ? t.name : 'unknown'))
283 // Only compute per-tool hashes when the aggregate changed — common case
284 // (tools unchanged) skips N extra jsonStringify calls.
285 const computeToolHashes = () =>
286 computePerToolHashes(strippedTools, toolNames)
287 const systemCharCount = getSystemCharCount(system)
288 const lazyDiffableContent = () =>
289 buildDiffableContent(system, toolSchemas, model)
290 const isFastMode = fastMode ?? false
291 const sortedBetas = [...betas].sort()
292 const effortStr = effortValue === undefined ? '' : String(effortValue)
293 const extraBodyHash =
294 extraBodyParams === undefined ? 0 : computeHash(extraBodyParams)
295
296 const prev = previousStateBySource.get(key)
297
298 if (!prev) {
299 // Evict oldest entries if map is at capacity
300 while (previousStateBySource.size >= MAX_TRACKED_SOURCES) {
301 const oldest = previousStateBySource.keys().next().value
302 if (oldest !== undefined) previousStateBySource.delete(oldest)
303 }
304

Callers 1

queryModelFunction · 0.85

Calls 13

getTrackingKeyFunction · 0.85
stripCacheControlFunction · 0.85
computeHashFunction · 0.85
getSystemCharCountFunction · 0.85
computeToolHashesFunction · 0.85
nextMethod · 0.80
keysMethod · 0.80
getMethod · 0.65
deleteMethod · 0.65
logErrorFunction · 0.50
setMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected