tokenusage-cli 0.1.0
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/CHANGELOG.md +19 -0
- package/LICENSE +22 -0
- package/README.md +94 -0
- package/THIRD_PARTY_NOTICES.md +140 -0
- package/bin/tokenusage.js +69 -0
- package/package.json +59 -0
- package/src/cli.js +134 -0
- package/src/commands/device-login.js +205 -0
- package/src/commands/diagnostics.js +38 -0
- package/src/commands/doctor.js +110 -0
- package/src/commands/init.js +2436 -0
- package/src/commands/sessions.js +29 -0
- package/src/commands/status.js +1747 -0
- package/src/commands/sync.js +7934 -0
- package/src/commands/uninstall.js +312 -0
- package/src/lib/account-usage-dedup.js +267 -0
- package/src/lib/antigravity-paths.js +48 -0
- package/src/lib/ark-agent-plan-limits.js +438 -0
- package/src/lib/ark-coding-plan-limits.js +537 -0
- package/src/lib/async-queue-reader.js +138 -0
- package/src/lib/atomic-json.js +27 -0
- package/src/lib/bearer-token.js +11 -0
- package/src/lib/categorizer-utils.js +232 -0
- package/src/lib/claude-categorizer.js +1343 -0
- package/src/lib/claude-config.js +279 -0
- package/src/lib/claude-user-identity.js +56 -0
- package/src/lib/cli-ui.js +179 -0
- package/src/lib/cloud-account.js +541 -0
- package/src/lib/cloud-device-token.js +141 -0
- package/src/lib/cloud-sync-prefs.js +14 -0
- package/src/lib/cloud-usage-reset.js +115 -0
- package/src/lib/codex-config.js +595 -0
- package/src/lib/codex-context-breakdown.js +806 -0
- package/src/lib/codex-model-attribution.js +128 -0
- package/src/lib/codex-rollout-parser.js +1471 -0
- package/src/lib/codex-service-tier.js +49 -0
- package/src/lib/codex-token-refresh.js +140 -0
- package/src/lib/codex-token-usage.js +195 -0
- package/src/lib/codex-usage-record.js +195 -0
- package/src/lib/command-runner.js +313 -0
- package/src/lib/commandcode-limits.js +317 -0
- package/src/lib/context-health.js +124 -0
- package/src/lib/cursor-config.js +605 -0
- package/src/lib/cursor-store.js +1918 -0
- package/src/lib/debug-flags.js +7 -0
- package/src/lib/devin-limits.js +249 -0
- package/src/lib/devin-usage.js +212 -0
- package/src/lib/diagnostics.js +383 -0
- package/src/lib/doctor.js +385 -0
- package/src/lib/dumate.js +95 -0
- package/src/lib/fs.js +455 -0
- package/src/lib/function-url.js +14 -0
- package/src/lib/gemini-config.js +291 -0
- package/src/lib/git-outcomes.js +243 -0
- package/src/lib/grok-context-breakdown.js +890 -0
- package/src/lib/grok-hook.js +360 -0
- package/src/lib/grok-limits.js +865 -0
- package/src/lib/grok-usage.js +98 -0
- package/src/lib/heatmap-compact.js +114 -0
- package/src/lib/ingest-sanitize.js +41 -0
- package/src/lib/init-flow.js +42 -0
- package/src/lib/install-resolver.js +170 -0
- package/src/lib/jsonl-lines.js +134 -0
- package/src/lib/kimi-model-repair.js +173 -0
- package/src/lib/kimi-profile.js +61 -0
- package/src/lib/kimi-session.js +216 -0
- package/src/lib/local-api.js +3588 -0
- package/src/lib/machine-id.js +238 -0
- package/src/lib/multi-install-parser.js +113 -0
- package/src/lib/notify-command.js +38 -0
- package/src/lib/omp-hook.js +589 -0
- package/src/lib/openclaw-cli.js +104 -0
- package/src/lib/openclaw-hook.js +375 -0
- package/src/lib/openclaw-session-plugin.js +560 -0
- package/src/lib/opencode-config.js +121 -0
- package/src/lib/opencode-go-limits.js +807 -0
- package/src/lib/outcomes-engine.js +253 -0
- package/src/lib/passive-mode.js +219 -0
- package/src/lib/pet-packages.js +722 -0
- package/src/lib/pricing/curated-overrides.json +822 -0
- package/src/lib/pricing/index.js +379 -0
- package/src/lib/pricing/litellm-fetcher.js +172 -0
- package/src/lib/pricing/matcher.js +456 -0
- package/src/lib/pricing/seed-snapshot.json +1 -0
- package/src/lib/progress.js +76 -0
- package/src/lib/project-usage-purge.js +117 -0
- package/src/lib/prompt.js +24 -0
- package/src/lib/provider-status.js +104 -0
- package/src/lib/proxy-env.js +489 -0
- package/src/lib/proxy-settings.js +163 -0
- package/src/lib/qoder-cn-cli-limits.js +119 -0
- package/src/lib/qoder-limits.js +880 -0
- package/src/lib/quiet-warnings.js +26 -0
- package/src/lib/rollout.js +25966 -0
- package/src/lib/runtime-config.js +259 -0
- package/src/lib/scan-roots.js +277 -0
- package/src/lib/sentry.js +231 -0
- package/src/lib/session-analytics.js +2972 -0
- package/src/lib/session-performance.js +237 -0
- package/src/lib/session-upload.js +379 -0
- package/src/lib/skill-usage.js +267 -0
- package/src/lib/skills-manager.js +1591 -0
- package/src/lib/source-metadata.js +48 -0
- package/src/lib/sqlite-reader.js +215 -0
- package/src/lib/star-cta.js +73 -0
- package/src/lib/subscription-manager.js +293 -0
- package/src/lib/subscriptions.js +508 -0
- package/src/lib/telemetry.js +193 -0
- package/src/lib/tracker-paths.js +16 -0
- package/src/lib/trae-cn-config.js +548 -0
- package/src/lib/trae-db.js +127 -0
- package/src/lib/trae-snapshot.js +230 -0
- package/src/lib/trae-sqlite.js +143 -0
- package/src/lib/trae-usage.js +121 -0
- package/src/lib/upload-queue.js +134 -0
- package/src/lib/upload-throttle.js +171 -0
- package/src/lib/usage-limits.js +4552 -0
- package/src/lib/wrapped-aggregator.js +236 -0
- package/src/lib/wsl-probe.js +281 -0
- package/src/lib/zcode-limits.js +1172 -0
- package/src/lib/zcode-reset-credits.js +66 -0
- package/test/cli-smoke.test.js +53 -0
- package/test/ingest-sanitize.test.js +162 -0
- package/test/quiet-warnings.test.js +53 -0
- package/test/session-upload.test.js +427 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
const os = require("node:os");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const fs = require("node:fs/promises");
|
|
4
|
+
|
|
5
|
+
const { ensureDir, readJson, writeJson } = require("./fs");
|
|
6
|
+
const { buildNotifyCommand } = require("./notify-command");
|
|
7
|
+
|
|
8
|
+
const DEFAULT_EVENT = "SessionEnd";
|
|
9
|
+
const DEFAULT_HOOK_NAME = "tokenusage";
|
|
10
|
+
const DEFAULT_MATCHER = "exit|clear|logout|prompt_input_exit|other";
|
|
11
|
+
|
|
12
|
+
function resolveGeminiConfigDir({ home = os.homedir(), env = process.env } = {}) {
|
|
13
|
+
const explicit = typeof env.GEMINI_HOME === "string" ? env.GEMINI_HOME.trim() : "";
|
|
14
|
+
if (explicit) return path.resolve(explicit);
|
|
15
|
+
return path.join(home, ".gemini");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function resolveGeminiSettingsPath({ configDir }) {
|
|
19
|
+
return path.join(configDir, "settings.json");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function upsertGeminiHook({
|
|
23
|
+
settingsPath,
|
|
24
|
+
hookCommand,
|
|
25
|
+
hookName = DEFAULT_HOOK_NAME,
|
|
26
|
+
matcher = DEFAULT_MATCHER,
|
|
27
|
+
event = DEFAULT_EVENT,
|
|
28
|
+
}) {
|
|
29
|
+
const existing = await readJson(settingsPath);
|
|
30
|
+
const settings = normalizeSettings(existing);
|
|
31
|
+
const enableResult = ensureHooksEnabled(settings);
|
|
32
|
+
const baseSettings = enableResult.settings;
|
|
33
|
+
const hooks = normalizeHooks(settings.hooks);
|
|
34
|
+
const entries = normalizeEntries(hooks[event]);
|
|
35
|
+
|
|
36
|
+
const normalized = normalizeEntriesForHook(entries, { hookCommand, hookName });
|
|
37
|
+
let nextEntries = normalized.entries;
|
|
38
|
+
let changed = normalized.changed || enableResult.changed;
|
|
39
|
+
|
|
40
|
+
if (!hasHook(nextEntries, { hookCommand, hookName })) {
|
|
41
|
+
nextEntries = nextEntries.concat([buildHookEntry({ hookCommand, hookName, matcher })]);
|
|
42
|
+
changed = true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!changed) return { changed: false, backupPath: null };
|
|
46
|
+
|
|
47
|
+
const nextHooks = { ...hooks, [event]: nextEntries };
|
|
48
|
+
const nextSettings = { ...baseSettings, hooks: nextHooks };
|
|
49
|
+
const backupPath = await writeGeminiSettings({ settingsPath, settings: nextSettings });
|
|
50
|
+
return { changed: true, backupPath };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function removeGeminiHook({
|
|
54
|
+
settingsPath,
|
|
55
|
+
hookCommand,
|
|
56
|
+
hookName = DEFAULT_HOOK_NAME,
|
|
57
|
+
event = DEFAULT_EVENT,
|
|
58
|
+
}) {
|
|
59
|
+
const existing = await readJson(settingsPath);
|
|
60
|
+
if (!existing) return { removed: false, skippedReason: "settings-missing" };
|
|
61
|
+
|
|
62
|
+
const settings = normalizeSettings(existing);
|
|
63
|
+
const hooks = normalizeHooks(settings.hooks);
|
|
64
|
+
const entries = normalizeEntries(hooks[event]);
|
|
65
|
+
if (entries.length === 0) return { removed: false, skippedReason: "hook-missing" };
|
|
66
|
+
|
|
67
|
+
let removed = false;
|
|
68
|
+
const nextEntries = [];
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const res = stripHookFromEntry(entry, { hookCommand, hookName });
|
|
71
|
+
if (res.removed) removed = true;
|
|
72
|
+
if (res.entry) nextEntries.push(res.entry);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!removed) return { removed: false, skippedReason: "hook-missing" };
|
|
76
|
+
|
|
77
|
+
const nextHooks = { ...hooks };
|
|
78
|
+
if (nextEntries.length > 0) nextHooks[event] = nextEntries;
|
|
79
|
+
else delete nextHooks[event];
|
|
80
|
+
|
|
81
|
+
const nextSettings = { ...settings };
|
|
82
|
+
if (Object.keys(nextHooks).length > 0) nextSettings.hooks = nextHooks;
|
|
83
|
+
else delete nextSettings.hooks;
|
|
84
|
+
|
|
85
|
+
const backupPath = await writeGeminiSettings({ settingsPath, settings: nextSettings });
|
|
86
|
+
return { removed: true, skippedReason: null, backupPath };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function isGeminiHookConfigured({
|
|
90
|
+
settingsPath,
|
|
91
|
+
hookCommand,
|
|
92
|
+
hookName = DEFAULT_HOOK_NAME,
|
|
93
|
+
event = DEFAULT_EVENT,
|
|
94
|
+
}) {
|
|
95
|
+
const settings = await readJson(settingsPath);
|
|
96
|
+
if (!settings || typeof settings !== "object") return false;
|
|
97
|
+
const hooks = settings.hooks;
|
|
98
|
+
if (!hooks || typeof hooks !== "object") return false;
|
|
99
|
+
const entries = normalizeEntries(hooks[event]);
|
|
100
|
+
return hasHook(entries, { hookCommand, hookName });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function buildGeminiHookCommand(notifyPath, options) {
|
|
104
|
+
const cmd = typeof notifyPath === "string" ? notifyPath : "";
|
|
105
|
+
return buildNotifyCommand(cmd, "gemini", options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function buildHookEntry({ hookCommand, hookName, matcher }) {
|
|
109
|
+
const hook = {
|
|
110
|
+
name: hookName,
|
|
111
|
+
type: "command",
|
|
112
|
+
command: hookCommand,
|
|
113
|
+
};
|
|
114
|
+
const entry = { hooks: [hook] };
|
|
115
|
+
if (typeof matcher === "string" && matcher.length > 0) entry.matcher = matcher;
|
|
116
|
+
return entry;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function normalizeSettings(raw) {
|
|
120
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function normalizeHooks(raw) {
|
|
124
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeTools(raw) {
|
|
128
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function normalizeEntries(raw) {
|
|
132
|
+
return Array.isArray(raw) ? raw.slice() : [];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function normalizeCommand(cmd) {
|
|
136
|
+
if (Array.isArray(cmd)) return cmd.map((v) => String(v)).join("\u0000");
|
|
137
|
+
if (typeof cmd === "string") return cmd.trim();
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function normalizeName(name) {
|
|
142
|
+
if (typeof name !== "string") return null;
|
|
143
|
+
const trimmed = name.trim();
|
|
144
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function ensureHooksEnabled(settings) {
|
|
148
|
+
const tools = normalizeTools(settings.tools);
|
|
149
|
+
if (tools.enableHooks === true) return { settings, changed: false };
|
|
150
|
+
const nextTools = { ...tools, enableHooks: true };
|
|
151
|
+
return { settings: { ...settings, tools: nextTools }, changed: true };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function hookMatches(hook, { hookCommand, hookName, requireCommand = false }) {
|
|
155
|
+
if (!hook || typeof hook !== "object") return false;
|
|
156
|
+
const name = normalizeName(hook.name);
|
|
157
|
+
const targetName = normalizeName(hookName);
|
|
158
|
+
const cmd = normalizeCommand(hook.command);
|
|
159
|
+
const targetCmd = normalizeCommand(hookCommand);
|
|
160
|
+
const commandMatches = Boolean(cmd && targetCmd && cmd === targetCmd);
|
|
161
|
+
if (requireCommand) return commandMatches;
|
|
162
|
+
const nameMatches = Boolean(name && targetName && name === targetName);
|
|
163
|
+
return Boolean(commandMatches || nameMatches);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function entryMatches(entry, { hookCommand, hookName, requireCommand = false }) {
|
|
167
|
+
if (!entry || typeof entry !== "object") return false;
|
|
168
|
+
if (entry.command || entry.name)
|
|
169
|
+
return hookMatches(entry, { hookCommand, hookName, requireCommand });
|
|
170
|
+
if (!Array.isArray(entry.hooks)) return false;
|
|
171
|
+
return entry.hooks.some((hook) => hookMatches(hook, { hookCommand, hookName, requireCommand }));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function hasHook(entries, { hookCommand, hookName }) {
|
|
175
|
+
return entries.some((entry) => entryMatches(entry, { hookCommand, hookName }));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function normalizeEntriesForHook(entries, { hookCommand, hookName }) {
|
|
179
|
+
let changed = false;
|
|
180
|
+
const nextEntries = entries.map((entry) => {
|
|
181
|
+
if (!entry || typeof entry !== "object") return entry;
|
|
182
|
+
|
|
183
|
+
if (entry.command || entry.name) {
|
|
184
|
+
if (hookMatches(entry, { hookCommand, hookName })) {
|
|
185
|
+
const next = normalizeHookObject(entry, { hookCommand, hookName });
|
|
186
|
+
if (next !== entry) changed = true;
|
|
187
|
+
return next;
|
|
188
|
+
}
|
|
189
|
+
return entry;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const hooks = Array.isArray(entry.hooks) ? entry.hooks : null;
|
|
193
|
+
if (!hooks) return entry;
|
|
194
|
+
|
|
195
|
+
let hooksChanged = false;
|
|
196
|
+
const nextHooks = hooks.map((hook) => {
|
|
197
|
+
if (hookMatches(hook, { hookCommand, hookName })) {
|
|
198
|
+
const next = normalizeHookObject(hook, { hookCommand, hookName });
|
|
199
|
+
if (next !== hook) hooksChanged = true;
|
|
200
|
+
return next;
|
|
201
|
+
}
|
|
202
|
+
return hook;
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if (!hooksChanged) return entry;
|
|
206
|
+
changed = true;
|
|
207
|
+
return { ...entry, hooks: nextHooks };
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
return { entries: nextEntries, changed };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function normalizeHookObject(hook, { hookCommand, hookName }) {
|
|
214
|
+
const next = { ...hook };
|
|
215
|
+
let changed = false;
|
|
216
|
+
|
|
217
|
+
if (next.type !== "command") {
|
|
218
|
+
next.type = "command";
|
|
219
|
+
changed = true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (hookCommand && next.command !== hookCommand) {
|
|
223
|
+
next.command = hookCommand;
|
|
224
|
+
changed = true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (hookName && next.name !== hookName) {
|
|
228
|
+
next.name = hookName;
|
|
229
|
+
changed = true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return changed ? next : hook;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function stripHookFromEntry(entry, { hookCommand, hookName }) {
|
|
236
|
+
if (!entry || typeof entry !== "object") return { entry, removed: false };
|
|
237
|
+
|
|
238
|
+
if (entry.command || entry.name) {
|
|
239
|
+
if (hookMatches(entry, { hookCommand, hookName, requireCommand: true }))
|
|
240
|
+
return { entry: null, removed: true };
|
|
241
|
+
return { entry, removed: false };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const hooks = Array.isArray(entry.hooks) ? entry.hooks : null;
|
|
245
|
+
if (!hooks) return { entry, removed: false };
|
|
246
|
+
|
|
247
|
+
const nextHooks = hooks.filter(
|
|
248
|
+
(hook) => !hookMatches(hook, { hookCommand, hookName, requireCommand: true }),
|
|
249
|
+
);
|
|
250
|
+
if (nextHooks.length === hooks.length) return { entry, removed: false };
|
|
251
|
+
if (nextHooks.length === 0) return { entry: null, removed: true };
|
|
252
|
+
|
|
253
|
+
return { entry: { ...entry, hooks: nextHooks }, removed: true };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function quoteArg(value) {
|
|
257
|
+
const v = typeof value === "string" ? value : "";
|
|
258
|
+
if (!v) return '""';
|
|
259
|
+
if (/^[A-Za-z0-9_\-./:@]+$/.test(v)) return v;
|
|
260
|
+
// Escape backslashes before quotes: a trailing backslash (Windows paths)
|
|
261
|
+
// would otherwise escape the closing quote and corrupt the hook command.
|
|
262
|
+
return `"${v.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function writeGeminiSettings({ settingsPath, settings }) {
|
|
266
|
+
await ensureDir(path.dirname(settingsPath));
|
|
267
|
+
let backupPath = null;
|
|
268
|
+
try {
|
|
269
|
+
const st = await fs.stat(settingsPath);
|
|
270
|
+
if (st && st.isFile()) {
|
|
271
|
+
backupPath = `${settingsPath}.bak.${new Date().toISOString().replace(/[:.]/g, "-")}`;
|
|
272
|
+
await fs.copyFile(settingsPath, backupPath);
|
|
273
|
+
}
|
|
274
|
+
} catch (_e) {
|
|
275
|
+
// Ignore missing file.
|
|
276
|
+
}
|
|
277
|
+
await writeJson(settingsPath, settings);
|
|
278
|
+
return backupPath;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
module.exports = {
|
|
282
|
+
DEFAULT_EVENT,
|
|
283
|
+
DEFAULT_HOOK_NAME,
|
|
284
|
+
DEFAULT_MATCHER,
|
|
285
|
+
resolveGeminiConfigDir,
|
|
286
|
+
resolveGeminiSettingsPath,
|
|
287
|
+
buildGeminiHookCommand,
|
|
288
|
+
upsertGeminiHook,
|
|
289
|
+
removeGeminiHook,
|
|
290
|
+
isGeminiHookConfigured,
|
|
291
|
+
};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Conservative, metadata-only Git attribution. A commit is attributed only
|
|
4
|
+
// when exactly one local AI session overlaps its author timestamp.
|
|
5
|
+
|
|
6
|
+
const { execFile } = require("node:child_process");
|
|
7
|
+
const fs = require("node:fs");
|
|
8
|
+
const fsp = require("node:fs/promises");
|
|
9
|
+
const os = require("node:os");
|
|
10
|
+
const path = require("node:path");
|
|
11
|
+
const crypto = require("node:crypto");
|
|
12
|
+
|
|
13
|
+
function resolveAutoOutcomesPath(home = os.homedir()) {
|
|
14
|
+
return path.join(home, ".tokenusage", "tracker", "auto-outcomes.jsonl");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function readJsonl(filePath) {
|
|
18
|
+
try { return fs.readFileSync(filePath, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)); }
|
|
19
|
+
catch { return []; }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Async on purpose: this runs inside the local API request path, where a
|
|
23
|
+
// synchronous spawn would freeze every other endpoint for the duration of
|
|
24
|
+
// each git invocation.
|
|
25
|
+
function runGit(cwd, args) {
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
execFile(
|
|
28
|
+
"git",
|
|
29
|
+
args,
|
|
30
|
+
{ cwd, encoding: "utf8", timeout: 15_000, maxBuffer: 32 * 1024 * 1024 },
|
|
31
|
+
(error, stdout) => resolve(error ? "" : String(stdout).trim()),
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function repoRoot(cwd) {
|
|
37
|
+
if (!cwd || !fs.existsSync(cwd)) return null;
|
|
38
|
+
return (await runGit(cwd, ["rev-parse", "--show-toplevel"])) || null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function commitsForWindow(root, from, to) {
|
|
42
|
+
const format = "%H%x1f%aI%x1f%P%x1f%s%x1e";
|
|
43
|
+
const raw = await runGit(root, ["log", "--all", `--since=${from}`, `--until=${to}`, `--format=${format}`]);
|
|
44
|
+
if (!raw) return [];
|
|
45
|
+
return raw.split("\x1e").map((item) => item.trim()).filter(Boolean).map((item) => {
|
|
46
|
+
const [sha, timestamp, parents, subject] = item.split("\x1f");
|
|
47
|
+
return {
|
|
48
|
+
sha,
|
|
49
|
+
timestamp,
|
|
50
|
+
parent_count: String(parents || "").split(/\s+/).filter(Boolean).length,
|
|
51
|
+
reverted: /^revert\b/i.test(subject || ""),
|
|
52
|
+
};
|
|
53
|
+
}).filter((row) => row.sha && Number.isFinite(Date.parse(row.timestamp)));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function writeAtomic(filePath, content) {
|
|
57
|
+
await fsp.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
58
|
+
const temp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
59
|
+
await fsp.writeFile(temp, content, { encoding: "utf8", mode: 0o600 });
|
|
60
|
+
await fsp.rename(temp, filePath);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Attribution walks into the working directory of every recent session and
|
|
64
|
+
// runs git there. On macOS each distinct protected location that gets touched
|
|
65
|
+
// (~/Documents, ~/Downloads, another app's container) raises its own TCC
|
|
66
|
+
// consent prompt, so probing must happen only when there is new work to
|
|
67
|
+
// attribute — never on every dashboard poll.
|
|
68
|
+
const GIT_PROBE_TTL_MS = 30 * 60 * 1000;
|
|
69
|
+
|
|
70
|
+
// A data-only fingerprint of the sessions we would attribute. Computing it
|
|
71
|
+
// touches no filesystem and spawns no git, so an unchanged fingerprint lets us
|
|
72
|
+
// serve the cached sidecar without entering a single project directory.
|
|
73
|
+
function sessionsFingerprint(sessions, maxAgeDays) {
|
|
74
|
+
const hash = crypto.createHash("sha256");
|
|
75
|
+
hash.update(`v1\0${maxAgeDays}\n`);
|
|
76
|
+
const rows = sessions.map(
|
|
77
|
+
(session) => `${session.project_ref}\0${session.session_hash}\0${session.started_at}\0${session.ended_at}`,
|
|
78
|
+
);
|
|
79
|
+
for (const row of rows.sort()) hash.update(`${row}\n`);
|
|
80
|
+
return hash.digest("hex");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The sessions attribution is allowed to look at. Filtering here — before the
|
|
84
|
+
// fingerprint and before any filesystem access — keeps the cache signature and
|
|
85
|
+
// the probe set in agreement, so a skipped directory is never entered and never
|
|
86
|
+
// invalidates the cache either.
|
|
87
|
+
function attributableSessions(sessions, cutoff, home) {
|
|
88
|
+
const kept = [];
|
|
89
|
+
for (const session of sessions || []) {
|
|
90
|
+
if (!session?.project_ref || !session.started_at || !session.ended_at) continue;
|
|
91
|
+
if (Date.parse(session.ended_at) < cutoff) continue;
|
|
92
|
+
if (isTccProtectedPath(session.project_ref, home)) continue;
|
|
93
|
+
kept.push(session);
|
|
94
|
+
}
|
|
95
|
+
return kept;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function gitAttributionDisabled() {
|
|
99
|
+
return ["1", "true"].includes(String(process.env.TOKENUSAGE_DISABLE_GIT_ATTRIBUTION || "").toLowerCase());
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// macOS gates these locations behind TCC: the first access to each one raises
|
|
103
|
+
// its own "Token Usage would like to access data in …" consent dialog, and
|
|
104
|
+
// ad-hoc signing resets that consent on every update, so the dialogs come back
|
|
105
|
+
// each time. Attribution is an opt-in beta that walks into whatever directory a
|
|
106
|
+
// session happened to run in — a token tracker has no business making the OS
|
|
107
|
+
// ask for ~/Documents. Skip these by default; anyone who keeps repos there can
|
|
108
|
+
// opt back in and answer the prompts knowingly.
|
|
109
|
+
const TCC_PROTECTED_HOME_DIRS = ["Documents", "Downloads", "Desktop", "Movies", "Music", "Pictures", "Library"];
|
|
110
|
+
|
|
111
|
+
function protectedProbeAllowed() {
|
|
112
|
+
return ["1", "true"].includes(String(process.env.TOKENUSAGE_GIT_ATTRIBUTION_PROTECTED_DIRS || "").toLowerCase());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isTccProtectedPath(target, home) {
|
|
116
|
+
if (process.platform !== "darwin" || protectedProbeAllowed()) return false;
|
|
117
|
+
const resolved = path.resolve(String(target || ""));
|
|
118
|
+
if (resolved === "/Volumes" || resolved.startsWith("/Volumes/")) return true;
|
|
119
|
+
const base = path.resolve(String(home || ""));
|
|
120
|
+
if (resolved !== base && !resolved.startsWith(`${base}${path.sep}`)) return false;
|
|
121
|
+
const [first] = path.relative(base, resolved).split(path.sep);
|
|
122
|
+
return TCC_PROTECTED_HOME_DIRS.includes(first);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function buildGitOutcomesInternal(sessions, { home = os.homedir(), force = false, maxAgeDays = 90 } = {}) {
|
|
126
|
+
const outputPath = resolveAutoOutcomesPath(home);
|
|
127
|
+
const metaPath = `${outputPath}.meta.json`;
|
|
128
|
+
const cutoff = Date.now() - Math.max(1, Number(maxAgeDays) || 90) * 86400_000;
|
|
129
|
+
|
|
130
|
+
// Escape hatch for anyone who would rather keep Token Usage out of their
|
|
131
|
+
// project directories entirely. Serves whatever was already attributed.
|
|
132
|
+
if (gitAttributionDisabled()) return readJsonl(outputPath);
|
|
133
|
+
|
|
134
|
+
// Cheap gate, before any filesystem access: same sessions as last time and
|
|
135
|
+
// the last probe is recent, so there is nothing new to attribute.
|
|
136
|
+
const candidates = attributableSessions(sessions, cutoff, home);
|
|
137
|
+
const sessionSignature = sessionsFingerprint(candidates, maxAgeDays);
|
|
138
|
+
if (!force) {
|
|
139
|
+
try {
|
|
140
|
+
const meta = JSON.parse(fs.readFileSync(metaPath, "utf8"));
|
|
141
|
+
const age = Date.now() - Date.parse(meta.generated_at || "");
|
|
142
|
+
if (
|
|
143
|
+
meta.version === 2
|
|
144
|
+
&& meta.session_signature === sessionSignature
|
|
145
|
+
&& Number.isFinite(age)
|
|
146
|
+
&& age >= 0
|
|
147
|
+
&& age < GIT_PROBE_TTL_MS
|
|
148
|
+
) {
|
|
149
|
+
return readJsonl(outputPath);
|
|
150
|
+
}
|
|
151
|
+
} catch { /* first run, or a v1 sidecar written before this gate existed */ }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const groups = new Map();
|
|
155
|
+
const rootCache = new Map();
|
|
156
|
+
for (const session of candidates) {
|
|
157
|
+
let root = rootCache.get(session.project_ref);
|
|
158
|
+
if (root === undefined) {
|
|
159
|
+
root = await repoRoot(session.project_ref);
|
|
160
|
+
rootCache.set(session.project_ref, root);
|
|
161
|
+
}
|
|
162
|
+
if (!root) continue;
|
|
163
|
+
if (!groups.has(root)) groups.set(root, []);
|
|
164
|
+
groups.get(root).push(session);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const signatureHash = crypto.createHash("sha256");
|
|
168
|
+
for (const [root, repoSessions] of [...groups.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
169
|
+
signatureHash.update(`${root}\0${await runGit(root, ["for-each-ref", "--format=%(objectname)"])}\n`);
|
|
170
|
+
for (const session of repoSessions) signatureHash.update(`${session.session_hash}\0${session.ended_at}\n`);
|
|
171
|
+
}
|
|
172
|
+
const signature = signatureHash.digest("hex");
|
|
173
|
+
if (!force) {
|
|
174
|
+
try {
|
|
175
|
+
const meta = JSON.parse(fs.readFileSync(metaPath, "utf8"));
|
|
176
|
+
// Git state is unchanged too — refresh the timestamp so the cheap gate
|
|
177
|
+
// above can short-circuit the next poll instead of re-probing.
|
|
178
|
+
if (meta.signature === signature) {
|
|
179
|
+
await writeAtomic(metaPath, `${JSON.stringify({ ...meta, version: 2, session_signature: sessionSignature, generated_at: new Date().toISOString() })}\n`);
|
|
180
|
+
return readJsonl(outputPath);
|
|
181
|
+
}
|
|
182
|
+
} catch { /* first run */ }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const outcomes = [];
|
|
186
|
+
for (const [root, repoSessions] of groups) {
|
|
187
|
+
const start = repoSessions.reduce((min, row) => !min || row.started_at < min ? row.started_at : min, "");
|
|
188
|
+
const endMs = Math.max(...repoSessions.map((row) => Date.parse(row.ended_at))) + 60 * 60 * 1000;
|
|
189
|
+
const commits = await commitsForWindow(root, start, new Date(endMs).toISOString());
|
|
190
|
+
for (const commit of commits) {
|
|
191
|
+
const commitMs = Date.parse(commit.timestamp);
|
|
192
|
+
const candidates = repoSessions.filter((session) => {
|
|
193
|
+
const sessionStart = Date.parse(session.started_at) - 10 * 60 * 1000;
|
|
194
|
+
const sessionEnd = Date.parse(session.ended_at) + 60 * 60 * 1000;
|
|
195
|
+
return commitMs >= sessionStart && commitMs <= sessionEnd;
|
|
196
|
+
});
|
|
197
|
+
if (candidates.length !== 1) continue;
|
|
198
|
+
const session = candidates[0];
|
|
199
|
+
outcomes.push({
|
|
200
|
+
timestamp: commit.timestamp,
|
|
201
|
+
model: session.model || "unknown",
|
|
202
|
+
tool: session.source || "unknown",
|
|
203
|
+
accepted: !commit.reverted,
|
|
204
|
+
task_type: "git_commit",
|
|
205
|
+
status: commit.reverted ? "reverted" : "committed",
|
|
206
|
+
session_hash: session.session_hash,
|
|
207
|
+
commit_hash: commit.sha.slice(0, 12),
|
|
208
|
+
parent_count: commit.parent_count,
|
|
209
|
+
confidence: "heuristic",
|
|
210
|
+
methodology: "single-overlapping-session",
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
outcomes.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
215
|
+
await writeAtomic(outputPath, outcomes.map((row) => JSON.stringify(row)).join("\n") + (outcomes.length ? "\n" : ""));
|
|
216
|
+
await writeAtomic(metaPath, `${JSON.stringify({
|
|
217
|
+
version: 2,
|
|
218
|
+
signature,
|
|
219
|
+
session_signature: sessionSignature,
|
|
220
|
+
generated_at: new Date().toISOString(),
|
|
221
|
+
max_age_days: maxAgeDays,
|
|
222
|
+
})}\n`);
|
|
223
|
+
return outcomes;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Overlapping outcomes/session-insights requests must not spawn duplicate git
|
|
227
|
+
// scans nor race the atomic sidecar write; share one build per home.
|
|
228
|
+
const gitOutcomesBuilds = new Map();
|
|
229
|
+
|
|
230
|
+
function buildGitOutcomes(sessions, options = {}) {
|
|
231
|
+
const home = path.resolve(String(options.home || os.homedir()));
|
|
232
|
+
const existing = gitOutcomesBuilds.get(home);
|
|
233
|
+
if (existing) return existing;
|
|
234
|
+
const promise = buildGitOutcomesInternal(sessions, { ...options, home });
|
|
235
|
+
gitOutcomesBuilds.set(home, promise);
|
|
236
|
+
const clear = () => {
|
|
237
|
+
if (gitOutcomesBuilds.get(home) === promise) gitOutcomesBuilds.delete(home);
|
|
238
|
+
};
|
|
239
|
+
promise.then(clear, clear);
|
|
240
|
+
return promise;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
module.exports = { resolveAutoOutcomesPath, buildGitOutcomes, repoRoot, commitsForWindow };
|