aicp-tracker 1.1.4 → 1.1.6

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.4",
3
+ "version": "1.1.6",
4
4
  "description": "AI Code Pulse — Claude Code usage tracker for JIRA cost attribution",
5
5
  "main": "src/daemon.js",
6
6
  "bin": {
@@ -5,11 +5,22 @@
5
5
  const path = require('path');
6
6
  const fs = require('fs');
7
7
  const { CONFIG_DIR } = require('./config');
8
- const { tick } = require('./daemon');
8
+ const { tick, PROJECT_PATH } = require('./daemon');
9
9
 
10
10
  const PID_FILE = path.join(CONFIG_DIR, 'daemon.pid');
11
+ const LOG_FILE = path.join(CONFIG_DIR, 'daemon.log');
12
+
13
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
11
14
  fs.writeFileSync(PID_FILE, String(process.pid), 'utf8');
12
15
 
16
+ // Redirect stdout/stderr to log file so we can inspect daemon activity
17
+ const logStream = fs.createWriteStream(LOG_FILE, { flags: 'a' });
18
+ const origWrite = (chunk) => logStream.write(`[${new Date().toISOString()}] ${chunk}`);
19
+ process.stdout.write = origWrite;
20
+ process.stderr.write = origWrite;
21
+
22
+ console.log(`[daemon-worker] started, tracking project: ${PROJECT_PATH}`);
23
+
13
24
  process.on('SIGTERM', () => {
14
25
  try { fs.unlinkSync(PID_FILE); } catch {}
15
26
  process.exit(0);
package/src/daemon.js CHANGED
@@ -2,19 +2,33 @@
2
2
 
3
3
  const path = require('path');
4
4
  const fs = require('fs');
5
+ const os = require('os');
5
6
  const { findJsonlFiles } = require('./log-scanner');
6
7
  const { parseNewLines } = require('./log-parser');
7
8
  const wal = require('./wal');
8
9
  const { sendPending } = require('./sender');
9
- const config = require('./config');
10
- const { CONFIG_DIR } = config;
10
+ const { CONFIG_DIR } = require('./config');
11
11
 
12
12
  const PID_FILE = path.join(CONFIG_DIR, 'daemon.pid');
13
13
 
14
+ // When installed locally: __dirname = <project>/node_modules/aicp-tracker/src
15
+ // Resolve three levels up to get the host project root, then verify it has a
16
+ // package.json that isn't ours (guards against global/npx installs).
17
+ function detectProjectPath() {
18
+ const candidate = path.resolve(__dirname, '..', '..', '..');
19
+ const hostPkg = path.join(candidate, 'package.json');
20
+ try {
21
+ const pkg = JSON.parse(fs.readFileSync(hostPkg, 'utf8'));
22
+ if (pkg.name !== 'aicp-tracker') return candidate;
23
+ } catch {}
24
+ // Global install or npx — fall back to cwd at start time
25
+ return process.cwd();
26
+ }
27
+
28
+ const PROJECT_PATH = detectProjectPath();
29
+
14
30
  async function tick() {
15
- const cfg = (() => { try { return config.load(); } catch { return null; } })();
16
- const projectPath = cfg?.projectPath || null;
17
- const files = findJsonlFiles(projectPath);
31
+ const files = findJsonlFiles(PROJECT_PATH);
18
32
  let total = 0;
19
33
  for (const f of files) {
20
34
  const records = parseNewLines(f);
@@ -31,31 +45,23 @@ function readPid() {
31
45
  try { return parseInt(fs.readFileSync(PID_FILE, 'utf8'), 10); } catch { return null; }
32
46
  }
33
47
 
34
- function saveProjectPath() {
35
- const cfg = config.load() || {};
36
- config.save({ ...cfg, projectPath: process.cwd() });
37
- }
38
-
39
48
  function start() {
40
- saveProjectPath();
41
-
42
49
  const existingPid = readPid();
43
50
  if (existingPid) {
44
51
  try {
45
- process.kill(existingPid, 0); // check if running
46
- console.log(`[daemon] Already running (pid ${existingPid}), tracking: ${process.cwd()}`);
52
+ process.kill(existingPid, 0);
53
+ console.log(`[daemon] Already running (pid ${existingPid}), tracking: ${PROJECT_PATH}`);
47
54
  return;
48
55
  } catch {}
49
56
  }
50
57
 
51
- // Detach and run as background process
52
58
  const child = require('child_process').spawn(
53
59
  process.execPath,
54
60
  [path.join(__dirname, 'daemon-worker.js')],
55
61
  { detached: true, stdio: 'ignore' }
56
62
  );
57
63
  child.unref();
58
- console.log(`[daemon] Started (pid ${child.pid}), tracking: ${process.cwd()}`);
64
+ console.log(`[daemon] Started (pid ${child.pid}), tracking: ${PROJECT_PATH}`);
59
65
  }
60
66
 
61
67
  function stop() {
@@ -73,11 +79,17 @@ function stop() {
73
79
  function status() {
74
80
  const pid = readPid();
75
81
  const running = pid ? (() => { try { process.kill(pid, 0); return true; } catch { return false; } })() : false;
76
- const cfg = (() => { try { return config.load(); } catch { return null; } })();
77
- console.log(`[daemon] Status: ${running ? `running (pid ${pid})` : 'stopped'}`);
78
- console.log(`[daemon] Project: ${cfg?.projectPath || '(all projects)'}`);
79
- console.log(`[daemon] WAL pending: ${wal.pendingCount()} records`);
82
+ const { pathToProjectFolder } = require('./log-scanner');
83
+ const claudeFolder = pathToProjectFolder(PROJECT_PATH);
84
+ const claudeDir = path.join(os.homedir(), '.claude', 'projects');
85
+ const available = fs.existsSync(claudeDir) ? fs.readdirSync(claudeDir).filter(e => fs.statSync(path.join(claudeDir, e)).isDirectory()) : [];
86
+ const matched = available.includes(claudeFolder);
87
+ console.log(`[daemon] Status: ${running ? `running (pid ${pid})` : 'stopped'}`);
88
+ console.log(`[daemon] Project path: ${PROJECT_PATH}`);
89
+ console.log(`[daemon] Claude folder: ${claudeFolder} ${matched ? '✔ found' : '✘ NOT FOUND in ~/.claude/projects'}`);
90
+ if (!matched) console.log(`[daemon] Available: ${available.join(', ')}`);
91
+ console.log(`[daemon] WAL pending: ${wal.pendingCount()} records`);
92
+ console.log(`[daemon] Log file: ${path.join(CONFIG_DIR, 'daemon.log')}`);
80
93
  }
81
94
 
82
- // When required directly (e.g. aicp-tracker start calls this)
83
- module.exports = { start, stop, status, tick };
95
+ module.exports = { start, stop, status, tick, PROJECT_PATH };