aicp-tracker 1.1.3 → 1.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aicp-tracker",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "AI Code Pulse — Claude Code usage tracker for JIRA cost attribution",
5
5
  "main": "src/daemon.js",
6
6
  "bin": {
package/src/daemon.js CHANGED
@@ -2,18 +2,32 @@
2
2
 
3
3
  const path = require('path');
4
4
  const fs = require('fs');
5
- const os = require('os');
6
5
  const { findJsonlFiles } = require('./log-scanner');
7
6
  const { parseNewLines } = require('./log-parser');
8
7
  const wal = require('./wal');
9
8
  const { sendPending } = require('./sender');
10
9
  const { CONFIG_DIR } = require('./config');
11
10
 
12
- const PID_FILE = path.join(CONFIG_DIR, 'daemon.pid');
13
- const INTERVAL_MS = 5 * 60 * 1000; // 5 minutes
11
+ const PID_FILE = path.join(CONFIG_DIR, 'daemon.pid');
12
+
13
+ // When installed locally: __dirname = <project>/node_modules/aicp-tracker/src
14
+ // Resolve three levels up to get the host project root, then verify it has a
15
+ // package.json that isn't ours (guards against global/npx installs).
16
+ function detectProjectPath() {
17
+ const candidate = path.resolve(__dirname, '..', '..', '..');
18
+ const hostPkg = path.join(candidate, 'package.json');
19
+ try {
20
+ const pkg = JSON.parse(fs.readFileSync(hostPkg, 'utf8'));
21
+ if (pkg.name !== 'aicp-tracker') return candidate;
22
+ } catch {}
23
+ // Global install or npx — fall back to cwd at start time
24
+ return process.cwd();
25
+ }
26
+
27
+ const PROJECT_PATH = detectProjectPath();
14
28
 
15
29
  async function tick() {
16
- const files = findJsonlFiles();
30
+ const files = findJsonlFiles(PROJECT_PATH);
17
31
  let total = 0;
18
32
  for (const f of files) {
19
33
  const records = parseNewLines(f);
@@ -26,11 +40,6 @@ async function tick() {
26
40
  await sendPending();
27
41
  }
28
42
 
29
- function writePid() {
30
- fs.mkdirSync(CONFIG_DIR, { recursive: true });
31
- fs.writeFileSync(PID_FILE, String(process.pid), 'utf8');
32
- }
33
-
34
43
  function readPid() {
35
44
  try { return parseInt(fs.readFileSync(PID_FILE, 'utf8'), 10); } catch { return null; }
36
45
  }
@@ -39,20 +48,19 @@ function start() {
39
48
  const existingPid = readPid();
40
49
  if (existingPid) {
41
50
  try {
42
- process.kill(existingPid, 0); // check if running
43
- console.log(`[daemon] Already running (pid ${existingPid})`);
51
+ process.kill(existingPid, 0);
52
+ console.log(`[daemon] Already running (pid ${existingPid}), tracking: ${PROJECT_PATH}`);
44
53
  return;
45
54
  } catch {}
46
55
  }
47
56
 
48
- // Detach and run as background process
49
57
  const child = require('child_process').spawn(
50
58
  process.execPath,
51
59
  [path.join(__dirname, 'daemon-worker.js')],
52
60
  { detached: true, stdio: 'ignore' }
53
61
  );
54
62
  child.unref();
55
- console.log(`[daemon] Started (pid ${child.pid})`);
63
+ console.log(`[daemon] Started (pid ${child.pid}), tracking: ${PROJECT_PATH}`);
56
64
  }
57
65
 
58
66
  function stop() {
@@ -71,8 +79,8 @@ function status() {
71
79
  const pid = readPid();
72
80
  const running = pid ? (() => { try { process.kill(pid, 0); return true; } catch { return false; } })() : false;
73
81
  console.log(`[daemon] Status: ${running ? `running (pid ${pid})` : 'stopped'}`);
82
+ console.log(`[daemon] Project: ${PROJECT_PATH}`);
74
83
  console.log(`[daemon] WAL pending: ${wal.pendingCount()} records`);
75
84
  }
76
85
 
77
- // When required directly (e.g. aicp-tracker start calls this)
78
86
  module.exports = { start, stop, status, tick };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
- // Finds all Claude Code session jsonl files under ~/.claude/projects/
3
+ // Finds Claude Code session jsonl files under ~/.claude/projects/
4
+ // Scoped to the project folder that matches the configured projectPath.
4
5
 
5
6
  const fs = require('fs');
6
7
  const path = require('path');
@@ -8,12 +9,28 @@ const os = require('os');
8
9
 
9
10
  const CLAUDE_DIR = path.join(os.homedir(), '.claude', 'projects');
10
11
 
11
- function findJsonlFiles() {
12
+ // Converts an absolute project path to the folder name Claude Code uses.
13
+ // C:\Users\Alexey\Project → c--Users-Alexey-Project
14
+ // /home/user/project → -home-user-project
15
+ function pathToProjectFolder(p) {
16
+ if (process.platform === 'win32') {
17
+ return p
18
+ .replace(/\\/g, '/')
19
+ .replace(/^([A-Za-z]):\//, (_, d) => d.toLowerCase() + '--')
20
+ .replace(/\//g, '-');
21
+ }
22
+ return p.replace(/\//g, '-');
23
+ }
24
+
25
+ function findJsonlFiles(projectPath) {
12
26
  const results = [];
13
27
  if (!fs.existsSync(CLAUDE_DIR)) return results;
14
28
 
15
- for (const projectHash of fs.readdirSync(CLAUDE_DIR)) {
16
- const projectDir = path.join(CLAUDE_DIR, projectHash);
29
+ const targetFolder = projectPath ? pathToProjectFolder(projectPath) : null;
30
+
31
+ for (const entry of fs.readdirSync(CLAUDE_DIR)) {
32
+ if (targetFolder && entry !== targetFolder) continue;
33
+ const projectDir = path.join(CLAUDE_DIR, entry);
17
34
  if (!fs.statSync(projectDir).isDirectory()) continue;
18
35
  for (const file of fs.readdirSync(projectDir)) {
19
36
  if (file.endsWith('.jsonl')) {
@@ -25,4 +42,4 @@ function findJsonlFiles() {
25
42
  return results;
26
43
  }
27
44
 
28
- module.exports = { findJsonlFiles };
45
+ module.exports = { findJsonlFiles, pathToProjectFolder };