ccsniff 1.1.6 → 1.1.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/cli.js +88 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccsniff",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Watch Claude Code JSONL output files and emit structured events as a Node.js EventEmitter",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/cli.js CHANGED
@@ -4,6 +4,7 @@ import { toUnslothMessages, toShareGPT } from './unsloth.js';
4
4
  import { parseTime, compileRegexes, buildFilter } from './filters.js';
5
5
  import fs from 'fs';
6
6
  import path from 'path';
7
+ import os from 'os';
7
8
 
8
9
  if (process.argv[2] === 'gui') {
9
10
  const { createServer } = await import('./gui-server.js');
@@ -26,10 +27,10 @@ if (process.argv[2] === 'gui') {
26
27
  } else {
27
28
 
28
29
  const FLAGS = {
29
- string: ['since', 'until', 'before', 'after', 'grep', 'igrep', 'cwd', 'project', 'role', 'type', 'tool', 'session', 'sid', 'parent', 'rollup', 'format', 'sort', 'unsloth', 'unsloth-format', 'exclude-sess', 'exclude-sid', 'exclude-cwd', 'exclude-project'],
30
+ string: ['since', 'until', 'before', 'after', 'grep', 'igrep', 'cwd', 'project', 'role', 'type', 'tool', 'session', 'sid', 'sess', 'parent', 'rollup', 'format', 'sort', 'unsloth', 'unsloth-format', 'exclude-sess', 'exclude-sid', 'exclude-cwd', 'exclude-project'],
30
31
  multi: ['grep', 'igrep', 'role', 'type', 'tool', 'session', 'sid', 'project', 'cwd', 'exclude-sess', 'exclude-sid', 'exclude-cwd', 'exclude-project'],
31
- number: ['limit', 'head', 'tail-n', 'ctx', 'truncate'],
32
- bool: ['json', 'ndjson', 'tail', 'f', 'full', 'reverse', 'invert', 'no-subagents', 'only-subagents', 'no-meta', 'only-meta', 'list-sessions', 'list-projects', 'list-tools', 'bash-discipline', 'git-discipline', 'include-subagents', 'stats', 'count', 'help', 'h'],
32
+ number: ['limit', 'head', 'tail-n', 'ctx', 'truncate', 'days'],
33
+ bool: ['json', 'ndjson', 'tail', 'f', 'full', 'reverse', 'invert', 'no-subagents', 'only-subagents', 'no-meta', 'only-meta', 'list-sessions', 'list-projects', 'list-tools', 'bash-discipline', 'git-discipline', 'learning-xref', 'include-subagents', 'stats', 'count', 'help', 'h'],
33
34
  };
34
35
 
35
36
  function parseArgs(argv) {
@@ -63,6 +64,7 @@ USAGE
63
64
  ccsniff --list-projects
64
65
  ccsniff --list-tools
65
66
  ccsniff --bash-discipline [--stats] Bash calls that should have used Read/Glob/Grep
67
+ ccsniff --learning-xref [--sess <id>] [--days N] join transcript turns to rs-learn recall/memorize
66
68
  ccsniff --git-discipline [--stats] git push from a dirty/unwitnessed tree
67
69
  (excludes subagents by default — --include-subagents to opt in;
68
70
  excludes 'echo > .gm/exec-spool/in/...' as canonical spool-write)
@@ -314,6 +316,7 @@ if (opts['git-discipline']) {
314
316
  const includeSubagents = opts['include-subagents'];
315
317
  const PUSH = /\bgit\s+push\b/;
316
318
  const PORCELAIN_CLEAN = /\bgit\s+status\s+(--porcelain|-s)\b/;
319
+ const stripQuoted = (s) => s.replace(/"(?:\\.|[^"\\])*"/g, '""').replace(/'(?:\\.|[^'\\])*'/g, "''");
317
320
  const bySid = new Map();
318
321
  for (const ev of all) {
319
322
  if (!filter(ev)) continue;
@@ -329,9 +332,10 @@ if (opts['git-discipline']) {
329
332
  for (let i = 0; i < evs.length; i++) {
330
333
  const ev = evs[i];
331
334
  const cmd = ev.block?.input?.command || '';
332
- if (!PUSH.test(cmd)) continue;
335
+ const cmdStripped = stripQuoted(cmd);
336
+ if (!PUSH.test(cmdStripped)) continue;
333
337
  const lookback = evs.slice(Math.max(0, i - 20), i);
334
- const witnessed = lookback.some(e => PORCELAIN_CLEAN.test(e.block?.input?.command || ''));
338
+ const witnessed = lookback.some(e => PORCELAIN_CLEAN.test(stripQuoted(e.block?.input?.command || '')));
335
339
  if (witnessed) continue;
336
340
  violations.push({ ts: ev.timestamp, sid, project: path.basename(ev.conversation.cwd || ''), kind: 'push-no-porcelain-witness', cmd: cmd.slice(0, 200) });
337
341
  }
@@ -352,6 +356,85 @@ if (opts['git-discipline']) {
352
356
  process.exit(0);
353
357
  }
354
358
 
359
+ // ---------- learning-xref (join transcript turns to gm-log rs_learn signals)
360
+ if (opts['learning-xref']) {
361
+ const days = opts.days || 1;
362
+ const wantSess = opts.sess || null;
363
+ const bySid = new Map();
364
+ for (const ev of all) {
365
+ if (!filter(ev)) continue;
366
+ if (wantSess && !ev.conversation?.id?.startsWith(wantSess)) continue;
367
+ const sid = ev.conversation?.id;
368
+ if (!sid) continue;
369
+ if (!bySid.has(sid)) bySid.set(sid, { cwd: ev.conversation.cwd, evs: [] });
370
+ bySid.get(sid).evs.push(ev);
371
+ }
372
+ const dates = [];
373
+ const now = Date.now();
374
+ for (let i = 0; i < days; i++) {
375
+ dates.push(new Date(now - i * 86400000).toISOString().slice(0, 10));
376
+ }
377
+ const gmLogDir = path.join(os.homedir(), '.claude', 'gm-log');
378
+ const rsLearn = [], bootstrap = [];
379
+ for (const d of dates) {
380
+ for (const [file, sink] of [['rs_learn.jsonl', rsLearn], ['bootstrap.jsonl', bootstrap]]) {
381
+ const fp = path.join(gmLogDir, d, file);
382
+ if (!fs.existsSync(fp)) continue;
383
+ const lines = fs.readFileSync(fp, 'utf8').split('\n');
384
+ for (const ln of lines) {
385
+ if (!ln.trim()) continue;
386
+ try {
387
+ const j = JSON.parse(ln);
388
+ const ts = typeof j.ts === 'number' ? j.ts : Date.parse(j.ts);
389
+ if (Number.isFinite(ts)) { j._ts = ts; sink.push(j); }
390
+ } catch {}
391
+ }
392
+ }
393
+ }
394
+ rsLearn.sort((a, b) => a._ts - b._ts);
395
+ let totals = { turns: 0, tool_uses: 0, memorize: 0, recall: 0, hit: 0, miss: 0, embed_fail: 0 };
396
+ let anyMatched = 0;
397
+ for (const [sid, info] of bySid) {
398
+ info.evs.sort((a, b) => a.timestamp - b.timestamp);
399
+ const project = path.basename(info.cwd || '');
400
+ const skillTs = info.evs
401
+ .filter(e => e.block?.type === 'tool_use' && e.block?.name === 'Skill' && e.block?.input?.skill === 'gm-skill')
402
+ .map(e => e.timestamp);
403
+ if (!skillTs.length) continue;
404
+ const sessFirst = info.evs[0].timestamp;
405
+ const sessLast = info.evs[info.evs.length - 1].timestamp;
406
+ const bounds = [...skillTs, sessLast + 1];
407
+ process.stdout.write(`# session ${sid.slice(0, 8)} [${project}] turns=${skillTs.length}\n`);
408
+ for (let i = 0; i < skillTs.length; i++) {
409
+ const winStart = bounds[i];
410
+ const winEnd = bounds[i + 1];
411
+ const toolUses = info.evs.filter(e => e.timestamp >= winStart && e.timestamp < winEnd && e.block?.type === 'tool_use').length;
412
+ const rsInWin = rsLearn.filter(j => j._ts >= winStart && j._ts < winEnd && (!j.project || j.project === project) && (!wantSess || !j.sess || j.sess === sid || sid.startsWith(j.sess)));
413
+ let memorize = 0, recall = 0, hit = 0, miss = 0, embed_fail = 0;
414
+ for (const j of rsInWin) {
415
+ if (j.event === 'memorize') memorize++;
416
+ else if (j.event === 'recall') { recall++; if (j.hit) hit++; else miss++; }
417
+ else if (j.event === 'embed_fail' || /embed.*fail/i.test(j.event || '')) embed_fail++;
418
+ }
419
+ anyMatched += rsInWin.length;
420
+ totals.turns++;
421
+ totals.tool_uses += toolUses;
422
+ totals.memorize += memorize;
423
+ totals.recall += recall;
424
+ totals.hit += hit;
425
+ totals.miss += miss;
426
+ totals.embed_fail += embed_fail;
427
+ const ts = new Date(winStart).toISOString().slice(0, 19).replace('T', ' ');
428
+ process.stdout.write(`${ts} | tool_uses=${toolUses} | memorize=${memorize} | recall=${recall} (hit=${hit} miss=${miss}) | embed_fail=${embed_fail}\n`);
429
+ }
430
+ }
431
+ if (anyMatched === 0 && wantSess) {
432
+ process.stdout.write(`# no rs-learn events for sess ${wantSess} — confirm bootstrap fires gm-log writes\n`);
433
+ }
434
+ process.stderr.write(`# totals: sessions=${bySid.size} turns=${totals.turns} tool_uses=${totals.tool_uses} memorize=${totals.memorize} recall=${totals.recall} (hit=${totals.hit} miss=${totals.miss}) embed_fail=${totals.embed_fail} (scanned ${dates.length}d, rs_learn=${rsLearn.length} bootstrap=${bootstrap.length})\n`);
435
+ process.exit(0);
436
+ }
437
+
355
438
  // ---------- list-tools
356
439
  if (opts['list-tools']) {
357
440
  const tools = new Map();