@saulwade/swl-ses 1.9.0 → 2.0.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 (108) hide show
  1. package/CLAUDE.md +8 -8
  2. package/README.md +12 -12
  3. package/agentes/accesibilidad-wcag-swl.md +3 -3
  4. package/agentes/auto-evolucion-swl.md +908 -908
  5. package/agentes/disenador-ui-swl.md +6 -5
  6. package/agentes/frontend-angular-swl.md +2 -2
  7. package/agentes/frontend-css-swl.md +2 -2
  8. package/agentes/frontend-react-swl.md +4 -4
  9. package/agentes/frontend-swl.md +6 -6
  10. package/agentes/investigador-ux-swl.md +5 -5
  11. package/agentes/orquestador-swl.md +7 -7
  12. package/agentes/perfilador-usuario-swl.md +308 -308
  13. package/agentes/producto-prd-swl.md +1 -1
  14. package/agentes/red-team-swl.md +218 -218
  15. package/agentes/tdd-qa-swl.md +17 -1
  16. package/comandos/swl/actualizar.md +1 -1
  17. package/comandos/swl/aprender.md +2 -2
  18. package/comandos/swl/aprobar-plan.md +152 -0
  19. package/comandos/swl/ayuda.md +3 -3
  20. package/comandos/swl/discutir-fase.md +20 -2
  21. package/comandos/swl/ejecutar-fase.md +53 -6
  22. package/comandos/swl/evolucionar.md +1 -1
  23. package/comandos/swl/inbox.md +1 -1
  24. package/comandos/swl/instalar.md +1 -1
  25. package/comandos/swl/nemesis.md +1 -1
  26. package/comandos/swl/planear-fase.md +17 -1
  27. package/comandos/swl/plugins.md +1 -1
  28. package/comandos/swl/release.md +1 -1
  29. package/comandos/swl/status.md +279 -0
  30. package/comandos/swl/verificar.md +26 -1
  31. package/habilidades/ai-runtime-security/SKILL.md +1 -1
  32. package/habilidades/auto-evolucion-protocolo/SKILL.md +276 -276
  33. package/habilidades/benchmark-memoria/SKILL.md +1 -1
  34. package/habilidades/calidad-contract-testing/SKILL.md +165 -0
  35. package/habilidades/changelog-generator/SKILL.md +9 -2
  36. package/habilidades/changelog-generator/scripts/parse-commits.js +11 -1
  37. package/habilidades/diagrama-arquitectura/SKILL.md +1 -1
  38. package/habilidades/drift-detection/SKILL.md +179 -179
  39. package/habilidades/ejecutar-fase/SKILL.md +64 -14
  40. package/habilidades/estructura-proyecto-claude/SKILL.md +17 -14
  41. package/habilidades/estructura-proyecto-claude/recursos/configuracion-y-extensiones.md +34 -23
  42. package/habilidades/estructura-proyecto-claude/recursos/frontmatter-y-hooks-referencia.md +70 -53
  43. package/habilidades/estructura-proyecto-claude/recursos/mcp-json-template.json +57 -77
  44. package/habilidades/extractor-de-aprendizajes/SKILL.md +9 -5
  45. package/habilidades/harness-claude-code/SKILL.md +10 -7
  46. package/{reglas/harness-claude-code.md → habilidades/harness-claude-code/recursos/disciplina-harness-regla.md} +2 -2
  47. package/habilidades/instalar-sistema/SKILL.md +3 -3
  48. package/habilidades/meta-skills-estandar/recursos/frameworks-seguridad.md +1 -1
  49. package/habilidades/perfil-usuario/SKILL.md +200 -200
  50. package/habilidades/planear-fase/SKILL.md +25 -4
  51. package/habilidades/proceso-ddia-fundamentos/SKILL.md +1 -1
  52. package/habilidades/proceso-ddia-streaming/SKILL.md +4 -4
  53. package/habilidades/proceso-debate-adversarial/SKILL.md +2 -2
  54. package/habilidades/protocolo-revision-swl/SKILL.md +1 -1
  55. package/habilidades/seguridad-skills-ia/SKILL.md +1 -1
  56. package/habilidades/swl-claudemd/SKILL.md +50 -210
  57. package/habilidades/swl-claudemd/recursos/contrato-aprender.md +83 -0
  58. package/habilidades/swl-claudemd/recursos/duplicacion-reglas-globales.md +85 -0
  59. package/habilidades/swl-claudemd/recursos/plantillas-init.md +94 -0
  60. package/habilidades/swl-dashboard/SKILL.md +9 -9
  61. package/habilidades/swl-revisar-impacto/SKILL.md +1 -1
  62. package/habilidades/tdd-workflow/SKILL.md +45 -5
  63. package/habilidades/validacion-ci-sistema/SKILL.md +3 -3
  64. package/hooks/calidad-pre-commit.js +340 -3
  65. package/hooks/ciclo-evolucion-subagente.js +26 -0
  66. package/hooks/ciclo-evolucion.js +26 -0
  67. package/hooks/extraccion-aprendizajes.js +13 -0
  68. package/hooks/lib/ciclo-evolucion.js +47 -0
  69. package/hooks/{auto-evolucion.js → lib/etapa-auto-evolucion.js} +701 -700
  70. package/hooks/{metricas-evolucion.js → lib/etapa-metricas.js} +388 -376
  71. package/hooks/{actualizar-perfil-usuario.js → lib/etapa-perfil-usuario.js} +376 -364
  72. package/hooks/lib/evolution-tracker.js +24 -3
  73. package/hooks/spec-gate.js +211 -0
  74. package/hooks/tdd-gate.js +241 -0
  75. package/hooks/validar-intent-spec.js +30 -10
  76. package/llms.txt +6 -6
  77. package/manifiestos/hooks-config.json +26 -17
  78. package/manifiestos/modulos.json +17 -14
  79. package/manifiestos/skills-lock.json +63 -56
  80. package/package.json +2 -2
  81. package/plugin.json +6 -10
  82. package/reglas/accesibilidad.md +10 -0
  83. package/reglas/api-diseno.md +9 -0
  84. package/reglas/auditorias-documentales-estructurales.md +7 -0
  85. package/reglas/cloud-infra.md +8 -0
  86. package/reglas/fragmentos-compartidos.md +5 -0
  87. package/reglas/gobernanza.md +4 -4
  88. package/reglas/hooks.md +6 -0
  89. package/reglas/intent-engineering.md +4 -0
  90. package/reglas/markitdown.md +8 -0
  91. package/reglas/memoria-consolidada.md +1 -1
  92. package/reglas/patrones.md +6 -0
  93. package/reglas/registro-componentes-nuevos.md +10 -1
  94. package/reglas/seguridad-agentes.md +1 -1
  95. package/reglas/skills-estandar.md +6 -0
  96. package/reglas/testing.md +7 -0
  97. package/reglas/tests-cleanup.md +4 -0
  98. package/reglas/usar-sistema-swl.md +1 -1
  99. package/scripts/lib/gitignore-manifest.js +29 -1
  100. package/scripts/lib/plan-lock.js +275 -0
  101. package/scripts/migrar-fase-dominio.js +0 -1
  102. package/scripts/verificar-trazabilidad.js +292 -0
  103. package/agentes/ux-disenador-swl.md +0 -503
  104. package/comandos/swl/dashboard.md +0 -146
  105. package/comandos/swl/evolucion-estado.md +0 -191
  106. package/comandos/swl/metricas.md +0 -376
  107. package/comandos/swl/salud.md +0 -481
  108. package/reglas/verificar-citas-temporales.md +0 -139
