File size: 2,957 Bytes
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
}