@a-company/paradigm 3.23.3 → 3.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/{accept-orchestration-ORQRKKGR.js → accept-orchestration-AAYFKS74.js} +5 -5
  2. package/dist/chunk-4UC6AQOC.js +631 -0
  3. package/dist/{chunk-YOFP72IB.js → chunk-6EQRU7WC.js} +4 -4
  4. package/dist/{chunk-K34C7NAN.js → chunk-6UV47VRD.js} +1 -1
  5. package/dist/{chunk-Z42FOOVT.js → chunk-GC6X3YM7.js} +6 -6
  6. package/dist/{chunk-C3BK3E23.js → chunk-OXG5GVDJ.js} +1 -1
  7. package/dist/{chunk-XKAFTZOZ.js → chunk-VHSTF72C.js} +1 -1
  8. package/dist/{chunk-UI3XXVJ6.js → chunk-W4VFKZVF.js} +58 -1
  9. package/dist/{graph-5VSRBRKZ.js → chunk-Z7W7HNRG.js} +2 -1
  10. package/dist/context-audit-RI4R2WRH.js +549 -0
  11. package/dist/{diff-4XJZN4OB.js → diff-QC7PWIPF.js} +5 -5
  12. package/dist/{doctor-FINKMI66.js → doctor-RVODPMHJ.js} +1 -1
  13. package/dist/graph-ERNQQQ7C.js +12 -0
  14. package/dist/index.js +64 -30
  15. package/dist/mcp.js +841 -17
  16. package/dist/{orchestrate-6XGEA655.js → orchestrate-NNNWNELP.js} +8 -8
  17. package/dist/pipeline-3G2FRAKM.js +263 -0
  18. package/dist/{probe-T77FFIAG.js → probe-SN4BNXOC.js} +2 -1
  19. package/dist/{providers-VIBWDN5D.js → providers-NKGY36QF.js} +1 -1
  20. package/dist/{shift-SW3GSODO.js → shift-G42AEUHE.js} +15 -14
  21. package/dist/{spawn-JSV2HST3.js → spawn-52PASJJL.js} +3 -3
  22. package/dist/sweep-5POCF2E4.js +934 -0
  23. package/dist/{team-YIYA4ZLX.js → team-JZHIH7H5.js} +6 -6
  24. package/dist/university-content/courses/.purpose +307 -0
  25. package/dist/university-content/plsat/.purpose +131 -0
  26. package/dist/{workspace-S5Q5LVA6.js → workspace-L27RR5MF.js} +3 -2
  27. package/package.json +1 -1
  28. package/dist/chunk-ZMN3RAIT.js +0 -564
  29. package/dist/{chunk-XNUWLW73.js → chunk-7WTOOH23.js} +0 -0
  30. package/dist/{flow-UFMPVOEM.js → flow-KZKMMXJC.js} +1 -1
