Download src/drift/detect.ts from hongcoding/agentlens: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/datasets/hongcoding/agentlens/resolve/main/src/drift/detect.ts
- Command line
-
hf download hf://datasets/hongcoding/agentlens/src/drift/detect.ts
-
curl -L -o detect.ts https://huggingface.co/datasets/hongcoding/agentlens/resolve/main/src/drift/detect.ts
2.96 kB
| import type { Rule, DriftEntry, AgentKind } from "../core/types.js"; | |
| /** | |
| * Detect drift across agent rule sets: | |
| * 1. Same conceptual rule with different wording across agents → "inconsistent" | |
| * 2. Rule referencing tools/paths that don't exist → "stale" | |
| * 3. Rule present in one agent but missing in another → "missing" | |
| */ | |
| /** Normalize rule text for fuzzy comparison. */ | |
| function normalize(text: string): string { | |
| return text | |
| .toLowerCase() | |
| .replace(/[^\w\s一-鿿]/g, "") | |
| .replace(/\s+/g, " ") | |
| .trim(); | |
| } | |
| /** Compute word overlap similarity between two strings (0-1). */ | |
| function similarity(a: string, b: string): number { | |
| const wordsA = new Set(normalize(a).split(" ")); | |
| const wordsB = new Set(normalize(b).split(" ")); | |
| if (wordsA.size === 0 || wordsB.size === 0) return 0; | |
| let common = 0; | |
| for (const w of wordsA) { | |
| if (wordsB.has(w)) common++; | |
| } | |
| return common / Math.max(wordsA.size, wordsB.size); | |
| } | |
| export function detectDriftEntries(allRules: Rule[]): DriftEntry[] { | |
| const entries: DriftEntry[] = []; | |
| const processed = new Set<string>(); | |
| for (let i = 0; i < allRules.length; i++) { | |
| for (let j = i + 1; j < allRules.length; j++) { | |
| const a = allRules[i]; | |
| const b = allRules[j]; | |
| // Skip if from the same file | |
| if (a.source.file === b.source.file && a.source.agent === b.source.agent) continue; | |
| const sim = similarity(a.content, b.content); | |
| // Similar but not identical → inconsistent drift | |
| if (sim >= 0.5 && sim < 1.0 && a.content !== b.content) { | |
| const key = [a.id, b.id].sort().join("|"); | |
| if (!processed.has(key)) { | |
| processed.add(key); | |
| entries.push({ | |
| ruleId: a.id, | |
| type: "inconsistent", | |
| description: `Similar rules with different wording (${Math.round(sim * 100)}% match):\n "${a.content.slice(0, 80)}" (${a.source.agent})\n "${b.content.slice(0, 80)}" (${b.source.agent})`, | |
| locations: [a.source, b.source], | |
| }); | |
| } | |
| } | |
| } | |
| } | |
| return entries; | |
| } | |
| /** | |
| * Find rules that exist in one agent but not another (by category coverage). | |
| */ | |
| export function findMissingRules( | |
| sourceRules: Rule[], | |
| targetRules: Rule[], | |
| sourceAgent: AgentKind, | |
| targetAgent: AgentKind | |
| ): DriftEntry[] { | |
| const entries: DriftEntry[] = []; | |
| const sourceCategories = new Set(sourceRules.map((r) => r.category)); | |
| const targetCategories = new Set(targetRules.map((r) => r.category)); | |
| for (const cat of sourceCategories) { | |
| if (!targetCategories.has(cat)) { | |
| const sample = sourceRules.find((r) => r.category === cat); | |
| entries.push({ | |
| ruleId: `missing-${cat}`, | |
| type: "missing", | |
| description: `Category "${cat}" has rules in ${sourceAgent} but none in ${targetAgent} (example: "${sample?.content.slice(0, 60)}")`, | |
| locations: [{ agent: sourceAgent, file: "source" }], | |
| }); | |
| } | |
| } | |
| return entries; | |
| } | |