File size: 2,294 Bytes
7bedc79 0fc6e34 7bedc79 031fd6d 7bedc79 031fd6d 7bedc79 031fd6d 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 | import { readFileSync, existsSync, readdirSync, statSync } from "node:fs";
import { resolve, join } from "node:path";
import type { Rule } from "../../core/types.js";
import { parsers } from "../../parsers/index.js";
import { detectDriftEntries, findMissingRules } from "../../drift/detect.js";
import { findAgentFiles } from "../../core/files.js";
export async function detectDrift(root: string): Promise<void> {
const absRoot = resolve(root);
console.log(`Detecting config drift in ${absRoot}...\n`);
// Collect all rules per agent
const perAgent = new Map<string, Rule[]>();
const seenFiles = new Set<string>();
for (const [agentName, parser] of Object.entries(parsers)) {
const files = findAgentFiles(absRoot, agentName).filter((f) => !seenFiles.has(f));
if (files.length === 0) continue;
files.forEach((f) => seenFiles.add(f));
const rules: Rule[] = [];
for (const file of files) {
const content = readFileSync(file, "utf-8");
rules.push(...parser.parse(content, file));
}
perAgent.set(agentName, rules);
}
const allRules = [...perAgent.values()].flat();
if (allRules.length === 0) {
console.log(" No agent rules found. Nothing to compare.");
return;
}
console.log(` Analyzing ${allRules.length} rules across ${perAgent.size} agent(s)...\n`);
// 1. Inconsistent wording detection
const drifts = detectDriftEntries(allRules);
// 2. Missing category detection between agent pairs
const agents = [...perAgent.keys()];
if (agents.length >= 2) {
const base = perAgent.get(agents[0])!;
for (let i = 1; i < agents.length; i++) {
const other = perAgent.get(agents[i])!;
drifts.push(...findMissingRules(base, other, agents[0] as any, agents[i] as any));
}
}
if (drifts.length === 0) {
console.log(" ✅ No config drift detected. All rules are consistent.\n");
return;
}
console.log(` ⚠ Found ${drifts.length} drift issue(s):\n`);
for (const drift of drifts) {
const icon = drift.type === "inconsistent" ? "🔄" : drift.type === "missing" ? "❌" : "⏰";
console.log(` ${icon} [${drift.type}]`);
console.log(` ${drift.description}`);
console.log();
}
console.log(" 💡 Consider consolidating these rules with `agentlens move`.");
}
|