-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-pruner.ts
More file actions
42 lines (39 loc) · 1.26 KB
/
pattern-pruner.ts
File metadata and controls
42 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { createTracedOp } from "../tracing/weave";
import { listPatterns, deletePattern } from "../redis/patterns";
import { computePatternFitness } from "./pattern-fitness";
/**
* Remove dead patterns: fitness < 0.05 AND failure_count >= 3.
* This prevents the vector cache from being polluted with unreliable patterns.
*/
export const pruneDeadPatterns = createTracedOp(
"pruneDeadPatterns",
async function pruneDeadPatterns(): Promise<{
pruned: number;
remaining: number;
prunedIds: string[];
}> {
const { patterns } = await listPatterns(1000, 0);
const prunedIds: string[] = [];
for (const pattern of patterns) {
const fitness = computePatternFitness(pattern);
if (fitness < 0.05 && pattern.failure_count >= 3) {
await deletePattern(pattern.id);
prunedIds.push(pattern.id);
console.log(
`[Pruner] Removed dead pattern ${pattern.id} (fitness=${fitness.toFixed(3)}, failures=${pattern.failure_count})`
);
}
}
return {
pruned: prunedIds.length,
remaining: patterns.length - prunedIds.length,
prunedIds,
};
},
{
summarize: (result) => ({
"webscout.patterns_pruned": result.pruned,
"webscout.patterns_remaining": result.remaining,
}),
}
);