From f0c4500632bb0dd85d162573e3d00c4563a35243 Mon Sep 17 00:00:00 2001 From: TheFlow Date: Sun, 26 Oct 2025 09:51:00 +1300 Subject: [PATCH] fix(session): use marker recovery_doc for reliable handoff selection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Problem**: session-init.js used alphabetical sorting to select handoff document, which worked by accident but was fragile and unreliable. **Solution**: Prefer explicit recovery_doc from compaction marker before falling back to alphabetical sort. **Architecture**: 1. session-closedown.js sets recovery_doc in marker file 2. session-init.js reads recovery_doc BEFORE deleting marker 3. Explicitly uses marker's recovery_doc if available 4. Falls back to alphabetical sort only when no marker exists **Verification**: - Tested with no marker (uses alphabetical fallback) ✅ - session-closedown.js sets recovery_doc at line 1021 ✅ - Non-interactive operation maintained ✅ **Strengthens**: inst_083 (handoff document auto-injection) **Resolves**: User concern about reliable handoff selection 🤖 Generated with Claude Code Co-Authored-By: Claude --- scripts/session-init.js | 45 ++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) 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('');