mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-04-04 08:13:30 +08:00
fix: clean up observer sessions on lifecycle end
This commit is contained in:
@@ -2,12 +2,50 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Session end marker hook - outputs stdin to stdout unchanged.
|
||||
* Exports run() for in-process execution (avoids spawnSync issues on Windows).
|
||||
* Session end marker hook - performs lightweight observer cleanup and
|
||||
* outputs stdin to stdout unchanged. Exports run() for in-process execution.
|
||||
*/
|
||||
|
||||
const {
|
||||
resolveProjectContext,
|
||||
removeSessionLease,
|
||||
listSessionLeases,
|
||||
stopObserverForContext,
|
||||
resolveSessionId
|
||||
} = require('../lib/observer-sessions');
|
||||
|
||||
function log(message) {
|
||||
process.stderr.write(`[SessionEnd] ${message}\n`);
|
||||
}
|
||||
|
||||
function run(rawInput) {
|
||||
return rawInput || '';
|
||||
const output = rawInput || '';
|
||||
const sessionId = resolveSessionId();
|
||||
|
||||
if (!sessionId) {
|
||||
log('No CLAUDE_SESSION_ID available; skipping observer cleanup');
|
||||
return output;
|
||||
}
|
||||
|
||||
try {
|
||||
const observerContext = resolveProjectContext();
|
||||
removeSessionLease(observerContext, sessionId);
|
||||
const remainingLeases = listSessionLeases(observerContext);
|
||||
|
||||
if (remainingLeases.length === 0) {
|
||||
if (stopObserverForContext(observerContext)) {
|
||||
log(`Stopped observer for project ${observerContext.projectId} after final session lease ended`);
|
||||
} else {
|
||||
log(`No running observer to stop for project ${observerContext.projectId}`);
|
||||
}
|
||||
} else {
|
||||
log(`Retained observer for project ${observerContext.projectId}; ${remainingLeases.length} session lease(s) remain`);
|
||||
}
|
||||
} catch (err) {
|
||||
log(`Observer cleanup skipped: ${err.message}`);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// Legacy CLI execution (when run directly)
|
||||
@@ -22,7 +60,7 @@ if (require.main === module) {
|
||||
}
|
||||
});
|
||||
process.stdin.on('end', () => {
|
||||
process.stdout.write(raw);
|
||||
process.stdout.write(run(raw));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ const {
|
||||
stripAnsi,
|
||||
log
|
||||
} = require('../lib/utils');
|
||||
const { resolveProjectContext, writeSessionLease, resolveSessionId } = require('../lib/observer-sessions');
|
||||
const { getPackageManager, getSelectionPrompt } = require('../lib/package-manager');
|
||||
const { listAliases } = require('../lib/session-aliases');
|
||||
const { detectProjectType } = require('../lib/project-detect');
|
||||
@@ -163,6 +164,18 @@ async function main() {
|
||||
ensureDir(sessionsDir);
|
||||
ensureDir(learnedDir);
|
||||
|
||||
const observerSessionId = resolveSessionId();
|
||||
if (observerSessionId) {
|
||||
const observerContext = resolveProjectContext();
|
||||
writeSessionLease(observerContext, observerSessionId, {
|
||||
hook: 'SessionStart',
|
||||
projectRoot: observerContext.projectRoot
|
||||
});
|
||||
log(`[SessionStart] Registered observer lease for ${observerSessionId}`);
|
||||
} else {
|
||||
log('[SessionStart] No CLAUDE_SESSION_ID available; skipping observer lease registration');
|
||||
}
|
||||
|
||||
// Check for recent session files (last 7 days)
|
||||
const recentSessions = dedupeRecentSessions(getSessionSearchDirs());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user