claude-roi 0.2.1 → 0.2.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Akshat Sahu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -151,36 +151,7 @@ Codelens-AI/
151
151
 
152
152
  ## Contributing
153
153
 
154
- Contributions welcome! Here's how to get started:
155
-
156
- ```bash
157
- # 1. Fork the repo on GitHub
158
-
159
- # 2. Clone your fork
160
- git clone https://github.com/YOUR_USERNAME/Codelens-AI.git
161
- cd Codelens-AI
162
-
163
- # 3. Install dependencies
164
- npm install
165
-
166
- # 4. Run in dev mode (no auto-open browser)
167
- node src/index.js --no-open
168
-
169
- # 5. Make your changes and test
170
- node src/index.js --json | head -30 # verify JSON output
171
- node src/index.js --no-open # test dashboard at localhost:3457
172
-
173
- # 6. Submit a pull request
174
- ```
175
-
176
- ### Ideas for contributions
177
-
178
- - Support for other AI coding tools (Copilot, Cursor, etc.)
179
- - Git blame-based line survival tracking (more accurate than the 24h heuristic)
180
- - Export dashboard as PDF/PNG
181
- - Historical trend tracking across multiple runs
182
- - Team/multi-user support
183
- - Custom pricing configuration via CLI flag or config file
154
+ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, guidelines, and ideas for contributions.
184
155
 
185
156
  ## Privacy
186
157
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-roi",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Correlate Claude Code token usage with git output to measure AI coding agent ROI",
5
5
  "type": "module",
6
6
  "bin": {
@@ -9,7 +9,8 @@
9
9
  "main": "./src/index.js",
10
10
  "scripts": {
11
11
  "start": "node src/index.js",
12
- "dev": "node src/index.js --no-open"
12
+ "dev": "node src/index.js --no-open",
13
+ "test": "npx playwright test"
13
14
  },
14
15
  "keywords": [
15
16
  "claude",
@@ -21,17 +22,26 @@
21
22
  "git",
22
23
  "dashboard"
23
24
  ],
25
+ "author": "Akshat Sahu",
24
26
  "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/Akshat2634/Codelens-AI.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/Akshat2634/Codelens-AI/issues"
33
+ },
34
+ "homepage": "https://github.com/Akshat2634/Codelens-AI#readme",
25
35
  "engines": {
26
36
  "node": ">=18.0.0"
27
37
  },
28
38
  "dependencies": {
29
39
  "commander": "^13.0.0",
30
40
  "express": "^5.0.0",
31
- "open": "^10.0.0",
32
- "playwright": "^1.58.2"
41
+ "open": "^10.0.0"
33
42
  },
34
43
  "devDependencies": {
35
- "@playwright/test": "^1.56.1"
44
+ "@playwright/test": "^1.56.1",
45
+ "playwright": "^1.58.2"
36
46
  }
37
47
  }
package/src/correlator.js CHANGED
@@ -14,67 +14,100 @@ function computeOverlappingLines(commits, sessionFiles) {
14
14
  return { linesAdded, linesDeleted };
15
15
  }
16
16
 
17
+ /**
18
+ * Compute the temporal distance between a commit and a session.
19
+ * Returns the minimum absolute distance from the commit to the session's
20
+ * time range (0 if the commit falls within the session window).
21
+ */
22
+ function temporalDistance(commitMs, sessionStartMs, sessionEndMs) {
23
+ if (commitMs >= sessionStartMs && commitMs <= sessionEndMs) return 0;
24
+ return Math.min(
25
+ Math.abs(commitMs - sessionStartMs),
26
+ Math.abs(commitMs - sessionEndMs)
27
+ );
28
+ }
29
+
17
30
  /**
18
31
  * Correlate sessions to commits using file-based matching.
19
32
  *
33
+ * Uses a two-pass "nearest session wins" approach:
34
+ * 1. For each commit, find all candidate sessions (file overlap + time window)
35
+ * and assign it to the temporally closest one.
36
+ * 2. Build correlated results from those assignments.
37
+ *
20
38
  * Primary: match commits whose changed files overlap with session.filesWritten.
21
- * Time constraint: commit is on the same calendar day or the next day.
39
+ * Time constraint: commit is within [sessionStart, sessionEnd + 2 hours].
22
40
  * Fallback: for sessions with no filesWritten (chat-only), use time window
23
41
  * [sessionStart, sessionEnd + 2 hours].
24
42
  */
