hookStop[*]informa
hilo-checkpoint
Actualizar Hilo y verificar cambios sin commit
Qué comprueba
- Escribe ESTADO_PROYECTO.json SIEMPRE via updateHiloJson (FB-004 + ADR-068: atomico y bajo candado, UTF-8 sin BOM, serializador unico). - Git read-only con GIT_OPTIONAL_LOCKS=0 (v1.0.1: evitar colisiones index.lock con IDE).
Códigos de salida
| Exit | Significado |
|---|---|
0 | siempre (no bloquea cierre) |
Política
Informa: nunca bloquea.
| Clave | Tipo | Default | Qué hace |
|---|---|---|---|
hooks.policy | string | "warn" | WARN-first por defecto (avisan sin bloquear); 'block' eleva todos los hooks defensivos a bloqueo. Los hooks intrinsecamente BLOCK (bash-guard, protect-files certs/keys, hilo-json-guard) no se degradan nunca. |
hooks.blockList | array | [] | Hooks concretos elevados a BLOCK aunque policy=warn (ej. ['secret-scanner']). |
Código fuente
hilo-checkpoint.js · 112 líneas
#!/usr/bin/env node
// hilo-checkpoint.js - Actualizar Hilo y verificar cambios sin commit
// Evento: Stop
// Exit 0 = siempre (no bloquea cierre)
//
// - Escribe ESTADO_PROYECTO.json SIEMPRE via updateHiloJson (FB-004 + ADR-068: atomico y bajo candado,
// UTF-8 sin BOM, serializador unico).
// - Git read-only con GIT_OPTIONAL_LOCKS=0 (v1.0.1: evitar colisiones index.lock con IDE).
'use strict';
const fs = require('fs');
const path = require('path');
const h = require(path.join(__dirname, 'lib', 'helpers'));
function pad2(n) {
return String(n).padStart(2, '0');
}
function main() {
const projectDir = h.projectDir();
const duranDir = path.join(projectDir, '_hilo');
if (!fs.existsSync(duranDir)) return 0;
// === 1. Verificar cambios sin commit (read-only, sin lock) ===
let uncommitted = [];
const st = h.gitReadOnly(['status', '--porcelain'], projectDir);
if (st.exitCode === 0 && st.stdOut && st.stdOut.trim()) {
uncommitted = st.stdOut
.split(/\r?\n/)
.filter((l) => l.trim())
.map((l) => l.substring(3).trim());
}
if (uncommitted.length > 0) {
console.log('');
console.log(h.txt(`AVISO: ${uncommitted.length} archivo(s) sin commit:`,
`WARNING: ${uncommitted.length} uncommitted file(s):`));
for (const f of uncommitted.slice(0, 10)) console.log(` - ${f}`);
if (uncommitted.length > 10) {
console.log(h.txt(` ... y ${uncommitted.length - 10} mas`,
` ... and ${uncommitted.length - 10} more`));
}
console.log(h.txt(`Considera ejecutar ${h.cmd('commit')} antes de cerrar.`,
`Consider running ${h.cmd('commit')} before closing.`));
console.log('');
}
// === 2. Actualizar timestamp en ESTADO_PROYECTO.json ===
const estadoFile = path.join(duranDir, 'ESTADO_PROYECTO.json');
if (fs.existsSync(estadoFile)) {
try {
const now = new Date();
const ahora = `${now.getFullYear()}-${pad2(now.getMonth() + 1)}-${pad2(now.getDate())}`;
let usuario = null;
const gu = h.gitReadOnly(['config', 'user.name'], projectDir);
if (gu.exitCode === 0 && gu.stdOut && gu.stdOut.trim()) usuario = gu.stdOut.trim();
if (!usuario) usuario = process.env.USERNAME || process.env.USER || null;
// LEER-MODIFICAR-ESCRIBIR BAJO CANDADO (ADR-068). Este hook y el de `session-checkpoint`
// corren los dos en `Stop`: dos sesiones que terminan a la vez leian el mismo estado y la
// segunda publicaba encima de la primera. Medido: se perdia el 67 % de las actualizaciones.
h.updateHiloJson(estadoFile, (estado) => {
if (!estado) return null; // nada que actualizar: destino intacto
// Actualizar ultimaSesion
if (estado.ultimaSesion) {
estado.ultimaSesion.fecha = ahora;
estado.ultimaSesion.usuario = usuario;
if (uncommitted.length > 0) {
estado.ultimaSesion.archivosModificados = uncommitted.slice(0, 20);
}
}
// Actualizar estado.ultima_actualizacion
if (estado.estado) {
estado.estado.ultima_actualizacion = ahora;
estado.estado.actualizado_por = usuario;
}
return estado;
});
} catch {
// Silencioso - no bloquear cierre por error de escritura
}
}
// === 3. Verificar si hay alertas pendientes ===
if (fs.existsSync(estadoFile)) {
const estado = h.readJsonSafe(estadoFile);
if (
estado &&
estado.alertas &&
Array.isArray(estado.alertas.activas) &&
estado.alertas.activas.length > 0
) {
console.log(h.txt(`RECORDATORIO: ${estado.alertas.activas.length} alerta(s) activa(s) en el proyecto.`,
`REMINDER: ${estado.alertas.activas.length} active alert(s) in the project.`));
}
}
return 0;
}
try {
process.exit(main());
} catch (e) {
console.error(h.txt(`[hilo-checkpoint] error no fatal: ${e.message}`,
`[hilo-checkpoint] non-fatal error: ${e.message}`));
process.exit(0);
}
Historial
6 cambios · 2026-09-07 → 2026-09-22. Último cambio: fix(higiene): lo que nadie mira no esta bien (ADR-076).