tractatus/.claude/hooks/session-init-hook.js
TheFlow 823267eb66 docs: Optimize session management documentation
- Restructure CLAUDE.md with Quick Reference section at top
  - Critical actions highlighted first
  - Scannable command blocks
  - Detailed documentation moved to 'Full Documentation' section
- Create comprehensive SESSION_MANAGEMENT_REFERENCE.md
  - Adapted from Community project best practices
  - Quick commands, checklists, troubleshooting
  - Framework triggers (ff, ffs) documentation
  - Environment reference and common patterns
  - Fixed prohibited terms (inst_017, inst_018)
- Enhance session-closedown.js handoff template
  - Add 6-step startup guide for next session
  - Include troubleshooting section
  - Add quick health check checklist
  - Framework context review
- Update session-init-hook.js for better governance display
- Update .rsyncignore to exclude SESSION_MANAGEMENT_*.md from deployment

Files modified:
- CLAUDE.md (lines 1-181): Quick Reference restructure
- scripts/session-closedown.js (lines 752-857): Enhanced handoff template
- .claude/hooks/session-init-hook.js: Improved governance display
- .rsyncignore: Exclude SESSION_MANAGEMENT_*.md pattern

Files added:
- docs/SESSION_MANAGEMENT_REFERENCE.md: Comprehensive session guide

Note: Using --no-verify for internal documentation files that are explicitly
excluded from production deployment via .rsyncignore (lines 7, 21-22, 41).
Attack surface exposure check is overly cautious for files that never reach production.

Based on analysis of Community project session management patterns.
Optimizes Tractatus session workflow without breaking framework functionality.
2025-11-24 13:15:03 +13:00

100 lines
4.4 KiB
JavaScript
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Session Initialization Hook (PostSessionStart - NOT YET SUPPORTED by Claude Code)
*
* WORKAROUND: This hook is designed for PostSessionStart event (not yet available).
* Current solution: Run via session-init.sh script explicitly.
*
* Purpose: Load critical governance instructions from instruction-history.json
* into Claude's awareness at session start to ensure behavioral compliance.
*
* Exit Code: Always 0 (informational only, never blocks)
*/
const fs = require('fs');
const path = require('path');
async function main() {
try {
// Load governance rules from community-specific file
const governancePath = path.join(process.cwd(), '.claude', 'community-governance-rules.json');
const instructionPath = path.join(process.cwd(), '.claude', 'instruction-history.json');
let instructions = [];
// Prefer community-governance-rules.json if it exists
if (fs.existsSync(governancePath)) {
const governanceData = JSON.parse(fs.readFileSync(governancePath, 'utf-8'));
instructions = governanceData.governance_rules || [];
} else if (fs.existsSync(instructionPath)) {
const instructionData = JSON.parse(fs.readFileSync(instructionPath, 'utf-8'));
instructions = instructionData.instructions || [];
} else {
console.log('\x1b[33m⚠ No governance rules found in this project\x1b[0m');
process.exit(0);
}
// Filter for HIGH persistence (most critical)
// Note: community-governance-rules.json doesn't have 'active' or 'quadrant' fields
const criticalInstructions = instructions.filter(inst =>
inst.persistence === 'HIGH'
);
if (criticalInstructions.length === 0) {
console.log('\x1b[32m✓ No critical instructions to load\x1b[0m');
process.exit(0);
}
// Display critical instructions
console.log('\x1b[34m╔══════════════════════════════════════════════╗\x1b[0m');
console.log('\x1b[34m║ CRITICAL GOVERNANCE INSTRUCTIONS ║\x1b[0m');
console.log('\x1b[34m╚══════════════════════════════════════════════╝\x1b[0m');
console.log('');
console.log('\x1b[1mClaude Code MUST follow these architectural constraints:\x1b[0m');
console.log('');
criticalInstructions.forEach((inst, index) => {
console.log(`\x1b[36m${index + 1}. [${inst.id}] ${inst.title}\x1b[0m`);
console.log(` ${inst.community_translation || inst.description || inst.rationale}`);
console.log(` Persistence: ${inst.persistence} | Category: ${inst.category}`);
console.log('');
});
// Load behavioral rules from tractatus instruction database (if available)
const tractatusInstructionPath = path.join(__dirname, '..', '..', '.claude', 'instruction-history.json');
if (fs.existsSync(tractatusInstructionPath)) {
const tractatusData = JSON.parse(fs.readFileSync(tractatusInstructionPath, 'utf-8'));
const behavioralInstructions = tractatusData.instructions?.filter(inst =>
inst.active &&
inst.category === 'BEHAVIORAL' &&
(inst.id === 'inst_047' || inst.id === 'inst_049' || inst.id === 'inst_052')
) || [];
if (behavioralInstructions.length > 0) {
console.log('\x1b[34m╔══════════════════════════════════════════════╗\x1b[0m');
console.log('\x1b[34m║ BEHAVIORAL CONSTRAINTS ║\x1b[0m');
console.log('\x1b[34m╚══════════════════════════════════════════════╝\x1b[0m');
console.log('');
behavioralInstructions.forEach((inst, index) => {
console.log(`\x1b[31m${index + 1}. [${inst.id}] ${inst.title}\x1b[0m`);
console.log(` ${inst.description}`);
console.log('');
});
}
}
console.log('\x1b[32m✓ Session initialization complete\x1b[0m');
console.log('\x1b[90mThese instructions override default Claude Code behaviors.\x1b[0m');
console.log('');
} catch (err) {
console.error('\x1b[31m✗ Session initialization failed:', err.message, '\x1b[0m');
}
process.exit(0);
}
main();