Skip to content

Commit fe696ab

Browse files
committed
refactor(api): pull recommendation strings + reasonings into named constants
cache-readonly.types.ts now exports two const-object enums: THRESHOLD_RECOMMENDATIONS = { TIGHTEN, LOOSEN, OPTIMAL, INSUFFICIENT_DATA } TOOL_EFFECTIVENESS_RECOMMENDATIONS = { INCREASE_TTL, OPTIMAL, DECREASE_TTL_OR_DISABLE } with the existing union types (ThresholdRecommendationKind, ToolEffectivenessRecommendation) derived from them via typeof[keyof typeof]. The four threshold-recommendation reasoning templates also move out of the service, into a THRESHOLD_REASONINGS map of small functions that take only the relevant context. The "% of foo" formatting is now expressed once via a local formatPct helper. CacheReadonlyService imports the consts and the reasoning map and references them by name, so adding or renaming a recommendation no longer requires touching the inline strings in two places.
1 parent 8a79e74 commit fe696ab

2 files changed

Lines changed: 47 additions & 18 deletions

File tree

apps/api/src/cache-proposals/cache-readonly.service.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { ConnectionRegistry } from '../connections/connection-registry.service';
77
import { CacheResolverService, type ResolvedCache } from './cache-resolver.service';
88
import { CacheNotFoundError, InvalidCacheTypeError } from './errors';
99
import { readHashInt } from '../common/utils/valkey-fields';
10+
import {
11+
THRESHOLD_RECOMMENDATIONS,
12+
THRESHOLD_REASONINGS,
13+
TOOL_EFFECTIVENESS_RECOMMENDATIONS,
14+
} from './cache-readonly.types';
1015
import type {
1116
CacheHealth,
1217
CacheHealthWarning,
@@ -190,8 +195,8 @@ export class CacheReadonlyService {
190195
near_miss_rate: 0,
191196
avg_hit_similarity: 0,
192197
avg_miss_similarity: 0,
193-
recommendation: 'insufficient_data',
194-
reasoning: `Only ${sampleCount} samples collected; ${minSamples} required for a reliable recommendation.`,
198+
recommendation: THRESHOLD_RECOMMENDATIONS.INSUFFICIENT_DATA,
199+
reasoning: THRESHOLD_REASONINGS.insufficientData(sampleCount, minSamples),
195200
};
196201
}
197202
const hits = filtered.filter((s) => s.result === 'hit');
@@ -214,16 +219,16 @@ export class CacheReadonlyService {
214219
let recommendedThreshold: number | undefined;
215220
let reasoning: string;
216221
if (uncertainHitRate > 0.2) {
217-
recommendation = 'tighten_threshold';
222+
recommendation = THRESHOLD_RECOMMENDATIONS.TIGHTEN;
218223
recommendedThreshold = Math.max(0, threshold - config.uncertainty_band * 1.5);
219-
reasoning = `${(uncertainHitRate * 100).toFixed(1)}% of hits are in the uncertainty band — tighten the threshold.`;
224+
reasoning = THRESHOLD_REASONINGS.tighten(uncertainHitRate);
220225
} else if (nearMissRate > 0.3 && avgNearMissDelta < 0.03) {
221-
recommendation = 'loosen_threshold';
226+
recommendation = THRESHOLD_RECOMMENDATIONS.LOOSEN;
222227
recommendedThreshold = threshold + avgNearMissDelta;
223-
reasoning = `${(nearMissRate * 100).toFixed(1)}% of misses are very close to the threshold — consider loosening.`;
228+
reasoning = THRESHOLD_REASONINGS.loosen(nearMissRate);
224229
} else {
225-
recommendation = 'optimal';
226-
reasoning = `Hit rate ${(hitRate * 100).toFixed(1)}% with ${(uncertainHitRate * 100).toFixed(1)}% uncertain hits — threshold appears well-calibrated.`;
230+
recommendation = THRESHOLD_RECOMMENDATIONS.OPTIMAL;
231+
reasoning = THRESHOLD_REASONINGS.optimal(hitRate, uncertainHitRate);
227232
}
228233

229234
return {
@@ -257,11 +262,14 @@ export class CacheReadonlyService {
257262
const policyTtl = await this.readToolPolicyTtl(client, cache.prefix, toolName);
258263
let recommendation: ToolEffectivenessRecommendation;
259264
if (hitRate > 0.8) {
260-
recommendation = policyTtl !== null && policyTtl < 3600 ? 'increase_ttl' : 'optimal';
265+
recommendation =
266+
policyTtl !== null && policyTtl < 3600
267+
? TOOL_EFFECTIVENESS_RECOMMENDATIONS.INCREASE_TTL
268+
: TOOL_EFFECTIVENESS_RECOMMENDATIONS.OPTIMAL;
261269
} else if (hitRate >= 0.4) {
262-
recommendation = 'optimal';
270+
recommendation = TOOL_EFFECTIVENESS_RECOMMENDATIONS.OPTIMAL;
263271
} else {
264-
recommendation = 'decrease_ttl_or_disable';
272+
recommendation = TOOL_EFFECTIVENESS_RECOMMENDATIONS.DECREASE_TTL_OR_DISABLE;
265273
}
266274
entries.push({
267275
tool: toolName,

apps/api/src/cache-proposals/cache-readonly.types.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,28 @@ export interface AgentCacheHealth extends CacheHealthCommon {
4141

4242
export type CacheHealth = SemanticCacheHealth | AgentCacheHealth;
4343

44+
export const THRESHOLD_RECOMMENDATIONS = {
45+
TIGHTEN: 'tighten_threshold',
46+
LOOSEN: 'loosen_threshold',
47+
OPTIMAL: 'optimal',
48+
INSUFFICIENT_DATA: 'insufficient_data',
49+
} as const;
50+
4451
export type ThresholdRecommendationKind =
45-
| 'tighten_threshold'
46-
| 'loosen_threshold'
47-
| 'optimal'
48-
| 'insufficient_data';
52+
(typeof THRESHOLD_RECOMMENDATIONS)[keyof typeof THRESHOLD_RECOMMENDATIONS];
53+
54+
const formatPct = (value: number): string => `${(value * 100).toFixed(1)}%`;
55+
56+
export const THRESHOLD_REASONINGS = {
57+
insufficientData: (sampleCount: number, minSamples: number): string =>
58+
`Only ${sampleCount} samples collected; ${minSamples} required for a reliable recommendation.`,
59+
tighten: (uncertainHitRate: number): string =>
60+
`${formatPct(uncertainHitRate)} of hits are in the uncertainty band — tighten the threshold.`,
61+
loosen: (nearMissRate: number): string =>
62+
`${formatPct(nearMissRate)} of misses are very close to the threshold — consider loosening.`,
63+
optimal: (hitRate: number, uncertainHitRate: number): string =>
64+
`Hit rate ${formatPct(hitRate)} with ${formatPct(uncertainHitRate)} uncertain hits — threshold appears well-calibrated.`,
65+
} as const;
4966

5067
export interface ThresholdRecommendation {
5168
category: string;
@@ -61,10 +78,14 @@ export interface ThresholdRecommendation {
6178
reasoning: string;
6279
}
6380

81+
export const TOOL_EFFECTIVENESS_RECOMMENDATIONS = {
82+
INCREASE_TTL: 'increase_ttl',
83+
OPTIMAL: 'optimal',
84+
DECREASE_TTL_OR_DISABLE: 'decrease_ttl_or_disable',
85+
} as const;
86+
6487
export type ToolEffectivenessRecommendation =
65-
| 'increase_ttl'
66-
| 'optimal'
67-
| 'decrease_ttl_or_disable';
88+
(typeof TOOL_EFFECTIVENESS_RECOMMENDATIONS)[keyof typeof TOOL_EFFECTIVENESS_RECOMMENDATIONS];
6889

6990
export interface ToolEffectivenessEntry {
7091
tool: string;

0 commit comments

Comments
 (0)