@saulwade/swl-ses 1.5.0 → 1.5.2

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 (134) hide show
  1. package/CLAUDE.md +19 -2
  2. package/README.md +561 -561
  3. package/agentes/arquitecto-swl.md +33 -1
  4. package/agentes/nemesis-auditor-swl.md +59 -19
  5. package/bin/swl-mcp-server.js +214 -214
  6. package/comandos/swl/.evolved.json +22 -22
  7. package/comandos/swl/contribuir.md +233 -233
  8. package/comandos/swl/nemesis.md +230 -56
  9. package/gateway/lib/event-channel.js +191 -191
  10. package/habilidades/backend-production-resilience/SKILL.md +288 -288
  11. package/habilidades/benchmark-memoria/SKILL.md +186 -186
  12. package/habilidades/diagrama-arquitectura/assets/template.html +276 -276
  13. package/habilidades/doubt-driven-review/SKILL.md +171 -171
  14. package/habilidades/doubt-driven-review/recursos/EXAMPLES.md +130 -130
  15. package/habilidades/ejecutar-task-iterativo/SKILL.md +278 -278
  16. package/habilidades/eval-framework/SKILL.md +212 -212
  17. package/habilidades/feynman-auditor-swl/SKILL.md +123 -123
  18. package/habilidades/feynman-auditor-swl/recursos/preguntas-language-agnostic.md +108 -108
  19. package/habilidades/harness-claude-code/SKILL.md +299 -299
  20. package/habilidades/infra-github-actions/SKILL.md +166 -166
  21. package/habilidades/legacy-code-rescue/SKILL.md +267 -267
  22. package/habilidades/manejo-errores/.evolved.json +8 -8
  23. package/habilidades/meta-skills-estandar/SKILL.md +225 -1
  24. package/habilidades/meta-skills-estandar/recursos/convencion-examples.md +93 -93
  25. package/habilidades/meta-skills-estandar/recursos/skills-as-agents.md +163 -163
  26. package/habilidades/nemesis-evaluacion-json/SKILL.md +266 -0
  27. package/habilidades/nemesis-redistribuir/SKILL.md +341 -0
  28. package/habilidades/node-experto/SKILL.md +105 -4
  29. package/habilidades/patrones-python/SKILL.md +229 -229
  30. package/habilidades/patrones-python/recursos/patrones-avanzados.md +469 -469
  31. package/habilidades/planear-fase/SKILL.md +319 -319
  32. package/habilidades/protocolo-revision-swl/SKILL.md +350 -276
  33. package/habilidades/release-semver/.evolved.json +8 -8
  34. package/habilidades/state-inconsistency-auditor-swl/SKILL.md +166 -166
  35. package/habilidades/state-inconsistency-auditor-swl/recursos/coupled-state-patterns.md +147 -147
  36. package/habilidades/tdd-workflow/SKILL.md +150 -4
  37. package/habilidades/testing-python/SKILL.md +340 -340
  38. package/habilidades/verificar-trabajo/SKILL.md +8 -3
  39. package/habilidades/web-fetcher-routing/SKILL.md +75 -75
  40. package/hooks/check-update.js +31 -3
  41. package/hooks/claudemd-bloat-detector.js +161 -161
  42. package/hooks/lib/agent-routing.js +107 -107
  43. package/hooks/lib/auto-consolidator.js +335 -335
  44. package/hooks/lib/error-classifier.js +308 -308
  45. package/hooks/lib/merkle-audit.js +96 -96
  46. package/hooks/lib/provenance-tracker.js +191 -191
  47. package/hooks/lib/rate-limit-tracker.js +253 -253
  48. package/hooks/lib/resource-quota.js +122 -122
  49. package/hooks/lib/retry-jitter.js +165 -165
  50. package/hooks/lib/security-net.js +201 -201
  51. package/hooks/lib/skill-auditor.js +588 -588
  52. package/hooks/lib/sync-status.js +228 -228
  53. package/hooks/lib/taint-tracker.js +107 -107
  54. package/hooks/lib/text-similarity.js +241 -241
  55. package/hooks/lib/toon-compressor.js +245 -245
  56. package/hooks/registro-turnos.js +209 -209
  57. package/hooks/sugerir-regenerar-inventario.js +170 -170
  58. package/hooks/validar-formato-post-subagente.js +140 -140
  59. package/hooks/validar-memoria-hook.js +218 -218
  60. package/instintos/prompt-appendices.yaml +57 -57
  61. package/manifiestos/agent-output-schemas.json +57 -57
  62. package/manifiestos/modulos.json +1324 -1321
  63. package/manifiestos/skills-lock.json +1114 -1114
  64. package/package.json +2 -2
  65. package/plantillas/auditor-veto-template.md +105 -105
  66. package/plantillas/github-workflows/README.md +47 -47
  67. package/plantillas/github-workflows/release-please.yml +44 -44
  68. package/plantillas/github-workflows/swl-ci.yml +107 -107
  69. package/plantillas/github-workflows/swl-security.yml +51 -51
  70. package/plugin.json +353 -351
  71. package/reglas/analisis-previo-tareas-grandes.md +172 -172
  72. package/reglas/arreglar-al-detectar.md +147 -147
  73. package/reglas/fragmentos-compartidos.md +152 -152
  74. package/reglas/harness-claude-code.md +213 -213
  75. package/reglas/registro-componentes-nuevos.md +192 -0
  76. package/reglas/usar-context7.md +226 -226
  77. package/schemas/diary-entry.schema.json +80 -80
  78. package/scripts/actualizar.js +110 -1
  79. package/scripts/audit-tools/audit-history.js +330 -330
  80. package/scripts/audit-tools/bundle-tracker.js +290 -290
  81. package/scripts/audit-tools/canary-monitor.js +352 -352
  82. package/scripts/audit-tools/code-profiler.js +605 -605
  83. package/scripts/audit-tools/dep-doctor.js +320 -320
  84. package/scripts/audit-tools/env-validator.js +206 -206
  85. package/scripts/audit-tools/lib/fs-walk.js +48 -48
  86. package/scripts/audit-tools/lib/output.js +23 -23
  87. package/scripts/audit-tools/migration-checker.js +392 -392
  88. package/scripts/audit-tools/pentest-scanner.js +1436 -1436
  89. package/scripts/benchmark-memoria.js +167 -167
  90. package/scripts/configurar-branch-protection.js +418 -418
  91. package/scripts/derivar-feature-list.js +489 -489
  92. package/scripts/detectar-aprendizajes-duplicados.js +151 -151
  93. package/scripts/doctor.js +58 -4
  94. package/scripts/field-report.js +199 -199
  95. package/scripts/generar-checklists-consolidados.js +273 -273
  96. package/scripts/generar-inventario.js +420 -420
  97. package/scripts/generar-matriz-lenguajes.js +271 -271
  98. package/scripts/lib/artefactos-python.js +43 -43
  99. package/scripts/lib/benchmark-metrics.js +160 -160
  100. package/scripts/lib/budget-enforcer.js +252 -252
  101. package/scripts/lib/configurar-ci.js +380 -380
  102. package/scripts/lib/contadores-inventario.js +217 -217
  103. package/scripts/lib/detectar-stack-detallado.js +307 -307
  104. package/scripts/lib/diary-entry.js +234 -234
  105. package/scripts/lib/eval-metrics-store.js +218 -218
  106. package/scripts/lib/eval-quality.js +171 -171
  107. package/scripts/lib/eval-schemas.js +144 -144
  108. package/scripts/lib/eval-self-correct.js +106 -106
  109. package/scripts/lib/eval-validator.js +185 -185
  110. package/scripts/lib/expandir-targets.js +71 -71
  111. package/scripts/lib/jaccard-similarity.js +98 -98
  112. package/scripts/lib/longmemeval-runner.js +125 -125
  113. package/scripts/lib/mcp_config.py +127 -0
  114. package/scripts/lib/npm-version.js +261 -261
  115. package/scripts/lib/paquetes-conocidos.js +50 -50
  116. package/scripts/lib/prompt-builder.js +264 -264
  117. package/scripts/lib/rrf-fusion.js +175 -175
  118. package/scripts/lib/scoring-instintos.js +277 -277
  119. package/scripts/lib/semantic-search.js +252 -252
  120. package/scripts/lib/toml-merge.js +204 -204
  121. package/scripts/lib/transformadores/codex.js +375 -375
  122. package/scripts/lib/transformadores/cursor.js +359 -359
  123. package/scripts/limpiar-artefactos-python.js +131 -131
  124. package/scripts/mcp-orchestrator.py +8 -18
  125. package/scripts/mcp-pool-manager.py +12 -23
  126. package/scripts/mcp-server/README.md +170 -170
  127. package/scripts/mcp-server/auth.js +105 -105
  128. package/scripts/mcp-server/cache.js +106 -106
  129. package/scripts/mcp-server/telemetry.js +78 -78
  130. package/scripts/migrar-csv-a-array.js +168 -168
  131. package/scripts/migrar-fase-dominio.js +201 -201
  132. package/scripts/publicar.js +511 -511
  133. package/scripts/run-eval.js +141 -141
  134. package/scripts/validar-userland-vacio.js +110 -110
