@vibe-validate/history
Version:
Validation history tracking via git notes for vibe-validate
68 lines • 2.67 kB
JavaScript
/**
* History health check utilities
*/
import { DEFAULT_HISTORY_CONFIG } from './types.js';
import { getAllHistoryNotes } from './reader.js';
/**
* Check validation history health
*
* @param config - History configuration
* @returns Health check result
*/
export async function checkHistoryHealth(config = {}) {
const mergedConfig = {
...DEFAULT_HISTORY_CONFIG,
...config,
retention: {
...DEFAULT_HISTORY_CONFIG.retention,
...config.retention,
},
};
// Type assertion safe: DEFAULT_HISTORY_CONFIG is Required<HistoryConfig>
const warnAfterDays = (mergedConfig.retention.warnAfterDays ?? DEFAULT_HISTORY_CONFIG.retention.warnAfterDays);
const warnAfterCount = (mergedConfig.retention.warnAfterCount ?? DEFAULT_HISTORY_CONFIG.retention.warnAfterCount);
const allNotes = await getAllHistoryNotes(mergedConfig.gitNotes.ref);
const totalNotes = allNotes.length;
const cutoffTime = Date.now() - warnAfterDays * 24 * 60 * 60 * 1000;
let oldNotesCount = 0;
for (const note of allNotes) {
if (note.runs.length === 0) {
continue;
}
// Check oldest run
const oldestRun = note.runs[0];
const oldestTimestamp = new Date(oldestRun.timestamp).getTime();
if (oldestTimestamp < cutoffTime) {
oldNotesCount++;
}
}
// Determine if we should warn
const shouldWarnCount = totalNotes > warnAfterCount;
const shouldWarnAge = oldNotesCount > 0;
const shouldWarn = shouldWarnCount || shouldWarnAge;
let warningMessage;
if (shouldWarnCount && shouldWarnAge) {
warningMessage =
`ℹ️ Validation history has grown large (${totalNotes} tree hashes)\n` +
` Found ${oldNotesCount} notes older than ${warnAfterDays} days\n` +
` Consider pruning: vibe-validate history prune --older-than "${warnAfterDays} days"`;
}
else if (shouldWarnCount) {
warningMessage =
`ℹ️ Validation history has grown large (${totalNotes} tree hashes)\n` +
` Consider pruning: vibe-validate history prune --older-than "${warnAfterDays} days"`;
}
else if (shouldWarnAge) {
warningMessage =
`ℹ️ Found validation history older than ${warnAfterDays} days\n` +
` ${oldNotesCount} tree hashes can be pruned\n` +
` Run: vibe-validate history prune --older-than "${warnAfterDays} days"`;
}
return {
totalNotes,
oldNotesCount,
shouldWarn,
warningMessage,
};
}
//# sourceMappingURL=health-check.js.map