spendbyteam 0.2.2 → 0.2.3

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/README.md CHANGED
@@ -25,8 +25,18 @@ Total $1,086.20 112 2.4B
25
25
  **`report` makes no network calls at all.** It only reads those files and prints a table; you can run it with Wi-Fi off. The network code in this package belongs to `connect` and `init`, and runs only when you call them. On a work laptop you may prefer to pin a version instead of pulling the latest each time:
26
26
 
27
27
  ```bash
28
- npx spendbyteam@0.2.2 report
29
- ``` Costs use Anthropic's API list prices, including cache reads, 5-minute and 1-hour cache writes, and fast mode.
28
+ npx spendbyteam@0.2.3 report
29
+ ```
30
+
31
+ Costs use Anthropic's API list prices, including cache reads, 5-minute and 1-hour cache writes, and fast mode.
32
+
33
+ **Claude Code deletes these logs after 30 days by default** ([`cleanupPeriodDays`](https://code.claude.com/docs/en/settings)), so `report` can never look further back than that. It tells you when your logs are on the default and shows the oldest one left. To keep them, add this to `~/.claude/settings.json`:
34
+
35
+ ```json
36
+ { "cleanupPeriodDays": 3650 }
37
+ ```
38
+
39
+ If your organization sets `cleanupPeriodDays` in managed settings, that value wins and `report` says so.
30
40
 
31
41
  | Option | |
32
42
  |---|---|
package/dist/report.js CHANGED
@@ -5,6 +5,37 @@ import { costOf, normalizeModel } from './pricing.js';
5
5
  import { UsageError } from './env.js';
6
6
  export const GROUPS = ['project', 'model', 'branch', 'day', 'session'];
7
7
  const emptySplit = () => ({ input: 0, output: 0, cacheWrite: 0, cacheRead: 0 });
8
+ /** Claude Code's documented default for cleanupPeriodDays. */
9
+ export const DEFAULT_CLEANUP_DAYS = 30;
10
+ function readCleanupDays(file) {
11
+ try {
12
+ const v = JSON.parse(fs.readFileSync(file, 'utf8'))?.cleanupPeriodDays;
13
+ return typeof v === 'number' && Number.isFinite(v) && v >= 1 ? v : undefined;
14
+ }
15
+ catch {
16
+ return undefined;
17
+ }
18
+ }
19
+ /** Managed settings win over user settings; without either, Claude Code uses 30 days. */
20
+ export function cleanupPeriod(env = process.env, platform = process.platform) {
21
+ const managed = env.SPENDBYTEAM_MANAGED_SETTINGS ??
22
+ (platform === 'darwin'
23
+ ? '/Library/Application Support/ClaudeCode/managed-settings.json'
24
+ : platform === 'win32'
25
+ ? 'C:\\Program Files\\ClaudeCode\\managed-settings.json'
26
+ : '/etc/claude-code/managed-settings.json');
27
+ const m = readCleanupDays(managed);
28
+ if (m !== undefined)
29
+ return { days: m, source: 'managed' };
30
+ const configured = env.CLAUDE_CONFIG_DIR?.split(',').map((d) => d.trim()).filter(Boolean);
31
+ const bases = configured?.length ? configured : [path.join(homeDir(), '.claude')];
32
+ for (const b of bases) {
33
+ const u = readCleanupDays(path.join(b, 'settings.json'));
34
+ if (u !== undefined)
35
+ return { days: u, source: 'settings' };
36
+ }
37
+ return { days: DEFAULT_CLEANUP_DAYS, source: 'default' };
38
+ }
8
39
  /** Where Claude Code keeps its session logs: $CLAUDE_CONFIG_DIR (comma-separated), else ~/.claude and ~/.config/claude. */
9
40
  export function logDirs() {
10
41
  const configured = process.env.CLAUDE_CONFIG_DIR?.split(',').map((d) => d.trim()).filter(Boolean);
@@ -149,6 +180,9 @@ export async function buildReport(opts = {}) {
149
180
  const dirs = opts.dirs ?? logDirs();
150
181
  const files = dirs.flatMap((d) => jsonlFiles(d));
151
182
  const { requests, sessionStart } = await readRequests(files, since, opts.onProgress);
183
+ let oldestMs = Infinity;
184
+ for (const f of files)
185
+ oldestMs = Math.min(oldestMs, fs.statSync(f).mtimeMs);
152
186
  const groups = new Map();
153
187
  const unpriced = new Map();
154
188
  const sessions = new Set();
@@ -215,6 +249,7 @@ export async function buildReport(opts = {}) {
215
249
  total: { cost: total, requests: [...groups.values()].reduce((n, g) => n + g.requests, 0), sessions: sessions.size, tokens },
216
250
  unpriced: [...unpriced.entries()].map(([model, n]) => ({ model, requests: n })).sort((a, b) => b.requests - a.requests),
217
251
  files: files.length,
252
+ retention: { ...cleanupPeriod(), oldestLog: Number.isFinite(oldestMs) ? localDay(oldestMs) : null },
218
253
  };
219
254
  }
220
255
  const usd = (n) => `$${n.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
@@ -274,13 +309,25 @@ export function renderReport(r) {
274
309
  out.push(bold(fmt(totalRow)));
275
310
  return finish(r, out);
276
311
  }
312
+ /** Claude Code deletes session logs after cleanupPeriodDays (30 by default); say so, since this report can't see past it. */
313
+ export function retentionNote(t) {
314
+ if (t.days > 90)
315
+ return [];
316
+ const since = t.oldestLog ? `, so the oldest log left on this machine is from ${t.oldestLog}` : '';
317
+ const why = t.source === 'default' ? 'by default' : t.source === 'managed' ? 'set by your organization' : 'set in your settings.json';
318
+ const lines = ['', `${bold('Heads-up:')} Claude Code deletes these logs after ${t.days} days (${why})${since}.`];
319
+ if (t.source !== 'managed')
320
+ lines.push(dim(`To keep them, add "cleanupPeriodDays": 3650 to ~/.claude/settings.json.`));
321
+ return lines;
322
+ }
277
323
  function finish(r, out) {
278
324
  if (r.unpriced.length) {
279
325
  out.push('');
280
326
  out.push(dim(`Not priced (unknown model): ${r.unpriced.map((u) => `${u.model} × ${u.requests}`).join(', ')}`));
281
327
  }
328
+ out.push(...retentionNote(r.retention));
282
329
  out.push('');
283
- out.push(`This is one machine. To see it for your whole team, per project and client:`);
330
+ out.push(`This is one machine. To see it for your whole team, per project and client, with up to 13 months of history:`);
284
331
  out.push(` ${bold('npx spendbyteam connect')} ${dim(`(free for 3 developers · ${CTA_URL})`)}`);
285
332
  return out.join('\n');
286
333
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spendbyteam",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "See what Claude Code costs per project, model, branch or day (offline), and connect it to SpendByTeam for your team.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://spendbyteam.com/docs/install",