Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/agents/tools/web-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export function readCache<T>(
cache.delete(key);
return null;
}
// Promote to most-recent position for LRU eviction.
cache.delete(key);
cache.set(key, entry);
return { value: entry.value, cached: true };
}

Expand All @@ -47,16 +50,33 @@ export function writeCache<T>(
if (ttlMs <= 0) {
return;
}
// Remove existing entry so .set() moves it to the end (LRU order).
cache.delete(key);
if (cache.size >= DEFAULT_CACHE_MAX_ENTRIES) {
const oldest = cache.keys().next();
if (!oldest.done) {
cache.delete(oldest.value);
// Evict expired entries first, then fall back to LRU (oldest insertion order).
const now = Date.now();
let evicted = false;
for (const [k, v] of cache) {
if (now > v.expiresAt) {
cache.delete(k);
evicted = true;
if (cache.size < DEFAULT_CACHE_MAX_ENTRIES) {
break;
}
}
}
if (!evicted || cache.size >= DEFAULT_CACHE_MAX_ENTRIES) {
const oldest = cache.keys().next();
if (!oldest.done) {
cache.delete(oldest.value);
}
}
}
const now = Date.now();
cache.set(key, {
value,
expiresAt: Date.now() + ttlMs,
insertedAt: Date.now(),
expiresAt: now + ttlMs,
insertedAt: now,
});
}

Expand Down
36 changes: 33 additions & 3 deletions src/logging/redact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,53 @@ function resolveConfigRedaction(): RedactOptions {
};
}

/* ── Cached resolution ── */

let _cachedConfig: RedactOptions | undefined;
let _cachedCompiledPatterns: RegExp[] | undefined;
let _cachedPatternSource: string[] | undefined;

function getCachedConfig(): RedactOptions {
if (!_cachedConfig) {
_cachedConfig = resolveConfigRedaction();
}
return _cachedConfig;
}

function getCachedPatterns(source?: string[]): RegExp[] {
const key = source?.length ? source : DEFAULT_REDACT_PATTERNS;
if (_cachedCompiledPatterns && _cachedPatternSource === key) {
return _cachedCompiledPatterns;
}
_cachedPatternSource = key;
_cachedCompiledPatterns = resolvePatterns(key);
return _cachedCompiledPatterns;
}

/** Clear the cached config and patterns (useful for testing or config reload). */
export function clearRedactCache(): void {
_cachedConfig = undefined;
_cachedCompiledPatterns = undefined;
_cachedPatternSource = undefined;
}

export function redactSensitiveText(text: string, options?: RedactOptions): string {
if (!text) {
return text;
}
const resolved = options ?? resolveConfigRedaction();
const resolved = options ?? getCachedConfig();
if (normalizeMode(resolved.mode) === "off") {
return text;
}
const patterns = resolvePatterns(resolved.patterns);
const patterns = getCachedPatterns(resolved.patterns);
if (!patterns.length) {
return text;
}
return redactText(text, patterns);
}

export function redactToolDetail(detail: string): string {
const resolved = resolveConfigRedaction();
const resolved = getCachedConfig();
if (normalizeMode(resolved.mode) !== "tools") {
return detail;
}
Expand Down