@tekmidian/pai 0.8.5 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +121 -0
- package/FEATURE.md +5 -0
- package/README.md +54 -0
- package/dist/cli/index.mjs +5 -5
- package/dist/daemon/index.mjs +3 -3
- package/dist/{daemon-BaYX-w_d.mjs → daemon-VIFoKc_z.mjs} +25 -4
- package/dist/daemon-VIFoKc_z.mjs.map +1 -0
- package/dist/daemon-mcp/index.mjs +51 -0
- package/dist/daemon-mcp/index.mjs.map +1 -1
- package/dist/{factory-BzWfxsvK.mjs → factory-e0k1HWuc.mjs} +2 -2
- package/dist/{factory-BzWfxsvK.mjs.map → factory-e0k1HWuc.mjs.map} +1 -1
- package/dist/hooks/load-project-context.mjs +276 -89
- package/dist/hooks/load-project-context.mjs.map +4 -4
- package/dist/hooks/stop-hook.mjs +152 -2
- package/dist/hooks/stop-hook.mjs.map +3 -3
- package/dist/{postgres-DbUXNuy_.mjs → postgres-DvEPooLO.mjs} +22 -1
- package/dist/{postgres-DbUXNuy_.mjs.map → postgres-DvEPooLO.mjs.map} +1 -1
- package/dist/{tools-BXSwlzeH.mjs → tools-C4SBZHga.mjs} +797 -4
- package/dist/tools-C4SBZHga.mjs.map +1 -0
- package/package.json +1 -1
- package/src/hooks/ts/session-start/load-project-context.ts +36 -0
- package/src/hooks/ts/stop/stop-hook.ts +203 -1
- package/dist/daemon-BaYX-w_d.mjs.map +0 -1
- package/dist/tools-BXSwlzeH.mjs.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/hooks/ts/session-start/load-project-context.ts", "../../src/hooks/ts/lib/project-utils/paths.ts", "../../src/hooks/ts/lib/pai-paths.ts", "../../src/hooks/ts/lib/project-utils/notify.ts", "../../src/hooks/ts/lib/project-utils/session-notes.ts"],
|
|
4
|
-
"sourcesContent": ["#!/usr/bin/env node\n\n/**\n * load-project-context.ts\n *\n * SessionStart hook that sets up project context:\n * - Checks for CLAUDE.md in various locations (Claude Code handles loading)\n * - Sets up Notes/ directory in ~/.claude/projects/{encoded-path}/\n * - Ensures TODO.md exists\n * - Sends ntfy.sh notification (mandatory)\n * - Displays session continuity info (like session-init.sh)\n *\n * This hook complements Claude Code's native CLAUDE.md loading by:\n * - Setting up the Notes infrastructure\n * - Showing the latest session note for continuity\n * - Sending ntfy.sh notifications\n */\n\nimport { existsSync, readdirSync, readFileSync, statSync } from 'fs';\nimport { join, basename, dirname, resolve } from 'path';\nimport { homedir } from 'os';\nimport { execSync } from 'child_process';\nimport {\n PAI_DIR,\n findNotesDir,\n getProjectDir,\n getCurrentNotePath,\n createSessionNote,\n findTodoPath,\n findAllClaudeMdPaths,\n sendNtfyNotification,\n isProbeSession\n} from '../lib/project-utils';\n\n/**\n * Find the pai CLI binary path dynamically.\n * Tries `which pai` first, then common fallback locations.\n */\nfunction findPaiBinary(): string {\n try {\n return execSync('which pai', { encoding: 'utf-8' }).trim();\n } catch {\n // Fallback locations in order of preference\n const fallbacks = [\n '/usr/local/bin/pai',\n '/opt/homebrew/bin/pai',\n `${process.env.HOME}/.local/bin/pai`,\n ];\n for (const p of fallbacks) {\n if (existsSync(p)) return p;\n }\n }\n return 'pai'; // Last resort: rely on PATH at runtime\n}\n\n/**\n * Check session-routing.json for an active route.\n * Returns the routed Notes path if set, or null to use default behavior.\n */\nfunction getRoutedNotesPath(): string | null {\n const routingFile = join(PAI_DIR, 'session-routing.json');\n if (!existsSync(routingFile)) return null;\n\n try {\n const routing = JSON.parse(readFileSync(routingFile, 'utf-8'));\n const active = routing?.active_session;\n if (active?.notes_path) {\n return active.notes_path;\n }\n } catch {\n // Ignore parse errors\n }\n return null;\n}\n\n/**\n * Project signals that indicate a directory is a real project root.\n */\nconst PROJECT_SIGNALS = [\n '.git',\n 'package.json',\n 'pubspec.yaml',\n 'Cargo.toml',\n 'go.mod',\n 'pyproject.toml',\n 'setup.py',\n 'build.gradle',\n 'pom.xml',\n 'composer.json',\n 'Gemfile',\n 'Makefile',\n 'CMakeLists.txt',\n 'tsconfig.json',\n 'CLAUDE.md',\n join('Notes', 'PAI.md'),\n];\n\n/**\n * Returns true if the given directory looks like a project root.\n * Checks for the presence of well-known project signal files/dirs.\n */\nfunction hasProjectSignals(dir: string): boolean {\n for (const signal of PROJECT_SIGNALS) {\n if (existsSync(join(dir, signal))) return true;\n }\n return false;\n}\n\n/**\n * Returns true if the directory should NOT be auto-registered.\n * Guards: home directory, shallow paths, temp directories.\n */\nfunction isGuardedPath(dir: string): boolean {\n const home = homedir();\n const resolved = resolve(dir);\n\n // Never register the home directory itself\n if (resolved === home) return true;\n\n // Depth guard: require at least 3 path segments beyond root\n // e.g. /Users/i052341/foo is depth 3 on macOS \u2014 reject it\n const parts = resolved.split('/').filter(Boolean);\n if (parts.length < 3) return true;\n\n // Temp/system directories\n const forbidden = ['/tmp', '/var', '/private/tmp', '/private/var/folders'];\n for (const prefix of forbidden) {\n if (resolved === prefix || resolved.startsWith(prefix + '/')) return true;\n }\n\n return false;\n}\n\ninterface HookInput {\n session_id: string;\n cwd: string;\n hook_event_name: string;\n}\n\nasync function main() {\n console.error('\\nload-project-context.ts starting...');\n\n // Skip probe/health-check sessions (e.g. CodexBar ClaudeProbe)\n if (isProbeSession()) {\n console.error('Probe session detected - skipping project context loading');\n process.exit(0);\n }\n\n // Read hook input from stdin\n let hookInput: HookInput | null = null;\n try {\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk);\n }\n const input = Buffer.concat(chunks).toString('utf-8');\n if (input.trim()) {\n hookInput = JSON.parse(input);\n }\n } catch (error) {\n console.error('Could not parse hook input, using process.cwd()');\n }\n\n // Get current working directory\n const cwd = hookInput?.cwd || process.cwd();\n\n // Determine meaningful project name\n // If cwd is a Notes directory, use parent directory name instead\n let projectName = basename(cwd);\n if (projectName.toLowerCase() === 'notes') {\n projectName = basename(dirname(cwd));\n }\n\n console.error(`Working directory: ${cwd}`);\n console.error(`Project: ${projectName}`);\n\n // Check if this is a subagent session - skip for subagents\n const isSubagent = process.env.CLAUDE_AGENT_TYPE !== undefined ||\n (process.env.CLAUDE_PROJECT_DIR || '').includes('/.claude/agents/');\n\n if (isSubagent) {\n console.error('Subagent session - skipping project context setup');\n process.exit(0);\n }\n\n // 1. Find and READ all CLAUDE.md files - inject them into context\n // This ensures Claude actually processes the instructions, not just sees them in headers\n const claudeMdPaths = findAllClaudeMdPaths(cwd);\n const claudeMdContents: { path: string; content: string }[] = [];\n\n if (claudeMdPaths.length > 0) {\n console.error(`Found ${claudeMdPaths.length} CLAUDE.md file(s):`);\n for (const path of claudeMdPaths) {\n console.error(` - ${path}`);\n try {\n const content = readFileSync(path, 'utf-8');\n claudeMdContents.push({ path, content });\n console.error(` Read ${content.length} chars`);\n } catch (error) {\n console.error(` Could not read: ${error}`);\n }\n }\n } else {\n console.error('No CLAUDE.md found in project');\n console.error(' Consider creating one at ./CLAUDE.md or ./.claude/CLAUDE.md');\n }\n\n // 2. Find or create Notes directory\n // Priority:\n // 1. Active session routing (pai route <project>) \u2192 routed Obsidian path\n // 2. Local Notes/ in cwd \u2192 use it (git-trackable, e.g. symlink to Obsidian)\n // 3. Central ~/.claude/projects/.../Notes/ \u2192 fallback\n const routedPath = getRoutedNotesPath();\n let notesDir: string;\n\n if (routedPath) {\n // Routing is active - use the configured Obsidian Notes path\n const { mkdirSync } = await import('fs');\n if (!existsSync(routedPath)) {\n mkdirSync(routedPath, { recursive: true });\n console.error(`Created routed Notes: ${routedPath}`);\n } else {\n console.error(`Notes directory: ${routedPath} (routed via pai route)`);\n }\n notesDir = routedPath;\n } else {\n const notesInfo = findNotesDir(cwd);\n\n if (notesInfo.isLocal) {\n notesDir = notesInfo.path;\n console.error(`Notes directory: ${notesDir} (local)`);\n } else {\n // Create central Notes directory\n if (!existsSync(notesInfo.path)) {\n const { mkdirSync } = await import('fs');\n mkdirSync(notesInfo.path, { recursive: true });\n console.error(`Created central Notes: ${notesInfo.path}`);\n } else {\n console.error(`Notes directory: ${notesInfo.path} (central)`);\n }\n notesDir = notesInfo.path;\n }\n }\n\n // 3. Cleanup old .jsonl files from project root (move to sessions/)\n // Keep the newest one for potential resume, move older ones to sessions/\n const projectDir = getProjectDir(cwd);\n if (existsSync(projectDir)) {\n try {\n const files = readdirSync(projectDir);\n const jsonlFiles = files\n .filter(f => f.endsWith('.jsonl'))\n .map(f => ({\n name: f,\n path: join(projectDir, f),\n mtime: statSync(join(projectDir, f)).mtime.getTime()\n }))\n .sort((a, b) => b.mtime - a.mtime); // newest first\n\n if (jsonlFiles.length > 1) {\n const { mkdirSync, renameSync } = await import('fs');\n const sessionsDir = join(projectDir, 'sessions');\n if (!existsSync(sessionsDir)) {\n mkdirSync(sessionsDir, { recursive: true });\n }\n\n // Move all except the newest\n for (let i = 1; i < jsonlFiles.length; i++) {\n const file = jsonlFiles[i];\n const destPath = join(sessionsDir, file.name);\n if (!existsSync(destPath)) {\n renameSync(file.path, destPath);\n console.error(`Moved old session: ${file.name} \u2192 sessions/`);\n }\n }\n }\n } catch (error) {\n console.error(`Could not cleanup old .jsonl files: ${error}`);\n }\n }\n\n // 4. Find or create TODO.md\n const todoPath = findTodoPath(cwd);\n const hasTodo = existsSync(todoPath);\n if (hasTodo) {\n console.error(`TODO.md: ${todoPath}`);\n } else {\n // Create TODO.md in the Notes directory\n const newTodoPath = join(notesDir, 'TODO.md');\n const { writeFileSync } = await import('fs');\n writeFileSync(newTodoPath, `# TODO\\n\\n## Offen\\n\\n- [ ] \\n\\n---\\n\\n*Created: ${new Date().toISOString()}*\\n`);\n console.error(`Created TODO.md: ${newTodoPath}`);\n }\n\n // 5. Check for existing note or create new one\n let activeNotePath: string | null = null;\n\n if (notesDir) { // notesDir is always set now (local or central)\n const currentNotePath = getCurrentNotePath(notesDir);\n\n // Only create a new note if there is truly no note at all.\n // A completed note is still used \u2014 it will be updated or continued.\n // This prevents duplicate notes at month boundaries and on every compaction.\n if (!currentNotePath) {\n // Defensive: ensure projectName is a usable string\n const safeProjectName = (typeof projectName === 'string' && projectName.trim().length > 0)\n ? projectName.trim()\n : 'Untitled Session';\n console.error('\\nNo previous session notes found - creating new one');\n activeNotePath = createSessionNote(notesDir, String(safeProjectName));\n console.error(`Created: ${basename(activeNotePath)}`);\n } else {\n activeNotePath = currentNotePath!;\n console.error(`\\nUsing existing session note: ${basename(activeNotePath)}`);\n // Show preview of current note\n try {\n const content = readFileSync(activeNotePath, 'utf-8');\n const lines = content.split('\\n').slice(0, 12);\n console.error('--- Current Note Preview ---');\n for (const line of lines) {\n console.error(line);\n }\n console.error('--- End Preview ---\\n');\n } catch {\n // Ignore read errors\n }\n }\n }\n\n // 6. Show TODO.md preview\n if (existsSync(todoPath)) {\n try {\n const todoContent = readFileSync(todoPath, 'utf-8');\n const todoLines = todoContent.split('\\n').filter(l => l.includes('[ ]')).slice(0, 5);\n if (todoLines.length > 0) {\n console.error('\\nOpen TODOs:');\n for (const line of todoLines) {\n console.error(` ${line.trim()}`);\n }\n }\n } catch {\n // Ignore read errors\n }\n }\n\n // 7. Send ntfy.sh notification (MANDATORY)\n await sendNtfyNotification(`Session started in ${projectName}`);\n\n // 7.5. Run pai project detect to identify the registered PAI project\n const paiBin = findPaiBinary();\n let paiProjectBlock = '';\n try {\n const { execFileSync } = await import('child_process');\n const raw = execFileSync(paiBin, ['project', 'detect', '--json', cwd], {\n encoding: 'utf-8',\n env: process.env,\n }).trim();\n\n if (raw) {\n const detected = JSON.parse(raw) as {\n slug?: string;\n display_name?: string;\n root_path?: string;\n match_type?: string;\n relative_path?: string | null;\n session_count?: number;\n status?: string;\n error?: string;\n cwd?: string;\n };\n\n /**\n * Attempt to auto-register the CWD as a new PAI project.\n * Calls `pai project add <cwd>`, then re-detects to confirm registration.\n * Returns true if registration succeeded (or was attempted and project add ran),\n * and sets paiProjectBlock as a side effect on success.\n */\n const tryAutoRegister = async (): Promise<boolean> => {\n if (isGuardedPath(cwd) || !hasProjectSignals(cwd)) return false;\n\n try {\n execFileSync(paiBin, ['project', 'add', cwd], {\n encoding: 'utf-8',\n env: process.env,\n });\n console.error(`PAI auto-registered project at: ${cwd}`);\n\n // Re-run detect to confirm registration\n try {\n const raw2 = execFileSync(paiBin, ['project', 'detect', '--json', cwd], {\n encoding: 'utf-8',\n env: process.env,\n }).trim();\n\n if (raw2) {\n const detected2 = JSON.parse(raw2) as typeof detected;\n if (detected2.slug) {\n const name2 = detected2.display_name || detected2.slug;\n console.error(`PAI auto-registered: \"${detected2.slug}\" (${detected2.match_type})`);\n paiProjectBlock = `PAI Project Registry: ${name2} (slug: ${detected2.slug}) [AUTO-REGISTERED]\nMatch: ${detected2.match_type ?? 'exact'} | Sessions: 0`;\n return true;\n }\n }\n } catch (detectErr) {\n console.error('PAI auto-registration: project added but re-detect failed:', detectErr);\n return true; // project IS registered, just can't load context\n }\n } catch (addErr) {\n console.error('PAI auto-registration failed (project add):', addErr);\n }\n return false;\n };\n\n if (detected.error === 'no_match') {\n // Attempt auto-registration if the directory looks like a real project\n const autoRegistered = await tryAutoRegister();\n\n if (!autoRegistered) {\n paiProjectBlock = `PAI Project Registry: No registered project matches this directory.\nRun \"pai project add .\" to register this project, or use /route to tag the session.`;\n console.error('PAI detect: no match for', cwd);\n }\n } else if (\n detected.match_type === 'parent' &&\n detected.relative_path &&\n !isGuardedPath(cwd) &&\n hasProjectSignals(cwd)\n ) {\n // The CWD is inside a broader registered parent (e.g. \"i052341\" or \"apps\"),\n // but it has its own project signals \u2014 register it as a distinct project.\n console.error(\n `PAI detect: parent match to \"${detected.slug}\" via relative path \"${detected.relative_path}\" \u2014 CWD looks like its own project, attempting auto-registration`\n );\n const autoRegistered = await tryAutoRegister();\n\n if (!autoRegistered) {\n // Fall through: show the parent match as normal\n const name = detected.display_name || detected.slug;\n const nameSlug = ` (slug: ${detected.slug})`;\n const matchDesc = `parent (+${detected.relative_path ?? ''})`;\n const statusFlag = detected.status && detected.status !== 'active'\n ? ` [${detected.status.toUpperCase()}]`\n : '';\n paiProjectBlock = `PAI Project Registry: ${name}${statusFlag}${nameSlug}\nMatch: ${matchDesc} | Sessions: ${detected.session_count ?? 0}${detected.status && detected.status !== 'active' ? `\\nWARNING: Project status is \"${detected.status}\". Run: pai project health --fix` : ''}`;\n console.error(`PAI detect: kept parent match \"${detected.slug}\" (auto-register not applicable)`);\n }\n } else if (detected.slug) {\n const name = detected.display_name || detected.slug;\n const nameSlug = ` (slug: ${detected.slug})`;\n const matchDesc = detected.match_type === 'exact'\n ? 'exact'\n : `parent (+${detected.relative_path ?? ''})`;\n const statusFlag = detected.status && detected.status !== 'active'\n ? ` [${detected.status.toUpperCase()}]`\n : '';\n paiProjectBlock = `PAI Project Registry: ${name}${statusFlag}${nameSlug}\nMatch: ${matchDesc} | Sessions: ${detected.session_count ?? 0}${detected.status && detected.status !== 'active' ? `\\nWARNING: Project status is \"${detected.status}\". Run: pai project health --fix` : ''}`;\n console.error(`PAI detect: matched \"${detected.slug}\" (${detected.match_type})`);\n }\n }\n } catch (e) {\n // Non-fatal \u2014 don't break session start if pai is unavailable\n console.error('pai project detect failed:', e);\n }\n\n // 8. Output system reminder with session info\n const reminder = `\n<system-reminder>\nPROJECT CONTEXT LOADED\n\nProject: ${projectName}\nWorking Directory: ${cwd}\n${notesDir ? `Notes Directory: ${notesDir}${routedPath ? ' (routed via pai route)' : ''}` : 'Notes: disabled (no local Notes/ directory)'}\n${hasTodo ? `TODO: ${todoPath}` : 'TODO: not found'}\n${claudeMdPaths.length > 0 ? `CLAUDE.md: ${claudeMdPaths.join(', ')}` : 'No CLAUDE.md found'}\n${activeNotePath ? `Active Note: ${basename(activeNotePath)}` : ''}\n${routedPath ? `\\nNote Routing: ACTIVE (pai route is set - notes go to Obsidian vault)` : ''}\n${paiProjectBlock ? `\\n${paiProjectBlock}` : ''}\nSession Commands:\n- \"pause session\" \u2192 Save checkpoint, update TODO, exit (no compact)\n- \"end session\" \u2192 Finalize note, commit if needed, start fresh next time\n- \"pai route clear\" \u2192 Clear note routing (in a new session)\n</system-reminder>\n`;\n\n // Output to stdout for Claude to receive\n console.log(reminder);\n\n // 9. INJECT CLAUDE.md contents as system-reminders\n // This ensures Claude actually reads and processes the instructions\n for (const { path, content } of claudeMdContents) {\n const claudeMdReminder = `\n<system-reminder>\nLOCAL CLAUDE.md LOADED (MANDATORY - READ AND FOLLOW)\n\nSource: ${path}\n\n${content}\n\n---\nTHE ABOVE INSTRUCTIONS ARE MANDATORY. Follow them exactly.\n</system-reminder>\n`;\n console.log(claudeMdReminder);\n console.error(`Injected CLAUDE.md content from: ${path}`);\n }\n\n console.error('\\nProject context setup complete\\n');\n process.exit(0);\n}\n\nmain().catch(error => {\n console.error('load-project-context.ts error:', error);\n process.exit(0); // Don't block session start\n});\n", "/**\n * Path utilities \u2014 encoding, Notes/Sessions directory discovery and creation.\n */\n\nimport { existsSync, mkdirSync, readdirSync, renameSync } from 'fs';\nimport { join, basename } from 'path';\nimport { PAI_DIR } from '../pai-paths.js';\n\n// Re-export PAI_DIR for consumers\nexport { PAI_DIR };\nexport const PROJECTS_DIR = join(PAI_DIR, 'projects');\n\n/**\n * Directories known to be automated health-check / probe sessions.\n * Hooks should exit early for these to avoid registry clutter and wasted work.\n */\nconst PROBE_CWD_PATTERNS = [\n '/CodexBar/ClaudeProbe',\n '/ClaudeProbe',\n];\n\n/**\n * Check if the current working directory belongs to a probe/health-check session.\n * Returns true if hooks should skip this session entirely.\n */\nexport function isProbeSession(cwd?: string): boolean {\n const dir = cwd || process.cwd();\n return PROBE_CWD_PATTERNS.some(pattern => dir.includes(pattern));\n}\n\n/**\n * Encode a path the same way Claude Code does:\n * - Replace / with -\n * - Replace . with -\n * - Replace space with -\n */\nexport function encodePath(path: string): string {\n return path\n .replace(/\\//g, '-')\n .replace(/\\./g, '-')\n .replace(/ /g, '-');\n}\n\n/** Get the project directory for a given working directory. */\nexport function getProjectDir(cwd: string): string {\n const encoded = encodePath(cwd);\n return join(PROJECTS_DIR, encoded);\n}\n\n/** Get the Notes directory for a project (central location). */\nexport function getNotesDir(cwd: string): string {\n return join(getProjectDir(cwd), 'Notes');\n}\n\n/**\n * Find Notes directory \u2014 checks local first, falls back to central.\n * Does NOT create the directory.\n */\nexport function findNotesDir(cwd: string): { path: string; isLocal: boolean } {\n const cwdBasename = basename(cwd).toLowerCase();\n if (cwdBasename === 'notes' && existsSync(cwd)) {\n return { path: cwd, isLocal: true };\n }\n\n const localPaths = [\n join(cwd, 'Notes'),\n join(cwd, 'notes'),\n join(cwd, '.claude', 'Notes'),\n ];\n\n for (const path of localPaths) {\n if (existsSync(path)) {\n return { path, isLocal: true };\n }\n }\n\n return { path: getNotesDir(cwd), isLocal: false };\n}\n\n/** Get the sessions/ directory for a project (stores .jsonl transcripts). */\nexport function getSessionsDir(cwd: string): string {\n return join(getProjectDir(cwd), 'sessions');\n}\n\n/** Get the sessions/ directory from a project directory path. */\nexport function getSessionsDirFromProjectDir(projectDir: string): string {\n return join(projectDir, 'sessions');\n}\n\n// ---------------------------------------------------------------------------\n// Directory creation helpers\n// ---------------------------------------------------------------------------\n\n/** Ensure the Notes directory exists for a project. @deprecated Use ensureNotesDirSmart() */\nexport function ensureNotesDir(cwd: string): string {\n const notesDir = getNotesDir(cwd);\n if (!existsSync(notesDir)) {\n mkdirSync(notesDir, { recursive: true });\n console.error(`Created Notes directory: ${notesDir}`);\n }\n return notesDir;\n}\n\n/**\n * Smart Notes directory handling:\n * - If local Notes/ exists \u2192 use it (don't create anything new)\n * - If no local Notes/ \u2192 ensure central exists and use that\n */\nexport function ensureNotesDirSmart(cwd: string): { path: string; isLocal: boolean } {\n const found = findNotesDir(cwd);\n if (found.isLocal) return found;\n if (!existsSync(found.path)) {\n mkdirSync(found.path, { recursive: true });\n console.error(`Created central Notes directory: ${found.path}`);\n }\n return found;\n}\n\n/** Ensure the sessions/ directory exists for a project. */\nexport function ensureSessionsDir(cwd: string): string {\n const sessionsDir = getSessionsDir(cwd);\n if (!existsSync(sessionsDir)) {\n mkdirSync(sessionsDir, { recursive: true });\n console.error(`Created sessions directory: ${sessionsDir}`);\n }\n return sessionsDir;\n}\n\n/** Ensure the sessions/ directory exists (from project dir path). */\nexport function ensureSessionsDirFromProjectDir(projectDir: string): string {\n const sessionsDir = getSessionsDirFromProjectDir(projectDir);\n if (!existsSync(sessionsDir)) {\n mkdirSync(sessionsDir, { recursive: true });\n console.error(`Created sessions directory: ${sessionsDir}`);\n }\n return sessionsDir;\n}\n\n/**\n * Move all .jsonl session files from project root to sessions/ subdirectory.\n * Returns the number of files moved.\n */\nexport function moveSessionFilesToSessionsDir(\n projectDir: string,\n excludeFile?: string,\n silent = false\n): number {\n const sessionsDir = ensureSessionsDirFromProjectDir(projectDir);\n\n if (!existsSync(projectDir)) return 0;\n\n const files = readdirSync(projectDir);\n let movedCount = 0;\n\n for (const file of files) {\n if (file.endsWith('.jsonl') && file !== excludeFile) {\n const sourcePath = join(projectDir, file);\n const destPath = join(sessionsDir, file);\n try {\n renameSync(sourcePath, destPath);\n if (!silent) console.error(`Moved ${file} \u2192 sessions/`);\n movedCount++;\n } catch (error) {\n if (!silent) console.error(`Could not move ${file}: ${error}`);\n }\n }\n }\n\n return movedCount;\n}\n\n// ---------------------------------------------------------------------------\n// CLAUDE.md / TODO.md discovery\n// ---------------------------------------------------------------------------\n\n/** Find TODO.md \u2014 check local first, fallback to central. */\nexport function findTodoPath(cwd: string): string {\n const localPaths = [\n join(cwd, 'TODO.md'),\n join(cwd, 'notes', 'TODO.md'),\n join(cwd, 'Notes', 'TODO.md'),\n join(cwd, '.claude', 'TODO.md'),\n ];\n\n for (const path of localPaths) {\n if (existsSync(path)) return path;\n }\n\n return join(getNotesDir(cwd), 'TODO.md');\n}\n\n/** Find CLAUDE.md \u2014 returns the FIRST found path. */\nexport function findClaudeMdPath(cwd: string): string | null {\n const paths = findAllClaudeMdPaths(cwd);\n return paths.length > 0 ? paths[0] : null;\n}\n\n/**\n * Find ALL CLAUDE.md files in local locations in priority order.\n */\nexport function findAllClaudeMdPaths(cwd: string): string[] {\n const foundPaths: string[] = [];\n\n const localPaths = [\n join(cwd, '.claude', 'CLAUDE.md'),\n join(cwd, 'CLAUDE.md'),\n join(cwd, 'Notes', 'CLAUDE.md'),\n join(cwd, 'notes', 'CLAUDE.md'),\n join(cwd, 'Prompts', 'CLAUDE.md'),\n join(cwd, 'prompts', 'CLAUDE.md'),\n ];\n\n for (const path of localPaths) {\n if (existsSync(path)) foundPaths.push(path);\n }\n\n return foundPaths;\n}\n", "/**\n * PAI Path Resolution - Single Source of Truth\n *\n * This module provides consistent path resolution across all PAI hooks.\n * It handles PAI_DIR detection whether set explicitly or defaulting to ~/.claude\n *\n * ALSO loads .env file from PAI_DIR so all hooks get environment variables\n * without relying on Claude Code's settings.json injection.\n *\n * Usage in hooks:\n * import { PAI_DIR, HOOKS_DIR, SKILLS_DIR } from './lib/pai-paths';\n */\n\nimport { homedir } from 'os';\nimport { resolve, join } from 'path';\nimport { existsSync, readFileSync } from 'fs';\n\n/**\n * Load .env file and inject into process.env\n * Must run BEFORE PAI_DIR resolution so .env can set PAI_DIR if needed\n */\nfunction loadEnvFile(): void {\n // Check common locations for .env\n const possiblePaths = [\n resolve(process.env.PAI_DIR || '', '.env'),\n resolve(homedir(), '.claude', '.env'),\n ];\n\n for (const envPath of possiblePaths) {\n if (existsSync(envPath)) {\n try {\n const content = readFileSync(envPath, 'utf-8');\n for (const line of content.split('\\n')) {\n const trimmed = line.trim();\n // Skip comments and empty lines\n if (!trimmed || trimmed.startsWith('#')) continue;\n\n const eqIndex = trimmed.indexOf('=');\n if (eqIndex > 0) {\n const key = trimmed.substring(0, eqIndex).trim();\n let value = trimmed.substring(eqIndex + 1).trim();\n\n // Remove surrounding quotes if present\n if ((value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n value = value.slice(1, -1);\n }\n\n // Expand $HOME and ~ in values\n value = value.replace(/\\$HOME/g, homedir());\n value = value.replace(/^~(?=\\/|$)/, homedir());\n\n // Only set if not already defined (env vars take precedence)\n if (process.env[key] === undefined) {\n process.env[key] = value;\n }\n }\n }\n // Found and loaded, don't check other paths\n break;\n } catch {\n // Silently continue if .env can't be read\n }\n }\n }\n}\n\n// Load .env FIRST, before any other initialization\nloadEnvFile();\n\n/**\n * Smart PAI_DIR detection with fallback\n * Priority:\n * 1. PAI_DIR environment variable (if set)\n * 2. ~/.claude (standard location)\n */\nexport const PAI_DIR = process.env.PAI_DIR\n ? resolve(process.env.PAI_DIR)\n : resolve(homedir(), '.claude');\n\n/**\n * Common PAI directories\n */\nexport const HOOKS_DIR = join(PAI_DIR, 'Hooks');\nexport const SKILLS_DIR = join(PAI_DIR, 'Skills');\nexport const AGENTS_DIR = join(PAI_DIR, 'Agents');\nexport const HISTORY_DIR = join(PAI_DIR, 'History');\nexport const COMMANDS_DIR = join(PAI_DIR, 'Commands');\n\n/**\n * Validate PAI directory structure on first import\n * This fails fast with a clear error if PAI is misconfigured\n */\nfunction validatePAIStructure(): void {\n if (!existsSync(PAI_DIR)) {\n console.error(`PAI_DIR does not exist: ${PAI_DIR}`);\n console.error(` Expected ~/.claude or set PAI_DIR environment variable`);\n process.exit(1);\n }\n\n if (!existsSync(HOOKS_DIR)) {\n console.error(`PAI hooks directory not found: ${HOOKS_DIR}`);\n console.error(` Your PAI_DIR may be misconfigured`);\n console.error(` Current PAI_DIR: ${PAI_DIR}`);\n process.exit(1);\n }\n}\n\n// Run validation on module import\n// This ensures any hook that imports this module will fail fast if paths are wrong\nvalidatePAIStructure();\n\n/**\n * Helper to get history file path with date-based organization\n */\nexport function getHistoryFilePath(subdir: string, filename: string): string {\n const now = new Date();\n const tz = process.env.TIME_ZONE || Intl.DateTimeFormat().resolvedOptions().timeZone;\n const localDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));\n const year = localDate.getFullYear();\n const month = String(localDate.getMonth() + 1).padStart(2, '0');\n\n return join(HISTORY_DIR, subdir, `${year}-${month}`, filename);\n}\n", "/**\n * Push notification helpers \u2014 WhatsApp-aware with ntfy.sh fallback.\n */\n\nimport { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\n\n/**\n * Check if a messaging MCP server (AIBroker, Whazaa, or Telex) is configured.\n * When any messaging server is active, the AI handles notifications via MCP\n * and ntfy is skipped to avoid duplicates.\n */\nexport function isWhatsAppEnabled(): boolean {\n try {\n const settingsPath = join(homedir(), '.claude', 'settings.json');\n if (!existsSync(settingsPath)) return false;\n\n const settings = JSON.parse(readFileSync(settingsPath, 'utf-8'));\n const enabled: string[] = settings.enabledMcpjsonServers || [];\n return enabled.includes('aibroker') || enabled.includes('whazaa') || enabled.includes('telex');\n } catch {\n return false;\n }\n}\n\n/**\n * Send push notification \u2014 WhatsApp-aware with ntfy fallback.\n *\n * When WhatsApp (Whazaa) is enabled in MCP config, ntfy is SKIPPED\n * because the AI sends WhatsApp messages directly via MCP.\n * When WhatsApp is NOT configured, ntfy fires as the fallback channel.\n */\nexport async function sendNtfyNotification(message: string, retries = 2): Promise<boolean> {\n if (isWhatsAppEnabled()) {\n console.error(`WhatsApp (Whazaa) enabled in MCP config \u2014 skipping ntfy`);\n return true;\n }\n\n const topic = process.env.NTFY_TOPIC;\n\n if (!topic) {\n console.error('NTFY_TOPIC not set and WhatsApp not active \u2014 notifications disabled');\n return false;\n }\n\n for (let attempt = 0; attempt <= retries; attempt++) {\n try {\n const response = await fetch(`https://ntfy.sh/${topic}`, {\n method: 'POST',\n body: message,\n headers: {\n 'Title': 'Claude Code',\n 'Priority': 'default',\n },\n });\n\n if (response.ok) {\n console.error(`ntfy.sh notification sent (WhatsApp inactive): \"${message}\"`);\n return true;\n } else {\n console.error(`ntfy.sh attempt ${attempt + 1} failed: ${response.status}`);\n }\n } catch (error) {\n console.error(`ntfy.sh attempt ${attempt + 1} error: ${error}`);\n }\n\n if (attempt < retries) {\n await new Promise(resolve => setTimeout(resolve, 1000));\n }\n }\n\n console.error('ntfy.sh notification failed after all retries');\n return false;\n}\n", "/**\n * Session note creation, editing, checkpointing, renaming, and finalization.\n */\n\nimport { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync, renameSync } from 'fs';\nimport { join, basename } from 'path';\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/** Get or create the YYYY/MM subdirectory for the current month inside notesDir. */\nfunction getMonthDir(notesDir: string): string {\n const now = new Date();\n const year = String(now.getFullYear());\n const month = String(now.getMonth() + 1).padStart(2, '0');\n const monthDir = join(notesDir, year, month);\n if (!existsSync(monthDir)) {\n mkdirSync(monthDir, { recursive: true });\n }\n return monthDir;\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\n/**\n * Get the next note number (4-digit format: 0001, 0002, etc.).\n * Numbers are scoped per YYYY/MM directory.\n */\nexport function getNextNoteNumber(notesDir: string): string {\n const monthDir = getMonthDir(notesDir);\n\n const files = readdirSync(monthDir)\n .filter(f => f.match(/^\\d{3,4}[\\s_-]/))\n .sort();\n\n if (files.length === 0) return '0001';\n\n let maxNumber = 0;\n for (const file of files) {\n const digitMatch = file.match(/^(\\d+)/);\n if (digitMatch) {\n const num = parseInt(digitMatch[1], 10);\n if (num > maxNumber) maxNumber = num;\n }\n }\n\n return String(maxNumber + 1).padStart(4, '0');\n}\n\n/**\n * Get the current (latest) note file path, or null if none exists.\n * Searches current month \u2192 previous month \u2192 flat notesDir (legacy).\n */\nexport function getCurrentNotePath(notesDir: string): string | null {\n if (!existsSync(notesDir)) return null;\n\n const findLatestIn = (dir: string): string | null => {\n if (!existsSync(dir)) return null;\n const files = readdirSync(dir)\n .filter(f => f.match(/^\\d{3,4}[\\s_-].*\\.md$/))\n .sort((a, b) => {\n const numA = parseInt(a.match(/^(\\d+)/)?.[1] || '0', 10);\n const numB = parseInt(b.match(/^(\\d+)/)?.[1] || '0', 10);\n return numA - numB;\n });\n if (files.length === 0) return null;\n return join(dir, files[files.length - 1]);\n };\n\n const now = new Date();\n const year = String(now.getFullYear());\n const month = String(now.getMonth() + 1).padStart(2, '0');\n const currentMonthDir = join(notesDir, year, month);\n const found = findLatestIn(currentMonthDir);\n if (found) return found;\n\n const prevDate = new Date(now.getFullYear(), now.getMonth() - 1, 1);\n const prevYear = String(prevDate.getFullYear());\n const prevMonth = String(prevDate.getMonth() + 1).padStart(2, '0');\n const prevMonthDir = join(notesDir, prevYear, prevMonth);\n const prevFound = findLatestIn(prevMonthDir);\n if (prevFound) return prevFound;\n\n return findLatestIn(notesDir);\n}\n\n/**\n * Create a new session note.\n * Format: \"NNNN - YYYY-MM-DD - New Session.md\" filed into YYYY/MM subdirectory.\n * Claude MUST rename at session end with a meaningful description.\n */\nexport function createSessionNote(notesDir: string, description: string): string {\n const noteNumber = getNextNoteNumber(notesDir);\n const date = new Date().toISOString().split('T')[0];\n const monthDir = getMonthDir(notesDir);\n const filename = `${noteNumber} - ${date} - New Session.md`;\n const filepath = join(monthDir, filename);\n\n const content = `# Session ${noteNumber}: ${description}\n\n**Date:** ${date}\n**Status:** In Progress\n\n---\n\n## Work Done\n\n<!-- PAI will add completed work here during session -->\n\n---\n\n## Next Steps\n\n<!-- To be filled at session end -->\n\n---\n\n**Tags:** #Session\n`;\n\n writeFileSync(filepath, content);\n console.error(`Created session note: ${filename}`);\n\n return filepath;\n}\n\n/** Append a checkpoint to the current session note. */\nexport function appendCheckpoint(notePath: string, checkpoint: string): void {\n if (!existsSync(notePath)) {\n console.error(`Note file not found, recreating: ${notePath}`);\n try {\n const parentDir = join(notePath, '..');\n if (!existsSync(parentDir)) mkdirSync(parentDir, { recursive: true });\n const noteFilename = basename(notePath);\n const numberMatch = noteFilename.match(/^(\\d+)/);\n const noteNumber = numberMatch ? numberMatch[1] : '0000';\n const date = new Date().toISOString().split('T')[0];\n const content = `# Session ${noteNumber}: Recovered\\n\\n**Date:** ${date}\\n**Status:** In Progress\\n\\n---\\n\\n## Work Done\\n\\n<!-- PAI will add completed work here during session -->\\n\\n---\\n\\n## Next Steps\\n\\n<!-- To be filled at session end -->\\n\\n---\\n\\n**Tags:** #Session\\n`;\n writeFileSync(notePath, content);\n console.error(`Recreated session note: ${noteFilename}`);\n } catch (err) {\n console.error(`Failed to recreate note: ${err}`);\n return;\n }\n }\n\n const content = readFileSync(notePath, 'utf-8');\n const timestamp = new Date().toISOString();\n const checkpointText = `\\n### Checkpoint ${timestamp}\\n\\n${checkpoint}\\n`;\n\n const nextStepsIndex = content.indexOf('## Next Steps');\n const newContent = nextStepsIndex !== -1\n ? content.substring(0, nextStepsIndex) + checkpointText + content.substring(nextStepsIndex)\n : content + checkpointText;\n\n writeFileSync(notePath, newContent);\n console.error(`Checkpoint added to: ${basename(notePath)}`);\n}\n\n/** Work item for session notes. */\nexport interface WorkItem {\n title: string;\n details?: string[];\n completed?: boolean;\n}\n\n/** Add work items to the \"Work Done\" section of a session note. */\nexport function addWorkToSessionNote(notePath: string, workItems: WorkItem[], sectionTitle?: string): void {\n if (!existsSync(notePath)) {\n console.error(`Note file not found: ${notePath}`);\n return;\n }\n\n let content = readFileSync(notePath, 'utf-8');\n\n let workText = '';\n if (sectionTitle) workText += `\\n### ${sectionTitle}\\n\\n`;\n\n for (const item of workItems) {\n const checkbox = item.completed !== false ? '[x]' : '[ ]';\n workText += `- ${checkbox} **${item.title}**\\n`;\n if (item.details && item.details.length > 0) {\n for (const detail of item.details) {\n workText += ` - ${detail}\\n`;\n }\n }\n }\n\n const workDoneMatch = content.match(/## Work Done\\n\\n(<!-- .*? -->)?/);\n if (workDoneMatch) {\n const insertPoint = content.indexOf(workDoneMatch[0]) + workDoneMatch[0].length;\n content = content.substring(0, insertPoint) + workText + content.substring(insertPoint);\n } else {\n const nextStepsIndex = content.indexOf('## Next Steps');\n if (nextStepsIndex !== -1) {\n content = content.substring(0, nextStepsIndex) + workText + '\\n' + content.substring(nextStepsIndex);\n }\n }\n\n writeFileSync(notePath, content);\n console.error(`Added ${workItems.length} work item(s) to: ${basename(notePath)}`);\n}\n\n/**\n * Check if a candidate title is meaningless / garbage.\n * Public wrapper around the internal filter for use by other hooks.\n */\nexport function isMeaningfulTitle(text: string): boolean {\n return !isMeaninglessCandidate(text);\n}\n\n/** Sanitize a string for use in a filename. */\nexport function sanitizeForFilename(str: string): string {\n return str\n .toLowerCase()\n .replace(/[^a-z0-9\\s-]/g, '')\n .replace(/\\s+/g, '-')\n .replace(/-+/g, '-')\n .replace(/^-|-$/g, '')\n .substring(0, 50);\n}\n\n/**\n * Return true if the candidate string should be rejected as a meaningful name.\n * Rejects file paths, shebangs, timestamps, system noise, XML tags, hashes, etc.\n */\nfunction isMeaninglessCandidate(text: string): boolean {\n const t = text.trim();\n if (!t) return true;\n if (t.length < 5) return true; // too short to be meaningful\n if (t.startsWith('/') || t.startsWith('~')) return true; // file path\n if (t.startsWith('#!')) return true; // shebang\n if (t.includes('[object Object]')) return true; // serialization artifact\n if (/^\\d{4}-\\d{2}-\\d{2}(T[\\d:.Z+-]+)?$/.test(t)) return true; // ISO timestamp\n if (/^\\d{1,2}:\\d{2}(:\\d{2})?(\\s*(AM|PM))?$/i.test(t)) return true; // time-only\n if (/^<[a-z-]+[\\s/>]/i.test(t)) return true; // XML/HTML tags (<task-notification>, etc.)\n if (/^[0-9a-f]{10,}$/i.test(t)) return true; // hex hash strings\n if (/^Exit code \\d+/i.test(t)) return true; // exit code messages\n if (/^Error:/i.test(t)) return true; // error messages\n if (/^This session is being continued/i.test(t)) return true; // continuation boilerplate\n if (/^\\(Bash completed/i.test(t)) return true; // bash output noise\n if (/^Task Notification$/i.test(t)) return true; // literal \"Task Notification\"\n if (/^New Session$/i.test(t)) return true; // placeholder title\n if (/^Recovered Session$/i.test(t)) return true; // placeholder title\n if (/^Continued Session$/i.test(t)) return true; // placeholder title\n if (/^Untitled Session$/i.test(t)) return true; // placeholder title\n if (/^Context Compression$/i.test(t)) return true; // compression artifact\n if (/^[A-Fa-f0-9]{8,}\\s+Output$/i.test(t)) return true; // hash + \"Output\" pattern\n return false;\n}\n\n/**\n * Extract a meaningful name from session note content and summary.\n * Looks at Work Done section headers, bold text, and summary.\n */\nexport function extractMeaningfulName(noteContent: string, summary: string): string {\n const workDoneMatch = noteContent.match(/## Work Done\\n\\n([\\s\\S]*?)(?=\\n---|\\n## Next)/);\n\n if (workDoneMatch) {\n const workDoneSection = workDoneMatch[1];\n\n const subheadings = workDoneSection.match(/### ([^\\n]+)/g);\n if (subheadings && subheadings.length > 0) {\n const firstHeading = subheadings[0].replace('### ', '').trim();\n if (!isMeaninglessCandidate(firstHeading) && firstHeading.length > 5 && firstHeading.length < 60) {\n return sanitizeForFilename(firstHeading);\n }\n }\n\n const boldMatches = workDoneSection.match(/\\*\\*([^*]+)\\*\\*/g);\n if (boldMatches && boldMatches.length > 0) {\n const firstBold = boldMatches[0].replace(/\\*\\*/g, '').trim();\n if (!isMeaninglessCandidate(firstBold) && firstBold.length > 3 && firstBold.length < 50) {\n return sanitizeForFilename(firstBold);\n }\n }\n\n const numberedItems = workDoneSection.match(/^\\d+\\.\\s+\\*\\*([^*]+)\\*\\*/m);\n if (numberedItems && !isMeaninglessCandidate(numberedItems[1])) {\n return sanitizeForFilename(numberedItems[1]);\n }\n }\n\n if (summary && summary.length > 5 && summary !== 'Session completed.' && !isMeaninglessCandidate(summary)) {\n const cleanSummary = summary\n .replace(/[^\\w\\s-]/g, ' ')\n .trim()\n .split(/\\s+/)\n .slice(0, 5)\n .join(' ');\n if (cleanSummary.length > 3 && !isMeaninglessCandidate(cleanSummary)) {\n return sanitizeForFilename(cleanSummary);\n }\n }\n\n return '';\n}\n\n/**\n * Rename a session note with a meaningful name.\n * Always uses \"NNNN - YYYY-MM-DD - Description.md\" format.\n * Returns the new path, or original path if rename fails.\n */\nexport function renameSessionNote(notePath: string, meaningfulName: string): string {\n if (!meaningfulName || !existsSync(notePath)) return notePath;\n\n const dir = join(notePath, '..');\n const oldFilename = basename(notePath);\n\n const correctMatch = oldFilename.match(/^(\\d{3,4}) - (\\d{4}-\\d{2}-\\d{2}) - .*\\.md$/);\n const legacyMatch = oldFilename.match(/^(\\d{3,4})_(\\d{4}-\\d{2}-\\d{2})_.*\\.md$/);\n const match = correctMatch || legacyMatch;\n if (!match) return notePath;\n\n const [, noteNumber, date] = match;\n\n const titleCaseName = meaningfulName\n .split(/[\\s_-]+/)\n .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())\n .join(' ')\n .trim();\n\n const paddedNumber = noteNumber.padStart(4, '0');\n const newFilename = `${paddedNumber} - ${date} - ${titleCaseName}.md`;\n const newPath = join(dir, newFilename);\n\n if (newFilename === oldFilename) return notePath;\n\n try {\n renameSync(notePath, newPath);\n console.error(`Renamed note: ${oldFilename} \u2192 ${newFilename}`);\n return newPath;\n } catch (error) {\n console.error(`Could not rename note: ${error}`);\n return notePath;\n }\n}\n\n/** Update the session note's H1 title and rename the file. */\nexport function updateSessionNoteTitle(notePath: string, newTitle: string): void {\n if (!existsSync(notePath)) {\n console.error(`Note file not found: ${notePath}`);\n return;\n }\n\n let content = readFileSync(notePath, 'utf-8');\n content = content.replace(/^# Session \\d+:.*$/m, (match) => {\n const sessionNum = match.match(/Session (\\d+)/)?.[1] || '';\n return `# Session ${sessionNum}: ${newTitle}`;\n });\n writeFileSync(notePath, content);\n renameSessionNote(notePath, sanitizeForFilename(newTitle));\n}\n\n/**\n * Finalize session note \u2014 mark as complete, add summary, rename with meaningful name.\n * IDEMPOTENT: subsequent calls are no-ops if already finalized.\n * Returns the final path (may be renamed).\n */\nexport function finalizeSessionNote(notePath: string, summary: string): string {\n if (!existsSync(notePath)) {\n console.error(`Note file not found: ${notePath}`);\n return notePath;\n }\n\n let content = readFileSync(notePath, 'utf-8');\n\n if (content.includes('**Status:** Completed')) {\n console.error(`Note already finalized: ${basename(notePath)}`);\n return notePath;\n }\n\n content = content.replace('**Status:** In Progress', '**Status:** Completed');\n\n if (!content.includes('**Completed:**')) {\n const completionTime = new Date().toISOString();\n content = content.replace(\n '---\\n\\n## Work Done',\n `**Completed:** ${completionTime}\\n\\n---\\n\\n## Work Done`\n );\n }\n\n const nextStepsMatch = content.match(/## Next Steps\\n\\n(<!-- .*? -->)/);\n if (nextStepsMatch) {\n content = content.replace(\n nextStepsMatch[0],\n `## Next Steps\\n\\n${summary || 'Session completed.'}`\n );\n }\n\n writeFileSync(notePath, content);\n console.error(`Session note finalized: ${basename(notePath)}`);\n\n const meaningfulName = extractMeaningfulName(content, summary);\n if (meaningfulName) {\n return renameSessionNote(notePath, meaningfulName);\n }\n\n return notePath;\n}\n"],
|
|
5
|
-
"mappings": ";;;AAkBA,SAAS,cAAAA,aAAY,eAAAC,cAAa,gBAAAC,eAAc,gBAAgB;AAChE,SAAS,QAAAC,OAAM,YAAAC,WAAU,SAAS,WAAAC,gBAAe;AACjD,SAAS,WAAAC,gBAAe;AACxB,SAAS,gBAAgB;;;ACjBzB,SAAS,cAAAC,aAAY,WAAW,aAAa,kBAAkB;AAC/D,SAAS,QAAAC,OAAM,gBAAgB;;;ACQ/B,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AAC9B,SAAS,YAAY,oBAAoB;AAMzC,SAAS,cAAoB;AAE3B,QAAM,gBAAgB;AAAA,IACpB,QAAQ,QAAQ,IAAI,WAAW,IAAI,MAAM;AAAA,IACzC,QAAQ,QAAQ,GAAG,WAAW,MAAM;AAAA,EACtC;AAEA,aAAW,WAAW,eAAe;AACnC,QAAI,WAAW,OAAO,GAAG;AACvB,UAAI;AACF,cAAM,UAAU,aAAa,SAAS,OAAO;AAC7C,mBAAW,QAAQ,QAAQ,MAAM,IAAI,GAAG;AACtC,gBAAM,UAAU,KAAK,KAAK;AAE1B,cAAI,CAAC,WAAW,QAAQ,WAAW,GAAG,EAAG;AAEzC,gBAAM,UAAU,QAAQ,QAAQ,GAAG;AACnC,cAAI,UAAU,GAAG;AACf,kBAAM,MAAM,QAAQ,UAAU,GAAG,OAAO,EAAE,KAAK;AAC/C,gBAAI,QAAQ,QAAQ,U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AI,gBAAgB;AAC5B,YAAQ,MAAM,oCAAoC,IAAI,EAAE;AAAA,EAC1D;AAEA,UAAQ,MAAM,oCAAoC;AAClD,UAAQ,KAAK,CAAC;AAChB;AAEA,KAAK,EAAE,MAAM,WAAS;AACpB,UAAQ,MAAM,kCAAkC,KAAK;AACrD,UAAQ,KAAK,CAAC;AAChB,CAAC;",
|
|
6
|
-
"names": ["existsSync", "readdirSync", "readFileSync", "join", "basename", "resolve", "homedir", "existsSync", "join", "join", "existsSync", "join", "existsSync", "join", "existsSync", "existsSync", "readFileSync", "join", "homedir", "resolve", "existsSync", "mkdirSync", "readdirSync", "readFileSync", "renameSync", "join", "basename", "existsSync", "join", "readFileSync", "homedir", "resolve", "basename", "mkdirSync", "readdirSync", "renameSync", "writeFileSync"]
|
|
3
|
+
"sources": ["../../src/hooks/ts/session-start/load-project-context.ts", "../../src/memory/wakeup.ts", "../../src/hooks/ts/lib/project-utils/paths.ts", "../../src/hooks/ts/lib/pai-paths.ts", "../../src/hooks/ts/lib/project-utils/notify.ts", "../../src/hooks/ts/lib/project-utils/session-notes.ts"],
|
|
4
|
+
"sourcesContent": ["#!/usr/bin/env node\n\n/**\n * load-project-context.ts\n *\n * SessionStart hook that sets up project context:\n * - Checks for CLAUDE.md in various locations (Claude Code handles loading)\n * - Sets up Notes/ directory in ~/.claude/projects/{encoded-path}/\n * - Ensures TODO.md exists\n * - Sends ntfy.sh notification (mandatory)\n * - Displays session continuity info (like session-init.sh)\n *\n * This hook complements Claude Code's native CLAUDE.md loading by:\n * - Setting up the Notes infrastructure\n * - Showing the latest session note for continuity\n * - Sending ntfy.sh notifications\n */\n\nimport { existsSync, readdirSync, readFileSync, statSync } from 'fs';\nimport { join, basename, dirname, resolve } from 'path';\nimport { homedir } from 'os';\nimport { execSync } from 'child_process';\nimport { buildWakeupContext } from '../../../memory/wakeup.js';\nimport {\n PAI_DIR,\n findNotesDir,\n getProjectDir,\n getCurrentNotePath,\n createSessionNote,\n findTodoPath,\n findAllClaudeMdPaths,\n sendNtfyNotification,\n isProbeSession\n} from '../lib/project-utils';\n\n/**\n * Find the pai CLI binary path dynamically.\n * Tries `which pai` first, then common fallback locations.\n */\nfunction findPaiBinary(): string {\n try {\n return execSync('which pai', { encoding: 'utf-8' }).trim();\n } catch {\n // Fallback locations in order of preference\n const fallbacks = [\n '/usr/local/bin/pai',\n '/opt/homebrew/bin/pai',\n `${process.env.HOME}/.local/bin/pai`,\n ];\n for (const p of fallbacks) {\n if (existsSync(p)) return p;\n }\n }\n return 'pai'; // Last resort: rely on PATH at runtime\n}\n\n/**\n * Check session-routing.json for an active route.\n * Returns the routed Notes path if set, or null to use default behavior.\n */\nfunction getRoutedNotesPath(): string | null {\n const routingFile = join(PAI_DIR, 'session-routing.json');\n if (!existsSync(routingFile)) return null;\n\n try {\n const routing = JSON.parse(readFileSync(routingFile, 'utf-8'));\n const active = routing?.active_session;\n if (active?.notes_path) {\n return active.notes_path;\n }\n } catch {\n // Ignore parse errors\n }\n return null;\n}\n\n/**\n * Project signals that indicate a directory is a real project root.\n */\nconst PROJECT_SIGNALS = [\n '.git',\n 'package.json',\n 'pubspec.yaml',\n 'Cargo.toml',\n 'go.mod',\n 'pyproject.toml',\n 'setup.py',\n 'build.gradle',\n 'pom.xml',\n 'composer.json',\n 'Gemfile',\n 'Makefile',\n 'CMakeLists.txt',\n 'tsconfig.json',\n 'CLAUDE.md',\n join('Notes', 'PAI.md'),\n];\n\n/**\n * Returns true if the given directory looks like a project root.\n * Checks for the presence of well-known project signal files/dirs.\n */\nfunction hasProjectSignals(dir: string): boolean {\n for (const signal of PROJECT_SIGNALS) {\n if (existsSync(join(dir, signal))) return true;\n }\n return false;\n}\n\n/**\n * Returns true if the directory should NOT be auto-registered.\n * Guards: home directory, shallow paths, temp directories.\n */\nfunction isGuardedPath(dir: string): boolean {\n const home = homedir();\n const resolved = resolve(dir);\n\n // Never register the home directory itself\n if (resolved === home) return true;\n\n // Depth guard: require at least 3 path segments beyond root\n // e.g. /Users/i052341/foo is depth 3 on macOS \u2014 reject it\n const parts = resolved.split('/').filter(Boolean);\n if (parts.length < 3) return true;\n\n // Temp/system directories\n const forbidden = ['/tmp', '/var', '/private/tmp', '/private/var/folders'];\n for (const prefix of forbidden) {\n if (resolved === prefix || resolved.startsWith(prefix + '/')) return true;\n }\n\n return false;\n}\n\ninterface HookInput {\n session_id: string;\n cwd: string;\n hook_event_name: string;\n}\n\nasync function main() {\n console.error('\\nload-project-context.ts starting...');\n\n // Skip probe/health-check sessions (e.g. CodexBar ClaudeProbe)\n if (isProbeSession()) {\n console.error('Probe session detected - skipping project context loading');\n process.exit(0);\n }\n\n // Read hook input from stdin\n let hookInput: HookInput | null = null;\n try {\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk);\n }\n const input = Buffer.concat(chunks).toString('utf-8');\n if (input.trim()) {\n hookInput = JSON.parse(input);\n }\n } catch (error) {\n console.error('Could not parse hook input, using process.cwd()');\n }\n\n // Get current working directory\n const cwd = hookInput?.cwd || process.cwd();\n\n // Determine meaningful project name\n // If cwd is a Notes directory, use parent directory name instead\n let projectName = basename(cwd);\n if (projectName.toLowerCase() === 'notes') {\n projectName = basename(dirname(cwd));\n }\n\n console.error(`Working directory: ${cwd}`);\n console.error(`Project: ${projectName}`);\n\n // Check if this is a subagent session - skip for subagents\n const isSubagent = process.env.CLAUDE_AGENT_TYPE !== undefined ||\n (process.env.CLAUDE_PROJECT_DIR || '').includes('/.claude/agents/');\n\n if (isSubagent) {\n console.error('Subagent session - skipping project context setup');\n process.exit(0);\n }\n\n // 1. Find and READ all CLAUDE.md files - inject them into context\n // This ensures Claude actually processes the instructions, not just sees them in headers\n const claudeMdPaths = findAllClaudeMdPaths(cwd);\n const claudeMdContents: { path: string; content: string }[] = [];\n\n if (claudeMdPaths.length > 0) {\n console.error(`Found ${claudeMdPaths.length} CLAUDE.md file(s):`);\n for (const path of claudeMdPaths) {\n console.error(` - ${path}`);\n try {\n const content = readFileSync(path, 'utf-8');\n claudeMdContents.push({ path, content });\n console.error(` Read ${content.length} chars`);\n } catch (error) {\n console.error(` Could not read: ${error}`);\n }\n }\n } else {\n console.error('No CLAUDE.md found in project');\n console.error(' Consider creating one at ./CLAUDE.md or ./.claude/CLAUDE.md');\n }\n\n // 2. Find or create Notes directory\n // Priority:\n // 1. Active session routing (pai route <project>) \u2192 routed Obsidian path\n // 2. Local Notes/ in cwd \u2192 use it (git-trackable, e.g. symlink to Obsidian)\n // 3. Central ~/.claude/projects/.../Notes/ \u2192 fallback\n const routedPath = getRoutedNotesPath();\n let notesDir: string;\n\n if (routedPath) {\n // Routing is active - use the configured Obsidian Notes path\n const { mkdirSync } = await import('fs');\n if (!existsSync(routedPath)) {\n mkdirSync(routedPath, { recursive: true });\n console.error(`Created routed Notes: ${routedPath}`);\n } else {\n console.error(`Notes directory: ${routedPath} (routed via pai route)`);\n }\n notesDir = routedPath;\n } else {\n const notesInfo = findNotesDir(cwd);\n\n if (notesInfo.isLocal) {\n notesDir = notesInfo.path;\n console.error(`Notes directory: ${notesDir} (local)`);\n } else {\n // Create central Notes directory\n if (!existsSync(notesInfo.path)) {\n const { mkdirSync } = await import('fs');\n mkdirSync(notesInfo.path, { recursive: true });\n console.error(`Created central Notes: ${notesInfo.path}`);\n } else {\n console.error(`Notes directory: ${notesInfo.path} (central)`);\n }\n notesDir = notesInfo.path;\n }\n }\n\n // 3. Cleanup old .jsonl files from project root (move to sessions/)\n // Keep the newest one for potential resume, move older ones to sessions/\n const projectDir = getProjectDir(cwd);\n if (existsSync(projectDir)) {\n try {\n const files = readdirSync(projectDir);\n const jsonlFiles = files\n .filter(f => f.endsWith('.jsonl'))\n .map(f => ({\n name: f,\n path: join(projectDir, f),\n mtime: statSync(join(projectDir, f)).mtime.getTime()\n }))\n .sort((a, b) => b.mtime - a.mtime); // newest first\n\n if (jsonlFiles.length > 1) {\n const { mkdirSync, renameSync } = await import('fs');\n const sessionsDir = join(projectDir, 'sessions');\n if (!existsSync(sessionsDir)) {\n mkdirSync(sessionsDir, { recursive: true });\n }\n\n // Move all except the newest\n for (let i = 1; i < jsonlFiles.length; i++) {\n const file = jsonlFiles[i];\n const destPath = join(sessionsDir, file.name);\n if (!existsSync(destPath)) {\n renameSync(file.path, destPath);\n console.error(`Moved old session: ${file.name} \u2192 sessions/`);\n }\n }\n }\n } catch (error) {\n console.error(`Could not cleanup old .jsonl files: ${error}`);\n }\n }\n\n // 4. Find or create TODO.md\n const todoPath = findTodoPath(cwd);\n const hasTodo = existsSync(todoPath);\n if (hasTodo) {\n console.error(`TODO.md: ${todoPath}`);\n } else {\n // Create TODO.md in the Notes directory\n const newTodoPath = join(notesDir, 'TODO.md');\n const { writeFileSync } = await import('fs');\n writeFileSync(newTodoPath, `# TODO\\n\\n## Offen\\n\\n- [ ] \\n\\n---\\n\\n*Created: ${new Date().toISOString()}*\\n`);\n console.error(`Created TODO.md: ${newTodoPath}`);\n }\n\n // 5. Check for existing note or create new one\n let activeNotePath: string | null = null;\n\n if (notesDir) { // notesDir is always set now (local or central)\n const currentNotePath = getCurrentNotePath(notesDir);\n\n // Only create a new note if there is truly no note at all.\n // A completed note is still used \u2014 it will be updated or continued.\n // This prevents duplicate notes at month boundaries and on every compaction.\n if (!currentNotePath) {\n // Defensive: ensure projectName is a usable string\n const safeProjectName = (typeof projectName === 'string' && projectName.trim().length > 0)\n ? projectName.trim()\n : 'Untitled Session';\n console.error('\\nNo previous session notes found - creating new one');\n activeNotePath = createSessionNote(notesDir, String(safeProjectName));\n console.error(`Created: ${basename(activeNotePath)}`);\n } else {\n activeNotePath = currentNotePath!;\n console.error(`\\nUsing existing session note: ${basename(activeNotePath)}`);\n // Show preview of current note\n try {\n const content = readFileSync(activeNotePath, 'utf-8');\n const lines = content.split('\\n').slice(0, 12);\n console.error('--- Current Note Preview ---');\n for (const line of lines) {\n console.error(line);\n }\n console.error('--- End Preview ---\\n');\n } catch {\n // Ignore read errors\n }\n }\n }\n\n // 6. Show TODO.md preview\n if (existsSync(todoPath)) {\n try {\n const todoContent = readFileSync(todoPath, 'utf-8');\n const todoLines = todoContent.split('\\n').filter(l => l.includes('[ ]')).slice(0, 5);\n if (todoLines.length > 0) {\n console.error('\\nOpen TODOs:');\n for (const line of todoLines) {\n console.error(` ${line.trim()}`);\n }\n }\n } catch {\n // Ignore read errors\n }\n }\n\n // 7. Send ntfy.sh notification (MANDATORY)\n await sendNtfyNotification(`Session started in ${projectName}`);\n\n // 7.5. Run pai project detect to identify the registered PAI project\n const paiBin = findPaiBinary();\n let paiProjectBlock = '';\n try {\n const { execFileSync } = await import('child_process');\n const raw = execFileSync(paiBin, ['project', 'detect', '--json', cwd], {\n encoding: 'utf-8',\n env: process.env,\n }).trim();\n\n if (raw) {\n const detected = JSON.parse(raw) as {\n slug?: string;\n display_name?: string;\n root_path?: string;\n match_type?: string;\n relative_path?: string | null;\n session_count?: number;\n status?: string;\n error?: string;\n cwd?: string;\n };\n\n /**\n * Attempt to auto-register the CWD as a new PAI project.\n * Calls `pai project add <cwd>`, then re-detects to confirm registration.\n * Returns true if registration succeeded (or was attempted and project add ran),\n * and sets paiProjectBlock as a side effect on success.\n */\n const tryAutoRegister = async (): Promise<boolean> => {\n if (isGuardedPath(cwd) || !hasProjectSignals(cwd)) return false;\n\n try {\n execFileSync(paiBin, ['project', 'add', cwd], {\n encoding: 'utf-8',\n env: process.env,\n });\n console.error(`PAI auto-registered project at: ${cwd}`);\n\n // Re-run detect to confirm registration\n try {\n const raw2 = execFileSync(paiBin, ['project', 'detect', '--json', cwd], {\n encoding: 'utf-8',\n env: process.env,\n }).trim();\n\n if (raw2) {\n const detected2 = JSON.parse(raw2) as typeof detected;\n if (detected2.slug) {\n const name2 = detected2.display_name || detected2.slug;\n console.error(`PAI auto-registered: \"${detected2.slug}\" (${detected2.match_type})`);\n paiProjectBlock = `PAI Project Registry: ${name2} (slug: ${detected2.slug}) [AUTO-REGISTERED]\nMatch: ${detected2.match_type ?? 'exact'} | Sessions: 0`;\n return true;\n }\n }\n } catch (detectErr) {\n console.error('PAI auto-registration: project added but re-detect failed:', detectErr);\n return true; // project IS registered, just can't load context\n }\n } catch (addErr) {\n console.error('PAI auto-registration failed (project add):', addErr);\n }\n return false;\n };\n\n if (detected.error === 'no_match') {\n // Attempt auto-registration if the directory looks like a real project\n const autoRegistered = await tryAutoRegister();\n\n if (!autoRegistered) {\n paiProjectBlock = `PAI Project Registry: No registered project matches this directory.\nRun \"pai project add .\" to register this project, or use /route to tag the session.`;\n console.error('PAI detect: no match for', cwd);\n }\n } else if (\n detected.match_type === 'parent' &&\n detected.relative_path &&\n !isGuardedPath(cwd) &&\n hasProjectSignals(cwd)\n ) {\n // The CWD is inside a broader registered parent (e.g. \"i052341\" or \"apps\"),\n // but it has its own project signals \u2014 register it as a distinct project.\n console.error(\n `PAI detect: parent match to \"${detected.slug}\" via relative path \"${detected.relative_path}\" \u2014 CWD looks like its own project, attempting auto-registration`\n );\n const autoRegistered = await tryAutoRegister();\n\n if (!autoRegistered) {\n // Fall through: show the parent match as normal\n const name = detected.display_name || detected.slug;\n const nameSlug = ` (slug: ${detected.slug})`;\n const matchDesc = `parent (+${detected.relative_path ?? ''})`;\n const statusFlag = detected.status && detected.status !== 'active'\n ? ` [${detected.status.toUpperCase()}]`\n : '';\n paiProjectBlock = `PAI Project Registry: ${name}${statusFlag}${nameSlug}\nMatch: ${matchDesc} | Sessions: ${detected.session_count ?? 0}${detected.status && detected.status !== 'active' ? `\\nWARNING: Project status is \"${detected.status}\". Run: pai project health --fix` : ''}`;\n console.error(`PAI detect: kept parent match \"${detected.slug}\" (auto-register not applicable)`);\n }\n } else if (detected.slug) {\n const name = detected.display_name || detected.slug;\n const nameSlug = ` (slug: ${detected.slug})`;\n const matchDesc = detected.match_type === 'exact'\n ? 'exact'\n : `parent (+${detected.relative_path ?? ''})`;\n const statusFlag = detected.status && detected.status !== 'active'\n ? ` [${detected.status.toUpperCase()}]`\n : '';\n paiProjectBlock = `PAI Project Registry: ${name}${statusFlag}${nameSlug}\nMatch: ${matchDesc} | Sessions: ${detected.session_count ?? 0}${detected.status && detected.status !== 'active' ? `\\nWARNING: Project status is \"${detected.status}\". Run: pai project health --fix` : ''}`;\n console.error(`PAI detect: matched \"${detected.slug}\" (${detected.match_type})`);\n }\n }\n } catch (e) {\n // Non-fatal \u2014 don't break session start if pai is unavailable\n console.error('pai project detect failed:', e);\n }\n\n // 8. Output system reminder with session info\n const reminder = `\n<system-reminder>\nPROJECT CONTEXT LOADED\n\nProject: ${projectName}\nWorking Directory: ${cwd}\n${notesDir ? `Notes Directory: ${notesDir}${routedPath ? ' (routed via pai route)' : ''}` : 'Notes: disabled (no local Notes/ directory)'}\n${hasTodo ? `TODO: ${todoPath}` : 'TODO: not found'}\n${claudeMdPaths.length > 0 ? `CLAUDE.md: ${claudeMdPaths.join(', ')}` : 'No CLAUDE.md found'}\n${activeNotePath ? `Active Note: ${basename(activeNotePath)}` : ''}\n${routedPath ? `\\nNote Routing: ACTIVE (pai route is set - notes go to Obsidian vault)` : ''}\n${paiProjectBlock ? `\\n${paiProjectBlock}` : ''}\nSession Commands:\n- \"pause session\" \u2192 Save checkpoint, update TODO, exit (no compact)\n- \"end session\" \u2192 Finalize note, commit if needed, start fresh next time\n- \"pai route clear\" \u2192 Clear note routing (in a new session)\n</system-reminder>\n`;\n\n // Output to stdout for Claude to receive\n console.log(reminder);\n\n // 9. INJECT CLAUDE.md contents as system-reminders\n // This ensures Claude actually reads and processes the instructions\n for (const { path, content } of claudeMdContents) {\n const claudeMdReminder = `\n<system-reminder>\nLOCAL CLAUDE.md LOADED (MANDATORY - READ AND FOLLOW)\n\nSource: ${path}\n\n${content}\n\n---\nTHE ABOVE INSTRUCTIONS ARE MANDATORY. Follow them exactly.\n</system-reminder>\n`;\n console.log(claudeMdReminder);\n console.error(`Injected CLAUDE.md content from: ${path}`);\n }\n\n // 10. Inject wake-up context (L0 identity + L1 essential story) if available\n // The detected PAI project root_path is used for L1 note lookup.\n // We derive it from the `paiProjectBlock` detection result by re-running a\n // lightweight registry lookup, or by falling back to cwd for local-notes projects.\n try {\n // Attempt to find the project root path from the registry via `pai project detect --json`\n let wakeupRootPath: string | undefined;\n try {\n const { execFileSync: efs } = await import('child_process');\n const raw2 = efs(paiBin, ['project', 'detect', '--json', cwd], {\n encoding: 'utf-8',\n env: process.env,\n }).trim();\n if (raw2) {\n const det = JSON.parse(raw2) as { root_path?: string; slug?: string };\n if (det.root_path) wakeupRootPath = det.root_path;\n }\n } catch {\n // Non-fatal \u2014 fall back to cwd\n wakeupRootPath = cwd;\n }\n\n const wakeupBlock = buildWakeupContext(wakeupRootPath);\n if (wakeupBlock) {\n const wakeupReminder = `\\n<system-reminder>\\nWAKEUP CONTEXT\\n\\n${wakeupBlock}\\n</system-reminder>\\n`;\n console.log(wakeupReminder);\n console.error('Injected wake-up context (L0+L1)');\n } else {\n console.error('No wake-up context to inject (no identity file or session notes)');\n }\n } catch (wakeupError) {\n // Non-fatal \u2014 don't block session start\n console.error('Wake-up context injection failed:', wakeupError);\n }\n\n console.error('\\nProject context setup complete\\n');\n process.exit(0);\n}\n\nmain().catch(error => {\n console.error('load-project-context.ts error:', error);\n process.exit(0); // Don't block session start\n});\n", "/**\n * Wake-up context system \u2014 progressive context loading inspired by mempalace.\n *\n * Layers:\n * L0 Identity (~100 tokens) \u2014 user identity from ~/.pai/identity.txt. Always loaded.\n * L1 Essential Story (~500-800t) \u2014 top session notes for the project, key lines extracted.\n * L2 On-Demand \u2014 triggered by topic queries (handled by memory_search).\n * L3 Deep Search \u2014 unlimited federated memory search (memory_search tool).\n */\n\nimport { existsSync, readdirSync, readFileSync } from \"node:fs\";\nimport { join, basename } from \"node:path\";\nimport { homedir } from \"node:os\";\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\n/** Maximum tokens for the L1 essential story block. Approx 4 chars/token. */\nconst L1_TOKEN_BUDGET = 800;\nconst L1_CHAR_BUDGET = L1_TOKEN_BUDGET * 4; // ~3200 chars\n\n/** Maximum session notes to scan when building L1. */\nconst L1_MAX_NOTES = 10;\n\n/** Sections to extract from session notes (in priority order). */\nconst EXTRACT_SECTIONS = [\n \"Work Done\",\n \"Key Decisions\",\n \"Next Steps\",\n \"Checkpoint\",\n];\n\n/** Identity file location. */\nconst IDENTITY_FILE = join(homedir(), \".pai\", \"identity.txt\");\n\n// ---------------------------------------------------------------------------\n// L0: Identity\n// ---------------------------------------------------------------------------\n\n/**\n * Load L0 identity from ~/.pai/identity.txt.\n * Returns the file content, or an empty string if the file does not exist.\n * Never throws.\n */\nexport function loadL0Identity(): string {\n if (!existsSync(IDENTITY_FILE)) return \"\";\n try {\n return readFileSync(IDENTITY_FILE, \"utf-8\").trim();\n } catch {\n return \"\";\n }\n}\n\n// ---------------------------------------------------------------------------\n// L1: Essential Story\n// ---------------------------------------------------------------------------\n\n/**\n * Find the Notes directory for a project given its root_path from the registry.\n * Checks local Notes/ first, then central ~/.claude/projects/... path.\n */\nfunction findNotesDirForProject(rootPath: string): string | null {\n // Check local Notes directories first\n const localCandidates = [\n join(rootPath, \"Notes\"),\n join(rootPath, \"notes\"),\n join(rootPath, \".claude\", \"Notes\"),\n ];\n for (const p of localCandidates) {\n if (existsSync(p)) return p;\n }\n\n // Fall back to central ~/.claude/projects/{encoded}/Notes\n const encoded = rootPath\n .replace(/\\//g, \"-\")\n .replace(/\\./g, \"-\")\n .replace(/ /g, \"-\");\n const centralNotes = join(\n homedir(),\n \".claude\",\n \"projects\",\n encoded,\n \"Notes\"\n );\n if (existsSync(centralNotes)) return centralNotes;\n\n return null;\n}\n\n/**\n * Recursively find all .md session note files in a Notes directory.\n * Handles both flat layout (Notes/*.md) and month-subdirectory layout\n * (Notes/YYYY/MM/*.md). Returns files sorted newest-first by filename\n * (note numbers are monotonically increasing, so lexicographic = newest-last,\n * so we reverse).\n */\nfunction findSessionNotes(notesDir: string): string[] {\n const result: string[] = [];\n\n const scanDir = (dir: string) => {\n if (!existsSync(dir)) return;\n let entries: string[];\n try {\n entries = readdirSync(dir, { withFileTypes: true } as Parameters<typeof readdirSync>[1] as any)\n .map((e: any) => ({ name: e.name, isDir: e.isDirectory() }));\n } catch {\n return;\n }\n\n for (const entry of entries as Array<{ name: string; isDir: boolean }>) {\n const fullPath = join(dir, entry.name);\n if (entry.isDir) {\n // Recurse into YYYY/MM subdirectories\n scanDir(fullPath);\n } else if (entry.name.match(/^\\d{3,4}[\\s_-].*\\.md$/)) {\n result.push(fullPath);\n }\n }\n };\n\n scanDir(notesDir);\n\n // Sort: extract leading number, highest = most recent\n result.sort((a, b) => {\n const numA = parseInt(basename(a).match(/^(\\d+)/)?.[1] ?? \"0\", 10);\n const numB = parseInt(basename(b).match(/^(\\d+)/)?.[1] ?? \"0\", 10);\n return numB - numA; // newest first\n });\n\n return result;\n}\n\n/**\n * Extract the most important lines from a session note.\n * Prioritises: Work Done items, Key Decisions, Next Steps, Checkpoint headings.\n * Returns a condensed string under maxChars.\n */\nfunction extractKeyLines(content: string, maxChars: number): string {\n const lines = content.split(\"\\n\");\n const selected: string[] = [];\n let inTargetSection = false;\n let currentSection = \"\";\n let charCount = 0;\n\n // First pass: collect lines from priority sections\n for (const line of lines) {\n // Detect section headers\n const h2Match = line.match(/^## (.+)$/);\n const h3Match = line.match(/^### (.+)$/);\n if (h2Match) {\n currentSection = h2Match[1];\n inTargetSection = EXTRACT_SECTIONS.some((s) =>\n currentSection.toLowerCase().includes(s.toLowerCase())\n );\n continue;\n }\n if (h3Match) {\n // Checkpoints / sub-sections \u2014 include heading as label\n if (inTargetSection) {\n const label = `[${h3Match[1]}]`;\n if (charCount + label.length < maxChars) {\n selected.push(label);\n charCount += label.length + 1;\n }\n }\n continue;\n }\n\n if (!inTargetSection) continue;\n\n // Skip blank lines and HTML comments\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\"<!--\") || trimmed === \"---\") continue;\n\n // Include checkbox items, bold text, and plain text lines\n if (\n trimmed.startsWith(\"- \") ||\n trimmed.startsWith(\"* \") ||\n trimmed.match(/^\\d+\\./) ||\n trimmed.startsWith(\"**\")\n ) {\n if (charCount + trimmed.length + 1 > maxChars) break;\n selected.push(trimmed);\n charCount += trimmed.length + 1;\n }\n }\n\n return selected.join(\"\\n\");\n}\n\n/**\n * Build the L1 essential story block.\n *\n * Reads the most recent session notes for the project and extracts the key\n * lines (Work Done, Key Decisions, Next Steps) within the token budget.\n *\n * @param rootPath The project root path (from the registry).\n * @param tokenBudget Max tokens to consume. Default 800 (~3200 chars).\n * @returns Formatted L1 block, or empty string if no notes found.\n */\nexport function buildL1EssentialStory(\n rootPath: string,\n tokenBudget = L1_TOKEN_BUDGET\n): string {\n const charBudget = tokenBudget * 4;\n const notesDir = findNotesDirForProject(rootPath);\n if (!notesDir) return \"\";\n\n const noteFiles = findSessionNotes(notesDir).slice(0, L1_MAX_NOTES);\n if (noteFiles.length === 0) return \"\";\n\n const sections: string[] = [];\n let remaining = charBudget;\n\n for (const noteFile of noteFiles) {\n if (remaining <= 50) break;\n\n let content: string;\n try {\n content = readFileSync(noteFile, \"utf-8\");\n } catch {\n continue;\n }\n\n // Extract the note date and title from the filename\n const name = basename(noteFile);\n const titleMatch = name.match(/^\\d+ - (\\d{4}-\\d{2}-\\d{2}) - (.+)\\.md$/);\n const dateLabel = titleMatch ? titleMatch[1] : \"\";\n const titleLabel = titleMatch\n ? titleMatch[2]\n : name.replace(/^\\d+ - /, \"\").replace(/\\.md$/, \"\");\n\n // Skip if nothing useful extracted from this note\n const perNoteChars = Math.min(remaining, Math.floor(charBudget / noteFiles.length) + 200);\n const extracted = extractKeyLines(content, perNoteChars);\n if (!extracted) continue;\n\n const noteBlock = `[${dateLabel} - ${titleLabel}]\\n${extracted}`;\n sections.push(noteBlock);\n remaining -= noteBlock.length + 1;\n }\n\n if (sections.length === 0) return \"\";\n\n return sections.join(\"\\n\\n\");\n}\n\n// ---------------------------------------------------------------------------\n// Combined: buildWakeupContext\n// ---------------------------------------------------------------------------\n\n/**\n * Build the combined wake-up context block (L0 + L1).\n *\n * Returns a formatted string suitable for injection as a system-reminder,\n * or an empty string if both layers are empty.\n *\n * @param rootPath Project root path for L1 note lookup. Optional.\n * @param tokenBudget L1 token budget. Default 800.\n */\nexport function buildWakeupContext(\n rootPath?: string,\n tokenBudget = L1_TOKEN_BUDGET\n): string {\n const identity = loadL0Identity();\n const essentialStory = rootPath\n ? buildL1EssentialStory(rootPath, tokenBudget)\n : \"\";\n\n if (!identity && !essentialStory) return \"\";\n\n const parts: string[] = [];\n\n if (identity) {\n parts.push(`## L0 Identity\\n\\n${identity}`);\n }\n\n if (essentialStory) {\n parts.push(`## L1 Essential Story\\n\\n${essentialStory}`);\n }\n\n return parts.join(\"\\n\\n\");\n}\n", "/**\n * Path utilities \u2014 encoding, Notes/Sessions directory discovery and creation.\n */\n\nimport { existsSync, mkdirSync, readdirSync, renameSync } from 'fs';\nimport { join, basename } from 'path';\nimport { PAI_DIR } from '../pai-paths.js';\n\n// Re-export PAI_DIR for consumers\nexport { PAI_DIR };\nexport const PROJECTS_DIR = join(PAI_DIR, 'projects');\n\n/**\n * Directories known to be automated health-check / probe sessions.\n * Hooks should exit early for these to avoid registry clutter and wasted work.\n */\nconst PROBE_CWD_PATTERNS = [\n '/CodexBar/ClaudeProbe',\n '/ClaudeProbe',\n];\n\n/**\n * Check if the current working directory belongs to a probe/health-check session.\n * Returns true if hooks should skip this session entirely.\n */\nexport function isProbeSession(cwd?: string): boolean {\n const dir = cwd || process.cwd();\n return PROBE_CWD_PATTERNS.some(pattern => dir.includes(pattern));\n}\n\n/**\n * Encode a path the same way Claude Code does:\n * - Replace / with -\n * - Replace . with -\n * - Replace space with -\n */\nexport function encodePath(path: string): string {\n return path\n .replace(/\\//g, '-')\n .replace(/\\./g, '-')\n .replace(/ /g, '-');\n}\n\n/** Get the project directory for a given working directory. */\nexport function getProjectDir(cwd: string): string {\n const encoded = encodePath(cwd);\n return join(PROJECTS_DIR, encoded);\n}\n\n/** Get the Notes directory for a project (central location). */\nexport function getNotesDir(cwd: string): string {\n return join(getProjectDir(cwd), 'Notes');\n}\n\n/**\n * Find Notes directory \u2014 checks local first, falls back to central.\n * Does NOT create the directory.\n */\nexport function findNotesDir(cwd: string): { path: string; isLocal: boolean } {\n const cwdBasename = basename(cwd).toLowerCase();\n if (cwdBasename === 'notes' && existsSync(cwd)) {\n return { path: cwd, isLocal: true };\n }\n\n const localPaths = [\n join(cwd, 'Notes'),\n join(cwd, 'notes'),\n join(cwd, '.claude', 'Notes'),\n ];\n\n for (const path of localPaths) {\n if (existsSync(path)) {\n return { path, isLocal: true };\n }\n }\n\n return { path: getNotesDir(cwd), isLocal: false };\n}\n\n/** Get the sessions/ directory for a project (stores .jsonl transcripts). */\nexport function getSessionsDir(cwd: string): string {\n return join(getProjectDir(cwd), 'sessions');\n}\n\n/** Get the sessions/ directory from a project directory path. */\nexport function getSessionsDirFromProjectDir(projectDir: string): string {\n return join(projectDir, 'sessions');\n}\n\n// ---------------------------------------------------------------------------\n// Directory creation helpers\n// ---------------------------------------------------------------------------\n\n/** Ensure the Notes directory exists for a project. @deprecated Use ensureNotesDirSmart() */\nexport function ensureNotesDir(cwd: string): string {\n const notesDir = getNotesDir(cwd);\n if (!existsSync(notesDir)) {\n mkdirSync(notesDir, { recursive: true });\n console.error(`Created Notes directory: ${notesDir}`);\n }\n return notesDir;\n}\n\n/**\n * Smart Notes directory handling:\n * - If local Notes/ exists \u2192 use it (don't create anything new)\n * - If no local Notes/ \u2192 ensure central exists and use that\n */\nexport function ensureNotesDirSmart(cwd: string): { path: string; isLocal: boolean } {\n const found = findNotesDir(cwd);\n if (found.isLocal) return found;\n if (!existsSync(found.path)) {\n mkdirSync(found.path, { recursive: true });\n console.error(`Created central Notes directory: ${found.path}`);\n }\n return found;\n}\n\n/** Ensure the sessions/ directory exists for a project. */\nexport function ensureSessionsDir(cwd: string): string {\n const sessionsDir = getSessionsDir(cwd);\n if (!existsSync(sessionsDir)) {\n mkdirSync(sessionsDir, { recursive: true });\n console.error(`Created sessions directory: ${sessionsDir}`);\n }\n return sessionsDir;\n}\n\n/** Ensure the sessions/ directory exists (from project dir path). */\nexport function ensureSessionsDirFromProjectDir(projectDir: string): string {\n const sessionsDir = getSessionsDirFromProjectDir(projectDir);\n if (!existsSync(sessionsDir)) {\n mkdirSync(sessionsDir, { recursive: true });\n console.error(`Created sessions directory: ${sessionsDir}`);\n }\n return sessionsDir;\n}\n\n/**\n * Move all .jsonl session files from project root to sessions/ subdirectory.\n * Returns the number of files moved.\n */\nexport function moveSessionFilesToSessionsDir(\n projectDir: string,\n excludeFile?: string,\n silent = false\n): number {\n const sessionsDir = ensureSessionsDirFromProjectDir(projectDir);\n\n if (!existsSync(projectDir)) return 0;\n\n const files = readdirSync(projectDir);\n let movedCount = 0;\n\n for (const file of files) {\n if (file.endsWith('.jsonl') && file !== excludeFile) {\n const sourcePath = join(projectDir, file);\n const destPath = join(sessionsDir, file);\n try {\n renameSync(sourcePath, destPath);\n if (!silent) console.error(`Moved ${file} \u2192 sessions/`);\n movedCount++;\n } catch (error) {\n if (!silent) console.error(`Could not move ${file}: ${error}`);\n }\n }\n }\n\n return movedCount;\n}\n\n// ---------------------------------------------------------------------------\n// CLAUDE.md / TODO.md discovery\n// ---------------------------------------------------------------------------\n\n/** Find TODO.md \u2014 check local first, fallback to central. */\nexport function findTodoPath(cwd: string): string {\n const localPaths = [\n join(cwd, 'TODO.md'),\n join(cwd, 'notes', 'TODO.md'),\n join(cwd, 'Notes', 'TODO.md'),\n join(cwd, '.claude', 'TODO.md'),\n ];\n\n for (const path of localPaths) {\n if (existsSync(path)) return path;\n }\n\n return join(getNotesDir(cwd), 'TODO.md');\n}\n\n/** Find CLAUDE.md \u2014 returns the FIRST found path. */\nexport function findClaudeMdPath(cwd: string): string | null {\n const paths = findAllClaudeMdPaths(cwd);\n return paths.length > 0 ? paths[0] : null;\n}\n\n/**\n * Find ALL CLAUDE.md files in local locations in priority order.\n */\nexport function findAllClaudeMdPaths(cwd: string): string[] {\n const foundPaths: string[] = [];\n\n const localPaths = [\n join(cwd, '.claude', 'CLAUDE.md'),\n join(cwd, 'CLAUDE.md'),\n join(cwd, 'Notes', 'CLAUDE.md'),\n join(cwd, 'notes', 'CLAUDE.md'),\n join(cwd, 'Prompts', 'CLAUDE.md'),\n join(cwd, 'prompts', 'CLAUDE.md'),\n ];\n\n for (const path of localPaths) {\n if (existsSync(path)) foundPaths.push(path);\n }\n\n return foundPaths;\n}\n", "/**\n * PAI Path Resolution - Single Source of Truth\n *\n * This module provides consistent path resolution across all PAI hooks.\n * It handles PAI_DIR detection whether set explicitly or defaulting to ~/.claude\n *\n * ALSO loads .env file from PAI_DIR so all hooks get environment variables\n * without relying on Claude Code's settings.json injection.\n *\n * Usage in hooks:\n * import { PAI_DIR, HOOKS_DIR, SKILLS_DIR } from './lib/pai-paths';\n */\n\nimport { homedir } from 'os';\nimport { resolve, join } from 'path';\nimport { existsSync, readFileSync } from 'fs';\n\n/**\n * Load .env file and inject into process.env\n * Must run BEFORE PAI_DIR resolution so .env can set PAI_DIR if needed\n */\nfunction loadEnvFile(): void {\n // Check common locations for .env\n const possiblePaths = [\n resolve(process.env.PAI_DIR || '', '.env'),\n resolve(homedir(), '.claude', '.env'),\n ];\n\n for (const envPath of possiblePaths) {\n if (existsSync(envPath)) {\n try {\n const content = readFileSync(envPath, 'utf-8');\n for (const line of content.split('\\n')) {\n const trimmed = line.trim();\n // Skip comments and empty lines\n if (!trimmed || trimmed.startsWith('#')) continue;\n\n const eqIndex = trimmed.indexOf('=');\n if (eqIndex > 0) {\n const key = trimmed.substring(0, eqIndex).trim();\n let value = trimmed.substring(eqIndex + 1).trim();\n\n // Remove surrounding quotes if present\n if ((value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n value = value.slice(1, -1);\n }\n\n // Expand $HOME and ~ in values\n value = value.replace(/\\$HOME/g, homedir());\n value = value.replace(/^~(?=\\/|$)/, homedir());\n\n // Only set if not already defined (env vars take precedence)\n if (process.env[key] === undefined) {\n process.env[key] = value;\n }\n }\n }\n // Found and loaded, don't check other paths\n break;\n } catch {\n // Silently continue if .env can't be read\n }\n }\n }\n}\n\n// Load .env FIRST, before any other initialization\nloadEnvFile();\n\n/**\n * Smart PAI_DIR detection with fallback\n * Priority:\n * 1. PAI_DIR environment variable (if set)\n * 2. ~/.claude (standard location)\n */\nexport const PAI_DIR = process.env.PAI_DIR\n ? resolve(process.env.PAI_DIR)\n : resolve(homedir(), '.claude');\n\n/**\n * Common PAI directories\n */\nexport const HOOKS_DIR = join(PAI_DIR, 'Hooks');\nexport const SKILLS_DIR = join(PAI_DIR, 'Skills');\nexport const AGENTS_DIR = join(PAI_DIR, 'Agents');\nexport const HISTORY_DIR = join(PAI_DIR, 'History');\nexport const COMMANDS_DIR = join(PAI_DIR, 'Commands');\n\n/**\n * Validate PAI directory structure on first import\n * This fails fast with a clear error if PAI is misconfigured\n */\nfunction validatePAIStructure(): void {\n if (!existsSync(PAI_DIR)) {\n console.error(`PAI_DIR does not exist: ${PAI_DIR}`);\n console.error(` Expected ~/.claude or set PAI_DIR environment variable`);\n process.exit(1);\n }\n\n if (!existsSync(HOOKS_DIR)) {\n console.error(`PAI hooks directory not found: ${HOOKS_DIR}`);\n console.error(` Your PAI_DIR may be misconfigured`);\n console.error(` Current PAI_DIR: ${PAI_DIR}`);\n process.exit(1);\n }\n}\n\n// Run validation on module import\n// This ensures any hook that imports this module will fail fast if paths are wrong\nvalidatePAIStructure();\n\n/**\n * Helper to get history file path with date-based organization\n */\nexport function getHistoryFilePath(subdir: string, filename: string): string {\n const now = new Date();\n const tz = process.env.TIME_ZONE || Intl.DateTimeFormat().resolvedOptions().timeZone;\n const localDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));\n const year = localDate.getFullYear();\n const month = String(localDate.getMonth() + 1).padStart(2, '0');\n\n return join(HISTORY_DIR, subdir, `${year}-${month}`, filename);\n}\n", "/**\n * Push notification helpers \u2014 WhatsApp-aware with ntfy.sh fallback.\n */\n\nimport { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\n\n/**\n * Check if a messaging MCP server (AIBroker, Whazaa, or Telex) is configured.\n * When any messaging server is active, the AI handles notifications via MCP\n * and ntfy is skipped to avoid duplicates.\n */\nexport function isWhatsAppEnabled(): boolean {\n try {\n const settingsPath = join(homedir(), '.claude', 'settings.json');\n if (!existsSync(settingsPath)) return false;\n\n const settings = JSON.parse(readFileSync(settingsPath, 'utf-8'));\n const enabled: string[] = settings.enabledMcpjsonServers || [];\n return enabled.includes('aibroker') || enabled.includes('whazaa') || enabled.includes('telex');\n } catch {\n return false;\n }\n}\n\n/**\n * Send push notification \u2014 WhatsApp-aware with ntfy fallback.\n *\n * When WhatsApp (Whazaa) is enabled in MCP config, ntfy is SKIPPED\n * because the AI sends WhatsApp messages directly via MCP.\n * When WhatsApp is NOT configured, ntfy fires as the fallback channel.\n */\nexport async function sendNtfyNotification(message: string, retries = 2): Promise<boolean> {\n if (isWhatsAppEnabled()) {\n console.error(`WhatsApp (Whazaa) enabled in MCP config \u2014 skipping ntfy`);\n return true;\n }\n\n const topic = process.env.NTFY_TOPIC;\n\n if (!topic) {\n console.error('NTFY_TOPIC not set and WhatsApp not active \u2014 notifications disabled');\n return false;\n }\n\n for (let attempt = 0; attempt <= retries; attempt++) {\n try {\n const response = await fetch(`https://ntfy.sh/${topic}`, {\n method: 'POST',\n body: message,\n headers: {\n 'Title': 'Claude Code',\n 'Priority': 'default',\n },\n });\n\n if (response.ok) {\n console.error(`ntfy.sh notification sent (WhatsApp inactive): \"${message}\"`);\n return true;\n } else {\n console.error(`ntfy.sh attempt ${attempt + 1} failed: ${response.status}`);\n }\n } catch (error) {\n console.error(`ntfy.sh attempt ${attempt + 1} error: ${error}`);\n }\n\n if (attempt < retries) {\n await new Promise(resolve => setTimeout(resolve, 1000));\n }\n }\n\n console.error('ntfy.sh notification failed after all retries');\n return false;\n}\n", "/**\n * Session note creation, editing, checkpointing, renaming, and finalization.\n */\n\nimport { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync, renameSync } from 'fs';\nimport { join, basename } from 'path';\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/** Get or create the YYYY/MM subdirectory for the current month inside notesDir. */\nfunction getMonthDir(notesDir: string): string {\n const now = new Date();\n const year = String(now.getFullYear());\n const month = String(now.getMonth() + 1).padStart(2, '0');\n const monthDir = join(notesDir, year, month);\n if (!existsSync(monthDir)) {\n mkdirSync(monthDir, { recursive: true });\n }\n return monthDir;\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\n/**\n * Get the next note number (4-digit format: 0001, 0002, etc.).\n * Numbers are scoped per YYYY/MM directory.\n */\nexport function getNextNoteNumber(notesDir: string): string {\n const monthDir = getMonthDir(notesDir);\n\n const files = readdirSync(monthDir)\n .filter(f => f.match(/^\\d{3,4}[\\s_-]/))\n .sort();\n\n if (files.length === 0) return '0001';\n\n let maxNumber = 0;\n for (const file of files) {\n const digitMatch = file.match(/^(\\d+)/);\n if (digitMatch) {\n const num = parseInt(digitMatch[1], 10);\n if (num > maxNumber) maxNumber = num;\n }\n }\n\n return String(maxNumber + 1).padStart(4, '0');\n}\n\n/**\n * Get the current (latest) note file path, or null if none exists.\n * Searches current month \u2192 previous month \u2192 flat notesDir (legacy).\n */\nexport function getCurrentNotePath(notesDir: string): string | null {\n if (!existsSync(notesDir)) return null;\n\n const findLatestIn = (dir: string): string | null => {\n if (!existsSync(dir)) return null;\n const files = readdirSync(dir)\n .filter(f => f.match(/^\\d{3,4}[\\s_-].*\\.md$/))\n .sort((a, b) => {\n const numA = parseInt(a.match(/^(\\d+)/)?.[1] || '0', 10);\n const numB = parseInt(b.match(/^(\\d+)/)?.[1] || '0', 10);\n return numA - numB;\n });\n if (files.length === 0) return null;\n return join(dir, files[files.length - 1]);\n };\n\n const now = new Date();\n const year = String(now.getFullYear());\n const month = String(now.getMonth() + 1).padStart(2, '0');\n const currentMonthDir = join(notesDir, year, month);\n const found = findLatestIn(currentMonthDir);\n if (found) return found;\n\n const prevDate = new Date(now.getFullYear(), now.getMonth() - 1, 1);\n const prevYear = String(prevDate.getFullYear());\n const prevMonth = String(prevDate.getMonth() + 1).padStart(2, '0');\n const prevMonthDir = join(notesDir, prevYear, prevMonth);\n const prevFound = findLatestIn(prevMonthDir);\n if (prevFound) return prevFound;\n\n return findLatestIn(notesDir);\n}\n\n/**\n * Create a new session note.\n * Format: \"NNNN - YYYY-MM-DD - New Session.md\" filed into YYYY/MM subdirectory.\n * Claude MUST rename at session end with a meaningful description.\n */\nexport function createSessionNote(notesDir: string, description: string): string {\n const noteNumber = getNextNoteNumber(notesDir);\n const date = new Date().toISOString().split('T')[0];\n const monthDir = getMonthDir(notesDir);\n const filename = `${noteNumber} - ${date} - New Session.md`;\n const filepath = join(monthDir, filename);\n\n const content = `# Session ${noteNumber}: ${description}\n\n**Date:** ${date}\n**Status:** In Progress\n\n---\n\n## Work Done\n\n<!-- PAI will add completed work here during session -->\n\n---\n\n## Next Steps\n\n<!-- To be filled at session end -->\n\n---\n\n**Tags:** #Session\n`;\n\n writeFileSync(filepath, content);\n console.error(`Created session note: ${filename}`);\n\n return filepath;\n}\n\n/** Append a checkpoint to the current session note. */\nexport function appendCheckpoint(notePath: string, checkpoint: string): void {\n if (!existsSync(notePath)) {\n console.error(`Note file not found, recreating: ${notePath}`);\n try {\n const parentDir = join(notePath, '..');\n if (!existsSync(parentDir)) mkdirSync(parentDir, { recursive: true });\n const noteFilename = basename(notePath);\n const numberMatch = noteFilename.match(/^(\\d+)/);\n const noteNumber = numberMatch ? numberMatch[1] : '0000';\n const date = new Date().toISOString().split('T')[0];\n const content = `# Session ${noteNumber}: Recovered\\n\\n**Date:** ${date}\\n**Status:** In Progress\\n\\n---\\n\\n## Work Done\\n\\n<!-- PAI will add completed work here during session -->\\n\\n---\\n\\n## Next Steps\\n\\n<!-- To be filled at session end -->\\n\\n---\\n\\n**Tags:** #Session\\n`;\n writeFileSync(notePath, content);\n console.error(`Recreated session note: ${noteFilename}`);\n } catch (err) {\n console.error(`Failed to recreate note: ${err}`);\n return;\n }\n }\n\n const content = readFileSync(notePath, 'utf-8');\n const timestamp = new Date().toISOString();\n const checkpointText = `\\n### Checkpoint ${timestamp}\\n\\n${checkpoint}\\n`;\n\n const nextStepsIndex = content.indexOf('## Next Steps');\n const newContent = nextStepsIndex !== -1\n ? content.substring(0, nextStepsIndex) + checkpointText + content.substring(nextStepsIndex)\n : content + checkpointText;\n\n writeFileSync(notePath, newContent);\n console.error(`Checkpoint added to: ${basename(notePath)}`);\n}\n\n/** Work item for session notes. */\nexport interface WorkItem {\n title: string;\n details?: string[];\n completed?: boolean;\n}\n\n/** Add work items to the \"Work Done\" section of a session note. */\nexport function addWorkToSessionNote(notePath: string, workItems: WorkItem[], sectionTitle?: string): void {\n if (!existsSync(notePath)) {\n console.error(`Note file not found: ${notePath}`);\n return;\n }\n\n let content = readFileSync(notePath, 'utf-8');\n\n let workText = '';\n if (sectionTitle) workText += `\\n### ${sectionTitle}\\n\\n`;\n\n for (const item of workItems) {\n const checkbox = item.completed !== false ? '[x]' : '[ ]';\n workText += `- ${checkbox} **${item.title}**\\n`;\n if (item.details && item.details.length > 0) {\n for (const detail of item.details) {\n workText += ` - ${detail}\\n`;\n }\n }\n }\n\n const workDoneMatch = content.match(/## Work Done\\n\\n(<!-- .*? -->)?/);\n if (workDoneMatch) {\n const insertPoint = content.indexOf(workDoneMatch[0]) + workDoneMatch[0].length;\n content = content.substring(0, insertPoint) + workText + content.substring(insertPoint);\n } else {\n const nextStepsIndex = content.indexOf('## Next Steps');\n if (nextStepsIndex !== -1) {\n content = content.substring(0, nextStepsIndex) + workText + '\\n' + content.substring(nextStepsIndex);\n }\n }\n\n writeFileSync(notePath, content);\n console.error(`Added ${workItems.length} work item(s) to: ${basename(notePath)}`);\n}\n\n/**\n * Check if a candidate title is meaningless / garbage.\n * Public wrapper around the internal filter for use by other hooks.\n */\nexport function isMeaningfulTitle(text: string): boolean {\n return !isMeaninglessCandidate(text);\n}\n\n/** Sanitize a string for use in a filename. */\nexport function sanitizeForFilename(str: string): string {\n return str\n .toLowerCase()\n .replace(/[^a-z0-9\\s-]/g, '')\n .replace(/\\s+/g, '-')\n .replace(/-+/g, '-')\n .replace(/^-|-$/g, '')\n .substring(0, 50);\n}\n\n/**\n * Return true if the candidate string should be rejected as a meaningful name.\n * Rejects file paths, shebangs, timestamps, system noise, XML tags, hashes, etc.\n */\nfunction isMeaninglessCandidate(text: string): boolean {\n const t = text.trim();\n if (!t) return true;\n if (t.length < 5) return true; // too short to be meaningful\n if (t.startsWith('/') || t.startsWith('~')) return true; // file path\n if (t.startsWith('#!')) return true; // shebang\n if (t.includes('[object Object]')) return true; // serialization artifact\n if (/^\\d{4}-\\d{2}-\\d{2}(T[\\d:.Z+-]+)?$/.test(t)) return true; // ISO timestamp\n if (/^\\d{1,2}:\\d{2}(:\\d{2})?(\\s*(AM|PM))?$/i.test(t)) return true; // time-only\n if (/^<[a-z-]+[\\s/>]/i.test(t)) return true; // XML/HTML tags (<task-notification>, etc.)\n if (/^[0-9a-f]{10,}$/i.test(t)) return true; // hex hash strings\n if (/^Exit code \\d+/i.test(t)) return true; // exit code messages\n if (/^Error:/i.test(t)) return true; // error messages\n if (/^This session is being continued/i.test(t)) return true; // continuation boilerplate\n if (/^\\(Bash completed/i.test(t)) return true; // bash output noise\n if (/^Task Notification$/i.test(t)) return true; // literal \"Task Notification\"\n if (/^New Session$/i.test(t)) return true; // placeholder title\n if (/^Recovered Session$/i.test(t)) return true; // placeholder title\n if (/^Continued Session$/i.test(t)) return true; // placeholder title\n if (/^Untitled Session$/i.test(t)) return true; // placeholder title\n if (/^Context Compression$/i.test(t)) return true; // compression artifact\n if (/^[A-Fa-f0-9]{8,}\\s+Output$/i.test(t)) return true; // hash + \"Output\" pattern\n return false;\n}\n\n/**\n * Extract a meaningful name from session note content and summary.\n * Looks at Work Done section headers, bold text, and summary.\n */\nexport function extractMeaningfulName(noteContent: string, summary: string): string {\n const workDoneMatch = noteContent.match(/## Work Done\\n\\n([\\s\\S]*?)(?=\\n---|\\n## Next)/);\n\n if (workDoneMatch) {\n const workDoneSection = workDoneMatch[1];\n\n const subheadings = workDoneSection.match(/### ([^\\n]+)/g);\n if (subheadings && subheadings.length > 0) {\n const firstHeading = subheadings[0].replace('### ', '').trim();\n if (!isMeaninglessCandidate(firstHeading) && firstHeading.length > 5 && firstHeading.length < 60) {\n return sanitizeForFilename(firstHeading);\n }\n }\n\n const boldMatches = workDoneSection.match(/\\*\\*([^*]+)\\*\\*/g);\n if (boldMatches && boldMatches.length > 0) {\n const firstBold = boldMatches[0].replace(/\\*\\*/g, '').trim();\n if (!isMeaninglessCandidate(firstBold) && firstBold.length > 3 && firstBold.length < 50) {\n return sanitizeForFilename(firstBold);\n }\n }\n\n const numberedItems = workDoneSection.match(/^\\d+\\.\\s+\\*\\*([^*]+)\\*\\*/m);\n if (numberedItems && !isMeaninglessCandidate(numberedItems[1])) {\n return sanitizeForFilename(numberedItems[1]);\n }\n }\n\n if (summary && summary.length > 5 && summary !== 'Session completed.' && !isMeaninglessCandidate(summary)) {\n const cleanSummary = summary\n .replace(/[^\\w\\s-]/g, ' ')\n .trim()\n .split(/\\s+/)\n .slice(0, 5)\n .join(' ');\n if (cleanSummary.length > 3 && !isMeaninglessCandidate(cleanSummary)) {\n return sanitizeForFilename(cleanSummary);\n }\n }\n\n return '';\n}\n\n/**\n * Rename a session note with a meaningful name.\n * Always uses \"NNNN - YYYY-MM-DD - Description.md\" format.\n * Returns the new path, or original path if rename fails.\n */\nexport function renameSessionNote(notePath: string, meaningfulName: string): string {\n if (!meaningfulName || !existsSync(notePath)) return notePath;\n\n const dir = join(notePath, '..');\n const oldFilename = basename(notePath);\n\n const correctMatch = oldFilename.match(/^(\\d{3,4}) - (\\d{4}-\\d{2}-\\d{2}) - .*\\.md$/);\n const legacyMatch = oldFilename.match(/^(\\d{3,4})_(\\d{4}-\\d{2}-\\d{2})_.*\\.md$/);\n const match = correctMatch || legacyMatch;\n if (!match) return notePath;\n\n const [, noteNumber, date] = match;\n\n const titleCaseName = meaningfulName\n .split(/[\\s_-]+/)\n .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())\n .join(' ')\n .trim();\n\n const paddedNumber = noteNumber.padStart(4, '0');\n const newFilename = `${paddedNumber} - ${date} - ${titleCaseName}.md`;\n const newPath = join(dir, newFilename);\n\n if (newFilename === oldFilename) return notePath;\n\n try {\n renameSync(notePath, newPath);\n console.error(`Renamed note: ${oldFilename} \u2192 ${newFilename}`);\n return newPath;\n } catch (error) {\n console.error(`Could not rename note: ${error}`);\n return notePath;\n }\n}\n\n/** Update the session note's H1 title and rename the file. */\nexport function updateSessionNoteTitle(notePath: string, newTitle: string): void {\n if (!existsSync(notePath)) {\n console.error(`Note file not found: ${notePath}`);\n return;\n }\n\n let content = readFileSync(notePath, 'utf-8');\n content = content.replace(/^# Session \\d+:.*$/m, (match) => {\n const sessionNum = match.match(/Session (\\d+)/)?.[1] || '';\n return `# Session ${sessionNum}: ${newTitle}`;\n });\n writeFileSync(notePath, content);\n renameSessionNote(notePath, sanitizeForFilename(newTitle));\n}\n\n/**\n * Finalize session note \u2014 mark as complete, add summary, rename with meaningful name.\n * IDEMPOTENT: subsequent calls are no-ops if already finalized.\n * Returns the final path (may be renamed).\n */\nexport function finalizeSessionNote(notePath: string, summary: string): string {\n if (!existsSync(notePath)) {\n console.error(`Note file not found: ${notePath}`);\n return notePath;\n }\n\n let content = readFileSync(notePath, 'utf-8');\n\n if (content.includes('**Status:** Completed')) {\n console.error(`Note already finalized: ${basename(notePath)}`);\n return notePath;\n }\n\n content = content.replace('**Status:** In Progress', '**Status:** Completed');\n\n if (!content.includes('**Completed:**')) {\n const completionTime = new Date().toISOString();\n content = content.replace(\n '---\\n\\n## Work Done',\n `**Completed:** ${completionTime}\\n\\n---\\n\\n## Work Done`\n );\n }\n\n const nextStepsMatch = content.match(/## Next Steps\\n\\n(<!-- .*? -->)/);\n if (nextStepsMatch) {\n content = content.replace(\n nextStepsMatch[0],\n `## Next Steps\\n\\n${summary || 'Session completed.'}`\n );\n }\n\n writeFileSync(notePath, content);\n console.error(`Session note finalized: ${basename(notePath)}`);\n\n const meaningfulName = extractMeaningfulName(content, summary);\n if (meaningfulName) {\n return renameSessionNote(notePath, meaningfulName);\n }\n\n return notePath;\n}\n"],
|
|
5
|
+
"mappings": ";;;AAkBA,SAAS,cAAAA,aAAY,eAAAC,cAAa,gBAAAC,eAAc,gBAAgB;AAChE,SAAS,QAAAC,OAAM,YAAAC,WAAU,SAAS,WAAAC,gBAAe;AACjD,SAAS,WAAAC,gBAAe;AACxB,SAAS,gBAAgB;;;ACXzB,SAAS,YAAY,aAAa,oBAAoB;AACtD,SAAS,MAAM,gBAAgB;AAC/B,SAAS,eAAe;AAOxB,IAAM,kBAAkB;AACxB,IAAM,iBAAiB,kBAAkB;AAGzC,IAAM,eAAe;AAGrB,IAAM,mBAAmB;AAAA,EACvB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGA,IAAM,gBAAgB,KAAK,QAAQ,GAAG,QAAQ,cAAc;AAWrD,SAAS,iBAAyB;AACvC,MAAI,CAAC,WAAW,aAAa,EAAG,QAAO;AACvC,MAAI;AACF,WAAO,aAAa,eAAe,OAAO,EAAE,KAAK;AAAA,EACnD,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAUA,SAAS,uBAAuB,UAAiC;AAE/D,QAAM,kBAAkB;AAAA,IACtB,KAAK,UAAU,OAAO;AAAA,IACtB,KAAK,UAAU,OAAO;AAAA,IACtB,KAAK,UAAU,WAAW,OAAO;AAAA,EACnC;AACA,aAAW,KAAK,iBAAiB;AAC/B,QAAI,WAAW,CAAC,EAAG,QAAO;AAAA,EAC5B;AAGA,QAAM,UAAU,SACb,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,M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AK;AACrD,UAAQ,KAAK,CAAC;AAChB,CAAC;",
|
|
6
|
+
"names": ["existsSync", "readdirSync", "readFileSync", "join", "basename", "resolve", "homedir", "existsSync", "readdirSync", "join", "basename", "homedir", "join", "existsSync", "readFileSync", "join", "basename", "existsSync", "join", "existsSync", "join", "existsSync", "existsSync", "readFileSync", "join", "homedir", "resolve", "existsSync", "mkdirSync", "readdirSync", "readFileSync", "renameSync", "join", "basename", "existsSync", "join", "readFileSync", "homedir", "resolve", "basename", "mkdirSync", "readdirSync", "renameSync", "writeFileSync"]
|
|
7
7
|
}
|
package/dist/hooks/stop-hook.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/hooks/ts/stop/stop-hook.ts
|
|
4
|
-
import { readFileSync as readFileSync5 } from "fs";
|
|
5
|
-
import { basename as basename3, dirname } from "path";
|
|
4
|
+
import { readFileSync as readFileSync5, writeFileSync as writeFileSync3, mkdirSync as mkdirSync4, existsSync as existsSync6, unlinkSync } from "fs";
|
|
5
|
+
import { join as join6, basename as basename3, dirname } from "path";
|
|
6
6
|
import { connect } from "net";
|
|
7
7
|
import { randomUUID } from "crypto";
|
|
8
8
|
|
|
@@ -450,6 +450,64 @@ ${stateLines}
|
|
|
450
450
|
// src/hooks/ts/stop/stop-hook.ts
|
|
451
451
|
var DAEMON_SOCKET = process.env.PAI_SOCKET ?? "/tmp/pai.sock";
|
|
452
452
|
var DAEMON_TIMEOUT_MS = 3e3;
|
|
453
|
+
var AUTO_SAVE_INTERVAL = (() => {
|
|
454
|
+
const raw = process.env.PAI_AUTO_SAVE_INTERVAL;
|
|
455
|
+
if (raw) {
|
|
456
|
+
const n = parseInt(raw, 10);
|
|
457
|
+
if (!isNaN(n) && n > 0) return n;
|
|
458
|
+
}
|
|
459
|
+
return 15;
|
|
460
|
+
})();
|
|
461
|
+
var SESSION_STATE_DIR = join6(
|
|
462
|
+
process.env.HOME ?? "/tmp",
|
|
463
|
+
".config",
|
|
464
|
+
"pai",
|
|
465
|
+
"session-state"
|
|
466
|
+
);
|
|
467
|
+
function readSessionState(sessionId) {
|
|
468
|
+
try {
|
|
469
|
+
const stateFile = join6(SESSION_STATE_DIR, `${sessionId}.json`);
|
|
470
|
+
if (!existsSync6(stateFile)) return { humanMessageCount: 0 };
|
|
471
|
+
const raw = readFileSync5(stateFile, "utf-8");
|
|
472
|
+
const parsed = JSON.parse(raw);
|
|
473
|
+
return {
|
|
474
|
+
humanMessageCount: typeof parsed.humanMessageCount === "number" ? parsed.humanMessageCount : 0
|
|
475
|
+
};
|
|
476
|
+
} catch {
|
|
477
|
+
return { humanMessageCount: 0 };
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function writeSessionState(sessionId, state) {
|
|
481
|
+
try {
|
|
482
|
+
mkdirSync4(SESSION_STATE_DIR, { recursive: true });
|
|
483
|
+
const stateFile = join6(SESSION_STATE_DIR, `${sessionId}.json`);
|
|
484
|
+
writeFileSync3(stateFile, JSON.stringify(state, null, 2), "utf-8");
|
|
485
|
+
} catch (e) {
|
|
486
|
+
console.error(`STOP-HOOK: Could not write session state: ${e}`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function deleteSessionState(sessionId) {
|
|
490
|
+
try {
|
|
491
|
+
const stateFile = join6(SESSION_STATE_DIR, `${sessionId}.json`);
|
|
492
|
+
if (existsSync6(stateFile)) {
|
|
493
|
+
unlinkSync(stateFile);
|
|
494
|
+
}
|
|
495
|
+
} catch {
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
function countHumanMessages(lines) {
|
|
499
|
+
let count = 0;
|
|
500
|
+
for (const line of lines) {
|
|
501
|
+
try {
|
|
502
|
+
const entry = JSON.parse(line);
|
|
503
|
+
if (entry.type === "user" && entry.message?.role === "user") {
|
|
504
|
+
count++;
|
|
505
|
+
}
|
|
506
|
+
} catch {
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return count;
|
|
510
|
+
}
|
|
453
511
|
function contentToText(content) {
|
|
454
512
|
if (typeof content === "string") return content;
|
|
455
513
|
if (Array.isArray(content)) {
|
|
@@ -547,6 +605,60 @@ function enqueueWithDaemon(payload) {
|
|
|
547
605
|
}, DAEMON_TIMEOUT_MS);
|
|
548
606
|
});
|
|
549
607
|
}
|
|
608
|
+
function enqueueMidSessionSummaryWithDaemon(payload) {
|
|
609
|
+
return new Promise((resolve2) => {
|
|
610
|
+
let done = false;
|
|
611
|
+
let buffer = "";
|
|
612
|
+
let timer = null;
|
|
613
|
+
function finish(ok) {
|
|
614
|
+
if (done) return;
|
|
615
|
+
done = true;
|
|
616
|
+
if (timer !== null) {
|
|
617
|
+
clearTimeout(timer);
|
|
618
|
+
timer = null;
|
|
619
|
+
}
|
|
620
|
+
try {
|
|
621
|
+
client.destroy();
|
|
622
|
+
} catch {
|
|
623
|
+
}
|
|
624
|
+
resolve2(ok);
|
|
625
|
+
}
|
|
626
|
+
const client = connect(DAEMON_SOCKET, () => {
|
|
627
|
+
const msg = JSON.stringify({
|
|
628
|
+
id: randomUUID(),
|
|
629
|
+
method: "work_queue_enqueue",
|
|
630
|
+
params: {
|
|
631
|
+
type: "session-summary",
|
|
632
|
+
priority: 3,
|
|
633
|
+
payload: {
|
|
634
|
+
cwd: payload.cwd,
|
|
635
|
+
force: true
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}) + "\n";
|
|
639
|
+
client.write(msg);
|
|
640
|
+
});
|
|
641
|
+
client.on("data", (chunk) => {
|
|
642
|
+
buffer += chunk.toString();
|
|
643
|
+
const nl = buffer.indexOf("\n");
|
|
644
|
+
if (nl === -1) return;
|
|
645
|
+
const line = buffer.slice(0, nl);
|
|
646
|
+
try {
|
|
647
|
+
const response = JSON.parse(line);
|
|
648
|
+
if (response.ok) {
|
|
649
|
+
console.error(`STOP-HOOK: Mid-session summary enqueued (id=${response.result?.id}).`);
|
|
650
|
+
}
|
|
651
|
+
} catch {
|
|
652
|
+
}
|
|
653
|
+
finish(true);
|
|
654
|
+
});
|
|
655
|
+
client.on("error", () => finish(false));
|
|
656
|
+
client.on("end", () => {
|
|
657
|
+
if (!done) finish(false);
|
|
658
|
+
});
|
|
659
|
+
timer = setTimeout(() => finish(false), DAEMON_TIMEOUT_MS);
|
|
660
|
+
});
|
|
661
|
+
}
|
|
550
662
|
function enqueueSessionSummaryWithDaemon(payload) {
|
|
551
663
|
return new Promise((resolve2) => {
|
|
552
664
|
let done = false;
|
|
@@ -770,12 +882,18 @@ STOP-HOOK TRIGGERED AT ${timestamp}`);
|
|
|
770
882
|
}
|
|
771
883
|
let transcriptPath;
|
|
772
884
|
let cwd;
|
|
885
|
+
let stopHookActive = false;
|
|
886
|
+
let sessionId = "";
|
|
773
887
|
try {
|
|
774
888
|
const parsed = JSON.parse(input);
|
|
775
889
|
transcriptPath = parsed.transcript_path;
|
|
776
890
|
cwd = parsed.cwd || process.cwd();
|
|
891
|
+
stopHookActive = parsed.stop_hook_active === true;
|
|
892
|
+
sessionId = parsed.session_id ?? basename3(transcriptPath ?? "").replace(/\.jsonl$/, "");
|
|
777
893
|
console.error(`Transcript path: ${transcriptPath}`);
|
|
778
894
|
console.error(`Working directory: ${cwd}`);
|
|
895
|
+
console.error(`stop_hook_active: ${stopHookActive}`);
|
|
896
|
+
console.error(`session_id: ${sessionId}`);
|
|
779
897
|
} catch (e) {
|
|
780
898
|
console.error(`Error parsing input JSON: ${e}`);
|
|
781
899
|
process.exit(0);
|
|
@@ -793,6 +911,34 @@ STOP-HOOK TRIGGERED AT ${timestamp}`);
|
|
|
793
911
|
process.exit(0);
|
|
794
912
|
}
|
|
795
913
|
const lines = transcript.trim().split("\n");
|
|
914
|
+
if (!stopHookActive && sessionId) {
|
|
915
|
+
try {
|
|
916
|
+
const currentMsgCount = countHumanMessages(lines);
|
|
917
|
+
const state = readSessionState(sessionId);
|
|
918
|
+
const prevCount = state.humanMessageCount;
|
|
919
|
+
const newMessages = currentMsgCount - prevCount;
|
|
920
|
+
console.error(
|
|
921
|
+
`STOP-HOOK: human messages \u2014 total=${currentMsgCount} prev=${prevCount} new=${newMessages} interval=${AUTO_SAVE_INTERVAL}`
|
|
922
|
+
);
|
|
923
|
+
if (newMessages >= AUTO_SAVE_INTERVAL) {
|
|
924
|
+
writeSessionState(sessionId, { humanMessageCount: currentMsgCount });
|
|
925
|
+
console.error(`STOP-HOOK: Auto-save threshold reached. Triggering mid-session summary.`);
|
|
926
|
+
enqueueMidSessionSummaryWithDaemon({ cwd }).catch(() => {
|
|
927
|
+
});
|
|
928
|
+
process.stdout.write(
|
|
929
|
+
`<system-reminder>
|
|
930
|
+
[AUTO-SAVE] ${newMessages} messages processed. The daemon is now summarizing the session so far. Continue with your current task \u2014 this is background work.
|
|
931
|
+
</system-reminder>
|
|
932
|
+
`
|
|
933
|
+
);
|
|
934
|
+
process.exit(2);
|
|
935
|
+
} else {
|
|
936
|
+
writeSessionState(sessionId, { humanMessageCount: currentMsgCount });
|
|
937
|
+
}
|
|
938
|
+
} catch (autoSaveError) {
|
|
939
|
+
console.error(`STOP-HOOK: Auto-save check failed (non-fatal): ${autoSaveError}`);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
796
942
|
let lastUserQuery = "";
|
|
797
943
|
for (let i = lines.length - 1; i >= 0; i--) {
|
|
798
944
|
try {
|
|
@@ -851,6 +997,10 @@ STOP-HOOK TRIGGERED AT ${timestamp}`);
|
|
|
851
997
|
await executeDirectly(lines, transcriptPath, cwd, message, lastUserQuery);
|
|
852
998
|
}
|
|
853
999
|
await enqueueSessionSummaryWithDaemon({ cwd });
|
|
1000
|
+
if (sessionId) {
|
|
1001
|
+
deleteSessionState(sessionId);
|
|
1002
|
+
console.error(`STOP-HOOK: Session state cleaned up for ${sessionId}.`);
|
|
1003
|
+
}
|
|
854
1004
|
console.error(`STOP-HOOK COMPLETED SUCCESSFULLY at ${(/* @__PURE__ */ new Date()).toISOString()}
|
|
855
1005
|
`);
|
|
856
1006
|
}
|