fix(session): use marker recovery_doc for reliable handoff selection

**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 <noreply@anthropic.com>
This commit is contained in:
TheFlow 2025-10-26 09:51:00 +13:00
parent 8ca511e2b5
commit f0c4500632

View file

@ -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('');