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>
76 lines
2.3 KiB
JavaScript
Executable File
76 lines
2.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
/**
|
|
* ruflo-hook.cjs — cross-platform Windows shim for cost-tracker's Stop hook (#2132)
|
|
*
|
|
* The cost-tracker hooks.json declares `"_platform": "posix"` because its
|
|
* Stop hook uses bash:
|
|
*
|
|
* /bin/bash -c 'TRACK_QUIET=1 node "${CLAUDE_PLUGIN_ROOT}/scripts/track.mjs" >/dev/null 2>&1 || true'
|
|
*
|
|
* On Windows, `ruflo init` writes a `.claude/settings.json` that overrides
|
|
* the Stop hook to invoke this shim instead — `node ruflo-hook.cjs` —
|
|
* so cost tracking continues to capture session data without bash.
|
|
*
|
|
* Behaviour mirrors the bash hook:
|
|
* 1. Reads hook event payload from stdin (best effort — discarded).
|
|
* 2. Spawns track.mjs with TRACK_QUIET=1 and swallows all output.
|
|
* 3. Always exits 0 — telemetry is best-effort, must NEVER block a turn.
|
|
* 4. Times out at 30s so a hung track.mjs can't stall session shutdown.
|
|
* 5. If the spawn itself errors, times out, or exits non-zero, that
|
|
* failure is appended to the shared diagnostic log (_diagnostic-log.cjs,
|
|
* #3227) — still without ever changing the always-exit-0 contract.
|
|
*
|
|
* Usage: node ruflo-hook.cjs [hook-args ignored]
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const { spawnSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
function done() {
|
|
process.exit(0);
|
|
}
|
|
|
|
// Best-effort drain of stdin so the pipe doesn't EPIPE on the parent
|
|
try {
|
|
fs.readFileSync(0);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
|
|
// CLAUDE_PLUGIN_ROOT is set by Claude Code; fall back to this script's parent
|
|
const pluginRoot = process.env.CLAUDE_PLUGIN_ROOT
|
|
|| path.resolve(__dirname, '..');
|
|
const trackScript = path.join(pluginRoot, 'scripts', 'track.mjs');
|
|
|
|
if (!fs.existsSync(trackScript)) {
|
|
// Plugin layout drifted — exit clean, never block the turn
|
|
done();
|
|
}
|
|
|
|
const result = spawnSync(process.execPath, [trackScript], {
|
|
env: { ...process.env, TRACK_QUIET: '1' },
|
|
stdio: 'ignore',
|
|
timeout: 30_000,
|
|
});
|
|
|
|
if (result.error || result.status !== 0 || result.signal) {
|
|
try {
|
|
const { logFailure } = require('./_diagnostic-log.cjs');
|
|
logFailure({
|
|
source: 'ruflo-hook.cjs',
|
|
reason: result.error
|
|
? result.error.message
|
|
: result.signal
|
|
? `signal ${result.signal}`
|
|
: `exit ${result.status}`,
|
|
});
|
|
} catch {
|
|
// Diagnostic logging must never block the Stop hook — swallow and continue.
|
|
}
|
|
}
|
|
|
|
done();
|