tokenusage-cli 0.1.0 → 0.2.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 +39 -19
- package/LICENSE +22 -22
- package/README.md +94 -94
- package/THIRD_PARTY_NOTICES.md +140 -140
- package/bin/tokenusage.js +69 -69
- package/package.json +59 -59
- package/src/cli.js +146 -134
- package/src/commands/device-login.js +205 -205
- package/src/commands/diagnostics.js +38 -38
- package/src/commands/doctor.js +110 -110
- package/src/commands/init.js +2484 -2436
- package/src/commands/pause.js +77 -0
- package/src/commands/sessions.js +29 -29
- package/src/commands/status.js +1758 -1747
- package/src/commands/sync.js +8048 -7934
- package/src/commands/uninstall.js +322 -312
- package/src/lib/account-usage-dedup.js +267 -267
- package/src/lib/antigravity-paths.js +48 -48
- package/src/lib/ark-agent-plan-limits.js +438 -438
- package/src/lib/ark-coding-plan-limits.js +537 -537
- package/src/lib/async-queue-reader.js +138 -138
- package/src/lib/atomic-json.js +27 -27
- package/src/lib/bearer-token.js +11 -11
- package/src/lib/categorizer-utils.js +232 -232
- package/src/lib/claude-categorizer.js +1343 -1343
- package/src/lib/claude-config.js +279 -279
- package/src/lib/claude-user-identity.js +56 -56
- package/src/lib/cli-ui.js +179 -179
- package/src/lib/cloud-account.js +541 -541
- package/src/lib/cloud-device-token.js +141 -141
- package/src/lib/cloud-sync-prefs.js +14 -14
- package/src/lib/cloud-usage-reset.js +115 -115
- package/src/lib/codex-config.js +595 -595
- package/src/lib/codex-context-breakdown.js +806 -806
- package/src/lib/codex-model-attribution.js +128 -128
- package/src/lib/codex-rollout-parser.js +1471 -1471
- package/src/lib/codex-service-tier.js +49 -49
- package/src/lib/codex-token-refresh.js +140 -140
- package/src/lib/codex-token-usage.js +195 -195
- package/src/lib/codex-usage-record.js +195 -195
- package/src/lib/collection-pause.js +77 -0
- package/src/lib/command-runner.js +313 -313
- package/src/lib/commandcode-limits.js +317 -317
- package/src/lib/context-health.js +124 -124
- package/src/lib/cursor-config.js +605 -605
- package/src/lib/cursor-store.js +1918 -1918
- package/src/lib/debug-flags.js +7 -7
- package/src/lib/devin-limits.js +249 -249
- package/src/lib/devin-usage.js +212 -212
- package/src/lib/diagnostics.js +383 -383
- package/src/lib/doctor.js +385 -385
- package/src/lib/dumate.js +95 -95
- package/src/lib/fs.js +455 -455
- package/src/lib/function-url.js +14 -14
- package/src/lib/gemini-config.js +291 -291
- package/src/lib/git-outcomes.js +243 -243
- package/src/lib/grok-context-breakdown.js +890 -890
- package/src/lib/grok-hook.js +360 -360
- package/src/lib/grok-limits.js +865 -865
- package/src/lib/grok-usage.js +98 -98
- package/src/lib/heatmap-compact.js +114 -114
- package/src/lib/ingest-sanitize.js +41 -41
- package/src/lib/init-flow.js +42 -42
- package/src/lib/install-resolver.js +170 -170
- package/src/lib/jsonl-lines.js +134 -134
- package/src/lib/kimi-model-repair.js +173 -173
- package/src/lib/kimi-profile.js +61 -61
- package/src/lib/kimi-session.js +216 -216
- package/src/lib/local-api.js +3588 -3588
- package/src/lib/machine-id.js +238 -238
- package/src/lib/multi-install-parser.js +113 -113
- package/src/lib/notify-command.js +38 -38
- package/src/lib/omp-hook.js +589 -589
- package/src/lib/openclaw-cli.js +104 -104
- package/src/lib/openclaw-hook.js +375 -375
- package/src/lib/openclaw-session-plugin.js +560 -560
- package/src/lib/opencode-config.js +121 -121
- package/src/lib/opencode-go-limits.js +807 -807
- package/src/lib/outcomes-engine.js +253 -253
- package/src/lib/passive-mode.js +219 -219
- package/src/lib/pet-packages.js +722 -722
- package/src/lib/pricing/curated-overrides.json +822 -822
- package/src/lib/pricing/index.js +379 -379
- package/src/lib/pricing/litellm-fetcher.js +172 -172
- package/src/lib/pricing/matcher.js +456 -456
- package/src/lib/pricing/seed-snapshot.json +1 -1
- package/src/lib/progress.js +76 -76
- package/src/lib/project-usage-purge.js +117 -117
- package/src/lib/prompt.js +24 -24
- package/src/lib/provider-status.js +104 -104
- package/src/lib/proxy-env.js +489 -489
- package/src/lib/proxy-settings.js +163 -163
- package/src/lib/qoder-cn-cli-limits.js +119 -119
- package/src/lib/qoder-limits.js +880 -880
- package/src/lib/quiet-warnings.js +26 -26
- package/src/lib/rollout.js +26005 -25966
- package/src/lib/runtime-config.js +9 -4
- package/src/lib/scan-roots.js +277 -277
- package/src/lib/scheduled-task.js +145 -0
- package/src/lib/sentry.js +231 -231
- package/src/lib/session-analytics.js +2972 -2972
- package/src/lib/session-performance.js +237 -237
- package/src/lib/session-upload.js +379 -379
- package/src/lib/skill-usage.js +267 -267
- package/src/lib/skills-manager.js +1591 -1591
- package/src/lib/source-metadata.js +48 -48
- package/src/lib/sqlite-reader.js +215 -215
- package/src/lib/star-cta.js +73 -73
- package/src/lib/subscription-manager.js +293 -293
- package/src/lib/subscriptions.js +508 -508
- package/src/lib/sync-report.js +138 -0
- package/src/lib/telemetry.js +193 -193
- package/src/lib/tracker-paths.js +16 -16
- package/src/lib/trae-cn-config.js +548 -548
- package/src/lib/trae-db.js +127 -127
- package/src/lib/trae-snapshot.js +230 -230
- package/src/lib/trae-sqlite.js +143 -143
- package/src/lib/trae-usage.js +121 -121
- package/src/lib/upload-queue.js +134 -134
- package/src/lib/upload-throttle.js +171 -171
- package/src/lib/usage-limits.js +4552 -4552
- package/src/lib/wrapped-aggregator.js +236 -236
- package/src/lib/wsl-probe.js +281 -281
- package/src/lib/zcode-limits.js +1172 -1172
- package/src/lib/zcode-reset-credits.js +66 -66
- package/test/audit-fixes.test.js +200 -0
- package/test/cli-smoke.test.js +53 -53
- package/test/collection-controls.test.js +181 -0
- package/test/ingest-sanitize.test.js +162 -162
- package/test/quiet-warnings.test.js +53 -53
- package/test/session-upload.test.js +427 -427
package/src/lib/passive-mode.js
CHANGED
|
@@ -1,219 +1,219 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Passive-mode detection.
|
|
3
|
-
*
|
|
4
|
-
* Token Tracker normally collects token usage by installing SessionEnd hooks
|
|
5
|
-
* into supported CLIs (Claude Code, Codex CLI, Gemini CLI, …). The hook
|
|
6
|
-
* fires `notify.cjs` after each turn → sync runs immediately → data is
|
|
7
|
-
* fresh within seconds.
|
|
8
|
-
*
|
|
9
|
-
* On environments where hook installation fails — WSL with read-only mount,
|
|
10
|
-
* UNC paths on Windows, locked `settings.json`, sandboxed processes —
|
|
11
|
-
* SessionEnd hooks never fire, but the underlying CLIs *still* write
|
|
12
|
-
* session logs (`~/.claude/projects/`, `~/.gemini/sessions/`, etc.). Our
|
|
13
|
-
* parsers can read those logs directly without the hook; the only loss is
|
|
14
|
-
* "data is fresh within seconds" → it's now "data is fresh after the next
|
|
15
|
-
* scheduled `tracker sync`".
|
|
16
|
-
*
|
|
17
|
-
* This module surfaces that fact: for each hook-driven provider, decide
|
|
18
|
-
* whether the local install is in **passive mode** (no hook + log dir
|
|
19
|
-
* present). `tracker status` reports it, and the dashboard can show a
|
|
20
|
-
* single banner asking the user whether to acknowledge & continue in
|
|
21
|
-
* passive mode or attempt re-install (`tracker init`).
|
|
22
|
-
*
|
|
23
|
-
* Detection is purely best-effort — we never *fail* sync because hooks are
|
|
24
|
-
* missing; we just label the source so users / AI agents know latency is
|
|
25
|
-
* minutes, not seconds.
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
|
|
29
|
-
const fs = require("node:fs");
|
|
30
|
-
const path = require("node:path");
|
|
31
|
-
const { expandHome } = require("./scan-roots");
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @typedef {Object} PassiveProvider
|
|
35
|
-
* @property {string} name - canonical provider name
|
|
36
|
-
* @property {boolean} hook_expected - whether this provider supports hooks
|
|
37
|
-
* @property {boolean} hook_installed - true when the hook is set
|
|
38
|
-
* @property {boolean} logs_present - true when the provider's log dir
|
|
39
|
-
* exists and contains at least one
|
|
40
|
-
* session file we can parse
|
|
41
|
-
* @property {boolean} passive - true iff hook_expected &&
|
|
42
|
-
* !hook_installed && logs_present
|
|
43
|
-
* @property {string|null} hook_failure_reason - if hook_expected and not
|
|
44
|
-
* installed, why we think it failed
|
|
45
|
-
* (e.g. "settings.json not writable",
|
|
46
|
-
* "config dir not found")
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
function dirHasFile(dir, predicate) {
|
|
50
|
-
if (!dir || !fs.existsSync(dir)) return false;
|
|
51
|
-
let entries;
|
|
52
|
-
try {
|
|
53
|
-
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
54
|
-
} catch (_e) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
for (const entry of entries) {
|
|
58
|
-
const full = path.join(dir, entry.name);
|
|
59
|
-
// Let the predicate accept either a file OR a directory name (so
|
|
60
|
-
// "sessions" or "tmp" as a bare subdirectory still counts).
|
|
61
|
-
if (!predicate || predicate(full, entry.name, entry.isDirectory())) return true;
|
|
62
|
-
if (entry.isDirectory()) {
|
|
63
|
-
// Shallow recurse — at most 1 level (covers projects/<name>/*.jsonl)
|
|
64
|
-
if (dirHasFile(full, predicate)) return true;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function classifyWritableFailure(filePath) {
|
|
71
|
-
if (!filePath) return "no path";
|
|
72
|
-
try {
|
|
73
|
-
fs.accessSync(filePath, fs.constants.W_OK);
|
|
74
|
-
return "writable (hook may have been removed externally)";
|
|
75
|
-
} catch (e) {
|
|
76
|
-
if (e && e.code === "ENOENT") return "settings file missing";
|
|
77
|
-
if (e && e.code === "EACCES") return "permission denied";
|
|
78
|
-
if (e && e.code === "EROFS") return "read-only filesystem";
|
|
79
|
-
return e?.code || "unknown";
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Detect passive mode for each hook-driven provider.
|
|
85
|
-
*
|
|
86
|
-
* @param {Object} opts
|
|
87
|
-
* @param {string} opts.home - user home directory
|
|
88
|
-
* @param {Object} opts.hookStatus - per-provider hook-installed booleans
|
|
89
|
-
* (already collected by status.js); shape: { claude, gemini, codex,
|
|
90
|
-
* every_code, opencode, openclaw, codebuddy, workbuddy, grok }
|
|
91
|
-
* @returns {PassiveProvider[]}
|
|
92
|
-
*/
|
|
93
|
-
function detectPassiveProviders({ home, hookStatus, env = process.env }) {
|
|
94
|
-
const out = [];
|
|
95
|
-
|
|
96
|
-
// Claude Code — logs at ~/.claude/projects/<name>/*.jsonl
|
|
97
|
-
out.push(buildEntry({
|
|
98
|
-
name: "claude",
|
|
99
|
-
hookExpected: true,
|
|
100
|
-
hookInstalled: Boolean(hookStatus?.claude),
|
|
101
|
-
logsDir: path.join(home, ".claude", "projects"),
|
|
102
|
-
logsPredicate: (full) => full.endsWith(".jsonl"),
|
|
103
|
-
settingsPath: path.join(home, ".claude", "settings.json"),
|
|
104
|
-
}));
|
|
105
|
-
|
|
106
|
-
// Gemini CLI — logs at ~/.gemini/tmp/<session-id>/logs.json
|
|
107
|
-
// (path varies by version; presence of ~/.gemini/sessions or
|
|
108
|
-
// ~/.gemini/tmp is enough to call it "active")
|
|
109
|
-
out.push(buildEntry({
|
|
110
|
-
name: "gemini",
|
|
111
|
-
hookExpected: true,
|
|
112
|
-
hookInstalled: Boolean(hookStatus?.gemini),
|
|
113
|
-
logsDir: path.join(home, ".gemini"),
|
|
114
|
-
logsPredicate: (_full, name, isDir) =>
|
|
115
|
-
(isDir && (name === "tmp" || name === "sessions")) || name === "logs.json",
|
|
116
|
-
settingsPath: path.join(home, ".gemini", "settings.json"),
|
|
117
|
-
}));
|
|
118
|
-
|
|
119
|
-
// Codex CLI — logs at ~/.codex/sessions/
|
|
120
|
-
// Match either a "sessions" subdir (even if empty — the CLI created it)
|
|
121
|
-
// or any *.jsonl session file.
|
|
122
|
-
out.push(buildEntry({
|
|
123
|
-
name: "codex",
|
|
124
|
-
hookExpected: true,
|
|
125
|
-
hookInstalled: Boolean(hookStatus?.codex_notify ?? hookStatus?.codex),
|
|
126
|
-
logsDir: path.join(home, ".codex"),
|
|
127
|
-
logsPredicate: (_full, name, isDir) =>
|
|
128
|
-
(isDir && name === "sessions") || (!isDir && name.endsWith(".jsonl")),
|
|
129
|
-
settingsPath: path.join(home, ".codex", "config.toml"),
|
|
130
|
-
}));
|
|
131
|
-
|
|
132
|
-
const acodeHome = env.TOKENUSAGE_ACODE_HOME || path.join(home, ".acode");
|
|
133
|
-
out.push(buildEntry({
|
|
134
|
-
name: "acode",
|
|
135
|
-
hookExpected: true,
|
|
136
|
-
hookInstalled: Boolean(hookStatus?.acode_notify ?? hookStatus?.acode),
|
|
137
|
-
logsDir: acodeHome,
|
|
138
|
-
logsPredicate: (_full, name, isDir) =>
|
|
139
|
-
(isDir && (name === "sessions" || name === "archived_sessions")) ||
|
|
140
|
-
(!isDir && name.endsWith(".jsonl")),
|
|
141
|
-
settingsPath: path.join(acodeHome, "config.toml"),
|
|
142
|
-
}));
|
|
143
|
-
|
|
144
|
-
// Every Code — same shape as Codex, different home
|
|
145
|
-
out.push(buildEntry({
|
|
146
|
-
name: "every_code",
|
|
147
|
-
hookExpected: true,
|
|
148
|
-
hookInstalled: Boolean(hookStatus?.every_code_notify ?? hookStatus?.every_code),
|
|
149
|
-
logsDir: path.join(home, ".code"),
|
|
150
|
-
logsPredicate: (_full, name, isDir) =>
|
|
151
|
-
(isDir && name === "sessions") || (!isDir && name.endsWith(".jsonl")),
|
|
152
|
-
settingsPath: path.join(home, ".code", "config.toml"),
|
|
153
|
-
}));
|
|
154
|
-
|
|
155
|
-
// CodeBuddy — Claude-fork; hook in ~/.codebuddy/settings.json
|
|
156
|
-
out.push(buildEntry({
|
|
157
|
-
name: "codebuddy",
|
|
158
|
-
hookExpected: true,
|
|
159
|
-
hookInstalled: Boolean(hookStatus?.codebuddy),
|
|
160
|
-
logsDir: path.join(home, ".codebuddy"),
|
|
161
|
-
logsPredicate: (_full, name) => name === "projects" || name.endsWith(".jsonl"),
|
|
162
|
-
settingsPath: path.join(home, ".codebuddy", "settings.json"),
|
|
163
|
-
}));
|
|
164
|
-
|
|
165
|
-
// WorkBuddy — sibling Claude-fork; hook in ~/.workbuddy/settings.json
|
|
166
|
-
out.push(buildEntry({
|
|
167
|
-
name: "workbuddy",
|
|
168
|
-
hookExpected: true,
|
|
169
|
-
hookInstalled: Boolean(hookStatus?.workbuddy),
|
|
170
|
-
logsDir: path.join(home, ".workbuddy"),
|
|
171
|
-
logsPredicate: (_full, name) => name === "projects" || name.endsWith(".jsonl"),
|
|
172
|
-
settingsPath: path.join(home, ".workbuddy", "settings.json"),
|
|
173
|
-
}));
|
|
174
|
-
|
|
175
|
-
// WorkBuddy AI — international build; data + hooks in ~/.workbuddy-ai
|
|
176
|
-
const workbuddyAiHome = expandHome(env.WORKBUDDY_AI_HOME, home) || path.join(home, ".workbuddy-ai");
|
|
177
|
-
out.push(buildEntry({
|
|
178
|
-
name: "workbuddy-ai",
|
|
179
|
-
hookExpected: true,
|
|
180
|
-
hookInstalled: Boolean(hookStatus?.["workbuddy-ai"]),
|
|
181
|
-
logsDir: workbuddyAiHome,
|
|
182
|
-
logsPredicate: (_full, name) => name === "projects" || name.endsWith(".jsonl"),
|
|
183
|
-
settingsPath: path.join(workbuddyAiHome, "settings.json"),
|
|
184
|
-
}));
|
|
185
|
-
|
|
186
|
-
return out;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function buildEntry({ name, hookExpected, hookInstalled, logsDir, logsPredicate, settingsPath }) {
|
|
190
|
-
const logsPresent = dirHasFile(logsDir, logsPredicate);
|
|
191
|
-
const passive = hookExpected && !hookInstalled && logsPresent;
|
|
192
|
-
let reason = null;
|
|
193
|
-
if (hookExpected && !hookInstalled) {
|
|
194
|
-
reason = classifyWritableFailure(settingsPath);
|
|
195
|
-
}
|
|
196
|
-
return {
|
|
197
|
-
name,
|
|
198
|
-
hook_expected: hookExpected,
|
|
199
|
-
hook_installed: hookInstalled,
|
|
200
|
-
logs_present: logsPresent,
|
|
201
|
-
passive,
|
|
202
|
-
hook_failure_reason: reason,
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Convenience boolean: is at least one provider in passive mode?
|
|
208
|
-
*/
|
|
209
|
-
function isPassiveModeActive(providers) {
|
|
210
|
-
return providers.some((p) => p.passive);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
module.exports = {
|
|
214
|
-
detectPassiveProviders,
|
|
215
|
-
isPassiveModeActive,
|
|
216
|
-
// Exposed for unit testing
|
|
217
|
-
dirHasFile,
|
|
218
|
-
classifyWritableFailure,
|
|
219
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Passive-mode detection.
|
|
3
|
+
*
|
|
4
|
+
* Token Tracker normally collects token usage by installing SessionEnd hooks
|
|
5
|
+
* into supported CLIs (Claude Code, Codex CLI, Gemini CLI, …). The hook
|
|
6
|
+
* fires `notify.cjs` after each turn → sync runs immediately → data is
|
|
7
|
+
* fresh within seconds.
|
|
8
|
+
*
|
|
9
|
+
* On environments where hook installation fails — WSL with read-only mount,
|
|
10
|
+
* UNC paths on Windows, locked `settings.json`, sandboxed processes —
|
|
11
|
+
* SessionEnd hooks never fire, but the underlying CLIs *still* write
|
|
12
|
+
* session logs (`~/.claude/projects/`, `~/.gemini/sessions/`, etc.). Our
|
|
13
|
+
* parsers can read those logs directly without the hook; the only loss is
|
|
14
|
+
* "data is fresh within seconds" → it's now "data is fresh after the next
|
|
15
|
+
* scheduled `tracker sync`".
|
|
16
|
+
*
|
|
17
|
+
* This module surfaces that fact: for each hook-driven provider, decide
|
|
18
|
+
* whether the local install is in **passive mode** (no hook + log dir
|
|
19
|
+
* present). `tracker status` reports it, and the dashboard can show a
|
|
20
|
+
* single banner asking the user whether to acknowledge & continue in
|
|
21
|
+
* passive mode or attempt re-install (`tracker init`).
|
|
22
|
+
*
|
|
23
|
+
* Detection is purely best-effort — we never *fail* sync because hooks are
|
|
24
|
+
* missing; we just label the source so users / AI agents know latency is
|
|
25
|
+
* minutes, not seconds.
|
|
26
|
+
*/
|
|
27
|
+
"use strict";
|
|
28
|
+
|
|
29
|
+
const fs = require("node:fs");
|
|
30
|
+
const path = require("node:path");
|
|
31
|
+
const { expandHome } = require("./scan-roots");
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {Object} PassiveProvider
|
|
35
|
+
* @property {string} name - canonical provider name
|
|
36
|
+
* @property {boolean} hook_expected - whether this provider supports hooks
|
|
37
|
+
* @property {boolean} hook_installed - true when the hook is set
|
|
38
|
+
* @property {boolean} logs_present - true when the provider's log dir
|
|
39
|
+
* exists and contains at least one
|
|
40
|
+
* session file we can parse
|
|
41
|
+
* @property {boolean} passive - true iff hook_expected &&
|
|
42
|
+
* !hook_installed && logs_present
|
|
43
|
+
* @property {string|null} hook_failure_reason - if hook_expected and not
|
|
44
|
+
* installed, why we think it failed
|
|
45
|
+
* (e.g. "settings.json not writable",
|
|
46
|
+
* "config dir not found")
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
function dirHasFile(dir, predicate) {
|
|
50
|
+
if (!dir || !fs.existsSync(dir)) return false;
|
|
51
|
+
let entries;
|
|
52
|
+
try {
|
|
53
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
54
|
+
} catch (_e) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
for (const entry of entries) {
|
|
58
|
+
const full = path.join(dir, entry.name);
|
|
59
|
+
// Let the predicate accept either a file OR a directory name (so
|
|
60
|
+
// "sessions" or "tmp" as a bare subdirectory still counts).
|
|
61
|
+
if (!predicate || predicate(full, entry.name, entry.isDirectory())) return true;
|
|
62
|
+
if (entry.isDirectory()) {
|
|
63
|
+
// Shallow recurse — at most 1 level (covers projects/<name>/*.jsonl)
|
|
64
|
+
if (dirHasFile(full, predicate)) return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function classifyWritableFailure(filePath) {
|
|
71
|
+
if (!filePath) return "no path";
|
|
72
|
+
try {
|
|
73
|
+
fs.accessSync(filePath, fs.constants.W_OK);
|
|
74
|
+
return "writable (hook may have been removed externally)";
|
|
75
|
+
} catch (e) {
|
|
76
|
+
if (e && e.code === "ENOENT") return "settings file missing";
|
|
77
|
+
if (e && e.code === "EACCES") return "permission denied";
|
|
78
|
+
if (e && e.code === "EROFS") return "read-only filesystem";
|
|
79
|
+
return e?.code || "unknown";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Detect passive mode for each hook-driven provider.
|
|
85
|
+
*
|
|
86
|
+
* @param {Object} opts
|
|
87
|
+
* @param {string} opts.home - user home directory
|
|
88
|
+
* @param {Object} opts.hookStatus - per-provider hook-installed booleans
|
|
89
|
+
* (already collected by status.js); shape: { claude, gemini, codex,
|
|
90
|
+
* every_code, opencode, openclaw, codebuddy, workbuddy, grok }
|
|
91
|
+
* @returns {PassiveProvider[]}
|
|
92
|
+
*/
|
|
93
|
+
function detectPassiveProviders({ home, hookStatus, env = process.env }) {
|
|
94
|
+
const out = [];
|
|
95
|
+
|
|
96
|
+
// Claude Code — logs at ~/.claude/projects/<name>/*.jsonl
|
|
97
|
+
out.push(buildEntry({
|
|
98
|
+
name: "claude",
|
|
99
|
+
hookExpected: true,
|
|
100
|
+
hookInstalled: Boolean(hookStatus?.claude),
|
|
101
|
+
logsDir: path.join(home, ".claude", "projects"),
|
|
102
|
+
logsPredicate: (full) => full.endsWith(".jsonl"),
|
|
103
|
+
settingsPath: path.join(home, ".claude", "settings.json"),
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
// Gemini CLI — logs at ~/.gemini/tmp/<session-id>/logs.json
|
|
107
|
+
// (path varies by version; presence of ~/.gemini/sessions or
|
|
108
|
+
// ~/.gemini/tmp is enough to call it "active")
|
|
109
|
+
out.push(buildEntry({
|
|
110
|
+
name: "gemini",
|
|
111
|
+
hookExpected: true,
|
|
112
|
+
hookInstalled: Boolean(hookStatus?.gemini),
|
|
113
|
+
logsDir: path.join(home, ".gemini"),
|
|
114
|
+
logsPredicate: (_full, name, isDir) =>
|
|
115
|
+
(isDir && (name === "tmp" || name === "sessions")) || name === "logs.json",
|
|
116
|
+
settingsPath: path.join(home, ".gemini", "settings.json"),
|
|
117
|
+
}));
|
|
118
|
+
|
|
119
|
+
// Codex CLI — logs at ~/.codex/sessions/
|
|
120
|
+
// Match either a "sessions" subdir (even if empty — the CLI created it)
|
|
121
|
+
// or any *.jsonl session file.
|
|
122
|
+
out.push(buildEntry({
|
|
123
|
+
name: "codex",
|
|
124
|
+
hookExpected: true,
|
|
125
|
+
hookInstalled: Boolean(hookStatus?.codex_notify ?? hookStatus?.codex),
|
|
126
|
+
logsDir: path.join(home, ".codex"),
|
|
127
|
+
logsPredicate: (_full, name, isDir) =>
|
|
128
|
+
(isDir && name === "sessions") || (!isDir && name.endsWith(".jsonl")),
|
|
129
|
+
settingsPath: path.join(home, ".codex", "config.toml"),
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
const acodeHome = env.TOKENUSAGE_ACODE_HOME || path.join(home, ".acode");
|
|
133
|
+
out.push(buildEntry({
|
|
134
|
+
name: "acode",
|
|
135
|
+
hookExpected: true,
|
|
136
|
+
hookInstalled: Boolean(hookStatus?.acode_notify ?? hookStatus?.acode),
|
|
137
|
+
logsDir: acodeHome,
|
|
138
|
+
logsPredicate: (_full, name, isDir) =>
|
|
139
|
+
(isDir && (name === "sessions" || name === "archived_sessions")) ||
|
|
140
|
+
(!isDir && name.endsWith(".jsonl")),
|
|
141
|
+
settingsPath: path.join(acodeHome, "config.toml"),
|
|
142
|
+
}));
|
|
143
|
+
|
|
144
|
+
// Every Code — same shape as Codex, different home
|
|
145
|
+
out.push(buildEntry({
|
|
146
|
+
name: "every_code",
|
|
147
|
+
hookExpected: true,
|
|
148
|
+
hookInstalled: Boolean(hookStatus?.every_code_notify ?? hookStatus?.every_code),
|
|
149
|
+
logsDir: path.join(home, ".code"),
|
|
150
|
+
logsPredicate: (_full, name, isDir) =>
|
|
151
|
+
(isDir && name === "sessions") || (!isDir && name.endsWith(".jsonl")),
|
|
152
|
+
settingsPath: path.join(home, ".code", "config.toml"),
|
|
153
|
+
}));
|
|
154
|
+
|
|
155
|
+
// CodeBuddy — Claude-fork; hook in ~/.codebuddy/settings.json
|
|
156
|
+
out.push(buildEntry({
|
|
157
|
+
name: "codebuddy",
|
|
158
|
+
hookExpected: true,
|
|
159
|
+
hookInstalled: Boolean(hookStatus?.codebuddy),
|
|
160
|
+
logsDir: path.join(home, ".codebuddy"),
|
|
161
|
+
logsPredicate: (_full, name) => name === "projects" || name.endsWith(".jsonl"),
|
|
162
|
+
settingsPath: path.join(home, ".codebuddy", "settings.json"),
|
|
163
|
+
}));
|
|
164
|
+
|
|
165
|
+
// WorkBuddy — sibling Claude-fork; hook in ~/.workbuddy/settings.json
|
|
166
|
+
out.push(buildEntry({
|
|
167
|
+
name: "workbuddy",
|
|
168
|
+
hookExpected: true,
|
|
169
|
+
hookInstalled: Boolean(hookStatus?.workbuddy),
|
|
170
|
+
logsDir: path.join(home, ".workbuddy"),
|
|
171
|
+
logsPredicate: (_full, name) => name === "projects" || name.endsWith(".jsonl"),
|
|
172
|
+
settingsPath: path.join(home, ".workbuddy", "settings.json"),
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
// WorkBuddy AI — international build; data + hooks in ~/.workbuddy-ai
|
|
176
|
+
const workbuddyAiHome = expandHome(env.WORKBUDDY_AI_HOME, home) || path.join(home, ".workbuddy-ai");
|
|
177
|
+
out.push(buildEntry({
|
|
178
|
+
name: "workbuddy-ai",
|
|
179
|
+
hookExpected: true,
|
|
180
|
+
hookInstalled: Boolean(hookStatus?.["workbuddy-ai"]),
|
|
181
|
+
logsDir: workbuddyAiHome,
|
|
182
|
+
logsPredicate: (_full, name) => name === "projects" || name.endsWith(".jsonl"),
|
|
183
|
+
settingsPath: path.join(workbuddyAiHome, "settings.json"),
|
|
184
|
+
}));
|
|
185
|
+
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function buildEntry({ name, hookExpected, hookInstalled, logsDir, logsPredicate, settingsPath }) {
|
|
190
|
+
const logsPresent = dirHasFile(logsDir, logsPredicate);
|
|
191
|
+
const passive = hookExpected && !hookInstalled && logsPresent;
|
|
192
|
+
let reason = null;
|
|
193
|
+
if (hookExpected && !hookInstalled) {
|
|
194
|
+
reason = classifyWritableFailure(settingsPath);
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
name,
|
|
198
|
+
hook_expected: hookExpected,
|
|
199
|
+
hook_installed: hookInstalled,
|
|
200
|
+
logs_present: logsPresent,
|
|
201
|
+
passive,
|
|
202
|
+
hook_failure_reason: reason,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Convenience boolean: is at least one provider in passive mode?
|
|
208
|
+
*/
|
|
209
|
+
function isPassiveModeActive(providers) {
|
|
210
|
+
return providers.some((p) => p.passive);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
module.exports = {
|
|
214
|
+
detectPassiveProviders,
|
|
215
|
+
isPassiveModeActive,
|
|
216
|
+
// Exposed for unit testing
|
|
217
|
+
dirHasFile,
|
|
218
|
+
classifyWritableFailure,
|
|
219
|
+
};
|