@saulwade/swl-ses 1.4.1 → 1.5.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 (136) hide show
  1. package/CLAUDE.md +3 -3
  2. package/README.md +561 -560
  3. package/agentes/nemesis-auditor-swl.md +161 -161
  4. package/bin/swl-mcp-server.js +49 -22
  5. package/bin/swl-ses.js +74 -0
  6. package/comandos/swl/.evolved.json +22 -22
  7. package/comandos/swl/contribuir.md +233 -233
  8. package/comandos/swl/ejecutar-fase.md +33 -4
  9. package/comandos/swl/metricas.md +72 -0
  10. package/comandos/swl/nemesis.md +122 -122
  11. package/gateway/lib/event-channel.js +191 -191
  12. package/habilidades/backend-production-resilience/SKILL.md +288 -288
  13. package/habilidades/benchmark-memoria/SKILL.md +186 -186
  14. package/habilidades/diagrama-arquitectura/assets/template.html +276 -276
  15. package/habilidades/discutir-fase/SKILL.md +50 -2
  16. package/habilidades/doubt-driven-review/SKILL.md +171 -171
  17. package/habilidades/doubt-driven-review/recursos/EXAMPLES.md +130 -130
  18. package/habilidades/ejecutar-task-iterativo/SKILL.md +278 -0
  19. package/habilidades/eval-framework/SKILL.md +212 -212
  20. package/habilidades/feynman-auditor-swl/SKILL.md +123 -123
  21. package/habilidades/feynman-auditor-swl/recursos/preguntas-language-agnostic.md +108 -108
  22. package/habilidades/harness-claude-code/SKILL.md +299 -299
  23. package/habilidades/infra-github-actions/SKILL.md +166 -166
  24. package/habilidades/legacy-code-rescue/SKILL.md +267 -267
  25. package/habilidades/manejo-errores/.evolved.json +8 -8
  26. package/habilidades/meta-skills-estandar/recursos/convencion-examples.md +93 -93
  27. package/habilidades/meta-skills-estandar/recursos/skills-as-agents.md +163 -163
  28. package/habilidades/patrones-python/SKILL.md +229 -229
  29. package/habilidades/patrones-python/recursos/patrones-avanzados.md +469 -469
  30. package/habilidades/planear-fase/SKILL.md +319 -319
  31. package/habilidades/protocolo-revision-swl/SKILL.md +276 -0
  32. package/habilidades/release-semver/.evolved.json +8 -8
  33. package/habilidades/state-inconsistency-auditor-swl/SKILL.md +166 -166
  34. package/habilidades/state-inconsistency-auditor-swl/recursos/coupled-state-patterns.md +147 -147
  35. package/habilidades/testing-python/SKILL.md +340 -340
  36. package/habilidades/verificar-trabajo/SKILL.md +49 -5
  37. package/habilidades/web-fetcher-routing/SKILL.md +75 -75
  38. package/hooks/claudemd-bloat-detector.js +161 -161
  39. package/hooks/lib/agent-routing.js +107 -107
  40. package/hooks/lib/auto-consolidator.js +335 -335
  41. package/hooks/lib/error-classifier.js +308 -308
  42. package/hooks/lib/merkle-audit.js +96 -96
  43. package/hooks/lib/provenance-tracker.js +191 -191
  44. package/hooks/lib/rate-limit-tracker.js +253 -253
  45. package/hooks/lib/resource-quota.js +122 -122
  46. package/hooks/lib/retry-jitter.js +165 -165
  47. package/hooks/lib/security-net.js +201 -201
  48. package/hooks/lib/skill-auditor.js +588 -588
  49. package/hooks/lib/sync-status.js +228 -228
  50. package/hooks/lib/taint-tracker.js +107 -107
  51. package/hooks/lib/text-similarity.js +241 -241
  52. package/hooks/lib/toon-compressor.js +245 -245
  53. package/hooks/registro-turnos.js +209 -209
  54. package/hooks/sugerir-regenerar-inventario.js +170 -170
  55. package/hooks/validar-formato-post-subagente.js +140 -140
  56. package/hooks/validar-memoria-hook.js +218 -218
  57. package/instintos/prompt-appendices.yaml +57 -57
  58. package/manifiestos/agent-output-schemas.json +57 -57
  59. package/manifiestos/modulos.json +1321 -1262
  60. package/manifiestos/perfiles.json +2 -1
  61. package/manifiestos/skills-lock.json +1114 -1114
  62. package/package.json +3 -3
  63. package/plantillas/auditor-veto-template.md +105 -105
  64. package/plantillas/github-workflows/README.md +47 -47
  65. package/plantillas/github-workflows/release-please.yml +44 -44
  66. package/plantillas/github-workflows/swl-ci.yml +107 -107
  67. package/plantillas/github-workflows/swl-security.yml +51 -51
  68. package/plugin.json +351 -343
  69. package/reglas/analisis-previo-tareas-grandes.md +172 -172
  70. package/reglas/arreglar-al-detectar.md +147 -147
  71. package/reglas/fragmentos-compartidos.md +152 -152
  72. package/reglas/harness-claude-code.md +213 -213
  73. package/reglas/usar-context7.md +226 -226
  74. package/schemas/diary-entry.schema.json +80 -80
  75. package/scripts/audit-tools/audit-history.js +330 -330
  76. package/scripts/audit-tools/bundle-tracker.js +290 -290
  77. package/scripts/audit-tools/canary-monitor.js +352 -352
  78. package/scripts/audit-tools/code-profiler.js +605 -605
  79. package/scripts/audit-tools/dep-doctor.js +320 -320
  80. package/scripts/audit-tools/env-validator.js +206 -206
  81. package/scripts/audit-tools/lib/fs-walk.js +48 -48
  82. package/scripts/audit-tools/lib/output.js +23 -23
  83. package/scripts/audit-tools/migration-checker.js +392 -392
  84. package/scripts/audit-tools/pentest-scanner.js +1436 -1436
  85. package/scripts/benchmark-memoria.js +167 -167
  86. package/scripts/configurar-branch-protection.js +418 -418
  87. package/scripts/derivar-feature-list.js +489 -0
  88. package/scripts/detectar-aprendizajes-duplicados.js +151 -151
  89. package/scripts/doctor.js +31 -4
  90. package/scripts/field-report.js +199 -199
  91. package/scripts/generar-checklists-consolidados.js +273 -273
  92. package/scripts/generar-inventario.js +420 -420
  93. package/scripts/generar-matriz-lenguajes.js +271 -271
  94. package/scripts/instalador.js +56 -5
  95. package/scripts/lib/artefactos-python.js +43 -43
  96. package/scripts/lib/benchmark-metrics.js +160 -160
  97. package/scripts/lib/budget-enforcer.js +252 -252
  98. package/scripts/lib/configurar-ci.js +380 -380
  99. package/scripts/lib/contadores-inventario.js +217 -217
  100. package/scripts/lib/detectar-runtime.js +75 -9
  101. package/scripts/lib/detectar-stack-detallado.js +307 -307
  102. package/scripts/lib/diary-entry.js +234 -234
  103. package/scripts/lib/estado.js +13 -1
  104. package/scripts/lib/eval-metrics-store.js +218 -218
  105. package/scripts/lib/eval-quality.js +171 -171
  106. package/scripts/lib/eval-schemas.js +144 -144
  107. package/scripts/lib/eval-self-correct.js +106 -106
  108. package/scripts/lib/eval-validator.js +185 -185
  109. package/scripts/lib/expandir-targets.js +71 -0
  110. package/scripts/lib/jaccard-similarity.js +98 -98
  111. package/scripts/lib/longmemeval-runner.js +125 -125
  112. package/scripts/lib/manifiestos.js +42 -1
  113. package/scripts/lib/npm-version.js +261 -261
  114. package/scripts/lib/paquetes-conocidos.js +50 -50
  115. package/scripts/lib/parsear-opciones.js +3 -0
  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 -0
  121. package/scripts/lib/transformadores/base.js +43 -9
  122. package/scripts/lib/transformadores/codex.js +375 -115
  123. package/scripts/lib/transformadores/cursor.js +359 -0
  124. package/scripts/lib/transformadores/index.js +2 -0
  125. package/scripts/limpiar-artefactos-python.js +131 -131
  126. package/scripts/mcp-server/README.md +122 -80
  127. package/scripts/mcp-server/auth.js +105 -0
  128. package/scripts/mcp-server/cache.js +106 -0
  129. package/scripts/mcp-server/handlers.js +386 -206
  130. package/scripts/mcp-server/telemetry.js +78 -0
  131. package/scripts/migrar-csv-a-array.js +168 -168
  132. package/scripts/migrar-fase-dominio.js +201 -201
  133. package/scripts/publicar.js +511 -511
  134. package/scripts/run-eval.js +141 -141
  135. package/scripts/validar-manifest.js +231 -195
  136. 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
+ };