diff --git a/scripts/session-init.js b/scripts/session-init.js index 2412ca1b..035f9561 100755 --- a/scripts/session-init.js +++ b/scripts/session-init.js @@ -286,9 +286,16 @@ async function main() { // Check for post-compaction restart marker const markerPath = path.join(__dirname, '../.claude/session-complete.marker'); + let explicitRecoveryDoc = null; // Store recovery_doc before deleting marker (inst_083) + if (fs.existsSync(markerPath)) { try { const marker = JSON.parse(fs.readFileSync(markerPath, 'utf8')); + + // Store recovery_doc BEFORE deleting marker (inst_083: Ensure correct handoff) + if (marker.recovery_doc) { + explicitRecoveryDoc = marker.recovery_doc; + } console.log(''); log('═'.repeat(70), 'yellow'); warning('⚠️ PREVIOUS SESSION ENDED WITH CLOSEDOWN'); @@ -329,14 +336,38 @@ async function main() { // Check for handoff documents (inst_083: Auto-inject handoff context) section('1a. Previous Session Handoff Detection'); try { - const handoffFiles = fs.readdirSync(path.join(__dirname, '..')) - .filter(f => f.startsWith('SESSION_CLOSEDOWN_') && f.endsWith('.md')) - .sort() - .reverse(); + // Prefer explicit recovery_doc from marker, fall back to alphabetical sort + let latestHandoff = null; + let handoffPath = null; - if (handoffFiles.length > 0) { - const latestHandoff = handoffFiles[0]; - const handoffPath = path.join(__dirname, '..', latestHandoff); + if (explicitRecoveryDoc) { + // Use explicit recovery doc from compaction marker (inst_083: Reliable handoff) + const explicitPath = path.join(__dirname, '..', explicitRecoveryDoc); + if (fs.existsSync(explicitPath)) { + latestHandoff = explicitRecoveryDoc; + handoffPath = explicitPath; + log(` Using explicit recovery doc from marker: ${explicitRecoveryDoc}`, 'cyan'); + } else { + warning(` Marker specified ${explicitRecoveryDoc} but file not found`); + warning(` Falling back to alphabetical sort`); + } + } + + // Fall back to alphabetical sort if no explicit recovery doc + if (!latestHandoff) { + const handoffFiles = fs.readdirSync(path.join(__dirname, '..')) + .filter(f => f.startsWith('SESSION_CLOSEDOWN_') && f.endsWith('.md')) + .sort() + .reverse(); + + if (handoffFiles.length > 0) { + latestHandoff = handoffFiles[0]; + handoffPath = path.join(__dirname, '..', latestHandoff); + log(` Using alphabetical fallback: ${latestHandoff}`, 'cyan'); + } + } + + if (latestHandoff && handoffPath) { const handoffContent = fs.readFileSync(handoffPath, 'utf8'); console.log('');