@saulwade/swl-ses 1.4.1 → 1.4.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.
- package/CLAUDE.md +1 -1
- package/README.md +1 -1
- package/agentes/nemesis-auditor-swl.md +161 -161
- package/bin/swl-mcp-server.js +187 -187
- package/comandos/swl/.evolved.json +22 -22
- package/comandos/swl/contribuir.md +233 -233
- package/comandos/swl/nemesis.md +122 -122
- package/gateway/lib/event-channel.js +191 -191
- package/habilidades/backend-production-resilience/SKILL.md +288 -288
- package/habilidades/benchmark-memoria/SKILL.md +186 -186
- package/habilidades/diagrama-arquitectura/assets/template.html +276 -276
- package/habilidades/doubt-driven-review/SKILL.md +171 -171
- package/habilidades/doubt-driven-review/recursos/EXAMPLES.md +130 -130
- package/habilidades/eval-framework/SKILL.md +212 -212
- package/habilidades/feynman-auditor-swl/SKILL.md +123 -123
- package/habilidades/feynman-auditor-swl/recursos/preguntas-language-agnostic.md +108 -108
- package/habilidades/harness-claude-code/SKILL.md +299 -299
- package/habilidades/infra-github-actions/SKILL.md +166 -166
- package/habilidades/legacy-code-rescue/SKILL.md +267 -267
- package/habilidades/manejo-errores/.evolved.json +8 -8
- package/habilidades/meta-skills-estandar/recursos/convencion-examples.md +93 -93
- package/habilidades/meta-skills-estandar/recursos/skills-as-agents.md +163 -163
- package/habilidades/patrones-python/SKILL.md +229 -229
- package/habilidades/patrones-python/recursos/patrones-avanzados.md +469 -469
- package/habilidades/planear-fase/SKILL.md +319 -319
- package/habilidades/release-semver/.evolved.json +8 -8
- package/habilidades/state-inconsistency-auditor-swl/SKILL.md +166 -166
- package/habilidades/state-inconsistency-auditor-swl/recursos/coupled-state-patterns.md +147 -147
- package/habilidades/testing-python/SKILL.md +340 -340
- package/habilidades/web-fetcher-routing/SKILL.md +75 -75
- package/hooks/claudemd-bloat-detector.js +161 -161
- package/hooks/lib/agent-routing.js +107 -107
- package/hooks/lib/auto-consolidator.js +335 -335
- package/hooks/lib/error-classifier.js +308 -308
- package/hooks/lib/merkle-audit.js +96 -96
- package/hooks/lib/provenance-tracker.js +191 -191
- package/hooks/lib/rate-limit-tracker.js +253 -253
- package/hooks/lib/resource-quota.js +122 -122
- package/hooks/lib/retry-jitter.js +165 -165
- package/hooks/lib/security-net.js +201 -201
- package/hooks/lib/skill-auditor.js +588 -588
- package/hooks/lib/sync-status.js +228 -228
- package/hooks/lib/taint-tracker.js +107 -107
- package/hooks/lib/text-similarity.js +241 -241
- package/hooks/lib/toon-compressor.js +245 -245
- package/hooks/registro-turnos.js +209 -209
- package/hooks/sugerir-regenerar-inventario.js +170 -170
- package/hooks/validar-formato-post-subagente.js +140 -140
- package/hooks/validar-memoria-hook.js +218 -218
- package/instintos/prompt-appendices.yaml +57 -57
- package/manifiestos/agent-output-schemas.json +57 -57
- package/manifiestos/modulos.json +11 -6
- package/manifiestos/perfiles.json +2 -1
- package/manifiestos/skills-lock.json +1114 -1114
- package/package.json +1 -1
- package/plantillas/auditor-veto-template.md +105 -105
- package/plantillas/github-workflows/README.md +47 -47
- package/plantillas/github-workflows/release-please.yml +44 -44
- package/plantillas/github-workflows/swl-ci.yml +107 -107
- package/plantillas/github-workflows/swl-security.yml +51 -51
- package/plugin.json +9 -1
- package/reglas/analisis-previo-tareas-grandes.md +172 -172
- package/reglas/arreglar-al-detectar.md +147 -147
- package/reglas/fragmentos-compartidos.md +152 -152
- package/reglas/harness-claude-code.md +213 -213
- package/reglas/usar-context7.md +226 -226
- package/schemas/diary-entry.schema.json +80 -80
- package/scripts/audit-tools/audit-history.js +330 -330
- package/scripts/audit-tools/bundle-tracker.js +290 -290
- package/scripts/audit-tools/canary-monitor.js +352 -352
- package/scripts/audit-tools/code-profiler.js +605 -605
- package/scripts/audit-tools/dep-doctor.js +320 -320
- package/scripts/audit-tools/env-validator.js +206 -206
- package/scripts/audit-tools/lib/fs-walk.js +48 -48
- package/scripts/audit-tools/lib/output.js +23 -23
- package/scripts/audit-tools/migration-checker.js +392 -392
- package/scripts/audit-tools/pentest-scanner.js +1436 -1436
- package/scripts/benchmark-memoria.js +167 -167
- package/scripts/configurar-branch-protection.js +418 -418
- package/scripts/detectar-aprendizajes-duplicados.js +151 -151
- package/scripts/field-report.js +199 -199
- package/scripts/generar-checklists-consolidados.js +273 -273
- package/scripts/generar-inventario.js +420 -420
- package/scripts/generar-matriz-lenguajes.js +271 -271
- package/scripts/lib/artefactos-python.js +43 -43
- package/scripts/lib/benchmark-metrics.js +160 -160
- package/scripts/lib/budget-enforcer.js +252 -252
- package/scripts/lib/configurar-ci.js +380 -380
- package/scripts/lib/contadores-inventario.js +217 -217
- package/scripts/lib/detectar-stack-detallado.js +307 -307
- package/scripts/lib/diary-entry.js +234 -234
- package/scripts/lib/eval-metrics-store.js +218 -218
- package/scripts/lib/eval-quality.js +171 -171
- package/scripts/lib/eval-schemas.js +144 -144
- package/scripts/lib/eval-self-correct.js +106 -106
- package/scripts/lib/eval-validator.js +185 -185
- package/scripts/lib/jaccard-similarity.js +98 -98
- package/scripts/lib/longmemeval-runner.js +125 -125
- package/scripts/lib/manifiestos.js +42 -1
- package/scripts/lib/npm-version.js +261 -261
- package/scripts/lib/paquetes-conocidos.js +50 -50
- package/scripts/lib/prompt-builder.js +264 -264
- package/scripts/lib/rrf-fusion.js +175 -175
- package/scripts/lib/scoring-instintos.js +277 -277
- package/scripts/lib/semantic-search.js +252 -252
- package/scripts/limpiar-artefactos-python.js +131 -131
- package/scripts/mcp-server/README.md +128 -128
- package/scripts/mcp-server/handlers.js +206 -206
- package/scripts/migrar-csv-a-array.js +168 -168
- package/scripts/migrar-fase-dominio.js +201 -201
- package/scripts/publicar.js +511 -511
- package/scripts/run-eval.js +141 -141
- package/scripts/validar-manifest.js +231 -195
- package/scripts/validar-userland-vacio.js +110 -110
|
@@ -1,335 +1,335 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Auto-Consolidator — Consolidación automática de aprendizajes sin intervención humana.
|
|
5
|
-
*
|
|
6
|
-
* Cierra el ciclo: captura automática (extraccion-aprendizajes.js) →
|
|
7
|
-
* consolidación automática (este módulo) → aplicación (instintos/skills).
|
|
8
|
-
*
|
|
9
|
-
* Criterios conservadores para operar sin supervisión:
|
|
10
|
-
* - Solo promueve aprendizajes con ≥3 confirmaciones cruzadas
|
|
11
|
-
* - Solo degrada con ≥3 contradicciones
|
|
12
|
-
* - Solo poda entradas con >30 días sin referencia
|
|
13
|
-
* - Nunca modifica CLAUDE.md ni skills (eso requiere /swl:aprender manual)
|
|
14
|
-
* - Solo opera sobre APRENDIZAJES.md e instintos/proyecto.yaml
|
|
15
|
-
*
|
|
16
|
-
* Zero dependencias externas.
|
|
17
|
-
*
|
|
18
|
-
* @module hooks/lib/auto-consolidator
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
const fs = require('fs');
|
|
22
|
-
const path = require('path');
|
|
23
|
-
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
// Constantes
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
|
|
28
|
-
/** Días sin referencia para considerar un aprendizaje como candidato a poda. */
|
|
29
|
-
const DIAS_PODA = 30;
|
|
30
|
-
|
|
31
|
-
/** Mínimo de confirmaciones para promover un aprendizaje. */
|
|
32
|
-
const MIN_CONFIRMACIONES_PROMOCION = 3;
|
|
33
|
-
|
|
34
|
-
/** Mínimo de contradicciones para degradar un aprendizaje. */
|
|
35
|
-
const MIN_CONTRADICCIONES_DEGRADACION = 3;
|
|
36
|
-
|
|
37
|
-
/** Máximo de entradas automáticas a procesar por consolidación. */
|
|
38
|
-
const MAX_ENTRADAS_POR_CICLO = 50;
|
|
39
|
-
|
|
40
|
-
/** Patrón para detectar entradas automáticas del hook extraccion-aprendizajes. */
|
|
41
|
-
const PATRON_ENTRADA_AUTO = /^## \[(\d{4}-\d{2}-\d{2})\] ([\w\u00C0-\u024F-]+) — (.+)$/;
|
|
42
|
-
|
|
43
|
-
/** Patrón para detectar confirmaciones. */
|
|
44
|
-
const PATRON_CONFIRMADO = /\[CONFIRMADO(?:\s+x(\d+))?\]/;
|
|
45
|
-
|
|
46
|
-
/** Patrón para detectar contradicciones. */
|
|
47
|
-
const PATRON_CONTRADICHO = /\[CONTRADICHO(?::\s*\d{4}-\d{2}-\d{2})?\]/g;
|
|
48
|
-
|
|
49
|
-
/** Patrón para detectar degradados. */
|
|
50
|
-
const PATRON_DEGRADADO = /\[DEGRADADO\]/;
|
|
51
|
-
|
|
52
|
-
// ---------------------------------------------------------------------------
|
|
53
|
-
// Parsing de APRENDIZAJES.md
|
|
54
|
-
// ---------------------------------------------------------------------------
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @typedef {object} EntradaAprendizaje
|
|
58
|
-
* @property {string} fecha - YYYY-MM-DD
|
|
59
|
-
* @property {string} tipo - anti-patrón, patrón, gotcha, decisión, bug-fix, descubrimiento
|
|
60
|
-
* @property {string} titulo - Descripción corta
|
|
61
|
-
* @property {string} contenido - Contenido completo de la entrada
|
|
62
|
-
* @property {number} confirmaciones - Número de confirmaciones
|
|
63
|
-
* @property {number} contradicciones - Número de contradicciones
|
|
64
|
-
* @property {boolean} degradado - Si ya fue marcado como degradado
|
|
65
|
-
* @property {number} lineaInicio - Línea donde empieza en el archivo
|
|
66
|
-
* @property {number} lineaFin - Línea donde termina
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Parsea APRENDIZAJES.md en entradas estructuradas.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} contenido
|
|
73
|
-
* @returns {EntradaAprendizaje[]}
|
|
74
|
-
*/
|
|
75
|
-
function parsearAprendizajes(contenido) {
|
|
76
|
-
const lineas = contenido.split('\n');
|
|
77
|
-
const entradas = [];
|
|
78
|
-
let actual = null;
|
|
79
|
-
|
|
80
|
-
for (let i = 0; i < lineas.length; i++) {
|
|
81
|
-
const match = lineas[i].match(PATRON_ENTRADA_AUTO);
|
|
82
|
-
if (match) {
|
|
83
|
-
if (actual) {
|
|
84
|
-
actual.lineaFin = i - 1;
|
|
85
|
-
actual.contenido = lineas.slice(actual.lineaInicio, i).join('\n');
|
|
86
|
-
entradas.push(actual);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const [, fecha, tipo, titulo] = match;
|
|
90
|
-
|
|
91
|
-
// Contar confirmaciones
|
|
92
|
-
const textoHastaAhora = lineas[i];
|
|
93
|
-
const confMatch = textoHastaAhora.match(PATRON_CONFIRMADO);
|
|
94
|
-
const confirmaciones = confMatch ? parseInt(confMatch[1] || '1', 10) : 0;
|
|
95
|
-
|
|
96
|
-
actual = {
|
|
97
|
-
fecha,
|
|
98
|
-
tipo,
|
|
99
|
-
titulo,
|
|
100
|
-
contenido: '',
|
|
101
|
-
confirmaciones,
|
|
102
|
-
contradicciones: 0,
|
|
103
|
-
degradado: PATRON_DEGRADADO.test(lineas[i]),
|
|
104
|
-
lineaInicio: i,
|
|
105
|
-
lineaFin: i,
|
|
106
|
-
};
|
|
107
|
-
} else if (actual) {
|
|
108
|
-
// Buscar confirmaciones y contradicciones en el contenido
|
|
109
|
-
const confMatch = lineas[i].match(PATRON_CONFIRMADO);
|
|
110
|
-
if (confMatch) {
|
|
111
|
-
actual.confirmaciones = Math.max(actual.confirmaciones, parseInt(confMatch[1] || '1', 10));
|
|
112
|
-
}
|
|
113
|
-
const contMatches = lineas[i].match(PATRON_CONTRADICHO);
|
|
114
|
-
if (contMatches) actual.contradicciones += contMatches.length;
|
|
115
|
-
if (PATRON_DEGRADADO.test(lineas[i])) actual.degradado = true;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (actual) {
|
|
120
|
-
actual.lineaFin = lineas.length - 1;
|
|
121
|
-
actual.contenido = lineas.slice(actual.lineaInicio).join('\n');
|
|
122
|
-
entradas.push(actual);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return entradas;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// ---------------------------------------------------------------------------
|
|
129
|
-
// Deduplicación
|
|
130
|
-
// ---------------------------------------------------------------------------
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Detecta entradas duplicadas por similitud de título.
|
|
134
|
-
* Usa comparación simple normalizada (lowercase, sin acentos, sin puntuación).
|
|
135
|
-
*
|
|
136
|
-
* @param {EntradaAprendizaje[]} entradas
|
|
137
|
-
* @returns {number[]} Índices de entradas duplicadas (las más antiguas se eliminan)
|
|
138
|
-
*/
|
|
139
|
-
function detectarDuplicados(entradas) {
|
|
140
|
-
const duplicados = [];
|
|
141
|
-
const vistos = new Map(); // título normalizado → índice de la versión más reciente
|
|
142
|
-
|
|
143
|
-
for (let i = 0; i < entradas.length; i++) {
|
|
144
|
-
const normalizado = entradas[i].titulo
|
|
145
|
-
.toLowerCase()
|
|
146
|
-
.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
|
|
147
|
-
.replace(/[^a-z0-9\s]/g, '')
|
|
148
|
-
.replace(/\s+/g, ' ')
|
|
149
|
-
.trim();
|
|
150
|
-
|
|
151
|
-
if (vistos.has(normalizado)) {
|
|
152
|
-
// Mantener la más reciente (mayor fecha) o la más confirmada
|
|
153
|
-
const prevIdx = vistos.get(normalizado);
|
|
154
|
-
const prev = entradas[prevIdx];
|
|
155
|
-
const curr = entradas[i];
|
|
156
|
-
|
|
157
|
-
// Priorizar por confirmaciones, luego por fecha
|
|
158
|
-
const currGana = curr.confirmaciones > prev.confirmaciones
|
|
159
|
-
|| (curr.confirmaciones === prev.confirmaciones && curr.fecha > prev.fecha);
|
|
160
|
-
if (currGana) {
|
|
161
|
-
duplicados.push(prevIdx);
|
|
162
|
-
vistos.set(normalizado, i);
|
|
163
|
-
} else {
|
|
164
|
-
duplicados.push(i);
|
|
165
|
-
}
|
|
166
|
-
} else {
|
|
167
|
-
vistos.set(normalizado, i);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return duplicados;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// ---------------------------------------------------------------------------
|
|
175
|
-
// Poda
|
|
176
|
-
// ---------------------------------------------------------------------------
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Identifica entradas candidatas a poda (>30 días sin actualización, sin confirmaciones).
|
|
180
|
-
*
|
|
181
|
-
* @param {EntradaAprendizaje[]} entradas
|
|
182
|
-
* @returns {number[]} Índices de entradas a podar
|
|
183
|
-
*/
|
|
184
|
-
function detectarObsoletos(entradas) {
|
|
185
|
-
const ahora = new Date();
|
|
186
|
-
const obsoletos = [];
|
|
187
|
-
|
|
188
|
-
for (let i = 0; i < entradas.length; i++) {
|
|
189
|
-
const e = entradas[i];
|
|
190
|
-
|
|
191
|
-
// Nunca podar entradas confirmadas
|
|
192
|
-
if (e.confirmaciones >= 2) continue;
|
|
193
|
-
|
|
194
|
-
// Nunca podar secciones curadas manualmente (no tienen fecha de hook)
|
|
195
|
-
if (!e.fecha) continue;
|
|
196
|
-
|
|
197
|
-
const fechaEntrada = new Date(e.fecha);
|
|
198
|
-
const diasDesde = (ahora - fechaEntrada) / (1000 * 60 * 60 * 24);
|
|
199
|
-
|
|
200
|
-
// Podar si: >30 días + 0 confirmaciones + no degradado (ya marcado)
|
|
201
|
-
if (diasDesde > DIAS_PODA && e.confirmaciones === 0 && !e.degradado) {
|
|
202
|
-
obsoletos.push(i);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return obsoletos;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// ---------------------------------------------------------------------------
|
|
210
|
-
// Consolidación
|
|
211
|
-
// ---------------------------------------------------------------------------
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Ejecuta la consolidación automática sobre APRENDIZAJES.md.
|
|
215
|
-
*
|
|
216
|
-
* Operaciones (en orden):
|
|
217
|
-
* 1. Parsear entradas
|
|
218
|
-
* 2. Detectar y eliminar duplicados
|
|
219
|
-
* 3. Podar obsoletos (>30 días sin confirmación)
|
|
220
|
-
* 4. Marcar degradados (≥3 contradicciones)
|
|
221
|
-
*
|
|
222
|
-
* NO hace (requiere /swl:aprender manual):
|
|
223
|
-
* - Promover a CLAUDE.md (tipo A)
|
|
224
|
-
* - Crear skills nuevos (tipo C)
|
|
225
|
-
* - Modificar comandos (tipo D)
|
|
226
|
-
*
|
|
227
|
-
* @param {string} baseDir - Raíz del proyecto
|
|
228
|
-
* @returns {{ ejecutado: boolean, detalles: object }}
|
|
229
|
-
*/
|
|
230
|
-
function consolidar(baseDir) {
|
|
231
|
-
const ruta = path.join(baseDir, '.planning', 'APRENDIZAJES.md');
|
|
232
|
-
|
|
233
|
-
if (!fs.existsSync(ruta)) {
|
|
234
|
-
return { ejecutado: false, detalles: { razon: 'archivo no existe' } };
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const contenidoOriginal = fs.readFileSync(ruta, 'utf8');
|
|
238
|
-
const entradas = parsearAprendizajes(contenidoOriginal);
|
|
239
|
-
|
|
240
|
-
if (entradas.length === 0) {
|
|
241
|
-
return { ejecutado: false, detalles: { razon: 'sin entradas para consolidar' } };
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Limitar procesamiento por ciclo
|
|
245
|
-
const aConsolidar = entradas.slice(-MAX_ENTRADAS_POR_CICLO);
|
|
246
|
-
|
|
247
|
-
// Detectar duplicados
|
|
248
|
-
const duplicados = detectarDuplicados(aConsolidar);
|
|
249
|
-
|
|
250
|
-
// Detectar obsoletos
|
|
251
|
-
const obsoletos = detectarObsoletos(aConsolidar);
|
|
252
|
-
|
|
253
|
-
// Detectar degradables (≥3 contradicciones sin marcar)
|
|
254
|
-
const degradables = [];
|
|
255
|
-
for (let i = 0; i < aConsolidar.length; i++) {
|
|
256
|
-
if (aConsolidar[i].contradicciones >= MIN_CONTRADICCIONES_DEGRADACION && !aConsolidar[i].degradado) {
|
|
257
|
-
degradables.push(i);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Si no hay nada que hacer, salir
|
|
262
|
-
const totalAcciones = duplicados.length + obsoletos.length + degradables.length;
|
|
263
|
-
if (totalAcciones === 0) {
|
|
264
|
-
return {
|
|
265
|
-
ejecutado: false,
|
|
266
|
-
detalles: {
|
|
267
|
-
razon: 'sin acciones necesarias',
|
|
268
|
-
entradasAnalizadas: aConsolidar.length,
|
|
269
|
-
},
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Construir índice de líneas a eliminar y entradas a modificar
|
|
274
|
-
const lineasAEliminar = new Set();
|
|
275
|
-
const indicesAEliminar = new Set([...duplicados, ...obsoletos]);
|
|
276
|
-
|
|
277
|
-
for (const idx of indicesAEliminar) {
|
|
278
|
-
const e = aConsolidar[idx];
|
|
279
|
-
for (let l = e.lineaInicio; l <= e.lineaFin; l++) {
|
|
280
|
-
lineasAEliminar.add(l);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// Reconstruir archivo
|
|
285
|
-
const lineas = contenidoOriginal.split('\n');
|
|
286
|
-
const nuevasLineas = [];
|
|
287
|
-
|
|
288
|
-
for (let i = 0; i < lineas.length; i++) {
|
|
289
|
-
if (lineasAEliminar.has(i)) continue;
|
|
290
|
-
|
|
291
|
-
// Marcar degradables
|
|
292
|
-
let linea = lineas[i];
|
|
293
|
-
for (const idx of degradables) {
|
|
294
|
-
const e = aConsolidar[idx];
|
|
295
|
-
if (i === e.lineaInicio && !PATRON_DEGRADADO.test(linea)) {
|
|
296
|
-
linea = linea + ' [DEGRADADO]';
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
nuevasLineas.push(linea);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// Limpiar líneas vacías consecutivas (más de 2)
|
|
304
|
-
const resultado = nuevasLineas.join('\n').replace(/\n{4,}/g, '\n\n\n');
|
|
305
|
-
|
|
306
|
-
// Escribir resultado
|
|
307
|
-
fs.writeFileSync(ruta, resultado, 'utf8');
|
|
308
|
-
|
|
309
|
-
return {
|
|
310
|
-
ejecutado: true,
|
|
311
|
-
detalles: {
|
|
312
|
-
entradasAnalizadas: aConsolidar.length,
|
|
313
|
-
duplicadosEliminados: duplicados.length,
|
|
314
|
-
obsoletoPodados: obsoletos.length,
|
|
315
|
-
degradados: degradables.length,
|
|
316
|
-
lineasAntes: lineas.length,
|
|
317
|
-
lineasDespues: resultado.split('\n').length,
|
|
318
|
-
},
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// ---------------------------------------------------------------------------
|
|
323
|
-
// Exports
|
|
324
|
-
// ---------------------------------------------------------------------------
|
|
325
|
-
|
|
326
|
-
module.exports = {
|
|
327
|
-
consolidar,
|
|
328
|
-
parsearAprendizajes,
|
|
329
|
-
detectarDuplicados,
|
|
330
|
-
detectarObsoletos,
|
|
331
|
-
DIAS_PODA,
|
|
332
|
-
MIN_CONFIRMACIONES_PROMOCION,
|
|
333
|
-
MIN_CONTRADICCIONES_DEGRADACION,
|
|
334
|
-
MAX_ENTRADAS_POR_CICLO,
|
|
335
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Auto-Consolidator — Consolidación automática de aprendizajes sin intervención humana.
|
|
5
|
+
*
|
|
6
|
+
* Cierra el ciclo: captura automática (extraccion-aprendizajes.js) →
|
|
7
|
+
* consolidación automática (este módulo) → aplicación (instintos/skills).
|
|
8
|
+
*
|
|
9
|
+
* Criterios conservadores para operar sin supervisión:
|
|
10
|
+
* - Solo promueve aprendizajes con ≥3 confirmaciones cruzadas
|
|
11
|
+
* - Solo degrada con ≥3 contradicciones
|
|
12
|
+
* - Solo poda entradas con >30 días sin referencia
|
|
13
|
+
* - Nunca modifica CLAUDE.md ni skills (eso requiere /swl:aprender manual)
|
|
14
|
+
* - Solo opera sobre APRENDIZAJES.md e instintos/proyecto.yaml
|
|
15
|
+
*
|
|
16
|
+
* Zero dependencias externas.
|
|
17
|
+
*
|
|
18
|
+
* @module hooks/lib/auto-consolidator
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const fs = require('fs');
|
|
22
|
+
const path = require('path');
|
|
23
|
+
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Constantes
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
/** Días sin referencia para considerar un aprendizaje como candidato a poda. */
|
|
29
|
+
const DIAS_PODA = 30;
|
|
30
|
+
|
|
31
|
+
/** Mínimo de confirmaciones para promover un aprendizaje. */
|
|
32
|
+
const MIN_CONFIRMACIONES_PROMOCION = 3;
|
|
33
|
+
|
|
34
|
+
/** Mínimo de contradicciones para degradar un aprendizaje. */
|
|
35
|
+
const MIN_CONTRADICCIONES_DEGRADACION = 3;
|
|
36
|
+
|
|
37
|
+
/** Máximo de entradas automáticas a procesar por consolidación. */
|
|
38
|
+
const MAX_ENTRADAS_POR_CICLO = 50;
|
|
39
|
+
|
|
40
|
+
/** Patrón para detectar entradas automáticas del hook extraccion-aprendizajes. */
|
|
41
|
+
const PATRON_ENTRADA_AUTO = /^## \[(\d{4}-\d{2}-\d{2})\] ([\w\u00C0-\u024F-]+) — (.+)$/;
|
|
42
|
+
|
|
43
|
+
/** Patrón para detectar confirmaciones. */
|
|
44
|
+
const PATRON_CONFIRMADO = /\[CONFIRMADO(?:\s+x(\d+))?\]/;
|
|
45
|
+
|
|
46
|
+
/** Patrón para detectar contradicciones. */
|
|
47
|
+
const PATRON_CONTRADICHO = /\[CONTRADICHO(?::\s*\d{4}-\d{2}-\d{2})?\]/g;
|
|
48
|
+
|
|
49
|
+
/** Patrón para detectar degradados. */
|
|
50
|
+
const PATRON_DEGRADADO = /\[DEGRADADO\]/;
|
|
51
|
+
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Parsing de APRENDIZAJES.md
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @typedef {object} EntradaAprendizaje
|
|
58
|
+
* @property {string} fecha - YYYY-MM-DD
|
|
59
|
+
* @property {string} tipo - anti-patrón, patrón, gotcha, decisión, bug-fix, descubrimiento
|
|
60
|
+
* @property {string} titulo - Descripción corta
|
|
61
|
+
* @property {string} contenido - Contenido completo de la entrada
|
|
62
|
+
* @property {number} confirmaciones - Número de confirmaciones
|
|
63
|
+
* @property {number} contradicciones - Número de contradicciones
|
|
64
|
+
* @property {boolean} degradado - Si ya fue marcado como degradado
|
|
65
|
+
* @property {number} lineaInicio - Línea donde empieza en el archivo
|
|
66
|
+
* @property {number} lineaFin - Línea donde termina
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Parsea APRENDIZAJES.md en entradas estructuradas.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} contenido
|
|
73
|
+
* @returns {EntradaAprendizaje[]}
|
|
74
|
+
*/
|
|
75
|
+
function parsearAprendizajes(contenido) {
|
|
76
|
+
const lineas = contenido.split('\n');
|
|
77
|
+
const entradas = [];
|
|
78
|
+
let actual = null;
|
|
79
|
+
|
|
80
|
+
for (let i = 0; i < lineas.length; i++) {
|
|
81
|
+
const match = lineas[i].match(PATRON_ENTRADA_AUTO);
|
|
82
|
+
if (match) {
|
|
83
|
+
if (actual) {
|
|
84
|
+
actual.lineaFin = i - 1;
|
|
85
|
+
actual.contenido = lineas.slice(actual.lineaInicio, i).join('\n');
|
|
86
|
+
entradas.push(actual);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const [, fecha, tipo, titulo] = match;
|
|
90
|
+
|
|
91
|
+
// Contar confirmaciones
|
|
92
|
+
const textoHastaAhora = lineas[i];
|
|
93
|
+
const confMatch = textoHastaAhora.match(PATRON_CONFIRMADO);
|
|
94
|
+
const confirmaciones = confMatch ? parseInt(confMatch[1] || '1', 10) : 0;
|
|
95
|
+
|
|
96
|
+
actual = {
|
|
97
|
+
fecha,
|
|
98
|
+
tipo,
|
|
99
|
+
titulo,
|
|
100
|
+
contenido: '',
|
|
101
|
+
confirmaciones,
|
|
102
|
+
contradicciones: 0,
|
|
103
|
+
degradado: PATRON_DEGRADADO.test(lineas[i]),
|
|
104
|
+
lineaInicio: i,
|
|
105
|
+
lineaFin: i,
|
|
106
|
+
};
|
|
107
|
+
} else if (actual) {
|
|
108
|
+
// Buscar confirmaciones y contradicciones en el contenido
|
|
109
|
+
const confMatch = lineas[i].match(PATRON_CONFIRMADO);
|
|
110
|
+
if (confMatch) {
|
|
111
|
+
actual.confirmaciones = Math.max(actual.confirmaciones, parseInt(confMatch[1] || '1', 10));
|
|
112
|
+
}
|
|
113
|
+
const contMatches = lineas[i].match(PATRON_CONTRADICHO);
|
|
114
|
+
if (contMatches) actual.contradicciones += contMatches.length;
|
|
115
|
+
if (PATRON_DEGRADADO.test(lineas[i])) actual.degradado = true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (actual) {
|
|
120
|
+
actual.lineaFin = lineas.length - 1;
|
|
121
|
+
actual.contenido = lineas.slice(actual.lineaInicio).join('\n');
|
|
122
|
+
entradas.push(actual);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return entradas;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Deduplicación
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Detecta entradas duplicadas por similitud de título.
|
|
134
|
+
* Usa comparación simple normalizada (lowercase, sin acentos, sin puntuación).
|
|
135
|
+
*
|
|
136
|
+
* @param {EntradaAprendizaje[]} entradas
|
|
137
|
+
* @returns {number[]} Índices de entradas duplicadas (las más antiguas se eliminan)
|
|
138
|
+
*/
|
|
139
|
+
function detectarDuplicados(entradas) {
|
|
140
|
+
const duplicados = [];
|
|
141
|
+
const vistos = new Map(); // título normalizado → índice de la versión más reciente
|
|
142
|
+
|
|
143
|
+
for (let i = 0; i < entradas.length; i++) {
|
|
144
|
+
const normalizado = entradas[i].titulo
|
|
145
|
+
.toLowerCase()
|
|
146
|
+
.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
|
|
147
|
+
.replace(/[^a-z0-9\s]/g, '')
|
|
148
|
+
.replace(/\s+/g, ' ')
|
|
149
|
+
.trim();
|
|
150
|
+
|
|
151
|
+
if (vistos.has(normalizado)) {
|
|
152
|
+
// Mantener la más reciente (mayor fecha) o la más confirmada
|
|
153
|
+
const prevIdx = vistos.get(normalizado);
|
|
154
|
+
const prev = entradas[prevIdx];
|
|
155
|
+
const curr = entradas[i];
|
|
156
|
+
|
|
157
|
+
// Priorizar por confirmaciones, luego por fecha
|
|
158
|
+
const currGana = curr.confirmaciones > prev.confirmaciones
|
|
159
|
+
|| (curr.confirmaciones === prev.confirmaciones && curr.fecha > prev.fecha);
|
|
160
|
+
if (currGana) {
|
|
161
|
+
duplicados.push(prevIdx);
|
|
162
|
+
vistos.set(normalizado, i);
|
|
163
|
+
} else {
|
|
164
|
+
duplicados.push(i);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
vistos.set(normalizado, i);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return duplicados;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
// Poda
|
|
176
|
+
// ---------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Identifica entradas candidatas a poda (>30 días sin actualización, sin confirmaciones).
|
|
180
|
+
*
|
|
181
|
+
* @param {EntradaAprendizaje[]} entradas
|
|
182
|
+
* @returns {number[]} Índices de entradas a podar
|
|
183
|
+
*/
|
|
184
|
+
function detectarObsoletos(entradas) {
|
|
185
|
+
const ahora = new Date();
|
|
186
|
+
const obsoletos = [];
|
|
187
|
+
|
|
188
|
+
for (let i = 0; i < entradas.length; i++) {
|
|
189
|
+
const e = entradas[i];
|
|
190
|
+
|
|
191
|
+
// Nunca podar entradas confirmadas
|
|
192
|
+
if (e.confirmaciones >= 2) continue;
|
|
193
|
+
|
|
194
|
+
// Nunca podar secciones curadas manualmente (no tienen fecha de hook)
|
|
195
|
+
if (!e.fecha) continue;
|
|
196
|
+
|
|
197
|
+
const fechaEntrada = new Date(e.fecha);
|
|
198
|
+
const diasDesde = (ahora - fechaEntrada) / (1000 * 60 * 60 * 24);
|
|
199
|
+
|
|
200
|
+
// Podar si: >30 días + 0 confirmaciones + no degradado (ya marcado)
|
|
201
|
+
if (diasDesde > DIAS_PODA && e.confirmaciones === 0 && !e.degradado) {
|
|
202
|
+
obsoletos.push(i);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return obsoletos;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// Consolidación
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Ejecuta la consolidación automática sobre APRENDIZAJES.md.
|
|
215
|
+
*
|
|
216
|
+
* Operaciones (en orden):
|
|
217
|
+
* 1. Parsear entradas
|
|
218
|
+
* 2. Detectar y eliminar duplicados
|
|
219
|
+
* 3. Podar obsoletos (>30 días sin confirmación)
|
|
220
|
+
* 4. Marcar degradados (≥3 contradicciones)
|
|
221
|
+
*
|
|
222
|
+
* NO hace (requiere /swl:aprender manual):
|
|
223
|
+
* - Promover a CLAUDE.md (tipo A)
|
|
224
|
+
* - Crear skills nuevos (tipo C)
|
|
225
|
+
* - Modificar comandos (tipo D)
|
|
226
|
+
*
|
|
227
|
+
* @param {string} baseDir - Raíz del proyecto
|
|
228
|
+
* @returns {{ ejecutado: boolean, detalles: object }}
|
|
229
|
+
*/
|
|
230
|
+
function consolidar(baseDir) {
|
|
231
|
+
const ruta = path.join(baseDir, '.planning', 'APRENDIZAJES.md');
|
|
232
|
+
|
|
233
|
+
if (!fs.existsSync(ruta)) {
|
|
234
|
+
return { ejecutado: false, detalles: { razon: 'archivo no existe' } };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const contenidoOriginal = fs.readFileSync(ruta, 'utf8');
|
|
238
|
+
const entradas = parsearAprendizajes(contenidoOriginal);
|
|
239
|
+
|
|
240
|
+
if (entradas.length === 0) {
|
|
241
|
+
return { ejecutado: false, detalles: { razon: 'sin entradas para consolidar' } };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Limitar procesamiento por ciclo
|
|
245
|
+
const aConsolidar = entradas.slice(-MAX_ENTRADAS_POR_CICLO);
|
|
246
|
+
|
|
247
|
+
// Detectar duplicados
|
|
248
|
+
const duplicados = detectarDuplicados(aConsolidar);
|
|
249
|
+
|
|
250
|
+
// Detectar obsoletos
|
|
251
|
+
const obsoletos = detectarObsoletos(aConsolidar);
|
|
252
|
+
|
|
253
|
+
// Detectar degradables (≥3 contradicciones sin marcar)
|
|
254
|
+
const degradables = [];
|
|
255
|
+
for (let i = 0; i < aConsolidar.length; i++) {
|
|
256
|
+
if (aConsolidar[i].contradicciones >= MIN_CONTRADICCIONES_DEGRADACION && !aConsolidar[i].degradado) {
|
|
257
|
+
degradables.push(i);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Si no hay nada que hacer, salir
|
|
262
|
+
const totalAcciones = duplicados.length + obsoletos.length + degradables.length;
|
|
263
|
+
if (totalAcciones === 0) {
|
|
264
|
+
return {
|
|
265
|
+
ejecutado: false,
|
|
266
|
+
detalles: {
|
|
267
|
+
razon: 'sin acciones necesarias',
|
|
268
|
+
entradasAnalizadas: aConsolidar.length,
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Construir índice de líneas a eliminar y entradas a modificar
|
|
274
|
+
const lineasAEliminar = new Set();
|
|
275
|
+
const indicesAEliminar = new Set([...duplicados, ...obsoletos]);
|
|
276
|
+
|
|
277
|
+
for (const idx of indicesAEliminar) {
|
|
278
|
+
const e = aConsolidar[idx];
|
|
279
|
+
for (let l = e.lineaInicio; l <= e.lineaFin; l++) {
|
|
280
|
+
lineasAEliminar.add(l);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Reconstruir archivo
|
|
285
|
+
const lineas = contenidoOriginal.split('\n');
|
|
286
|
+
const nuevasLineas = [];
|
|
287
|
+
|
|
288
|
+
for (let i = 0; i < lineas.length; i++) {
|
|
289
|
+
if (lineasAEliminar.has(i)) continue;
|
|
290
|
+
|
|
291
|
+
// Marcar degradables
|
|
292
|
+
let linea = lineas[i];
|
|
293
|
+
for (const idx of degradables) {
|
|
294
|
+
const e = aConsolidar[idx];
|
|
295
|
+
if (i === e.lineaInicio && !PATRON_DEGRADADO.test(linea)) {
|
|
296
|
+
linea = linea + ' [DEGRADADO]';
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
nuevasLineas.push(linea);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Limpiar líneas vacías consecutivas (más de 2)
|
|
304
|
+
const resultado = nuevasLineas.join('\n').replace(/\n{4,}/g, '\n\n\n');
|
|
305
|
+
|
|
306
|
+
// Escribir resultado
|
|
307
|
+
fs.writeFileSync(ruta, resultado, 'utf8');
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
ejecutado: true,
|
|
311
|
+
detalles: {
|
|
312
|
+
entradasAnalizadas: aConsolidar.length,
|
|
313
|
+
duplicadosEliminados: duplicados.length,
|
|
314
|
+
obsoletoPodados: obsoletos.length,
|
|
315
|
+
degradados: degradables.length,
|
|
316
|
+
lineasAntes: lineas.length,
|
|
317
|
+
lineasDespues: resultado.split('\n').length,
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// ---------------------------------------------------------------------------
|
|
323
|
+
// Exports
|
|
324
|
+
// ---------------------------------------------------------------------------
|
|
325
|
+
|
|
326
|
+
module.exports = {
|
|
327
|
+
consolidar,
|
|
328
|
+
parsearAprendizajes,
|
|
329
|
+
detectarDuplicados,
|
|
330
|
+
detectarObsoletos,
|
|
331
|
+
DIAS_PODA,
|
|
332
|
+
MIN_CONFIRMACIONES_PROMOCION,
|
|
333
|
+
MIN_CONTRADICCIONES_DEGRADACION,
|
|
334
|
+
MAX_ENTRADAS_POR_CICLO,
|
|
335
|
+
};
|