(snapshot: PromptStateSnapshot)
| 245 | * Does NOT fire events — just stores pending changes for phase 2 to use. |
| 246 | */ |
| 247 | export 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 |
no test coverage detected