@@ -1,364 +1,376 @@
1
- #!/usr/bin/env node
2
- 'use strict';
3
-
4
- /**
5
- * Hook: actualizar-perfil-usuario.js
6
- * Tipo: Stop (async: true — fire-and-forget)
7
- * Registrado en: Stop (matcher vacío)
8
- *
9
- * Detecta señales en la sesión que justifican actualizar el modelo
10
- * persistente del usuario (instintos/perfil-usuario.yaml).
11
- *
12
- * No modifica el perfil directamente — solo acumula señales en un
13
- * "dirty-bit" (.planning/user-profile/dirty.json) y, cuando el umbral
14
- * se cruza, emite un nudge por stderr sugiriendo invocar el agente
15
- * perfilador-usuario-swl.
16
- *
17
- * Señales detectadas (en input stdin con la transcripción de la sesión):
18
- * 1. Correcciones del usuario: "no así", "mejor", "en vez de",
19
- * "en lugar de", "evita", "nunca", "siempre quiero", "prefiero".
20
- * 2. Preferencias explícitas: "me gusta", "quiero que", "recuerda que",
21
- * "a partir de ahora".
22
- * 3. Aprendizajes nuevos en .planning/APRENDIZAJES.md desde la última corrida.
23
- * 4. Instintos con evidence_count >= 3 en instintos/proyecto.yaml.
24
- *
25
- * Umbral por defecto: 3 señales acumuladas → nudge.
26
- *
27
- * El hook nunca bloquea (siempre exit 0). Zero-dependencies.
28
- */
29
-
30
- const fs = require('fs');
31
- const path = require('path');
32
-
33
- let atomicWriteJSON;
34
- try {
35
- ({ atomicWriteJSON } = require('./lib/atomic-write'));
36
- } catch {
37
- // Fallback sin atomic-write si la librería no está (ej. en test aislado)
38
- atomicWriteJSON = (p, obj) => fs.writeFileSync(p, JSON.stringify(obj, null, 2), 'utf8');
39
- }
40
-
41
- // Scanner de prompt injection — patrón adoptado de Hermes memory_tool.py.
42
- // Si la librería no está disponible (test aislado), fallback permisivo.
43
- let scanInjection;
44
- try {
45
- ({ scan: scanInjection } = require('./lib/prompt-injection-scanner'));
46
- } catch {
47
- scanInjection = () => ({ safe: true, threats: [], criticalCount: 0, highCount: 0 });
48
- }
49
-
50
- // Nudge tracker — registra cada nudge para medir tasa de acción.
51
- let nudgeTracker;
52
- try {
53
- nudgeTracker = require('./lib/nudge-tracker');
54
- } catch {
55
- nudgeTracker = null;
56
- }
57
-
58
- // ---------------------------------------------------------------------------
59
- // Configuración
60
- // ---------------------------------------------------------------------------
61
-
62
- const UMBRAL_NUDGE = parseInt(process.env.SWL_PERFIL_UMBRAL || '3', 10);
63
- const DIR_PERFIL = path.join(process.cwd(), '.planning', 'user-profile');
64
- const DIRTY_PATH = path.join(DIR_PERFIL, 'dirty.json');
65
- const PERFIL_PATH = path.join(process.cwd(), 'instintos', 'perfil-usuario.yaml');
66
- const APRENDIZAJES_PATH = path.join(process.cwd(), '.planning', 'APRENDIZAJES.md');
67
- const INSTINTOS_PATH = path.join(process.cwd(), 'instintos', 'proyecto.yaml');
68
-
69
- // Keywords de corrección (señal fuerte: el usuario está corrigiendo)
70
- const KEYWORDS_CORRECCION = [
71
- 'no así', 'no asi', 'mejor', 'en vez de', 'en lugar de',
72
- 'evita', 'evites', 'no uses', 'nunca uses', 'nunca hagas',
73
- 'siempre quiero', 'siempre usa', 'prefiero', 'prefiere',
74
- 'no me gusta', 'deja de',
75
- ];
76
-
77
- // Keywords de preferencia explícita (señal media)
78
- const KEYWORDS_PREFERENCIA = [
79
- 'me gusta', 'quiero que', 'recuerda que', 'a partir de ahora',
80
- 'de ahora en adelante', 'ten en cuenta',
81
- ];
82
-
83
- // ---------------------------------------------------------------------------
84
- // Utilidades
85
- // ---------------------------------------------------------------------------
86
-
87
- function ensureDir(dir) {
88
- try { fs.mkdirSync(dir, { recursive: true }); } catch { /* ignore */ }
89
- }
90
-
91
- function leerDirty() {
92
- try {
93
- const raw = fs.readFileSync(DIRTY_PATH, 'utf8');
94
- return JSON.parse(raw);
95
- } catch {
96
- // Primera corrida: inicializar snapshot al estado actual para no
97
- // generar falsos "nuevos" por el histórico preexistente.
98
- return {
99
- version: '1.0',
100
- creado: new Date().toISOString(),
101
- senales: [],
102
- ultima_nudge: null,
103
- snapshot: {
104
- aprendizajes_headers: contarAprendizajesNuevos(0),
105
- instintos_maduros: contarInstintosMaduros(),
106
- },
107
- primera_corrida: true,
108
- };
109
- }
110
- }
111
-
112
- function escribirDirty(estado) {
113
- ensureDir(DIR_PERFIL);
114
- atomicWriteJSON(DIRTY_PATH, estado);
115
- }
116
-
117
- function contarLineasArchivo(p) {
118
- try {
119
- return fs.readFileSync(p, 'utf8').split(/\r?\n/).length;
120
- } catch {
121
- return 0;
122
- }
123
- }
124
-
125
- // ---------------------------------------------------------------------------
126
- // Detectores de señales
127
- // ---------------------------------------------------------------------------
128
-
129
- /**
130
- * Busca keywords de corrección/preferencia en el texto.
131
- * @returns {Array<{tipo:string, keyword:string, snippet:string}>}
132
- */
133
- function detectarKeywords(texto) {
134
- if (!texto || typeof texto !== 'string') return [];
135
- const textoLow = texto.toLowerCase();
136
- const señales = [];
137
-
138
- for (const kw of KEYWORDS_CORRECCION) {
139
- const idx = textoLow.indexOf(kw);
140
- if (idx !== -1) {
141
- señales.push({
142
- tipo: 'correccion',
143
- keyword: kw,
144
- snippet: texto.slice(Math.max(0, idx - 20), idx + kw.length + 60).trim(),
145
- });
146
- }
147
- }
148
- for (const kw of KEYWORDS_PREFERENCIA) {
149
- const idx = textoLow.indexOf(kw);
150
- if (idx !== -1) {
151
- señales.push({
152
- tipo: 'preferencia',
153
- keyword: kw,
154
- snippet: texto.slice(Math.max(0, idx - 20), idx + kw.length + 60).trim(),
155
- });
156
- }
157
- }
158
-
159
- return señales;
160
- }
161
-
162
- /**
163
- * Cuenta entradas en APRENDIZAJES.md desde una línea de referencia.
164
- * @returns {number} cantidad de entradas nuevas
165
- */
166
- function contarAprendizajesNuevos(lineaReferencia) {
167
- try {
168
- const contenido = fs.readFileSync(APRENDIZAJES_PATH, 'utf8');
169
- const lineas = contenido.split(/\r?\n/);
170
- const headers = lineas.filter(l => /^##\s+\[\d{4}-\d{2}-\d{2}\]/.test(l));
171
- const headersAntes = Math.max(0, lineaReferencia);
172
- return Math.max(0, headers.length - headersAntes);
173
- } catch {
174
- return 0;
175
- }
176
- }
177
-
178
- /**
179
- * Cuenta instintos con evidence_count >= 3 en instintos/proyecto.yaml.
180
- * @returns {number}
181
- */
182
- function contarInstintosMaduros() {
183
- try {
184
- const contenido = fs.readFileSync(INSTINTOS_PATH, 'utf8');
185
- const matches = contenido.match(/evidence_count:\s*([3-9]|\d{2,})/g);
186
- return matches ? matches.length : 0;
187
- } catch {
188
- return 0;
189
- }
190
- }
191
-
192
- // ---------------------------------------------------------------------------
193
- // Extracción del texto del usuario desde stdin del hook
194
- // ---------------------------------------------------------------------------
195
-
196
- /**
197
- * Extrae los prompts del usuario de la transcripción que Claude Code pasa
198
- * al hook Stop. El input viene como JSON con un array de mensajes.
199
- *
200
- * @param {string} inputRaw
201
- * @returns {string} concatenación de prompts del usuario
202
- */
203
- function extraerPromptsUsuario(inputRaw) {
204
- if (!inputRaw) return '';
205
- try {
206
- const data = JSON.parse(inputRaw);
207
- // Estructuras comunes: { messages: [...] } o { transcript: [...] }
208
- const mensajes = data.messages || data.transcript || data.conversation || [];
209
- if (!Array.isArray(mensajes)) return '';
210
-
211
- return mensajes
212
- .filter(m => m && (m.role === 'user' || m.type === 'user'))
213
- .map(m => {
214
- if (typeof m.content === 'string') return m.content;
215
- if (Array.isArray(m.content)) {
216
- return m.content
217
- .filter(c => c && c.type === 'text' && typeof c.text === 'string')
218
- .map(c => c.text)
219
- .join('\n');
220
- }
221
- return '';
222
- })
223
- .join('\n\n');
224
- } catch {
225
- return '';
226
- }
227
- }
228
-
229
- // ---------------------------------------------------------------------------
230
- // Entrypoint
231
- // ---------------------------------------------------------------------------
232
-
233
- let inputRaw = '';
234
- process.stdin.on('data', chunk => { inputRaw += chunk; });
235
-
236
- process.stdin.on('end', () => {
237
- try {
238
- const textoUsuario = extraerPromptsUsuario(inputRaw);
239
-
240
- const estado = leerDirty();
241
- const totalPrevio = estado.senales.length;
242
-
243
- // 1. Señales desde el texto del usuario, con escaneo de prompt injection
244
- // sobre cada snippet antes de persistir. Patrón adoptado de
245
- // Hermes memory_tool.py: el contenido que entra a memoria persistente
246
- // debe escanearse por inyección de lo contrario un atacante con
247
- // acceso al prompt puede inyectar instrucciones que luego se recargan
248
- // cada sesión.
249
- const señalesKw = detectarKeywords(textoUsuario)
250
- .map(s => {
251
- const r = scanInjection(s.snippet, 'perfil-usuario/dirty.json');
252
- return { ...s, _scan: r };
253
- })
254
- .filter(s => {
255
- // Descartar snippets con amenazas CRÍTICAS (prompt injection directa)
256
- if (s._scan.criticalCount > 0) {
257
- process.stderr.write(
258
- `[perfil-usuario] señal descartada por injection crítica (${s._scan.threats[0]?.type}): keyword="${s.keyword}"\n`
259
- );
260
- return false;
261
- }
262
- return true;
263
- })
264
- .map(s => {
265
- // Conservar snippet pero marcar si hubo patrones high (no críticos)
266
- const out = { tipo: s.tipo, keyword: s.keyword, snippet: s.snippet };
267
- if (s._scan.highCount > 0) {
268
- out.warnings = s._scan.threats
269
- .filter(t => t.severity === 'high')
270
- .map(t => t.type);
271
- }
272
- return out;
273
- });
274
-
275
- // 2. Aprendizajes nuevos (delta desde el último snapshot)
276
- // En la primera corrida NO contamos histórico como "nuevo".
277
- const snapshot = estado.snapshot || { aprendizajes_headers: 0, instintos_maduros: 0 };
278
- const instintosMadurosActual = contarInstintosMaduros();
279
- const aprendizajesNuevos = estado.primera_corrida
280
- ? 0
281
- : contarAprendizajesNuevos(snapshot.aprendizajes_headers);
282
- const instintosNuevos = estado.primera_corrida
283
- ? 0
284
- : Math.max(0, instintosMadurosActual - snapshot.instintos_maduros);
285
-
286
- delete estado.primera_corrida;
287
-
288
- // 3. Agregar al dirty-bit
289
- const ahora = new Date().toISOString();
290
- const nuevas = [
291
- ...señalesKw.map(s => ({ ...s, ts: ahora })),
292
- ...(aprendizajesNuevos > 0
293
- ? [{ tipo: 'aprendizaje-nuevo', count: aprendizajesNuevos, ts: ahora }]
294
- : []),
295
- ...(instintosNuevos > 0
296
- ? [{ tipo: 'instinto-maduro', count: instintosNuevos, ts: ahora }]
297
- : []),
298
- ];
299
-
300
- if (nuevas.length === 0) {
301
- // Nada que registrar — actualizar snapshot y salir
302
- estado.snapshot = {
303
- aprendizajes_headers: contarAprendizajesNuevos(0),
304
- instintos_maduros: instintosMadurosActual,
305
- };
306
- escribirDirty(estado);
307
- return;
308
- }
309
-
310
- estado.senales.push(...nuevas);
311
- estado.snapshot = {
312
- aprendizajes_headers: contarAprendizajesNuevos(0),
313
- instintos_maduros: instintosMadurosActual,
314
- };
315
-
316
- // 4. Verificar umbral para nudge
317
- const total = estado.senales.length;
318
- const cruzaUmbral = totalPrevio < UMBRAL_NUDGE && total >= UMBRAL_NUDGE;
319
-
320
- // Evitar nudges repetidos: solo uno por día
321
- let hacerNudge = cruzaUmbral;
322
- if (hacerNudge && estado.ultima_nudge) {
323
- const horas = (Date.now() - new Date(estado.ultima_nudge).getTime()) / 3600000;
324
- if (horas < 24) hacerNudge = false;
325
- }
326
-
327
- if (hacerNudge) {
328
- estado.ultima_nudge = ahora;
329
-
330
- const perfilExiste = fs.existsSync(PERFIL_PATH);
331
- const accion = perfilExiste
332
- ? 'actualizar tu perfil de usuario'
333
- : 'inicializar tu perfil de usuario';
334
-
335
- const mensaje = [
336
- '',
337
- `[perfil-usuario] ${total} señales acumuladas sin consolidar.`,
338
- ` Para ${accion}, invoca el agente:`,
339
- ` "actualiza mi perfil de usuario"`,
340
- ` (ejecuta el agente perfilador-usuario-swl)`,
341
- '',
342
- ].join('\n');
343
-
344
- process.stderr.write(mensaje);
345
-
346
- // Registrar en nudge-tracker para medir tasa de acción
347
- if (nudgeTracker) {
348
- try {
349
- nudgeTracker.emit({
350
- kind: 'perfil-usuario',
351
- target: 'perfilador-usuario-swl',
352
- message: mensaje.trim(),
353
- source: 'hooks/actualizar-perfil-usuario.js',
354
- data: { senalesAcumuladas: total },
355
- });
356
- } catch { /* silencioso */ }
357
- }
358
- }
359
-
360
- escribirDirty(estado);
361
- } catch {
362
- // Nunca bloquear por error interno
363
- }
364
- });
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * Hook: actualizar-perfil-usuario.js
6
+ * Tipo: Stop (async: true — fire-and-forget)
7
+ * Registrado en: Stop (matcher vacío)
8
+ *
9
+ * Detecta señales en la sesión que justifican actualizar el modelo
10
+ * persistente del usuario (instintos/perfil-usuario.yaml).
11
+ *
12
+ * No modifica el perfil directamente — solo acumula señales en un
13
+ * "dirty-bit" (.planning/user-profile/dirty.json) y, cuando el umbral
14
+ * se cruza, emite un nudge por stderr sugiriendo invocar el agente
15
+ * perfilador-usuario-swl.
16
+ *
17
+ * Señales detectadas (en input stdin con la transcripción de la sesión):
18
+ * 1. Correcciones del usuario: "no así", "mejor", "en vez de",
19
+ * "en lugar de", "evita", "nunca", "siempre quiero", "prefiero".
20
+ * 2. Preferencias explícitas: "me gusta", "quiero que", "recuerda que",
21
+ * "a partir de ahora".
22
+ * 3. Aprendizajes nuevos en .planning/APRENDIZAJES.md desde la última corrida.
23
+ * 4. Instintos con evidence_count >= 3 en instintos/proyecto.yaml.
24
+ *
25
+ * Umbral por defecto: 3 señales acumuladas → nudge.
26
+ *
27
+ * El hook nunca bloquea (siempre exit 0). Zero-dependencies.
28
+ */
29
+
30
+ const fs = require('fs');
31
+ const path = require('path');
32
+
33
+ let atomicWriteJSON;
34
+ try {
35
+ ({ atomicWriteJSON } = require('./atomic-write'));
36
+ } catch {
37
+ // Fallback sin atomic-write si la librería no está (ej. en test aislado)
38
+ atomicWriteJSON = (p, obj) => fs.writeFileSync(p, JSON.stringify(obj, null, 2), 'utf8');
39
+ }
40
+
41
+ // Scanner de prompt injection — patrón adoptado de Hermes memory_tool.py.
42
+ // Si la librería no está disponible (test aislado), fallback permisivo.
43
+ let scanInjection;
44
+ try {
45
+ ({ scan: scanInjection } = require('./prompt-injection-scanner'));
46
+ } catch {
47
+ scanInjection = () => ({ safe: true, threats: [], criticalCount: 0, highCount: 0 });
48
+ }
49
+
50
+ // Nudge tracker — registra cada nudge para medir tasa de acción.
51
+ let nudgeTracker;
52
+ try {
53
+ nudgeTracker = require('./nudge-tracker');
54
+ } catch {
55
+ nudgeTracker = null;
56
+ }
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // Configuración
60
+ // ---------------------------------------------------------------------------
61
+
62
+ const UMBRAL_NUDGE = parseInt(process.env.SWL_PERFIL_UMBRAL || '3', 10);
63
+ const DIR_PERFIL = path.join(process.cwd(), '.planning', 'user-profile');
64
+ const DIRTY_PATH = path.join(DIR_PERFIL, 'dirty.json');
65
+ const PERFIL_PATH = path.join(process.cwd(), 'instintos', 'perfil-usuario.yaml');
66
+ const APRENDIZAJES_PATH = path.join(process.cwd(), '.planning', 'APRENDIZAJES.md');
67
+ const INSTINTOS_PATH = path.join(process.cwd(), 'instintos', 'proyecto.yaml');
68
+
69
+ // Keywords de corrección (señal fuerte: el usuario está corrigiendo)
70
+ const KEYWORDS_CORRECCION = [
71
+ 'no así', 'no asi', 'mejor', 'en vez de', 'en lugar de',
72
+ 'evita', 'evites', 'no uses', 'nunca uses', 'nunca hagas',
73
+ 'siempre quiero', 'siempre usa', 'prefiero', 'prefiere',
74
+ 'no me gusta', 'deja de',
75
+ ];
76
+
77
+ // Keywords de preferencia explícita (señal media)
78
+ const KEYWORDS_PREFERENCIA = [
79
+ 'me gusta', 'quiero que', 'recuerda que', 'a partir de ahora',
80
+ 'de ahora en adelante', 'ten en cuenta',
81
+ ];
82
+
83
+ // ---------------------------------------------------------------------------
84
+ // Utilidades
85
+ // ---------------------------------------------------------------------------
86
+
87
+ function ensureDir(dir) {
88
+ try { fs.mkdirSync(dir, { recursive: true }); } catch { /* ignore */ }
89
+ }
90
+
91
+ function leerDirty() {
92
+ try {
93
+ const raw = fs.readFileSync(DIRTY_PATH, 'utf8');
94
+ return JSON.parse(raw);
95
+ } catch {
96
+ // Primera corrida: inicializar snapshot al estado actual para no
97
+ // generar falsos "nuevos" por el histórico preexistente.
98
+ return {
99
+ version: '1.0',
100
+ creado: new Date().toISOString(),
101
+ senales: [],
102
+ ultima_nudge: null,
103
+ snapshot: {
104
+ aprendizajes_headers: contarAprendizajesNuevos(0),
105
+ instintos_maduros: contarInstintosMaduros(),
106
+ },
107
+ primera_corrida: true,
108
+ };
109
+ }
110
+ }
111
+
112
+ function escribirDirty(estado) {
113
+ ensureDir(DIR_PERFIL);
114
+ atomicWriteJSON(DIRTY_PATH, estado);
115
+ }
116
+
117
+ function contarLineasArchivo(p) {
118
+ try {
119
+ return fs.readFileSync(p, 'utf8').split(/\r?\n/).length;
120
+ } catch {
121
+ return 0;
122
+ }
123
+ }
124
+
125
+ // ---------------------------------------------------------------------------
126
+ // Detectores de señales
127
+ // ---------------------------------------------------------------------------
128
+
129
+ /**
130
+ * Busca keywords de corrección/preferencia en el texto.
131
+ * @returns {Array<{tipo:string, keyword:string, snippet:string}>}
132
+ */
133
+ function detectarKeywords(texto) {
134
+ if (!texto || typeof texto !== 'string') return [];
135
+ const textoLow = texto.toLowerCase();
136
+ const señales = [];
137
+
138
+ for (const kw of KEYWORDS_CORRECCION) {
139
+ const idx = textoLow.indexOf(kw);
140
+ if (idx !== -1) {
141
+ señales.push({
142
+ tipo: 'correccion',
143
+ keyword: kw,
144
+ snippet: texto.slice(Math.max(0, idx - 20), idx + kw.length + 60).trim(),
145
+ });
146
+ }
147
+ }
148
+ for (const kw of KEYWORDS_PREFERENCIA) {
149
+ const idx = textoLow.indexOf(kw);
150
+ if (idx !== -1) {
151
+ señales.push({
152
+ tipo: 'preferencia',
153
+ keyword: kw,
154
+ snippet: texto.slice(Math.max(0, idx - 20), idx + kw.length + 60).trim(),
155
+ });
156
+ }
157
+ }
158
+
159
+ return señales;
160
+ }
161
+
162
+ /**
163
+ * Cuenta entradas en APRENDIZAJES.md desde una línea de referencia.
164
+ * @returns {number} cantidad de entradas nuevas
165
+ */
166
+ function contarAprendizajesNuevos(lineaReferencia) {
167
+ try {
168
+ const contenido = fs.readFileSync(APRENDIZAJES_PATH, 'utf8');
169
+ const lineas = contenido.split(/\r?\n/);
170
+ const headers = lineas.filter(l => /^##\s+\[\d{4}-\d{2}-\d{2}\]/.test(l));
171
+ const headersAntes = Math.max(0, lineaReferencia);
172
+ return Math.max(0, headers.length - headersAntes);
173
+ } catch {
174
+ return 0;
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Cuenta instintos con evidence_count >= 3 en instintos/proyecto.yaml.
180
+ * @returns {number}
181
+ */
182
+ function contarInstintosMaduros() {
183
+ try {
184
+ const contenido = fs.readFileSync(INSTINTOS_PATH, 'utf8');
185
+ const matches = contenido.match(/evidence_count:\s*([3-9]|\d{2,})/g);
186
+ return matches ? matches.length : 0;
187
+ } catch {
188
+ return 0;
189
+ }
190
+ }
191
+
192
+ // ---------------------------------------------------------------------------
193
+ // Extracción del texto del usuario desde stdin del hook
194
+ // ---------------------------------------------------------------------------
195
+
196
+ /**
197
+ * Extrae los prompts del usuario de la transcripción que Claude Code pasa
198
+ * al hook Stop. El input viene como JSON con un array de mensajes.
199
+ *
200
+ * @param {string} inputRaw
201
+ * @returns {string} concatenación de prompts del usuario
202
+ */
203
+ function extraerPromptsUsuario(inputRaw) {
204
+ if (!inputRaw) return '';
205
+ try {
206
+ const data = JSON.parse(inputRaw);
207
+ // Estructuras comunes: { messages: [...] } o { transcript: [...] }
208
+ const mensajes = data.messages || data.transcript || data.conversation || [];
209
+ if (!Array.isArray(mensajes)) return '';
210
+
211
+ return mensajes
212
+ .filter(m => m && (m.role === 'user' || m.type === 'user'))
213
+ .map(m => {
214
+ if (typeof m.content === 'string') return m.content;
215
+ if (Array.isArray(m.content)) {
216
+ return m.content
217
+ .filter(c => c && c.type === 'text' && typeof c.text === 'string')
218
+ .map(c => c.text)
219
+ .join('\n');
220
+ }
221
+ return '';
222
+ })
223
+ .join('\n\n');
224
+ } catch {
225
+ return '';
226
+ }
227
+ }
228
+
229
+ // ---------------------------------------------------------------------------
230
+ // Entrypoint
231
+ // ---------------------------------------------------------------------------
232
+
233
+ /**
234
+ * Etapa de actualización del perfil de usuario (sub-etapa de hooks/ciclo-evolucion.js).
235
+ * Consume la transcripción de stdin para detectar señales del usuario.
236
+ * Best-effort: nunca lanza.
237
+ * @param {string} inputRaw transcripción de la sesión (payload del hook Stop)
238
+ */
239
+ function ejecutar(inputRaw) {
240
+ try {
241
+ const textoUsuario = extraerPromptsUsuario(inputRaw);
242
+
243
+ const estado = leerDirty();
244
+ const totalPrevio = estado.senales.length;
245
+
246
+ // 1. Señales desde el texto del usuario, con escaneo de prompt injection
247
+ // sobre cada snippet antes de persistir. Patrón adoptado de
248
+ // Hermes memory_tool.py: el contenido que entra a memoria persistente
249
+ // debe escanearse por inyección — de lo contrario un atacante con
250
+ // acceso al prompt puede inyectar instrucciones que luego se recargan
251
+ // cada sesión.
252
+ const señalesKw = detectarKeywords(textoUsuario)
253
+ .map(s => {
254
+ const r = scanInjection(s.snippet, 'perfil-usuario/dirty.json');
255
+ return { ...s, _scan: r };
256
+ })
257
+ .filter(s => {
258
+ // Descartar snippets con amenazas CRÍTICAS (prompt injection directa)
259
+ if (s._scan.criticalCount > 0) {
260
+ process.stderr.write(
261
+ `[perfil-usuario] señal descartada por injection crítica (${s._scan.threats[0]?.type}): keyword="${s.keyword}"\n`
262
+ );
263
+ return false;
264
+ }
265
+ return true;
266
+ })
267
+ .map(s => {
268
+ // Conservar snippet pero marcar si hubo patrones high (no críticos)
269
+ const out = { tipo: s.tipo, keyword: s.keyword, snippet: s.snippet };
270
+ if (s._scan.highCount > 0) {
271
+ out.warnings = s._scan.threats
272
+ .filter(t => t.severity === 'high')
273
+ .map(t => t.type);
274
+ }
275
+ return out;
276
+ });
277
+
278
+ // 2. Aprendizajes nuevos (delta desde el último snapshot)
279
+ // En la primera corrida NO contamos histórico como "nuevo".
280
+ const snapshot = estado.snapshot || { aprendizajes_headers: 0, instintos_maduros: 0 };
281
+ const instintosMadurosActual = contarInstintosMaduros();
282
+ const aprendizajesNuevos = estado.primera_corrida
283
+ ? 0
284
+ : contarAprendizajesNuevos(snapshot.aprendizajes_headers);
285
+ const instintosNuevos = estado.primera_corrida
286
+ ? 0
287
+ : Math.max(0, instintosMadurosActual - snapshot.instintos_maduros);
288
+
289
+ delete estado.primera_corrida;
290
+
291
+ // 3. Agregar al dirty-bit
292
+ const ahora = new Date().toISOString();
293
+ const nuevas = [
294
+ ...señalesKw.map(s => ({ ...s, ts: ahora })),
295
+ ...(aprendizajesNuevos > 0
296
+ ? [{ tipo: 'aprendizaje-nuevo', count: aprendizajesNuevos, ts: ahora }]
297
+ : []),
298
+ ...(instintosNuevos > 0
299
+ ? [{ tipo: 'instinto-maduro', count: instintosNuevos, ts: ahora }]
300
+ : []),
301
+ ];
302
+
303
+ if (nuevas.length === 0) {
304
+ // Nada que registrar — actualizar snapshot y salir
305
+ estado.snapshot = {
306
+ aprendizajes_headers: contarAprendizajesNuevos(0),
307
+ instintos_maduros: instintosMadurosActual,
308
+ };
309
+ escribirDirty(estado);
310
+ return;
311
+ }
312
+
313
+ estado.senales.push(...nuevas);
314
+ estado.snapshot = {
315
+ aprendizajes_headers: contarAprendizajesNuevos(0),
316
+ instintos_maduros: instintosMadurosActual,
317
+ };
318
+
319
+ // 4. Verificar umbral para nudge
320
+ const total = estado.senales.length;
321
+ const cruzaUmbral = totalPrevio < UMBRAL_NUDGE && total >= UMBRAL_NUDGE;
322
+
323
+ // Evitar nudges repetidos: solo uno por día
324
+ let hacerNudge = cruzaUmbral;
325
+ if (hacerNudge && estado.ultima_nudge) {
326
+ const horas = (Date.now() - new Date(estado.ultima_nudge).getTime()) / 3600000;
327
+ if (horas < 24) hacerNudge = false;
328
+ }
329
+
330
+ if (hacerNudge) {
331
+ estado.ultima_nudge = ahora;
332
+
333
+ const perfilExiste = fs.existsSync(PERFIL_PATH);
334
+ const accion = perfilExiste
335
+ ? 'actualizar tu perfil de usuario'
336
+ : 'inicializar tu perfil de usuario';
337
+
338
+ const mensaje = [
339
+ '',
340
+ `[perfil-usuario] ${total} señales acumuladas sin consolidar.`,
341
+ ` Para ${accion}, invoca el agente:`,
342
+ ` "actualiza mi perfil de usuario"`,
343
+ ` (ejecuta el agente perfilador-usuario-swl)`,
344
+ '',
345
+ ].join('\n');
346
+
347
+ process.stderr.write(mensaje);
348
+
349
+ // Registrar en nudge-tracker para medir tasa de acción
350
+ if (nudgeTracker) {
351
+ try {
352
+ nudgeTracker.emit({
353
+ kind: 'perfil-usuario',
354
+ target: 'perfilador-usuario-swl',
355
+ message: mensaje.trim(),
356
+ source: 'hooks/actualizar-perfil-usuario.js',
357
+ data: { senalesAcumuladas: total },
358
+ });
359
+ } catch { /* silencioso */ }
360
+ }
361
+ }
362
+
363
+ escribirDirty(estado);
364
+ } catch {
365
+ // Nunca bloquear por error interno
366
+ }
367
+ }
368
+
369
+ module.exports = { ejecutar };
370
+
371
+ // Ejecución directa (compat: sigue invocable standalone para diagnóstico).
372
+ if (require.main === module) {
373
+ let inputRaw = '';
374
+ process.stdin.on('data', chunk => { inputRaw += chunk; });
375
+ process.stdin.on('end', () => ejecutar(inputRaw));
376
+ }