mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 14:36:35 +09:00
The Stop hook is right to never block a turn, but it also never said anything when a write failed — those are two independent decisions that were conflated into one. track.mjs's persistToMemory() already computed a real, useful failure reason on a failed `memory store` call, but TRACK_QUIET=1 (always set by the Stop hook) made main() return before that reason was ever printed anywhere, discarding it unconditionally for every failure mode — WAL refusal, schema drift, permissions, a future backend change — identically and invisibly. Separately, ruflo-hook.cjs (the actual registered cross-platform Stop-hook shim) ran track.mjs with stdio:'ignore' and never inspected spawnSync's own result, so a crash/timeout/non-zero exit in track.mjs itself was equally invisible. Added _diagnostic-log.cjs: a shared, best-effort logFailure() that appends one JSON line (ts, source, reason, sessionId) to ~/.claude-flow/logs/cost-tracker-failures.log — a home-relative path consistent with the existing ~/.claude-flow convention used elsewhere (global-ai-budget.ts). Capped at 512KB, rotated to the most recent 200 lines rather than left to grow unboundedly. Every operation is wrapped in try/catch, including a call made with no argument or a null/undefined one (destructuring happens inside the try, not in the parameter list) — the module's whole reason for existing is to never throw, since it sits behind the single most load-bearing invariant in this issue: the Stop hook must still exit 0 no matter what, even if the diagnostic log write itself fails. track.mjs now logs a real persistToMemory() failure before the TRACK_QUIET-gated return that used to discard it; TRACK_DRY_RUN's intentional placeholder is never logged as a failure. ruflo-hook.cjs now inspects spawnSync's result and logs a spawn error, non-zero exit, or signal; still calls done() unconditionally regardless. 21 new tests cover: a real failure logging correctly even under TRACK_QUIET=1 (the actual bug condition, not just the easy unset case), dry-run never logging, ruflo-hook.cjs's own spawn-failure layer, the never-block invariant surviving an unwritable log directory, cap/rotation truncating correctly, and logFailure's null/undefined argument safety. Verified end-to-end against this machine's real local ruflo CLI in addition to the hermetic fixtures. Full plugin smoke suite (64 checks) passes with no regressions. Co-Authored-By: RuFlo <ruv@ruv.net>
50 lines
1.9 KiB
JavaScript
50 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
// _diagnostic-log.cjs — shared best-effort failure log for #3227.
|
|
//
|
|
// track.mjs's persistToMemory() and ruflo-hook.cjs's Stop-hook shim both run
|
|
// with TRACK_QUIET=1 / stdio:'ignore', so a write failure previously vanished
|
|
// with zero trace. This appends one JSON line per failure to a home-relative
|
|
// log so the failure is at least discoverable later. It is diagnostic only —
|
|
// never load-bearing — so every operation here is wrapped to never throw.
|
|
//
|
|
// .cjs (not .mjs) so both ruflo-hook.cjs (require) and track.mjs (import,
|
|
// via Node's CJS/ESM named-export interop) can share one implementation.
|
|
|
|
const { appendFileSync, readFileSync, writeFileSync, mkdirSync, statSync, existsSync } = require('fs');
|
|
const { join } = require('path');
|
|
const { homedir } = require('os');
|
|
|
|
const LOG_DIR = join(homedir(), '.claude-flow', 'logs');
|
|
const LOG_PATH = join(LOG_DIR, 'cost-tracker-failures.log');
|
|
const LOG_MAX_BYTES = 512 * 1024;
|
|
const LOG_KEEP_LINES = 200;
|
|
|
|
function rotateIfNeeded() {
|
|
if (!existsSync(LOG_PATH)) return;
|
|
if (statSync(LOG_PATH).size <= LOG_MAX_BYTES) return;
|
|
const lines = readFileSync(LOG_PATH, 'utf-8').split('\n').filter(Boolean);
|
|
const kept = lines.slice(-LOG_KEEP_LINES);
|
|
writeFileSync(LOG_PATH, kept.length ? kept.join('\n') + '\n' : '');
|
|
}
|
|
|
|
/** Append one failure line. Never throws, never blocks the caller. */
|
|
function logFailure(opts = {}) {
|
|
try {
|
|
const { source, reason, sessionId } = opts || {};
|
|
if (!existsSync(LOG_DIR)) mkdirSync(LOG_DIR, { recursive: true });
|
|
rotateIfNeeded();
|
|
const entry = {
|
|
ts: new Date().toISOString(),
|
|
source,
|
|
reason: String(reason ?? 'unknown').slice(0, 500),
|
|
};
|
|
if (sessionId) entry.sessionId = sessionId;
|
|
appendFileSync(LOG_PATH, JSON.stringify(entry) + '\n');
|
|
} catch {
|
|
// Logging itself must never escalate — swallow and move on.
|
|
}
|
|
}
|
|
|
|
module.exports = { logFailure, LOG_PATH };
|