25
43
  export function correlateSessions(sessions, commitsByRepo) {
26
- const result = [];
27
- const claimedCommits = new Set();
28
-
29
- // Sort sessions by end time so earlier sessions claim commits first
30
- const sorted = [...sessions].sort(
31
- (a, b) => new Date(a.endTime).getTime() - new Date(b.endTime).getTime()
32
- );
44
+ // Phase 1: Build candidate map — for each commit, find the best session
45
+ // commitHash -> { session, hasFileOverlap }
46
+ const commitAssignment = new Map();
33
47
 
34
- for (const session of sorted) {
48
+ for (const session of sessions) {
35
49
  const repoAnalysis = commitsByRepo[session.repoPath];
36
50
  const repoCommits = repoAnalysis?.commits || [];
37
51
 
38
- const sessionStart = new Date(session.startTime).getTime();
39
- const sessionEnd = new Date(session.endTime).getTime();
52
+ const sessionStartMs = new Date(session.startTime).getTime();
53
+ const sessionEndMs = new Date(session.endTime).getTime();
40
54
  const sessionFiles = new Set(session.filesWritten || []);
41
-
42
- let matched;
43
-
44
- if (sessionFiles.size > 0) {
45
- // PRIMARY: File-based correlation
46
- // Time window: same calendar day as session, or next calendar day
47
- const sessionDay = new Date(session.startTime);
48
- const dayStart = new Date(sessionDay.getFullYear(), sessionDay.getMonth(), sessionDay.getDate()).getTime();
49
- const dayEnd = dayStart + 2 * 24 * 60 * 60 * 1000; // end of next calendar day
50
-
51
- matched = repoCommits.filter(c => {
52
- if (claimedCommits.has(c.hash)) return false;
53
- if (c.timestampMs < dayStart || c.timestampMs >= dayEnd) return false;
54
- // Check file overlap: any commit file matches a session file
55
- return c.files.some(f => sessionFiles.has(f.path));
56
- });
57
- } else {
58
- // FALLBACK: Time-based for chat-only sessions (no files written)
59
- const windowEnd = sessionEnd + FALLBACK_BUFFER_MS;
60
- matched = repoCommits.filter(c =>
61
- c.timestampMs >= sessionStart &&
62
- c.timestampMs <= windowEnd &&
63
- !claimedCommits.has(c.hash)
64
- );
55
+ const windowEnd = sessionEndMs + FALLBACK_BUFFER_MS;
56
+
57
+ for (const commit of repoCommits) {
58
+ // Time constraint: commit must fall within [sessionStart, sessionEnd + 2h]
59
+ if (commit.timestampMs < sessionStartMs || commit.timestampMs > windowEnd) continue;
60
+
61
+ const hasFileOverlap = sessionFiles.size > 0 &&
62
+ commit.files.some(f => sessionFiles.has(f.path));
63
+ const isChatOnly = sessionFiles.size === 0;
64
+
65
+ // Must have file overlap, or be a chat-only session (time-based fallback)
66
+ if (!hasFileOverlap && !isChatOnly) continue;
67
+
68
+ const distance = temporalDistance(commit.timestampMs, sessionStartMs, sessionEndMs);
69
+ const existing = commitAssignment.get(commit.hash);
70
+
71
+ if (!existing) {
72
+ commitAssignment.set(commit.hash, { session, hasFileOverlap, distance });
73
+ } else {
74
+ // Prefer file-based match over time-only match
75
+ if (hasFileOverlap && !existing.hasFileOverlap) {
76
+ commitAssignment.set(commit.hash, { session, hasFileOverlap, distance });
77
+ } else if (hasFileOverlap === existing.hasFileOverlap && distance < existing.distance) {
78
+ // Same match type — prefer temporally closer session
79
+ commitAssignment.set(commit.hash, { session, hasFileOverlap, distance });
80
+ }
81
+ }
65
82
  }
83
+ }
66
84
 
67
- // Claim matched commits
68
- for (const c of matched) {
69
- claimedCommits.add(c.hash);
85
+ // Phase 2: Group commits by assigned session
86
+ const sessionCommits = new Map(); // sessionId -> commit[]
87
+ for (const [hash, { session }] of commitAssignment) {
88
+ if (!sessionCommits.has(session.sessionId)) {
89
+ sessionCommits.set(session.sessionId, []);
90
+ }
91
+ // Find the actual commit object from the repo
92
+ const repoAnalysis = commitsByRepo[session.repoPath];
93
+ const commit = repoAnalysis?.commits?.find(c => c.hash === hash);
94
+ if (commit) {
95
+ sessionCommits.get(session.sessionId).push(commit);
70
96
  }
97
+ }
98
+
99
+ // Phase 3: Build correlated results
100
+ const claimedCommits = new Set(commitAssignment.keys());
101
+ const result = [];
102
+
103
+ for (const session of sessions) {
104
+ const sessionFiles = new Set(session.filesWritten || []);
105
+ const matched = sessionCommits.get(session.sessionId) || [];
71
106
 
72
107
  let linesAdded, linesDeleted;
73
108
  if (sessionFiles.size > 0) {
74
- // Only count lines from files Claude actually edited
75
109
  ({ linesAdded, linesDeleted } = computeOverlappingLines(matched, sessionFiles));
76
110
  } else {
77
- // Fallback (chat-only): count all lines in matched commits
78
111
  linesAdded = matched.reduce((s, c) => s + c.totalAdded, 0);
79
112
  linesDeleted = matched.reduce((s, c) => s + c.totalDeleted, 0);
80
113
  }
@@ -87,7 +120,6 @@ export function correlateSessions(sessions, commitsByRepo) {
87
120
  const messageCount = session.userMessageCount + session.assistantMessageCount;
88
121
  const isOrphaned = messageCount > 10 && matched.length === 0;
89
122
 
90
- // Calculate which session files were committed vs not
91
123
  const committedFiles = new Set(matched.flatMap(c => c.files.map(f => f.path)));
92
124
  const uncommittedFiles = [...sessionFiles].filter(f => !committedFiles.has(f));
93
125
 
@@ -119,7 +151,7 @@ export function correlateSessions(sessions, commitsByRepo) {
119
151
  }
120
152
  }
121
153
 
122
- // Re-sort result by start time descending (most recent first for display)
154
+ // Sort result by start time descending (most recent first for display)
123
155
  result.sort((a, b) => new Date(b.startTime).getTime() - new Date(a.startTime).getTime());
124
156
 
125
157
  return { correlatedSessions: result, organicCommits };
package/src/index.js CHANGED
@@ -9,8 +9,11 @@ import { correlateSessions } from './correlator.js';
9
9
  import { computeMetrics } from './metrics.js';
10
10
  import { loadCache, saveCache, deleteCache, getStaleFiles } from './cache.js';
11
11
  import { createServer } from './server.js';
12
+ import { readFileSync } from 'node:fs';
12
13
 
13
- const VERSION = '0.1.1';
14
+ const { version: VERSION } = JSON.parse(
15
+ readFileSync(new URL('../package.json', import.meta.url), 'utf8')
16
+ );
14
17
 
15
18
  async function main() {
16
19
  const program = new Command();
@@ -1,9 +0,0 @@
1
- import { defineConfig } from '@playwright/test';
2
-
3
- export default defineConfig({
4
- testDir: './tests',
5
- timeout: 30000,
6
- use: {
7
- baseURL: 'http://localhost:3457',
8
- },
9
- });