Skip to content

Commit 5c8caec

Browse files
committed
fix(session-start): support ECC_SESSION_RETENTION_DAYS opt-out + document env var
The retention pass for *-session.tmp files (issue #2151) landed previously, but the env var that controls it was undocumented in the README and rejected falsy values (0, off, disabled), silently falling back to the 30-day default. Users who want to keep all sessions for forensic or research workflows had no way to opt out. This patch: - Extends getSessionRetentionDays() so 0|off|false|disabled|never|none disables pruning entirely (returns null sentinel; default behavior unchanged). - Updates the call site in main() to skip pruneExpiredSessions when retention is null and emits a clear "[SessionStart] Pruning disabled via ECC_SESSION_RETENTION_DAYS" log line so the operator can tell pruning is off. - Documents ECC_SESSION_RETENTION_DAYS in the README "Hook Runtime Controls" section alongside the other ECC_SESSION_* knobs. - Adds three regression tests in tests/hooks/hooks.test.js covering opt-out via 0, opt-out via off, and garbage-value fallback to default 30. Verification: - node tests/hooks/hooks.test.js — 240/240 green (incl. 3 new retention tests) - node tests/run-all.js — 2622/2622 green - npx eslint scripts/hooks/session-start.js tests/hooks/hooks.test.js — clean - node scripts/ci/validate-no-personal-paths.js — clean - node scripts/ci/check-unicode-safety.js — clean - node scripts/ci/validate-hooks.js — 28 matchers validated - node scripts/ci/validate-rules.js — 115 files validated Fixes #2151
1 parent 7113b5b commit 5c8caec

3 files changed

Lines changed: 120 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ export ECC_SESSION_START_MAX_CHARS=4000
479479
# Disable SessionStart additional context entirely for low-context/local-model setups
480480
export ECC_SESSION_START_CONTEXT=off
481481

482+
# Session-tmp retention window in days (default: 30). Set to 0/off/disabled to keep all sessions.
483+
export ECC_SESSION_RETENTION_DAYS=14
484+
482485
# Keep context/scope/loop warnings but suppress API-rate cost estimates
483486
export ECC_CONTEXT_MONITOR_COST_WARNINGS=off
484487
```

‎scripts/hooks/session-start.js‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,22 @@ function dedupeRecentSessions(searchDirs) {
8383
.sort((left, right) => right.mtime - left.mtime || left.dirIndex - right.dirIndex);
8484
}
8585

86+
/**
87+
* Resolve session retention days from the ECC_SESSION_RETENTION_DAYS env var.
88+
*
89+
* @returns {number|null} The retention window in days, or `null` when the
90+
* user has explicitly opted out of pruning. Falsy/garbage values fall back
91+
* to {@link DEFAULT_SESSION_RETENTION_DAYS}.
92+
*/
8693
function getSessionRetentionDays() {
8794
const raw = process.env.ECC_SESSION_RETENTION_DAYS;
8895
if (!raw) return DEFAULT_SESSION_RETENTION_DAYS;
96+
97+
const normalized = String(raw).trim().toLowerCase();
98+
if (['0', 'off', 'false', 'disabled', 'never', 'none'].includes(normalized)) {
99+
return null;
100+
}
101+
89102
const parsed = Number.parseInt(raw, 10);
90103
return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_SESSION_RETENTION_DAYS;
91104
}
@@ -526,9 +539,13 @@ async function main() {
526539
ensureDir(learnedDir);
527540

528541
const retentionDays = getSessionRetentionDays();
529-
const prunedSessions = pruneExpiredSessions(sessionSearchDirs, retentionDays);
530-
if (prunedSessions > 0) {
531-
log(`[SessionStart] Pruned ${prunedSessions} expired session(s) older than ${retentionDays} day(s)`);
542+
if (retentionDays === null) {
543+
log('[SessionStart] Pruning disabled via ECC_SESSION_RETENTION_DAYS');
544+
} else {
545+
const prunedSessions = pruneExpiredSessions(sessionSearchDirs, retentionDays);
546+
if (prunedSessions > 0) {
547+
log(`[SessionStart] Pruned ${prunedSessions} expired session(s) older than ${retentionDays} day(s)`);
548+
}
532549
}
533550

534551
const observerSessionId = resolveSessionId();

‎tests/hooks/hooks.test.js‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5006,6 +5006,103 @@ async function runTests() {
50065006
passed++;
50075007
else failed++;
50085008

5009+
if (
5010+
await asyncTest('disables pruning when ECC_SESSION_RETENTION_DAYS=0', async () => {
5011+
const isoHome = path.join(os.tmpdir(), `ecc-start-prune-off-${Date.now()}`);
5012+
const sessionsDir = getCanonicalSessionsDir(isoHome);
5013+
fs.mkdirSync(sessionsDir, { recursive: true });
5014+
fs.mkdirSync(path.join(isoHome, '.claude', 'skills', 'learned'), { recursive: true });
5015+
5016+
const expiredFile = path.join(sessionsDir, '2026-01-01-keepme-session.tmp');
5017+
fs.writeFileSync(expiredFile, '# Old Session\n\nSHOULD STILL EXIST');
5018+
const ninetyDaysAgo = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
5019+
fs.utimesSync(expiredFile, ninetyDaysAgo, ninetyDaysAgo);
5020+
5021+
try {
5022+
const result = await runScript(path.join(scriptsDir, 'session-start.js'), '', {
5023+
HOME: isoHome,
5024+
USERPROFILE: isoHome,
5025+
ECC_SESSION_RETENTION_DAYS: '0',
5026+
});
5027+
5028+
assert.strictEqual(result.code, 0);
5029+
assert.ok(fs.existsSync(expiredFile), 'Should keep all sessions when retention is opt-out=0');
5030+
assert.ok(result.stderr.includes('Pruning disabled via ECC_SESSION_RETENTION_DAYS'),
5031+
`Should log pruning disabled, stderr: ${result.stderr}`);
5032+
assert.ok(!result.stderr.includes('Pruned'), `Should not log any pruning, stderr: ${result.stderr}`);
5033+
} finally {
5034+
fs.rmSync(isoHome, { recursive: true, force: true });
5035+
}
5036+
})
5037+
)
5038+
passed++;
5039+
else failed++;
5040+
5041+
if (
5042+
await asyncTest('disables pruning when ECC_SESSION_RETENTION_DAYS=off', async () => {
5043+
const isoHome = path.join(os.tmpdir(), `ecc-start-prune-offstr-${Date.now()}`);
5044+
const sessionsDir = getCanonicalSessionsDir(isoHome);
5045+
fs.mkdirSync(sessionsDir, { recursive: true });
5046+
fs.mkdirSync(path.join(isoHome, '.claude', 'skills', 'learned'), { recursive: true });
5047+
5048+
const expiredFile = path.join(sessionsDir, '2025-12-15-keepme-session.tmp');
5049+
fs.writeFileSync(expiredFile, '# Forensic Session\n\nKEEP ME');
5050+
const sixtyDaysAgo = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000);
5051+
fs.utimesSync(expiredFile, sixtyDaysAgo, sixtyDaysAgo);
5052+
5053+
try {
5054+
const result = await runScript(path.join(scriptsDir, 'session-start.js'), '', {
5055+
HOME: isoHome,
5056+
USERPROFILE: isoHome,
5057+
ECC_SESSION_RETENTION_DAYS: 'off',
5058+
});
5059+
5060+
assert.strictEqual(result.code, 0);
5061+
assert.ok(fs.existsSync(expiredFile), 'Should keep all sessions when retention is opt-out=off');
5062+
assert.ok(result.stderr.includes('Pruning disabled via ECC_SESSION_RETENTION_DAYS'),
5063+
`Should log pruning disabled, stderr: ${result.stderr}`);
5064+
} finally {
5065+
fs.rmSync(isoHome, { recursive: true, force: true });
5066+
}
5067+
})
5068+
)
5069+
passed++;
5070+
else failed++;
5071+
5072+
if (
5073+
await asyncTest('falls back to default retention when ECC_SESSION_RETENTION_DAYS is garbage', async () => {
5074+
const isoHome = path.join(os.tmpdir(), `ecc-start-prune-garbage-${Date.now()}`);
5075+
const sessionsDir = getCanonicalSessionsDir(isoHome);
5076+
fs.mkdirSync(sessionsDir, { recursive: true });
5077+
fs.mkdirSync(path.join(isoHome, '.claude', 'skills', 'learned'), { recursive: true });
5078+
5079+
const expiredFile = path.join(sessionsDir, '2026-01-01-pruneme-session.tmp');
5080+
fs.writeFileSync(expiredFile, '# Old Session\n\nDELETE ME');
5081+
const fortyDaysAgo = new Date(Date.now() - 40 * 24 * 60 * 60 * 1000);
5082+
fs.utimesSync(expiredFile, fortyDaysAgo, fortyDaysAgo);
5083+
5084+
try {
5085+
const result = await runScript(path.join(scriptsDir, 'session-start.js'), '', {
5086+
HOME: isoHome,
5087+
USERPROFILE: isoHome,
5088+
ECC_SESSION_RETENTION_DAYS: 'bogus-value',
5089+
});
5090+
5091+
assert.strictEqual(result.code, 0);
5092+
assert.ok(!fs.existsSync(expiredFile),
5093+
'Should fall back to default 30-day retention and prune the 40-day-old file');
5094+
assert.ok(result.stderr.includes('Pruned 1 expired session'),
5095+
`Should log pruning at default retention, stderr: ${result.stderr}`);
5096+
assert.ok(!result.stderr.includes('Pruning disabled'),
5097+
'Should NOT treat garbage as opt-out');
5098+
} finally {
5099+
fs.rmSync(isoHome, { recursive: true, force: true });
5100+
}
5101+
})
5102+
)
5103+
passed++;
5104+
else failed++;
5105+
50095106
console.log('\nRound 55: session-start.js (newest session selection):');
50105107

50115108
if (

0 commit comments

Comments
 (0)