fix(hooks): reconcile diverged CLI-package helper copies (FIX 2/4/5)

Apply the analogous fixes to the package-distributed copies under
v3/@claude-flow/cli/.claude/helpers so `ruflo init` in new projects ships the
fixed behaviour:
- intelligence.cjs: cross-platform projectSlug (FIX 5) + clip() truncation
  transparency on memory-content (FIX 4).
- auto-memory-hook.mjs: scoped/debug-gated unhandledRejection (FIX 2) +
  SIGTERM/SIGINT backend flush (FIX 3).

FIX 1 was already applied to this copy's hook-handler.cjs in the previous
commit. context-persistence-hook.mjs has only one (root) copy.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
tjaiyenandClaude Fable 5 committed 2026-06-16 08:40:24 -07:00
1 parent cb1e93e8db
commit e7783f2f0e
2 files changed
+50 -6

No files matched your search

@@ -31,6 +31,27 @@ const log = (msg) => console.log(`${CYAN}[AutoMemory] ${msg}${RESET}`);
const success = (msg) => console.log(`${GREEN}[AutoMemory] ✓ ${msg}${RESET}`);
const dim = (msg) => console.log(` ${DIM}${msg}${RESET}`);
const DEBUG = !!(process.env.RUFLO_DEBUG || process.env.DEBUG);
// ── Graceful shutdown (FIX 3) ───────────────────────────────────────────────
// Track the backend in use so a SIGTERM/SIGINT mid-run can still flush it
// (the JSON backend persists; a SQLite-backed one closes/flushes WAL) instead
// of leaving a half-written store or a stale lock behind.
let activeBackend = null;
let shuttingDown = false;
function trackBackend(b) { activeBackend = b; return b; }
async function gracefulExit(signal) {
if (shuttingDown) return;
shuttingDown = true;
if (DEBUG) process.stderr.write(`[AutoMemory] received ${signal}, flushing backend before exit\n`);
try {
if (activeBackend && typeof activeBackend.shutdown === 'function') await activeBackend.shutdown();
} catch { /* best effort — never block exit on cleanup */ }
process.exit(0);
}
process.on('SIGTERM', () => { gracefulExit('SIGTERM'); });
process.on('SIGINT', () => { gracefulExit('SIGINT'); });
// Ensure data dir
if (!existsSync(DATA_DIR)) mkdirSync(DATA_DIR, { recursive: true });
@@ -219,7 +240,7 @@ async function doImport() {
}
const config = readConfig();
const backend = new JsonFileBackend(STORE_PATH);
const backend = trackBackend(new JsonFileBackend(STORE_PATH));
await backend.initialize();
const bridgeConfig = {
@@ -272,7 +293,7 @@ async function doSync() {
}
const config = readConfig();
const backend = new JsonFileBackend(STORE_PATH);
const backend = trackBackend(new JsonFileBackend(STORE_PATH));
await backend.initialize();
const entryCount = await backend.count();
@@ -348,8 +369,17 @@ async function doStatus() {
const command = process.argv[2] || 'status';
// Suppress unhandled rejection warnings from dynamic import() failures
process.on('unhandledRejection', () => {});
// Dynamic import() failures can surface as unhandled rejections on a later
// microtask even when the awaiting call site already caught them, which would
// otherwise force a non-zero exit. Swallow to keep hooks exit-0, but surface the
// reason under RUFLO_DEBUG/DEBUG so genuine async bugs aren't silently hidden
// (FIX 2 — the previous `() => {}` discarded every rejection process-wide).
process.on('unhandledRejection', (reason) => {
if (DEBUG) {
const detail = reason && reason.message ? reason.message : String(reason);
process.stderr.write(`[AutoMemory] unhandledRejection (suppressed): ${detail}\n`);
}
});
try {
switch (command) {
@@ -318,7 +318,11 @@ function bootstrapFromMemoryFiles() {
// For the projects dir, scope to CURRENT project only (not all 51+ dirs)
if (base.endsWith('projects')) {
try {
const projectSlug = cwd.replace(/^\//, '').replace(/\//g, '-');
// Match Claude Code's project-dir slug: every non-alphanumeric char -> '-'
// (e.g. "G:\\My Drive\\TJ_Vault" -> "G--My-Drive-TJ-Vault"). The old version
// only handled POSIX '/', so on Windows the slug kept ':' and '\\' and never
// matched the real <projects>/<slug>/memory dir — bootstrap found nothing (FIX 5).
const projectSlug = cwd.replace(/[^a-zA-Z0-9]/g, '-');
const memDir = path.join(base, projectSlug, 'memory');
if (fs.existsSync(memDir)) {
parseMemoryDir(memDir, entries);
@@ -332,6 +336,16 @@ function bootstrapFromMemoryFiles() {
return entries;
}
// Truncation transparency (FIX 4): mark the cut with an ellipsis and warn under
// debug, so later reasoning isn't silently built on severed text.
const CLIP_DEBUG = !!(process.env.RUFLO_DEBUG || process.env.DEBUG);
function clip(text, max, label) {
text = text == null ? '' : String(text);
if (text.length <= max) return text;
if (CLIP_DEBUG) process.stderr.write(`[INTELLIGENCE] WARN: truncated ${label || 'value'} from ${text.length} to ${max} chars\n`);
return text.slice(0, max - 1) + '…';
}
function parseMemoryDir(dir, entries) {
try {
const files = fs.readdirSync(dir).filter(f => f.endsWith('.md'));
@@ -353,7 +367,7 @@ function parseMemoryDir(dir, entries) {
entries.push({
id,
key: title.toLowerCase().replace(/[^a-z0-9]+/g, '-').slice(0, 50),
content: body.slice(0, 500),
content: clip(body, 500, 'memory content'),
summary: title,
namespace: file === 'MEMORY.md' ? 'core' : file.replace('.md', ''),
type: 'semantic',