@@ -1,330 +1,330 @@
1
- // Adaptado de temp/ultraship-main/tools/audit-history.mjs bajo MIT License
2
- // Fuente: Houseofmvps/ultraship (https://github.com/Houseofmvps/ultraship)
3
- 'use strict';
4
-
5
- const { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } = require('fs');
6
- const { join, resolve, dirname } = require('path');
7
- const { outputJSON, outputError } = require('./lib/output');
8
-
9
- /** Número máximo de entradas a conservar por categoría. */
10
- const MAX_ENTRIES_PER_CATEGORY = 100;
11
-
12
- /** Ruta por defecto del historial de auditorías. */
13
- const DEFAULT_HISTORY_PATH = join(process.cwd(), '.planning', 'audit', 'history.jsonl');
14
-
15
- /**
16
- * Lee todas las entradas del historial JSONL.
17
- * @param {string} historyPath
18
- * @returns {object[]}
19
- */
20
- function readHistory(historyPath) {
21
- if (!existsSync(historyPath)) return [];
22
- try {
23
- return readFileSync(historyPath, 'utf8')
24
- .split('\n')
25
- .filter(Boolean)
26
- .map(line => JSON.parse(line));
27
- } catch {
28
- return [];
29
- }
30
- }
31
-
32
- /**
33
- * Reescribe el archivo JSONL completo con las entradas proporcionadas.
34
- * @param {string} historyPath
35
- * @param {object[]} entries
36
- */
37
- function writeHistory(historyPath, entries) {
38
- const dir = dirname(historyPath);
39
- mkdirSync(dir, { recursive: true, mode: 0o700 });
40
- writeFileSync(historyPath, entries.map(e => JSON.stringify(e)).join('\n') + (entries.length > 0 ? '\n' : ''), 'utf8');
41
- }
42
-
43
- /**
44
- * Agrega una nueva entrada al historial JSONL, respetando el límite de 100 por categoría.
45
- * Si se supera el límite, reescribe el archivo con las entradas más recientes de esa categoría.
46
- *
47
- * @param {string} historyPath
48
- * @param {object} entry - Objeto que DEBE incluir campos `category` y `timestamp`.
49
- */
50
- function appendHistory(historyPath, entry) {
51
- const dir = dirname(historyPath);
52
- mkdirSync(dir, { recursive: true, mode: 0o700 });
53
-
54
- const existing = readHistory(historyPath);
55
- const category = entry.category || 'general';
56
-
57
- // Filtrar entradas de la misma categoría
58
- const sameCategory = existing.filter(e => (e.category || 'general') === category);
59
-
60
- if (sameCategory.length >= MAX_ENTRIES_PER_CATEGORY) {
61
- // Hay que podar: mantener las (MAX-1) más recientes de esta categoría + agregar la nueva
62
- const otherCategories = existing.filter(e => (e.category || 'general') !== category);
63
- const trimmed = sameCategory.slice(-(MAX_ENTRIES_PER_CATEGORY - 1));
64
- writeHistory(historyPath, [...otherCategories, ...trimmed, entry]);
65
- } else {
66
- // Hay espacio: solo hacer append
67
- appendFileSync(historyPath, JSON.stringify(entry) + '\n', 'utf8');
68
- }
69
- }
70
-
71
- /**
72
- * Obtiene la última entrada de una categoría específica.
73
- * @param {object[]} entries
74
- * @param {string} category
75
- * @returns {object|null}
76
- */
77
- function getLastEntryForCategory(entries, category) {
78
- const categoryEntries = entries.filter(e => (e.category || 'general') === category);
79
- if (categoryEntries.length === 0) return null;
80
- return categoryEntries[categoryEntries.length - 1];
81
- }
82
-
83
- /**
84
- * Calcula la tendencia comparando el score actual con el anterior.
85
- * @param {number} current
86
- * @param {number} previous
87
- * @returns {'improving'|'declining'|'stable'}
88
- */
89
- function calculateTrend(current, previous) {
90
- if (current > previous + 0.5) return 'improving';
91
- if (current < previous - 0.5) return 'declining';
92
- return 'stable';
93
- }
94
-
95
- /**
96
- * Formatea un número como diferencia con signo.
97
- * @param {number} diff
98
- * @returns {string}
99
- */
100
- function formatDiff(diff) {
101
- if (diff > 0) return `+${diff.toFixed(1)}`;
102
- if (diff < 0) return diff.toFixed(1);
103
- return '0.0';
104
- }
105
-
106
- /**
107
- * Guarda una nueva entrada de auditoría en el historial.
108
- * @param {object} options
109
- * @param {string} options.historyPath
110
- * @param {string} options.category - Categoría de la auditoría (ej: 'security', 'performance').
111
- * @param {number} options.score - Puntuación de 0 a 100.
112
- * @param {object} [options.metadata] - Datos adicionales opcionales.
113
- * @returns {object} Entrada guardada + tendencia detectada.
114
- */
115
- function saveEntry(options) {
116
- const { historyPath, category, score, metadata = {} } = options;
117
-
118
- const existing = readHistory(historyPath);
119
- const previous = getLastEntryForCategory(existing, category);
120
-
121
- const entry = {
122
- timestamp: new Date().toISOString(),
123
- category,
124
- score,
125
- metadata,
126
- };
127
-
128
- appendHistory(historyPath, entry);
129
-
130
- const trend = previous !== null
131
- ? calculateTrend(score, previous.score)
132
- : 'first_run';
133
-
134
- return {
135
- entry,
136
- trend,
137
- previous_score: previous ? previous.score : null,
138
- diff: previous ? score - previous.score : null,
139
- };
140
- }
141
-
142
- /**
143
- * Muestra el historial de una categoría con tendencias.
144
- * @param {string} historyPath
145
- * @param {string} [category] - Si se omite, muestra todas las categorías.
146
- * @param {number} [limit] - Número máximo de entradas a mostrar.
147
- * @returns {object}
148
- */
149
- function showHistory(historyPath, category, limit) {
150
- const all = readHistory(historyPath);
151
- const filtered = category
152
- ? all.filter(e => (e.category || 'general') === category)
153
- : all;
154
-
155
- const entries = limit ? filtered.slice(-limit) : filtered;
156
-
157
- // Agrupar por categoría para estadísticas
158
- const byCategory = {};
159
- for (const entry of all) {
160
- const cat = entry.category || 'general';
161
- if (!byCategory[cat]) byCategory[cat] = [];
162
- byCategory[cat].push(entry.score);
163
- }
164
-
165
- const summaries = Object.entries(byCategory).map(([cat, scores]) => {
166
- const avg = scores.reduce((s, v) => s + v, 0) / scores.length;
167
- const last = scores[scores.length - 1];
168
- const prev = scores.length >= 2 ? scores[scores.length - 2] : null;
169
- return {
170
- category: cat,
171
- entries: scores.length,
172
- avg_score: Math.round(avg * 10) / 10,
173
- last_score: last,
174
- trend: prev !== null ? calculateTrend(last, prev) : 'first_run',
175
- };
176
- });
177
-
178
- return {
179
- total_entries: filtered.length,
180
- shown: entries.length,
181
- category_filter: category || null,
182
- summaries,
183
- entries,
184
- };
185
- }
186
-
187
- /**
188
- * Calcula el diff entre la última y penúltima entrada de una categoría.
189
- * @param {string} historyPath
190
- * @param {string} category
191
- * @returns {object}
192
- */
193
- function diffHistory(historyPath, category) {
194
- const all = readHistory(historyPath);
195
- const categoryEntries = category
196
- ? all.filter(e => (e.category || 'general') === category)
197
- : all;
198
-
199
- if (categoryEntries.length === 0) {
200
- return { success: false, message: `Sin entradas en el historial para categoría: ${category || 'todas'}` };
201
- }
202
-
203
- if (categoryEntries.length === 1) {
204
- return {
205
- success: true,
206
- message: 'Solo existe una entrada — no hay diff disponible',
207
- current: categoryEntries[0],
208
- previous: null,
209
- diff: null,
210
- trend: 'first_run',
211
- };
212
- }
213
-
214
- const current = categoryEntries[categoryEntries.length - 1];
215
- const previous = categoryEntries[categoryEntries.length - 2];
216
- const diff = current.score - previous.score;
217
- const trend = calculateTrend(current.score, previous.score);
218
-
219
- // Comparar metadata keys si existen
220
- const metaDiff = [];
221
- const currMeta = current.metadata || {};
222
- const prevMeta = previous.metadata || {};
223
-
224
- const allKeys = new Set([...Object.keys(currMeta), ...Object.keys(prevMeta)]);
225
- for (const key of allKeys) {
226
- const curr = currMeta[key];
227
- const prev = prevMeta[key];
228
- if (curr !== prev) {
229
- metaDiff.push({ key, current: curr, previous: prev });
230
- }
231
- }
232
-
233
- return {
234
- success: true,
235
- category: category || 'todas',
236
- current,
237
- previous,
238
- diff: parseFloat(formatDiff(diff)),
239
- diff_formatted: formatDiff(diff),
240
- trend,
241
- metadata_changes: metaDiff,
242
- };
243
- }
244
-
245
- function main() {
246
- const args = process.argv.slice(2);
247
- const command = args[0];
248
- const historyPath = (() => {
249
- const hp = args.find(a => a.startsWith('--history='));
250
- return hp ? resolve(hp.split('=')[1]) : DEFAULT_HISTORY_PATH;
251
- })();
252
-
253
- if (command === 'save') {
254
- // save --category=X --score=N [--meta-key=value ...]
255
- const categoryArg = args.find(a => a.startsWith('--category='));
256
- const scoreArg = args.find(a => a.startsWith('--score='));
257
-
258
- if (!categoryArg || !scoreArg) {
259
- outputError('Uso: audit-history.js save --category=X --score=N [--history=ruta] [--meta-KEY=VALOR ...]');
260
- process.exit(0);
261
- }
262
-
263
- const category = categoryArg.split('=')[1];
264
- const score = parseFloat(scoreArg.split('=')[1]);
265
-
266
- if (isNaN(score)) {
267
- outputError(`Puntuación inválida: ${scoreArg.split('=')[1]}`);
268
- process.exit(0);
269
- }
270
-
271
- // Extraer metadata extra (--meta-KEY=VALOR)
272
- const metadata = {};
273
- for (const arg of args) {
274
- if (arg.startsWith('--meta-')) {
275
- const rest = arg.slice(7); // quitar "--meta-"
276
- const eqIndex = rest.indexOf('=');
277
- if (eqIndex > 0) {
278
- const key = rest.slice(0, eqIndex);
279
- const value = rest.slice(eqIndex + 1);
280
- metadata[key] = value;
281
- }
282
- }
283
- }
284
-
285
- const result = saveEntry({ historyPath, category, score, metadata });
286
- outputJSON({
287
- success: true,
288
- saved: result.entry,
289
- trend: result.trend,
290
- previous_score: result.previous_score,
291
- diff: result.diff,
292
- });
293
-
294
- } else if (command === 'show') {
295
- const categoryArg = args.find(a => a.startsWith('--category='));
296
- const limitArg = args.find(a => a.startsWith('--limit='));
297
- const category = categoryArg ? categoryArg.split('=')[1] : undefined;
298
- const limit = limitArg ? parseInt(limitArg.split('=')[1], 10) : 20;
299
-
300
- const result = showHistory(historyPath, category, limit);
301
- outputJSON({ success: true, ...result });
302
-
303
- } else if (command === 'diff') {
304
- const categoryArg = args.find(a => a.startsWith('--category='));
305
- const category = categoryArg ? categoryArg.split('=')[1] : undefined;
306
-
307
- const result = diffHistory(historyPath, category);
308
- outputJSON(result);
309
-
310
- } else {
311
- outputError(`Comando desconocido: "${command || '(ninguno)'}". Comandos válidos: save, show, diff`);
312
- process.exit(0);
313
- }
314
- }
315
-
316
- if (require.main === module) {
317
- main();
318
- }
319
-
320
- module.exports = {
321
- readHistory,
322
- writeHistory,
323
- appendHistory,
324
- saveEntry,
325
- showHistory,
326
- diffHistory,
327
- calculateTrend,
328
- getLastEntryForCategory,
329
- MAX_ENTRIES_PER_CATEGORY,
330
- };
1
+ // Adaptado de temp/ultraship-main/tools/audit-history.mjs bajo MIT License
2
+ // Fuente: Houseofmvps/ultraship (https://github.com/Houseofmvps/ultraship)
3
+ 'use strict';
4
+
5
+ const { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } = require('fs');
6
+ const { join, resolve, dirname } = require('path');
7
+ const { outputJSON, outputError } = require('./lib/output');
8
+
9
+ /** Número máximo de entradas a conservar por categoría. */
10
+ const MAX_ENTRIES_PER_CATEGORY = 100;
11
+
12
+ /** Ruta por defecto del historial de auditorías. */
13
+ const DEFAULT_HISTORY_PATH = join(process.cwd(), '.planning', 'audit', 'history.jsonl');
14
+
15
+ /**
16
+ * Lee todas las entradas del historial JSONL.
17
+ * @param {string} historyPath
18
+ * @returns {object[]}
19
+ */
20
+ function readHistory(historyPath) {
21
+ if (!existsSync(historyPath)) return [];
22
+ try {
23
+ return readFileSync(historyPath, 'utf8')
24
+ .split('\n')
25
+ .filter(Boolean)
26
+ .map(line => JSON.parse(line));
27
+ } catch {
28
+ return [];
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Reescribe el archivo JSONL completo con las entradas proporcionadas.
34
+ * @param {string} historyPath
35
+ * @param {object[]} entries
36
+ */
37
+ function writeHistory(historyPath, entries) {
38
+ const dir = dirname(historyPath);
39
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
40
+ writeFileSync(historyPath, entries.map(e => JSON.stringify(e)).join('\n') + (entries.length > 0 ? '\n' : ''), 'utf8');
41
+ }
42
+
43
+ /**
44
+ * Agrega una nueva entrada al historial JSONL, respetando el límite de 100 por categoría.
45
+ * Si se supera el límite, reescribe el archivo con las entradas más recientes de esa categoría.
46
+ *
47
+ * @param {string} historyPath
48
+ * @param {object} entry - Objeto que DEBE incluir campos `category` y `timestamp`.
49
+ */
50
+ function appendHistory(historyPath, entry) {
51
+ const dir = dirname(historyPath);
52
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
53
+
54
+ const existing = readHistory(historyPath);
55
+ const category = entry.category || 'general';
56
+
57
+ // Filtrar entradas de la misma categoría
58
+ const sameCategory = existing.filter(e => (e.category || 'general') === category);
59
+
60
+ if (sameCategory.length >= MAX_ENTRIES_PER_CATEGORY) {
61
+ // Hay que podar: mantener las (MAX-1) más recientes de esta categoría + agregar la nueva
62
+ const otherCategories = existing.filter(e => (e.category || 'general') !== category);
63
+ const trimmed = sameCategory.slice(-(MAX_ENTRIES_PER_CATEGORY - 1));
64
+ writeHistory(historyPath, [...otherCategories, ...trimmed, entry]);
65
+ } else {
66
+ // Hay espacio: solo hacer append
67
+ appendFileSync(historyPath, JSON.stringify(entry) + '\n', 'utf8');
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Obtiene la última entrada de una categoría específica.
73
+ * @param {object[]} entries
74
+ * @param {string} category
75
+ * @returns {object|null}
76
+ */
77
+ function getLastEntryForCategory(entries, category) {
78
+ const categoryEntries = entries.filter(e => (e.category || 'general') === category);
79
+ if (categoryEntries.length === 0) return null;
80
+ return categoryEntries[categoryEntries.length - 1];
81
+ }
82
+
83
+ /**
84
+ * Calcula la tendencia comparando el score actual con el anterior.
85
+ * @param {number} current
86
+ * @param {number} previous
87
+ * @returns {'improving'|'declining'|'stable'}
88
+ */
89
+ function calculateTrend(current, previous) {
90
+ if (current > previous + 0.5) return 'improving';
91
+ if (current < previous - 0.5) return 'declining';
92
+ return 'stable';
93
+ }
94
+
95
+ /**
96
+ * Formatea un número como diferencia con signo.
97
+ * @param {number} diff
98
+ * @returns {string}
99
+ */
100
+ function formatDiff(diff) {
101
+ if (diff > 0) return `+${diff.toFixed(1)}`;
102
+ if (diff < 0) return diff.toFixed(1);
103
+ return '0.0';
104
+ }
105
+
106
+ /**
107
+ * Guarda una nueva entrada de auditoría en el historial.
108
+ * @param {object} options
109
+ * @param {string} options.historyPath
110
+ * @param {string} options.category - Categoría de la auditoría (ej: 'security', 'performance').
111
+ * @param {number} options.score - Puntuación de 0 a 100.
112
+ * @param {object} [options.metadata] - Datos adicionales opcionales.
113
+ * @returns {object} Entrada guardada + tendencia detectada.
114
+ */
115
+ function saveEntry(options) {
116
+ const { historyPath, category, score, metadata = {} } = options;
117
+
118
+ const existing = readHistory(historyPath);
119
+ const previous = getLastEntryForCategory(existing, category);
120
+
121
+ const entry = {
122
+ timestamp: new Date().toISOString(),
123
+ category,
124
+ score,
125
+ metadata,
126
+ };
127
+
128
+ appendHistory(historyPath, entry);
129
+
130
+ const trend = previous !== null
131
+ ? calculateTrend(score, previous.score)
132
+ : 'first_run';
133
+
134
+ return {
135
+ entry,
136
+ trend,
137
+ previous_score: previous ? previous.score : null,
138
+ diff: previous ? score - previous.score : null,
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Muestra el historial de una categoría con tendencias.
144
+ * @param {string} historyPath
145
+ * @param {string} [category] - Si se omite, muestra todas las categorías.
146
+ * @param {number} [limit] - Número máximo de entradas a mostrar.
147
+ * @returns {object}
148
+ */
149
+ function showHistory(historyPath, category, limit) {
150
+ const all = readHistory(historyPath);
151
+ const filtered = category
152
+ ? all.filter(e => (e.category || 'general') === category)
153
+ : all;
154
+
155
+ const entries = limit ? filtered.slice(-limit) : filtered;
156
+
157
+ // Agrupar por categoría para estadísticas
158
+ const byCategory = {};
159
+ for (const entry of all) {
160
+ const cat = entry.category || 'general';
161
+ if (!byCategory[cat]) byCategory[cat] = [];
162
+ byCategory[cat].push(entry.score);
163
+ }
164
+
165
+ const summaries = Object.entries(byCategory).map(([cat, scores]) => {
166
+ const avg = scores.reduce((s, v) => s + v, 0) / scores.length;
167
+ const last = scores[scores.length - 1];
168
+ const prev = scores.length >= 2 ? scores[scores.length - 2] : null;
169
+ return {
170
+ category: cat,
171
+ entries: scores.length,
172
+ avg_score: Math.round(avg * 10) / 10,
173
+ last_score: last,
174
+ trend: prev !== null ? calculateTrend(last, prev) : 'first_run',
175
+ };
176
+ });
177
+
178
+ return {
179
+ total_entries: filtered.length,
180
+ shown: entries.length,
181
+ category_filter: category || null,
182
+ summaries,
183
+ entries,
184
+ };
185
+ }
186
+
187
+ /**
188
+ * Calcula el diff entre la última y penúltima entrada de una categoría.
189
+ * @param {string} historyPath
190
+ * @param {string} category
191
+ * @returns {object}
192
+ */
193
+ function diffHistory(historyPath, category) {
194
+ const all = readHistory(historyPath);
195
+ const categoryEntries = category
196
+ ? all.filter(e => (e.category || 'general') === category)
197
+ : all;
198
+
199
+ if (categoryEntries.length === 0) {
200
+ return { success: false, message: `Sin entradas en el historial para categoría: ${category || 'todas'}` };
201
+ }
202
+
203
+ if (categoryEntries.length === 1) {
204
+ return {
205
+ success: true,
206
+ message: 'Solo existe una entrada — no hay diff disponible',
207
+ current: categoryEntries[0],
208
+ previous: null,
209
+ diff: null,
210
+ trend: 'first_run',
211
+ };
212
+ }
213
+
214
+ const current = categoryEntries[categoryEntries.length - 1];
215
+ const previous = categoryEntries[categoryEntries.length - 2];
216
+ const diff = current.score - previous.score;
217
+ const trend = calculateTrend(current.score, previous.score);
218
+
219
+ // Comparar metadata keys si existen
220
+ const metaDiff = [];
221
+ const currMeta = current.metadata || {};
222
+ const prevMeta = previous.metadata || {};
223
+
224
+ const allKeys = new Set([...Object.keys(currMeta), ...Object.keys(prevMeta)]);
225
+ for (const key of allKeys) {
226
+ const curr = currMeta[key];
227
+ const prev = prevMeta[key];
228
+ if (curr !== prev) {
229
+ metaDiff.push({ key, current: curr, previous: prev });
230
+ }
231
+ }
232
+
233
+ return {
234
+ success: true,
235
+ category: category || 'todas',
236
+ current,
237
+ previous,
238
+ diff: parseFloat(formatDiff(diff)),
239
+ diff_formatted: formatDiff(diff),
240
+ trend,
241
+ metadata_changes: metaDiff,
242
+ };
243
+ }
244
+
245
+ function main() {
246
+ const args = process.argv.slice(2);
247
+ const command = args[0];
248
+ const historyPath = (() => {
249
+ const hp = args.find(a => a.startsWith('--history='));
250
+ return hp ? resolve(hp.split('=')[1]) : DEFAULT_HISTORY_PATH;
251
+ })();
252
+
253
+ if (command === 'save') {
254
+ // save --category=X --score=N [--meta-key=value ...]
255
+ const categoryArg = args.find(a => a.startsWith('--category='));
256
+ const scoreArg = args.find(a => a.startsWith('--score='));
257
+
258
+ if (!categoryArg || !scoreArg) {
259
+ outputError('Uso: audit-history.js save --category=X --score=N [--history=ruta] [--meta-KEY=VALOR ...]');
260
+ process.exit(0);
261
+ }
262
+
263
+ const category = categoryArg.split('=')[1];
264
+ const score = parseFloat(scoreArg.split('=')[1]);
265
+
266
+ if (isNaN(score)) {
267
+ outputError(`Puntuación inválida: ${scoreArg.split('=')[1]}`);
268
+ process.exit(0);
269
+ }
270
+
271
+ // Extraer metadata extra (--meta-KEY=VALOR)
272
+ const metadata = {};
273
+ for (const arg of args) {
274
+ if (arg.startsWith('--meta-')) {
275
+ const rest = arg.slice(7); // quitar "--meta-"
276
+ const eqIndex = rest.indexOf('=');
277
+ if (eqIndex > 0) {
278
+ const key = rest.slice(0, eqIndex);
279
+ const value = rest.slice(eqIndex + 1);
280
+ metadata[key] = value;
281
+ }
282
+ }
283
+ }
284
+
285
+ const result = saveEntry({ historyPath, category, score, metadata });
286
+ outputJSON({
287
+ success: true,
288
+ saved: result.entry,
289
+ trend: result.trend,
290
+ previous_score: result.previous_score,
291
+ diff: result.diff,
292
+ });
293
+
294
+ } else if (command === 'show') {
295
+ const categoryArg = args.find(a => a.startsWith('--category='));
296
+ const limitArg = args.find(a => a.startsWith('--limit='));
297
+ const category = categoryArg ? categoryArg.split('=')[1] : undefined;
298
+ const limit = limitArg ? parseInt(limitArg.split('=')[1], 10) : 20;
299
+
300
+ const result = showHistory(historyPath, category, limit);
301
+ outputJSON({ success: true, ...result });
302
+
303
+ } else if (command === 'diff') {
304
+ const categoryArg = args.find(a => a.startsWith('--category='));
305
+ const category = categoryArg ? categoryArg.split('=')[1] : undefined;
306
+
307
+ const result = diffHistory(historyPath, category);
308
+ outputJSON(result);
309
+
310
+ } else {
311
+ outputError(`Comando desconocido: "${command || '(ninguno)'}". Comandos válidos: save, show, diff`);
312
+ process.exit(0);
313
+ }
314
+ }
315
+
316
+ if (require.main === module) {
317
+ main();
318
+ }
319
+
320
+ module.exports = {
321
+ readHistory,
322
+ writeHistory,
323
+ appendHistory,
324
+ saveEntry,
325
+ showHistory,
326
+ diffHistory,
327
+ calculateTrend,
328
+ getLastEntryForCategory,
329
+ MAX_ENTRIES_PER_CATEGORY,
330
+ };