agentlens / src /cli /commands /drift.ts
hongcoding's picture
feat: DSH bot-instruction parser + dedupe + DSH panel project picker
0fc6e34 verified
Raw History Blame Contribute Delete
2.29 kB
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`.");
}