@saulwade/swl-ses 1.9.0 → 2.1.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.
- package/CLAUDE.md +196 -196
- package/README.md +579 -579
- package/agentes/_propose-step.md +90 -0
- package/agentes/accesibilidad-wcag-swl.md +3 -3
- package/agentes/auto-evolucion-swl.md +908 -908
- package/agentes/disenador-ui-swl.md +6 -5
- package/agentes/frontend-angular-swl.md +2 -2
- package/agentes/frontend-css-swl.md +2 -2
- package/agentes/frontend-react-swl.md +4 -4
- package/agentes/frontend-swl.md +6 -6
- package/agentes/implementador-swl.md +2 -0
- package/agentes/investigador-ux-swl.md +5 -5
- package/agentes/orquestador-swl.md +9 -7
- package/agentes/perfilador-usuario-swl.md +321 -308
- package/agentes/producto-prd-swl.md +1 -1
- package/agentes/red-team-swl.md +218 -218
- package/agentes/tdd-qa-swl.md +17 -1
- package/bin/swl-ses.js +1 -1
- package/comandos/swl/actualizar.md +1 -1
- package/comandos/swl/aprender.md +2 -2
- package/comandos/swl/aprobar-plan.md +153 -0
- package/comandos/swl/ayuda.md +3 -3
- package/comandos/swl/briefing.md +122 -0
- package/comandos/swl/compactar.md +29 -2
- package/comandos/swl/discutir-fase.md +23 -2
- package/comandos/swl/ejecutar-fase.md +59 -6
- package/comandos/swl/evolucionar.md +1 -1
- package/comandos/swl/inbox.md +1 -1
- package/comandos/swl/instalar.md +1 -1
- package/comandos/swl/nemesis.md +1 -1
- package/comandos/swl/planear-fase.md +19 -1
- package/comandos/swl/plugins.md +1 -1
- package/comandos/swl/release.md +47 -1
- package/comandos/swl/status.md +348 -0
- package/comandos/swl/verificar.md +27 -1
- package/habilidades/ai-runtime-security/SKILL.md +1 -1
- package/habilidades/auto-evolucion-protocolo/SKILL.md +276 -276
- package/habilidades/benchmark-memoria/SKILL.md +1 -1
- package/habilidades/calidad-contract-testing/SKILL.md +165 -0
- package/habilidades/changelog-generator/SKILL.md +9 -2
- package/habilidades/changelog-generator/scripts/parse-commits.js +13 -1
- package/habilidades/diagrama-arquitectura/SKILL.md +1 -1
- package/habilidades/drift-detection/SKILL.md +179 -179
- package/habilidades/ejecutar-fase/SKILL.md +541 -468
- package/habilidades/estructura-proyecto-claude/SKILL.md +17 -14
- package/habilidades/estructura-proyecto-claude/recursos/configuracion-y-extensiones.md +34 -23
- package/habilidades/estructura-proyecto-claude/recursos/frontmatter-y-hooks-referencia.md +70 -53
- package/habilidades/estructura-proyecto-claude/recursos/mcp-json-template.json +57 -77
- package/habilidades/extractor-de-aprendizajes/SKILL.md +9 -5
- package/habilidades/harness-claude-code/SKILL.md +10 -7
- package/{reglas/harness-claude-code.md → habilidades/harness-claude-code/recursos/disciplina-harness-regla.md} +2 -2
- package/habilidades/instalar-sistema/SKILL.md +3 -3
- package/habilidades/meta-skills-estandar/recursos/frameworks-seguridad.md +1 -1
- package/habilidades/perfil-usuario/SKILL.md +200 -200
- package/habilidades/planear-fase/SKILL.md +26 -4
- package/habilidades/proceso-ddia-fundamentos/SKILL.md +1 -1
- package/habilidades/proceso-ddia-streaming/SKILL.md +4 -4
- package/habilidades/proceso-debate-adversarial/SKILL.md +2 -2
- package/habilidades/protocolo-revision-swl/SKILL.md +1 -1
- package/habilidades/seguridad-skills-ia/SKILL.md +1 -1
- package/habilidades/swl-claudemd/SKILL.md +50 -210
- package/habilidades/swl-claudemd/recursos/contrato-aprender.md +83 -0
- package/habilidades/swl-claudemd/recursos/duplicacion-reglas-globales.md +85 -0
- package/habilidades/swl-claudemd/recursos/plantillas-init.md +94 -0
- package/habilidades/swl-dashboard/SKILL.md +9 -9
- package/habilidades/swl-revisar-impacto/SKILL.md +1 -1
- package/habilidades/tdd-workflow/SKILL.md +715 -673
- package/habilidades/validacion-ci-sistema/SKILL.md +20 -4
- package/hooks/calidad-pre-commit.js +344 -3
- package/hooks/check-update.js +39 -1
- package/hooks/ciclo-evolucion-subagente.js +26 -0
- package/hooks/ciclo-evolucion.js +26 -0
- package/hooks/extraccion-aprendizajes.js +13 -0
- package/hooks/lib/autonomia.js +208 -0
- package/hooks/lib/briefing.js +474 -0
- package/hooks/lib/ciclo-evolucion.js +47 -0
- package/hooks/{auto-evolucion.js → lib/etapa-auto-evolucion.js} +701 -700
- package/hooks/{metricas-evolucion.js → lib/etapa-metricas.js} +388 -376
- package/hooks/{actualizar-perfil-usuario.js → lib/etapa-perfil-usuario.js} +376 -364
- package/hooks/lib/evolution-tracker.js +24 -3
- package/hooks/lib/propose-step.js +357 -0
- package/hooks/session-briefing.js +98 -0
- package/hooks/spec-gate.js +211 -0
- package/hooks/tdd-gate.js +241 -0
- package/hooks/telemetria-skill-routing.js +100 -0
- package/hooks/validar-intent-spec.js +30 -10
- package/instintos/autonomia.yaml +27 -0
- package/llms.txt +6 -6
- package/manifiestos/hooks-config.json +44 -17
- package/manifiestos/modulos.json +40 -15
- package/manifiestos/skills-lock.json +64 -57
- package/package.json +93 -93
- package/plugin.json +371 -375
- package/reglas/accesibilidad.md +10 -0
- package/reglas/analizar-directorios-antes-de-escribir.md +228 -0
- package/reglas/api-diseno.md +9 -0
- package/reglas/auditorias-documentales-estructurales.md +7 -0
- package/reglas/cloud-infra.md +8 -0
- package/reglas/consultar-vault-primero.md +195 -0
- package/reglas/debatir-antes-de-aceptar.md +158 -0
- package/reglas/fragmentos-compartidos.md +5 -0
- package/reglas/git-coauthor.md +100 -0
- package/reglas/gobernanza.md +4 -4
- package/reglas/hooks.md +6 -0
- package/reglas/intent-engineering.md +4 -0
- package/reglas/markitdown.md +8 -0
- package/reglas/memoria-consolidada.md +1 -1
- package/reglas/monitor-ci.md +309 -0
- package/reglas/patrones.md +6 -0
- package/reglas/registro-componentes-nuevos.md +39 -2
- package/reglas/seguridad-agentes.md +1 -1
- package/reglas/sesiones-paralelas.md +180 -0
- package/reglas/skills-estandar.md +6 -0
- package/reglas/testing.md +7 -0
- package/reglas/tests-cleanup.md +4 -0
- package/reglas/usar-code-review-graph.md +155 -0
- package/reglas/usar-sistema-swl.md +1 -1
- package/reglas/verificar-citas-normativas.md +548 -0
- package/scripts/instalador.js +52 -6
- package/scripts/lib/ci-reader.js +193 -0
- package/scripts/lib/detectar-host-swl.js +175 -0
- package/scripts/lib/evidencia-release.js +322 -0
- package/scripts/lib/gate-hooks-requires.js +249 -0
- package/scripts/lib/gate-licencias.js +212 -0
- package/scripts/lib/git-metricas.js +257 -0
- package/scripts/lib/gitignore-manifest.js +29 -1
- package/scripts/lib/metricas-dora.js +204 -0
- package/scripts/lib/plan-lock.js +275 -0
- package/scripts/migrar-fase-dominio.js +0 -1
- package/scripts/tui/ejecutores.js +1 -1
- package/scripts/validar-manifest.js +92 -1
- package/scripts/verificar-evolucion.js +54 -4
- package/scripts/verificar-release.js +102 -0
- package/scripts/verificar-trazabilidad.js +298 -0
- package/agentes/ux-disenador-swl.md +0 -503
- package/comandos/swl/dashboard.md +0 -146
- package/comandos/swl/evolucion-estado.md +0 -191
- package/comandos/swl/metricas.md +0 -376
- package/comandos/swl/salud.md +0 -481
- package/reglas/arquitectura.evolved.json +0 -7
- package/reglas/seguridad.evolved.json +0 -7
- 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('./
|
|
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('./
|
|
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('./
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
? 0
|
|
284
|
-
:
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
...(
|
|
296
|
-
? [{ tipo: '
|
|
297
|
-
: []),
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
''
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
`
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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
|
+
}
|