Repository navigation
Expand file tree
/
Copy pathsession-logger.js
More file actions
365 lines (334 loc) · 14.3 KB
/
Copy pathsession-logger.js
File metadata and controls
365 lines (334 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env node
/**
* Session Logger - SessionStart | PostToolUse | SessionEnd Hook
* Writes a human-readable markdown log of every Claude Code session:
* timestamps, cwd, git repo/branch, files touched, and bash commands run.
* Logs to: ~/.claude/hooks-logs/ (hook diagnostics)
* Session notes go to: $CC_SESSION_LOG_DIR (default: ~/.claude/sessions/)
*
* Why: Claude Code sessions are ephemeral. You finish a session, switch repos,
* and two days later can't remember which session touched which file. This hook
* gives you a durable, greppable (and Obsidian-friendly) record of every session.
*
* One script, three registrations. Install as a plugin (recommended):
* /plugin install session-logger@claude-code-hooks
* The plugin's hooks.json registers all three events for you, with PostToolUse
* async. Configure via env vars, not by editing this file: plugin updates
* overwrite installed plugin files.
*
* PostToolUse uses "async": true so logging never blocks Claude: the hook
* fires after every Edit/Write/Read/Bash, so keep it non-blocking. Because
* async invocations can run concurrently (parallel tool calls), every write to
* a note goes through a cross-process file lock (withLock) so concurrent
* appends can't clobber each other. SessionStart is sync so the note file
* exists before PostToolUse tries to append to it; SessionEnd is sync so
* finalization completes before the session terminates.
*
* Secrets: bash commands are single-line-truncated AND run through a best-effort
* redactor (redactSecrets) that masks common inline-secret shapes: sensitive
* env assignments, --password/--token flags, Bearer tokens, and well-known key
* prefixes (ghp_, xox…, sk-, AKIA…). This is best-effort, NOT a guarantee; an
* unusual secret form can still slip through. Treat notes as sensitive and keep
* synced folders (Obsidian/iCloud) private. File CONTENTS are never logged -
* only paths for Edit/Write/Read.
*
* The classic path still works too: copy this script somewhere stable
* (e.g. ~/.claude/hooks/) and register it in .claude/settings.json:
*
* {
* "hooks": {
* "SessionStart": [{
* "hooks": [{ "type": "command", "command": "node ~/.claude/hooks/session-logger.js" }]
* }],
* "PostToolUse": [{
* "matcher": "Edit|Write|Bash|Read",
* "hooks": [{ "type": "command", "command": "node ~/.claude/hooks/session-logger.js", "async": true }]
* }],
* "SessionEnd": [{
* "hooks": [{ "type": "command", "command": "node ~/.claude/hooks/session-logger.js" }]
* }]
* }
* }
*
* Note: register against SessionEnd, NOT Stop. Stop fires at the end of every
* Claude turn (many times per session); SessionEnd fires once when the session
* actually ends. If the terminal is closed abruptly (SIGKILL), SessionEnd may
* not fire: the note remains as-is with all activity up to the last tool call
* preserved, just without a final ended: timestamp.
*
* Tip: point CC_SESSION_LOG_DIR at an Obsidian vault for cross-device sync.
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// ─── Config ──────────────────────────────────────────────────────────────────
// Where session notes are written. Customize this or set CC_SESSION_LOG_DIR.
// Tip: point this at an Obsidian vault or iCloud folder for cross-device sync.
// Examples:
// iCloud: path.join(process.env.HOME, 'Library/Mobile Documents/com~apple~CloudDocs/Claude-Code-Sessions')
// Obsidian: path.join(process.env.HOME, 'Obsidian/MyVault/claude-sessions')
const SESSION_LOG_DIR = path.join(process.env.HOME || process.env.USERPROFILE || require('os').homedir(), '.claude', 'sessions');
// Max characters logged per bash command (first line only, then truncated).
// Overridable via CC_SESSION_BASH_TRUNCATE (a positive integer); anything else
// falls back to 200.
const envTruncate = Number(process.env.CC_SESSION_BASH_TRUNCATE);
const BASH_TRUNCATE = Number.isInteger(envTruncate) && envTruncate > 0 ? envTruncate : 200;
// ─────────────────────────────────────────────────────────────────────────────
const LOG_DIR = path.join(process.env.HOME || process.env.USERPROFILE || require('os').homedir(), '.claude', 'hooks-logs');
const SESSION_DIR = process.env.CC_SESSION_LOG_DIR || SESSION_LOG_DIR;
function log(data) {
try {
if (!fs.existsSync(LOG_DIR)) fs.mkdirSync(LOG_DIR, { recursive: true });
const file = path.join(LOG_DIR, `${new Date().toISOString().slice(0, 10)}.jsonl`);
fs.appendFileSync(file, JSON.stringify({ ts: new Date().toISOString(), hook: 'session-logger', ...data }) + '\n');
} catch {}
}
function gitInfo(cwd) {
const info = { repo: null, branch: null, remote: null };
try {
info.branch = execSync('git rev-parse --abbrev-ref HEAD', { cwd, stdio: 'pipe' }).toString().trim();
} catch {}
try {
const remote = execSync('git config --get remote.origin.url', { cwd, stdio: 'pipe' }).toString().trim();
info.remote = remote;
// Extract "owner/repo" from common git URL formats
const m = remote.match(/[:/]([^/:]+\/[^/]+?)(?:\.git)?$/);
if (m) info.repo = m[1];
} catch {}
return info;
}
function sessionFilePath(sessionId, startedAt, cwd) {
const d = new Date(startedAt);
const yyyy = d.getUTCFullYear();
const mm = String(d.getUTCMonth() + 1).padStart(2, '0');
const dd = String(d.getUTCDate()).padStart(2, '0');
const hh = String(d.getUTCHours()).padStart(2, '0');
const mi = String(d.getUTCMinutes()).padStart(2, '0');
const shortId = (sessionId || 'unknown').slice(0, 8);
return path.join(SESSION_DIR, `${yyyy}-${mm}-${dd}_${hh}${mi}_${shortId}.md`);
}
function findExistingFile(sessionId) {
if (!fs.existsSync(SESSION_DIR)) return null;
const shortId = (sessionId || 'unknown').slice(0, 8);
const files = fs.readdirSync(SESSION_DIR).filter(f => f.endsWith(`_${shortId}.md`));
if (files.length === 0) return null;
// If multiple (shouldn't happen often), pick most recent
files.sort();
return path.join(SESSION_DIR, files[files.length - 1]);
}
function ensureDir(dir) {
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
}
// Synchronous sleep: hooks are short-lived one-shot processes, so a blocking
// wait while spinning for the lock is fine (and simpler than going async).
function sleepSync(ms) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}
// Cross-process advisory lock. PostToolUse runs with "async": true, so parallel
// tool calls can invoke this script concurrently; the note write is a
// read-modify-write and would otherwise lose entries (last writer wins). We
// serialize per-note via an exclusive lockfile. Best-effort: if we can't get
// the lock within the budget, we proceed unlocked rather than drop the write -
// a rare interleave beats silent data loss.
function withLock(filePath, fn, { retries = 60, delayMs = 15, staleMs = 10000, failOnTimeout = false } = {}) {
const lockPath = `${filePath}.lock`;
for (let i = 0; i < retries; i++) {
let fd;
try {
fd = fs.openSync(lockPath, 'wx'); // atomic create-exclusive
} catch (e) {
if (e.code !== 'EEXIST') throw e;
// Steal a stale lock left behind by a crashed/killed process.
try {
if (Date.now() - fs.statSync(lockPath).mtimeMs > staleMs) fs.unlinkSync(lockPath);
} catch {}
sleepSync(delayMs);
continue;
}
try {
return fn();
} finally {
fs.closeSync(fd);
try { fs.unlinkSync(lockPath); } catch {}
}
}
if (failOnTimeout) throw new Error('Session note lock timed out');
log({ level: 'LOCK_TIMEOUT', file: filePath });
return fn(); // proceed unlocked rather than lose the entry
}
// Best-effort masking of the most common inline-secret shapes on a single
// command line. NOT a guarantee: see the header note. Conservative by design:
// we'd rather miss an exotic secret than mangle legitimate commands in the log.
function redactSecrets(cmd) {
return cmd
// Sensitive-looking env-style assignments: TOKEN=…, AWS_SECRET_ACCESS_KEY=…
.replace(/\b([A-Z0-9_]*(?:TOKEN|SECRET|PASSWORD|PASSWD|API_?KEY|ACCESS_KEY|PRIVATE_KEY|CREDENTIAL|AUTH)[A-Z0-9_]*)=(\S+)/gi, '$1=***')
// Long-form flags with a value: --password foo, --token=foo, --api-key foo
.replace(/(--(?:password|token|secret|api[-_]?key)[= ])\S+/gi, '$1***')
// Authorization: Bearer <token>
.replace(/(Bearer\s+)[A-Za-z0-9._\-]+/gi, '$1***')
// Well-known token prefixes (GitHub, Slack, OpenAI, AWS, Stripe)
.replace(/\b(gh[pousr]_|xox[baprs]-|sk-|AKIA|ASIA|sk_live_|rk_live_)[A-Za-z0-9\-_]{6,}/g, '$1***');
}
function handleSessionStart(data) {
const { session_id, cwd } = data;
const started = new Date().toISOString();
ensureDir(SESSION_DIR);
const existing = findExistingFile(session_id);
if (existing) {
// Session resumed: append a resume marker, don't overwrite.
fs.appendFileSync(existing, `\n## Resumed at ${started}\n`);
log({ level: 'RESUME', session_id, file: existing });
return;
}
const git = gitInfo(cwd || process.cwd());
const cwdBase = path.basename(cwd || process.cwd());
const filePath = sessionFilePath(session_id, started, cwd);
const frontmatter = [
'---',
`session_id: ${session_id || 'unknown'}`,
`cwd: ${cwd || process.cwd()}`,
`git_repo: ${git.repo || 'n/a'}`,
`git_branch: ${git.branch || 'n/a'}`,
`started: ${started}`,
'ended:',
'---',
'',
`# Claude Code Session: ${cwdBase}`,
'',
`**Started:** ${started}`,
`**Working dir:** \`${cwd || process.cwd()}\``,
git.repo ? `**Repo:** ${git.repo} (${git.branch || 'detached'})` : null,
'',
'## Files Touched',
'',
'## Commands Run',
'',
].filter(l => l !== null).join('\n');
fs.writeFileSync(filePath, frontmatter);
log({ level: 'START', session_id, file: filePath });
}
function handlePostToolUse(data) {
const { session_id, tool_name, tool_input } = data;
if (!['Edit', 'Write', 'Bash', 'Read'].includes(tool_name)) return;
const filePath = findExistingFile(session_id);
if (!filePath) {
log({ level: 'SKIP', reason: 'no session file', session_id });
return;
}
const ts = new Date().toISOString().slice(11, 19); // HH:MM:SS
let entry = null;
let section = null;
if (tool_name === 'Edit' || tool_name === 'Write' || tool_name === 'Read') {
const f = tool_input?.file_path;
if (!f) return;
const verb = tool_name === 'Read' ? 'read' : tool_name === 'Write' ? 'wrote' : 'edited';
entry = `- \`${ts}\` ${verb} \`${f}\``;
section = '## Files Touched';
} else if (tool_name === 'Bash') {
let cmd = tool_input?.command || '';
cmd = redactSecrets(cmd.split('\n')[0]);
if (cmd.length > BASH_TRUNCATE) cmd = cmd.slice(0, BASH_TRUNCATE) + '…';
entry = `- \`${ts}\` \`${cmd}\``;
section = '## Commands Run';
}
if (!entry || !section) return;
appendUnderSection(filePath, section, entry);
log({ level: 'APPEND', session_id, tool: tool_name });
}
function appendUnderSection(filePath, section, entry) {
withLock(filePath, () => {
const body = fs.readFileSync(filePath, 'utf8');
const lines = body.split('\n');
const sectionIdx = lines.findIndex(l => l.trim() === section);
if (sectionIdx === -1) {
// Section missing: append at end
fs.appendFileSync(filePath, `\n${section}\n\n${entry}\n`);
return;
}
// Find end of section (next ## heading or EOF)
let insertAt = lines.length;
for (let i = sectionIdx + 1; i < lines.length; i++) {
if (/^##\s/.test(lines[i])) {
insertAt = i;
break;
}
}
// Skip trailing blank lines in the section
while (insertAt > sectionIdx + 1 && lines[insertAt - 1].trim() === '') insertAt--;
lines.splice(insertAt, 0, entry);
fs.writeFileSync(filePath, lines.join('\n'));
});
}
function handleSessionEnd(data) {
const { session_id, cwd } = data;
const filePath = findExistingFile(session_id);
if (!filePath) {
log({ level: 'SKIP', reason: 'no session file on end', session_id });
return;
}
const ended = new Date().toISOString();
// Capture final git status (short) outside the lock: no need to hold it
// during a subprocess call.
let gitStatus = '';
try {
gitStatus = execSync('git status --short', { cwd: cwd || process.cwd(), stdio: 'pipe' }).toString().trim();
} catch {}
withLock(filePath, () => {
// Idempotent: skip if already finalized (defensive: protects against the
// user accidentally registering against Stop, which fires every turn).
// The frontmatter is seeded with a literal "ended:" line; once we fill it,
// this regex no longer matches and we skip. The check + write are inside the
// lock so a duplicate SessionEnd can't race past the guard.
let body = fs.readFileSync(filePath, 'utf8');
if (!/^ended:[ \t]*$/m.test(body)) {
log({ level: 'SKIP', reason: 'already finalized', session_id });
return;
}
// Update frontmatter `ended:` line
body = body.replace(/^ended:[ \t]*$/m, `ended: ${ended}`);
const footer = [
'',
'## Session End',
'',
`**Ended:** ${ended}`,
];
if (gitStatus) {
footer.push('', '**Final git status:**', '```', gitStatus, '```');
}
fs.writeFileSync(filePath, body + footer.join('\n') + '\n');
log({ level: 'END', session_id, file: filePath });
});
}
async function main() {
let input = '';
for await (const chunk of process.stdin) input += chunk;
try {
const data = JSON.parse(input);
const event = data.hook_event_name;
if (event === 'SessionStart') handleSessionStart(data);
else if (event === 'PostToolUse') handlePostToolUse(data);
else if (event === 'SessionEnd') handleSessionEnd(data);
// Note: Stop is intentionally NOT handled. It fires every turn, not once
// per session. Register against SessionEnd instead.
console.log('{}');
} catch (e) {
log({ level: 'ERROR', error: e.message });
console.log('{}');
}
}
if (require.main === module) {
main();
} else {
module.exports = {
gitInfo,
sessionFilePath,
findExistingFile,
appendUnderSection,
redactSecrets,
withLock,
handleSessionStart,
handlePostToolUse,
handleSessionEnd,
};
}