Merge reviewed PR #3965: fix(skill-tracker): serialize concurrent append and retention

This commit is contained in:
haelyra committed 2026-11-19 15:14:54 -05:00
commit bd8affeb57
5 files changed
+185 -9

No files matched your search

+23 -8
View File
@@ -4,7 +4,8 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const { ensureDir } = require('../utils');
const { ensureDir, log } = require('../utils');
const { acquireSettingsLock } = require('../install/claude-settings-lock');
const VALID_OUTCOMES = new Set(['success', 'failure', 'partial']);
const VALID_FEEDBACK = new Set(['accepted', 'corrected', 'rejected']);
@@ -131,15 +132,29 @@ function appendRunRecord(runsFilePath, record, options = {}) {
: MAX_RUN_RECORDS;
ensureDir(path.dirname(runsFilePath));
fs.appendFileSync(runsFilePath, `${JSON.stringify(record)}\n`, { encoding: 'utf8', mode: RUNS_FILE_MODE });
// Append and retention must share one critical section. Otherwise a pruner
// can overwrite a run another process appended after its read snapshot.
const release = acquireSettingsLock(runsFilePath, {
label: 'skill run history', timeoutMs: options.lockTimeoutMs ?? 5000,
});
try {
fs.chmodSync(runsFilePath, RUNS_FILE_MODE);
} catch {
// Windows and some mounts do not support POSIX modes; the record still lands.
}
fs.appendFileSync(runsFilePath, `${JSON.stringify(record)}\n`, { encoding: 'utf8', mode: RUNS_FILE_MODE });
pruneRunRecords(runsFilePath, maxRecords);
try {
fs.chmodSync(runsFilePath, RUNS_FILE_MODE);
} catch {
// Windows and some mounts do not support POSIX modes; the record still lands.
}
pruneRunRecords(runsFilePath, maxRecords);
} finally {
try {
release();
} catch (error) {
// Preserve the append result or its original error if cleanup fails.
log(`[SkillTracker] Error releasing run history lock: ${error.message}`);
}
}
}
// Keep only the newest `maxRecords` lines. Rewrites the whole file, which is