@@ -0,0 +1,934 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ recordLore,
4
+ resolveAuthor
5
+ } from "./chunk-BRILIG7Z.js";
6
+ import {
7
+ log
8
+ } from "./chunk-4NCFWYGG.js";
9
+ import "./chunk-ZXMDA7VB.js";
10
+
11
+ // src/commands/sweep/index.ts
12
+ import * as fs from "fs";
13
+ import * as path from "path";
14
+ import chalk from "chalk";
15
+ import ora from "ora";
16
+ import * as yaml from "js-yaml";
17
+ import { glob } from "glob";
18
+ var SKIP_DIRS = /* @__PURE__ */ new Set(["node_modules", "dist", ".git", ".next", "build", "coverage", "__pycache__", "target"]);
19
+ var CODE_EXTS = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".java", ".rb", ".ex", ".exs"]);
20
+ var STALE_THRESHOLD_DAYS = 14;
21
+ var COVERAGE_THRESHOLD = 90;
22
+ function todayISO() {
23
+ return (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
24
+ }
25
+ function shouldFix(options) {
26
+ return !options.dry && !options.skipFix;
27
+ }
28
+ function findPurposeFilesSync(rootDir) {
29
+ const found = [];
30
+ function walk(dir) {
31
+ let entries;
32
+ try {
33
+ entries = fs.readdirSync(dir, { withFileTypes: true });
34
+ } catch {
35
+ return;
36
+ }
37
+ for (const entry of entries) {
38
+ if (SKIP_DIRS.has(entry.name)) continue;
39
+ const full = path.join(dir, entry.name);
40
+ if (entry.isDirectory()) {
41
+ walk(full);
42
+ } else if (entry.name === ".purpose") {
43
+ found.push(full);
44
+ }
45
+ }
46
+ }
47
+ walk(rootDir);
48
+ return found;
49
+ }
50
+ function findSourceDirs(rootDir) {
51
+ const dirs = /* @__PURE__ */ new Set();
52
+ function walk(dir) {
53
+ let entries;
54
+ try {
55
+ entries = fs.readdirSync(dir, { withFileTypes: true });
56
+ } catch {
57
+ return;
58
+ }
59
+ let hasCode = false;
60
+ for (const entry of entries) {
61
+ if (SKIP_DIRS.has(entry.name)) continue;
62
+ const full = path.join(dir, entry.name);
63
+ if (entry.isDirectory()) {
64
+ walk(full);
65
+ } else if (CODE_EXTS.has(path.extname(entry.name))) {
66
+ hasCode = true;
67
+ }
68
+ }
69
+ if (hasCode) {
70
+ dirs.add(dir);
71
+ }
72
+ }
73
+ walk(rootDir);
74
+ return [...dirs];
75
+ }
76
+ function loadScanIndex(rootDir) {
77
+ const indexPath = path.join(rootDir, ".paradigm", "scan-index.json");
78
+ if (!fs.existsSync(indexPath)) return null;
79
+ try {
80
+ return JSON.parse(fs.readFileSync(indexPath, "utf8"));
81
+ } catch {
82
+ return null;
83
+ }
84
+ }
85
+ function loadFlowIndex(rootDir) {
86
+ const indexPath = path.join(rootDir, ".paradigm", "flow-index.json");
87
+ if (!fs.existsSync(indexPath)) return null;
88
+ try {
89
+ return JSON.parse(fs.readFileSync(indexPath, "utf8"));
90
+ } catch {
91
+ return null;
92
+ }
93
+ }
94
+ function extractAllSymbols(index) {
95
+ const map = /* @__PURE__ */ new Map();
96
+ const categories = ["components", "features", "flows", "state", "gates", "signals", "aspects"];
97
+ for (const cat of categories) {
98
+ const bucket = index[cat];
99
+ if (!bucket || typeof bucket !== "object") continue;
100
+ for (const entry of Object.values(bucket)) {
101
+ if (entry.symbol) {
102
+ map.set(entry.symbol, entry);
103
+ }
104
+ }
105
+ }
106
+ return map;
107
+ }
108
+ function countCrossReferences(symbol, purposeFiles, definitionFile) {
109
+ let count = 0;
110
+ for (const pf of purposeFiles) {
111
+ if (pf === definitionFile) continue;
112
+ try {
113
+ const content = fs.readFileSync(pf, "utf8");
114
+ if (content.includes(symbol)) {
115
+ count++;
116
+ }
117
+ } catch {
118
+ }
119
+ }
120
+ return count;
121
+ }
122
+ function checkOrphanedSymbols(_rootDir, index, purposeFiles, options) {
123
+ const results = [];
124
+ if (!index) {
125
+ results.push({ check: "orphaned-symbols", category: "orphaned", status: "ok", message: "Skipped \u2014 no scan-index.json" });
126
+ return results;
127
+ }
128
+ const allSymbols = extractAllSymbols(index);
129
+ let orphanCount = 0;
130
+ let fixedCount = 0;
131
+ for (const [symbol, entry] of allSymbols) {
132
+ const refs = countCrossReferences(symbol, purposeFiles, entry.path);
133
+ if (refs === 0) {
134
+ orphanCount++;
135
+ if (shouldFix(options)) {
136
+ const purposePath = entry.path.endsWith(".purpose") ? entry.path : path.join(entry.path, ".purpose");
137
+ if (fs.existsSync(purposePath)) {
138
+ try {
139
+ let content = fs.readFileSync(purposePath, "utf8");
140
+ if (!content.includes(`# orphan-detected:`) || !content.includes(symbol)) {
141
+ content += `
142
+ # orphan-detected: ${todayISO()} \u2014 ${symbol} has 0 cross-references
143
+ `;
144
+ fs.writeFileSync(purposePath, content, "utf8");
145
+ fixedCount++;
146
+ results.push({
147
+ check: "orphaned-symbols",
148
+ category: "orphaned",
149
+ status: "fixed",
150
+ symbol,
151
+ file: purposePath,
152
+ message: `${symbol} \u2014 0 cross-references, marked orphan`,
153
+ fixAction: "Added orphan-detected comment"
154
+ });
155
+ continue;
156
+ }
157
+ } catch {
158
+ }
159
+ }
160
+ }
161
+ results.push({
162
+ check: "orphaned-symbols",
163
+ category: "orphaned",
164
+ status: "entropy",
165
+ symbol,
166
+ file: entry.path,
167
+ message: `${symbol} \u2014 0 cross-references from other .purpose files`
168
+ });
169
+ }
170
+ }
171
+ if (orphanCount === 0) {
172
+ results.push({ check: "orphaned-symbols", category: "orphaned", status: "ok", message: `All ${allSymbols.size} symbols have cross-references` });
173
+ }
174
+ return results;
175
+ }
176
+ function checkStalePurpose(rootDir, purposeFiles, options) {
177
+ const results = [];
178
+ let staleCount = 0;
179
+ for (const pf of purposeFiles) {
180
+ const dir = path.dirname(pf);
181
+ let purposeMtime;
182
+ try {
183
+ purposeMtime = fs.statSync(pf).mtime.getTime();
184
+ } catch {
185
+ continue;
186
+ }
187
+ let newestCodeMtime = 0;
188
+ let newestCodeFile = "";
189
+ try {
190
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
191
+ for (const entry of entries) {
192
+ if (!entry.isFile()) continue;
193
+ if (!CODE_EXTS.has(path.extname(entry.name))) continue;
194
+ const stat = fs.statSync(path.join(dir, entry.name));
195
+ if (stat.mtime.getTime() > newestCodeMtime) {
196
+ newestCodeMtime = stat.mtime.getTime();
197
+ newestCodeFile = entry.name;
198
+ }
199
+ }
200
+ } catch {
201
+ continue;
202
+ }
203
+ if (newestCodeMtime === 0) continue;
204
+ const diffDays = (newestCodeMtime - purposeMtime) / (1e3 * 60 * 60 * 24);
205
+ if (diffDays > STALE_THRESHOLD_DAYS) {
206
+ staleCount++;
207
+ const relPath = path.relative(rootDir, pf);
208
+ const staleDays = Math.floor(diffDays);
209
+ if (shouldFix(options)) {
210
+ try {
211
+ let content = fs.readFileSync(pf, "utf8");
212
+ if (!content.includes("# stale-since:")) {
213
+ content += `
214
+ # stale-since: ${todayISO()} \u2014 code in ${newestCodeFile} is ${staleDays} days newer
215
+ `;
216
+ fs.writeFileSync(pf, content, "utf8");
217
+ results.push({
218
+ check: "stale-purpose",
219
+ category: "stale",
220
+ status: "fixed",
221
+ file: relPath,
222
+ message: `${relPath} \u2014 ${staleDays} days behind ${newestCodeFile}`,
223
+ fixAction: "Added stale-since marker"
224
+ });
225
+ continue;
226
+ }
227
+ } catch {
228
+ }
229
+ }
230
+ results.push({
231
+ check: "stale-purpose",
232
+ category: "stale",
233
+ status: "entropy",
234
+ file: relPath,
235
+ message: `${relPath} \u2014 ${staleDays} days behind ${newestCodeFile}`
236
+ });
237
+ }
238
+ }
239
+ if (staleCount === 0) {
240
+ results.push({ check: "stale-purpose", category: "stale", status: "ok", message: `All ${purposeFiles.length} .purpose files are fresh (within ${STALE_THRESHOLD_DAYS} days)` });
241
+ }
242
+ return results;
243
+ }
244
+ async function checkPhantomGates(rootDir, options) {
245
+ const results = [];
246
+ const portalPath = path.join(rootDir, "portal.yaml");
247
+ if (!fs.existsSync(portalPath)) {
248
+ results.push({ check: "phantom-gates", category: "phantom", status: "ok", message: "No portal.yaml \u2014 skipped" });
249
+ return results;
250
+ }
251
+ let portalContent;
252
+ try {
253
+ portalContent = fs.readFileSync(portalPath, "utf8");
254
+ } catch {
255
+ results.push({ check: "phantom-gates", category: "phantom", status: "ok", message: "Could not read portal.yaml \u2014 skipped" });
256
+ return results;
257
+ }
258
+ const portal = yaml.load(portalContent);
259
+ if (!portal?.gates) {
260
+ results.push({ check: "phantom-gates", category: "phantom", status: "ok", message: "No gates defined in portal.yaml" });
261
+ return results;
262
+ }
263
+ const gateNames = Object.keys(portal.gates);
264
+ let phantomCount = 0;
265
+ const codeFiles = await glob("**/*.{ts,tsx,js,jsx,py,rs,go,java,rb}", {
266
+ cwd: rootDir,
267
+ absolute: true,
268
+ ignore: ["**/node_modules/**", "**/dist/**", "**/.git/**", "**/build/**", "**/coverage/**"]
269
+ });
270
+ for (const gateName of gateNames) {
271
+ const bareGate = gateName.startsWith("^") ? gateName.slice(1) : gateName;
272
+ let found = false;
273
+ for (const codeFile of codeFiles) {
274
+ try {
275
+ const content = fs.readFileSync(codeFile, "utf8");
276
+ if (content.includes(bareGate)) {
277
+ found = true;
278
+ break;
279
+ }
280
+ } catch {
281
+ continue;
282
+ }
283
+ }
284
+ if (!found) {
285
+ phantomCount++;
286
+ const symbol = gateName.startsWith("^") ? gateName : `^${gateName}`;
287
+ if (shouldFix(options)) {
288
+ try {
289
+ const freshContent = fs.readFileSync(portalPath, "utf8");
290
+ const freshPortal = yaml.load(freshContent);
291
+ if (freshPortal.gates && freshPortal.gates[gateName]) {
292
+ delete freshPortal.gates[gateName];
293
+ }
294
+ if (freshPortal.routes) {
295
+ for (const [route, gates] of Object.entries(freshPortal.routes)) {
296
+ if (Array.isArray(gates) && gates.includes(symbol)) {
297
+ const filtered = gates.filter((g) => g !== symbol);
298
+ if (filtered.length === 0) {
299
+ delete freshPortal.routes[route];
300
+ } else {
301
+ freshPortal.routes[route] = filtered;
302
+ }
303
+ }
304
+ }
305
+ }
306
+ fs.writeFileSync(portalPath, yaml.dump(freshPortal, { lineWidth: -1, noRefs: true }), "utf8");
307
+ results.push({
308
+ check: "phantom-gates",
309
+ category: "phantom",
310
+ status: "fixed",
311
+ symbol,
312
+ file: "portal.yaml",
313
+ message: `${symbol} \u2014 no code implementation found`,
314
+ fixAction: "Removed from portal.yaml"
315
+ });
316
+ continue;
317
+ } catch {
318
+ }
319
+ }
320
+ results.push({
321
+ check: "phantom-gates",
322
+ category: "phantom",
323
+ status: "entropy",
324
+ symbol,
325
+ file: "portal.yaml",
326
+ message: `${symbol} \u2014 defined in portal.yaml but no code references "${bareGate}"`
327
+ });
328
+ }
329
+ }
330
+ if (phantomCount === 0) {
331
+ results.push({ check: "phantom-gates", category: "phantom", status: "ok", message: `All ${gateNames.length} gates have code references` });
332
+ }
333
+ return results;
334
+ }
335
+ async function checkDeadSignals(rootDir, index, _purposeFiles, options) {
336
+ const results = [];
337
+ if (!index) {
338
+ results.push({ check: "dead-signals", category: "dead", status: "ok", message: "Skipped \u2014 no scan-index.json" });
339
+ return results;
340
+ }
341
+ const signals = index.signals || {};
342
+ const signalEntries = Object.values(signals);
343
+ if (signalEntries.length === 0) {
344
+ results.push({ check: "dead-signals", category: "dead", status: "ok", message: "No signals defined" });
345
+ return results;
346
+ }
347
+ const codeFiles = await glob("**/*.{ts,tsx,js,jsx,py,rs,go,java,rb}", {
348
+ cwd: rootDir,
349
+ absolute: true,
350
+ ignore: ["**/node_modules/**", "**/dist/**", "**/.git/**", "**/build/**", "**/coverage/**"]
351
+ });
352
+ let deadCount = 0;
353
+ for (const signal of signalEntries) {
354
+ const bareSignal = signal.id;
355
+ let found = false;
356
+ for (const codeFile of codeFiles) {
357
+ try {
358
+ const content = fs.readFileSync(codeFile, "utf8");
359
+ if (content.includes(bareSignal)) {
360
+ found = true;
361
+ break;
362
+ }
363
+ } catch {
364
+ continue;
365
+ }
366
+ }
367
+ if (!found) {
368
+ deadCount++;
369
+ const symbol = signal.symbol;
370
+ if (shouldFix(options)) {
371
+ const purposePath = signal.path.endsWith(".purpose") ? signal.path : path.join(signal.path, ".purpose");
372
+ if (fs.existsSync(purposePath)) {
373
+ try {
374
+ let content = fs.readFileSync(purposePath, "utf8");
375
+ if (!content.includes(`# dead-signal: ${symbol}`)) {
376
+ content += `
377
+ # dead-signal: ${symbol} \u2014 no handler/listener found (${todayISO()})
378
+ `;
379
+ fs.writeFileSync(purposePath, content, "utf8");
380
+ results.push({
381
+ check: "dead-signals",
382
+ category: "dead",
383
+ status: "fixed",
384
+ symbol,
385
+ file: purposePath,
386
+ message: `${symbol} \u2014 no handler or listener in codebase`,
387
+ fixAction: "Added dead-signal marker"
388
+ });
389
+ continue;
390
+ }
391
+ } catch {
392
+ }
393
+ }
394
+ }
395
+ results.push({
396
+ check: "dead-signals",
397
+ category: "dead",
398
+ status: "entropy",
399
+ symbol,
400
+ file: signal.path,
401
+ message: `${symbol} \u2014 no handler or listener references in codebase`
402
+ });
403
+ }
404
+ }
405
+ if (deadCount === 0) {
406
+ results.push({ check: "dead-signals", category: "dead", status: "ok", message: `All ${signalEntries.length} signals have code references` });
407
+ }
408
+ return results;
409
+ }
410
+ function checkBrokenFlows(rootDir, index, flowIndex, options) {
411
+ const results = [];
412
+ if (!flowIndex) {
413
+ results.push({ check: "broken-flows", category: "broken", status: "ok", message: "No flow-index.json \u2014 skipped" });
414
+ return results;
415
+ }
416
+ if (!index) {
417
+ results.push({ check: "broken-flows", category: "broken", status: "ok", message: "No scan-index.json \u2014 skipped" });
418
+ return results;
419
+ }
420
+ const allSymbols = extractAllSymbols(index);
421
+ let brokenCount = 0;
422
+ for (const [flowId, flow] of Object.entries(flowIndex.flows)) {
423
+ for (const step of flow.steps) {
424
+ if (!step.symbol) continue;
425
+ if (!allSymbols.has(step.symbol)) {
426
+ brokenCount++;
427
+ if (shouldFix(options)) {
428
+ const purposePath = path.join(rootDir, flow.definedIn);
429
+ if (fs.existsSync(purposePath)) {
430
+ try {
431
+ let content = fs.readFileSync(purposePath, "utf8");
432
+ if (!content.includes(`# broken: ${step.symbol}`)) {
433
+ content += `
434
+ # broken: ${step.symbol} not found \u2014 ${flowId} step ${step.id} (${todayISO()})
435
+ `;
436
+ fs.writeFileSync(purposePath, content, "utf8");
437
+ results.push({
438
+ check: "broken-flows",
439
+ category: "broken",
440
+ status: "fixed",
441
+ symbol: step.symbol,
442
+ file: flow.definedIn,
443
+ message: `${flowId} step ${step.id} references ${step.symbol} \u2014 not found`,
444
+ fixAction: "Added broken step comment"
445
+ });
446
+ continue;
447
+ }
448
+ } catch {
449
+ }
450
+ }
451
+ }
452
+ results.push({
453
+ check: "broken-flows",
454
+ category: "broken",
455
+ status: "entropy",
456
+ symbol: step.symbol,
457
+ file: flow.definedIn,
458
+ message: `${flowId} step ${step.id} references ${step.symbol} \u2014 symbol not in scan-index`
459
+ });
460
+ }
461
+ }
462
+ }
463
+ if (brokenCount === 0) {
464
+ results.push({ check: "broken-flows", category: "broken", status: "ok", message: `All flow steps reference valid symbols (${Object.keys(flowIndex.flows).length} flows)` });
465
+ }
466
+ return results;
467
+ }
468
+ function checkLoreRot(rootDir, index, options) {
469
+ const results = [];
470
+ const loreDir = path.join(rootDir, ".paradigm", "lore", "entries");
471
+ if (!fs.existsSync(loreDir)) {
472
+ results.push({ check: "lore-rot", category: "lore-rot", status: "ok", message: "No lore entries \u2014 skipped" });
473
+ return results;
474
+ }
475
+ const allSymbols = index ? extractAllSymbols(index) : /* @__PURE__ */ new Map();
476
+ let rotCount = 0;
477
+ let dateDirs;
478
+ try {
479
+ dateDirs = fs.readdirSync(loreDir).filter((d) => {
480
+ const full = path.join(loreDir, d);
481
+ return fs.statSync(full).isDirectory();
482
+ });
483
+ } catch {
484
+ results.push({ check: "lore-rot", category: "lore-rot", status: "ok", message: "Could not read lore directory \u2014 skipped" });
485
+ return results;
486
+ }
487
+ let entryCount = 0;
488
+ for (const dateDir of dateDirs) {
489
+ const datePath = path.join(loreDir, dateDir);
490
+ let files;
491
+ try {
492
+ files = fs.readdirSync(datePath).filter((f) => f.endsWith(".yaml") || f.endsWith(".lore"));
493
+ } catch {
494
+ continue;
495
+ }
496
+ for (const file of files) {
497
+ entryCount++;
498
+ const filePath = path.join(datePath, file);
499
+ let entry;
500
+ try {
501
+ entry = yaml.load(fs.readFileSync(filePath, "utf8"));
502
+ } catch {
503
+ continue;
504
+ }
505
+ const symbolsTouched = entry.symbols_touched || [];
506
+ const filesModified = entry.files_modified || [];
507
+ const filesCreated = entry.files_created || [];
508
+ const tags = entry.tags || [];
509
+ const deadSymbols = [];
510
+ if (index) {
511
+ for (const sym of symbolsTouched) {
512
+ if (!allSymbols.has(sym)) {
513
+ deadSymbols.push(sym);
514
+ }
515
+ }
516
+ }
517
+ const deadFiles = [];
518
+ for (const f of [...filesModified, ...filesCreated]) {
519
+ const absFile = path.isAbsolute(f) ? f : path.join(rootDir, f);
520
+ if (!fs.existsSync(absFile)) {
521
+ deadFiles.push(f);
522
+ }
523
+ }
524
+ if (deadSymbols.length > 0 || deadFiles.length > 0) {
525
+ rotCount++;
526
+ const entryId = entry.id || file;
527
+ const details = [
528
+ ...deadSymbols.length > 0 ? [`dead symbols: ${deadSymbols.join(", ")}`] : [],
529
+ ...deadFiles.length > 0 ? [`missing files: ${deadFiles.length}`] : []
530
+ ].join("; ");
531
+ if (shouldFix(options)) {
532
+ try {
533
+ if (!tags.includes("stale")) {
534
+ const updatedTags = [...tags, "stale"];
535
+ entry.tags = updatedTags;
536
+ fs.writeFileSync(filePath, yaml.dump(entry, { lineWidth: -1, noRefs: true }), "utf8");
537
+ results.push({
538
+ check: "lore-rot",
539
+ category: "lore-rot",
540
+ status: "fixed",
541
+ symbol: entryId,
542
+ file: filePath,
543
+ message: `${entryId} \u2014 ${details}`,
544
+ fixAction: "Added stale tag"
545
+ });
546
+ continue;
547
+ }
548
+ } catch {
549
+ }
550
+ }
551
+ results.push({
552
+ check: "lore-rot",
553
+ category: "lore-rot",
554
+ status: "entropy",
555
+ symbol: entryId,
556
+ file: filePath,
557
+ message: `${entryId} \u2014 ${details}`
558
+ });
559
+ }
560
+ }
561
+ }
562
+ if (rotCount === 0) {
563
+ results.push({ check: "lore-rot", category: "lore-rot", status: "ok", message: `All ${entryCount} lore entries have valid references` });
564
+ }
565
+ return results;
566
+ }
567
+ function checkTagOrphans(rootDir, purposeFiles, options) {
568
+ const results = [];
569
+ const tagsPath = path.join(rootDir, ".paradigm", "tags.yaml");
570
+ if (!fs.existsSync(tagsPath)) {
571
+ results.push({ check: "tag-orphans", category: "tag-orphan", status: "ok", message: "No tags.yaml \u2014 skipped" });
572
+ return results;
573
+ }
574
+ let tagsData;
575
+ try {
576
+ tagsData = yaml.load(fs.readFileSync(tagsPath, "utf8"));
577
+ } catch {
578
+ results.push({ check: "tag-orphans", category: "tag-orphan", status: "ok", message: "Could not parse tags.yaml \u2014 skipped" });
579
+ return results;
580
+ }
581
+ const definedTags = /* @__PURE__ */ new Set();
582
+ for (const section of ["core", "project"]) {
583
+ const bucket = tagsData[section];
584
+ if (bucket && typeof bucket === "object") {
585
+ for (const tag of Object.keys(bucket)) {
586
+ definedTags.add(tag);
587
+ }
588
+ }
589
+ }
590
+ if (definedTags.size === 0) {
591
+ results.push({ check: "tag-orphans", category: "tag-orphan", status: "ok", message: "No tags defined" });
592
+ return results;
593
+ }
594
+ const usedTags = /* @__PURE__ */ new Set();
595
+ for (const pf of purposeFiles) {
596
+ try {
597
+ const content = fs.readFileSync(pf, "utf8");
598
+ const tagMatches = content.matchAll(/tags:\s*\[([^\]]*)\]/g);
599
+ for (const match of tagMatches) {
600
+ const tagList = match[1].split(",").map((t) => t.trim().replace(/['"]/g, "")).filter(Boolean);
601
+ for (const tag of tagList) {
602
+ usedTags.add(tag);
603
+ }
604
+ }
605
+ const yamlData = yaml.load(content);
606
+ if (yamlData) {
607
+ let collectTags2 = function(obj) {
608
+ if (Array.isArray(obj)) {
609
+ for (const item of obj) collectTags2(item);
610
+ } else if (obj && typeof obj === "object") {
611
+ const rec = obj;
612
+ if (Array.isArray(rec.tags)) {
613
+ for (const t of rec.tags) {
614
+ if (typeof t === "string") usedTags.add(t);
615
+ }
616
+ }
617
+ for (const v of Object.values(rec)) collectTags2(v);
618
+ }
619
+ };
620
+ var collectTags = collectTags2;
621
+ collectTags2(yamlData);
622
+ }
623
+ } catch {
624
+ continue;
625
+ }
626
+ }
627
+ let orphanCount = 0;
628
+ for (const tag of definedTags) {
629
+ if (!usedTags.has(tag)) {
630
+ orphanCount++;
631
+ if (shouldFix(options)) {
632
+ try {
633
+ const freshData = yaml.load(fs.readFileSync(tagsPath, "utf8"));
634
+ let removed = false;
635
+ for (const section of ["core", "project"]) {
636
+ if (freshData[section] && freshData[section][tag]) {
637
+ delete freshData[section][tag];
638
+ removed = true;
639
+ }
640
+ }
641
+ if (removed) {
642
+ fs.writeFileSync(tagsPath, yaml.dump(freshData, { lineWidth: -1, noRefs: true }), "utf8");
643
+ results.push({
644
+ check: "tag-orphans",
645
+ category: "tag-orphan",
646
+ status: "fixed",
647
+ symbol: tag,
648
+ file: "tags.yaml",
649
+ message: `Tag "${tag}" \u2014 not used in any .purpose file`,
650
+ fixAction: "Removed from tags.yaml"
651
+ });
652
+ continue;
653
+ }
654
+ } catch {
655
+ }
656
+ }
657
+ results.push({
658
+ check: "tag-orphans",
659
+ category: "tag-orphan",
660
+ status: "entropy",
661
+ symbol: tag,
662
+ file: ".paradigm/tags.yaml",
663
+ message: `Tag "${tag}" \u2014 defined in tag bank but not used in any .purpose file`
664
+ });
665
+ }
666
+ }
667
+ if (orphanCount === 0) {
668
+ results.push({ check: "tag-orphans", category: "tag-orphan", status: "ok", message: `All ${definedTags.size} tags are in use` });
669
+ }
670
+ return results;
671
+ }
672
+ function checkAspectSemanticDrift(rootDir, index, _options) {
673
+ const results = [];
674
+ const dbPath = path.join(rootDir, ".paradigm", "aspect-graph.db");
675
+ if (!fs.existsSync(dbPath)) {
676
+ results.push({ check: "aspect-semantic-drift", category: "semantic-drift", status: "ok", message: "No aspect-graph.db \u2014 skipped" });
677
+ return results;
678
+ }
679
+ if (!index || !index.aspects) {
680
+ results.push({ check: "aspect-semantic-drift", category: "semantic-drift", status: "ok", message: "No aspects in scan-index \u2014 skipped" });
681
+ return results;
682
+ }
683
+ const aspects = index.aspects;
684
+ let driftCount = 0;
685
+ for (const [, aspect] of Object.entries(aspects)) {
686
+ const purposePath = aspect.path.endsWith(".purpose") ? aspect.path : path.join(aspect.path, ".purpose");
687
+ if (!fs.existsSync(purposePath)) continue;
688
+ try {
689
+ const content = fs.readFileSync(purposePath, "utf8");
690
+ const anchorPattern = /anchors?:\s*\n((?:\s+-\s+.+\n?)*)/g;
691
+ const anchorMatch = anchorPattern.exec(content);
692
+ if (!anchorMatch) continue;
693
+ const anchorLines = anchorMatch[1].split("\n").filter((l) => l.trim().startsWith("-"));
694
+ for (const line of anchorLines) {
695
+ const match = line.match(/- (.+):(\d+)(?:-(\d+))?/);
696
+ if (!match) continue;
697
+ const anchorFile = match[1].trim();
698
+ const startLine = parseInt(match[2], 10);
699
+ const endLine = match[3] ? parseInt(match[3], 10) : startLine;
700
+ const absAnchorFile = path.isAbsolute(anchorFile) ? anchorFile : path.join(rootDir, anchorFile);
701
+ if (!fs.existsSync(absAnchorFile)) continue;
702
+ try {
703
+ const fileContent = fs.readFileSync(absAnchorFile, "utf8");
704
+ const lineCount = fileContent.split("\n").length;
705
+ if (startLine > lineCount || endLine > lineCount) {
706
+ driftCount++;
707
+ }
708
+ } catch {
709
+ }
710
+ }
711
+ } catch {
712
+ continue;
713
+ }
714
+ }
715
+ if (driftCount > 0) {
716
+ results.push({
717
+ check: "aspect-semantic-drift",
718
+ category: "semantic-drift",
719
+ status: "entropy",
720
+ message: `${driftCount} aspect anchor(s) reference lines beyond file bounds \u2014 run "paradigm drift check" to repair`
721
+ });
722
+ } else {
723
+ results.push({ check: "aspect-semantic-drift", category: "semantic-drift", status: "ok", message: `Aspect anchors stable (use "paradigm drift check" for deep analysis)` });
724
+ }
725
+ return results;
726
+ }
727
+ function checkCoverageDecay(rootDir, purposeFiles, options) {
728
+ const results = [];
729
+ const sourceDirs = findSourceDirs(rootDir);
730
+ if (sourceDirs.length === 0) {
731
+ results.push({ check: "coverage-decay", category: "coverage-decay", status: "ok", message: "No source directories found" });
732
+ return results;
733
+ }
734
+ const purposeDirs = new Set(purposeFiles.map((pf) => path.dirname(pf)));
735
+ const uncoveredDirs = [];
736
+ for (const dir of sourceDirs) {
737
+ let covered = false;
738
+ let current = dir;
739
+ while (current.startsWith(rootDir)) {
740
+ if (purposeDirs.has(current)) {
741
+ covered = true;
742
+ break;
743
+ }
744
+ const parent = path.dirname(current);
745
+ if (parent === current) break;
746
+ current = parent;
747
+ }
748
+ if (!covered) {
749
+ uncoveredDirs.push(dir);
750
+ }
751
+ }
752
+ const coveragePct = sourceDirs.length > 0 ? Math.round((sourceDirs.length - uncoveredDirs.length) / sourceDirs.length * 100) : 100;
753
+ if (coveragePct < COVERAGE_THRESHOLD) {
754
+ if (shouldFix(options)) {
755
+ let generatedCount = 0;
756
+ for (const dir of uncoveredDirs) {
757
+ const purposePath = path.join(dir, ".purpose");
758
+ if (!fs.existsSync(purposePath)) {
759
+ try {
760
+ const dirName = path.basename(dir);
761
+ const relDir = path.relative(rootDir, dir);
762
+ const skeleton = [
763
+ `# ${dirName}`,
764
+ `# Auto-generated by paradigm sweep \u2014 ${todayISO()}`,
765
+ `# TODO: Add component descriptions and symbol declarations`,
766
+ ``,
767
+ `#${dirName}:`,
768
+ ` description: "${relDir}"`,
769
+ ` tags: []`,
770
+ ``
771
+ ].join("\n");
772
+ fs.writeFileSync(purposePath, skeleton, "utf8");
773
+ generatedCount++;
774
+ } catch {
775
+ }
776
+ }
777
+ }
778
+ if (generatedCount > 0) {
779
+ results.push({
780
+ check: "coverage-decay",
781
+ category: "coverage-decay",
782
+ status: "fixed",
783
+ message: `Coverage at ${coveragePct}% (threshold: ${COVERAGE_THRESHOLD}%) \u2014 generated ${generatedCount} skeleton .purpose files`,
784
+ fixAction: `Generated ${generatedCount} skeleton .purpose files`
785
+ });
786
+ return results;
787
+ }
788
+ }
789
+ results.push({
790
+ check: "coverage-decay",
791
+ category: "coverage-decay",
792
+ status: "entropy",
793
+ message: `Coverage at ${coveragePct}% \u2014 below ${COVERAGE_THRESHOLD}% threshold (${uncoveredDirs.length} directories uncovered)`
794
+ });
795
+ } else {
796
+ results.push({
797
+ check: "coverage-decay",
798
+ category: "coverage-decay",
799
+ status: "ok",
800
+ message: `Coverage at ${coveragePct}% (${sourceDirs.length} source dirs, ${purposeFiles.length} .purpose files)`
801
+ });
802
+ }
803
+ return results;
804
+ }
805
+ async function recordSweepLore(rootDir, allResults) {
806
+ const entropyResults = allResults.filter((r) => r.status === "entropy");
807
+ const fixedResults = allResults.filter((r) => r.status === "fixed");
808
+ const okResults = allResults.filter((r) => r.status === "ok");
809
+ const categories = new Set(allResults.map((r) => r.category));
810
+ const symbolsTouched = allResults.filter((r) => r.symbol).map((r) => r.symbol).filter((s, i, arr) => arr.indexOf(s) === i).slice(0, 20);
811
+ const summary = [
812
+ `Sweep completed: ${categories.size} categories checked.`,
813
+ entropyResults.length > 0 ? `${entropyResults.length} entropy issue(s) found.` : "",
814
+ fixedResults.length > 0 ? `${fixedResults.length} issue(s) auto-resolved.` : "",
815
+ `${okResults.length} categories healthy.`
816
+ ].filter(Boolean).join(" ");
817
+ const entry = {
818
+ id: "",
819
+ type: "insight",
820
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
821
+ author: resolveAuthor(),
822
+ title: `Sweep: ${entropyResults.length + fixedResults.length} issues, ${fixedResults.length} fixed`,
823
+ summary,
824
+ symbols_touched: symbolsTouched,
825
+ tags: ["arc:sweep", "automated"]
826
+ };
827
+ try {
828
+ await recordLore(rootDir, entry);
829
+ return entry.id;
830
+ } catch {
831
+ return null;
832
+ }
833
+ }
834
+ async function sweepCommand(options) {
835
+ const rootDir = options.rootDir || process.cwd();
836
+ const quiet = options.quiet;
837
+ const spinner = ora();
838
+ const tracker = log.command("sweep").start("Running entropy detection");
839
+ if (!quiet) {
840
+ console.log(chalk.blue("\nParadigm Sweep Report") + chalk.gray(` \u2014 ${todayISO()}`));
841
+ console.log(chalk.gray("==================================="));
842
+ if (options.dry || options.skipFix) {
843
+ console.log(chalk.yellow(" Mode: dry run (no fixes applied)\n"));
844
+ }
845
+ }
846
+ spinner.start("Loading project data...");
847
+ const purposeFiles = findPurposeFilesSync(rootDir);
848
+ const scanIndex = loadScanIndex(rootDir);
849
+ const flowIndex = loadFlowIndex(rootDir);
850
+ spinner.succeed(`Loaded: ${purposeFiles.length} .purpose files, scan-index ${scanIndex ? "present" : "absent"}`);
851
+ const allResults = [];
852
+ const checks = [
853
+ { name: "Orphaned symbols", run: () => checkOrphanedSymbols(rootDir, scanIndex, purposeFiles, options) },
854
+ { name: "Stale purpose files", run: () => checkStalePurpose(rootDir, purposeFiles, options) },
855
+ { name: "Phantom gates", run: () => checkPhantomGates(rootDir, options) },
856
+ { name: "Dead signals", run: () => checkDeadSignals(rootDir, scanIndex, purposeFiles, options) },
857
+ { name: "Broken flows", run: () => checkBrokenFlows(rootDir, scanIndex, flowIndex, options) },
858
+ { name: "Lore rot", run: () => checkLoreRot(rootDir, scanIndex, options) },
859
+ { name: "Tag orphans", run: () => checkTagOrphans(rootDir, purposeFiles, options) },
860
+ { name: "Aspect semantic drift", run: () => checkAspectSemanticDrift(rootDir, scanIndex, options) },
861
+ { name: "Coverage decay", run: () => checkCoverageDecay(rootDir, purposeFiles, options) }
862
+ ];
863
+ for (const check of checks) {
864
+ spinner.start(`Checking: ${check.name}...`);
865
+ const checkResults = await check.run();
866
+ allResults.push(...checkResults);
867
+ const hasEntropy2 = checkResults.some((r) => r.status === "entropy");
868
+ const hasFixed = checkResults.some((r) => r.status === "fixed");
869
+ if (hasEntropy2) {
870
+ spinner.warn(chalk.yellow(`${check.name}`));
871
+ } else if (hasFixed) {
872
+ spinner.succeed(chalk.green(`${check.name} (auto-fixed)`));
873
+ } else {
874
+ spinner.succeed(chalk.green(check.name));
875
+ }
876
+ }
877
+ if (!quiet) {
878
+ const symbolCount = scanIndex ? extractAllSymbols(scanIndex).size : 0;
879
+ console.log(chalk.gray(`
880
+ Checked: 9 categories, ${symbolCount} symbols, ${purposeFiles.length} .purpose files
881
+ `));
882
+ const entropyResults = allResults.filter((r) => r.status === "entropy");
883
+ if (entropyResults.length > 0) {
884
+ console.log(chalk.red.bold("ENTROPY FOUND:"));
885
+ for (const r of entropyResults) {
886
+ console.log(` ${chalk.red(`[${r.category}]`)} ${r.message}`);
887
+ }
888
+ console.log();
889
+ }
890
+ const okResults = allResults.filter((r) => r.status === "ok");
891
+ if (okResults.length > 0) {
892
+ console.log(chalk.green.bold("HEALTHY:"));
893
+ for (const r of okResults) {
894
+ console.log(` ${chalk.green("[ok]")} ${r.check} \u2014 ${r.message}`);
895
+ }
896
+ console.log();
897
+ }
898
+ const fixedResults = allResults.filter((r) => r.status === "fixed");
899
+ if (fixedResults.length > 0) {
900
+ console.log(chalk.cyan.bold("RESOLVED (auto-fixed):"));
901
+ for (const r of fixedResults) {
902
+ console.log(` ${chalk.cyan("[fixed]")} ${r.message}`);
903
+ if (r.fixAction) {
904
+ console.log(chalk.gray(` ${r.fixAction}`));
905
+ }
906
+ }
907
+ console.log();
908
+ }
909
+ const loreId = await recordSweepLore(rootDir, allResults);
910
+ const issueCount = entropyResults.length + fixedResults.length;
911
+ const parts = [
912
+ `${issueCount} issue${issueCount !== 1 ? "s" : ""} found`,
913
+ fixedResults.length > 0 ? `${fixedResults.length} auto-resolved` : null,
914
+ loreId ? `Lore recorded: ${loreId}` : null
915
+ ].filter(Boolean).join(", ");
916
+ console.log(chalk.gray(`Summary: ${parts}`));
917
+ console.log();
918
+ }
919
+ const hasEntropy = allResults.some((r) => r.status === "entropy");
920
+ if (hasEntropy) {
921
+ tracker.error("Entropy detected", {
922
+ entropy: allResults.filter((r) => r.status === "entropy").length,
923
+ fixed: allResults.filter((r) => r.status === "fixed").length
924
+ });
925
+ } else {
926
+ tracker.success("Clean sweep", {
927
+ fixed: allResults.filter((r) => r.status === "fixed").length,
928
+ ok: allResults.filter((r) => r.status === "ok").length
929
+ });
930
+ }
931
+ }
932
+ export {
933
+ sweepCommand
934
+ };