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.
Files changed (131) hide show
  1. package/CHANGELOG.md +39 -19
  2. package/LICENSE +22 -22
  3. package/README.md +94 -94
  4. package/THIRD_PARTY_NOTICES.md +140 -140
  5. package/bin/tokenusage.js +69 -69
  6. package/package.json +59 -59
  7. package/src/cli.js +146 -134
  8. package/src/commands/device-login.js +205 -205
  9. package/src/commands/diagnostics.js +38 -38
  10. package/src/commands/doctor.js +110 -110
  11. package/src/commands/init.js +2484 -2436
  12. package/src/commands/pause.js +77 -0
  13. package/src/commands/sessions.js +29 -29
  14. package/src/commands/status.js +1758 -1747
  15. package/src/commands/sync.js +8048 -7934
  16. package/src/commands/uninstall.js +322 -312
  17. package/src/lib/account-usage-dedup.js +267 -267
  18. package/src/lib/antigravity-paths.js +48 -48
  19. package/src/lib/ark-agent-plan-limits.js +438 -438
  20. package/src/lib/ark-coding-plan-limits.js +537 -537
  21. package/src/lib/async-queue-reader.js +138 -138
  22. package/src/lib/atomic-json.js +27 -27
  23. package/src/lib/bearer-token.js +11 -11
  24. package/src/lib/categorizer-utils.js +232 -232
  25. package/src/lib/claude-categorizer.js +1343 -1343
  26. package/src/lib/claude-config.js +279 -279
  27. package/src/lib/claude-user-identity.js +56 -56
  28. package/src/lib/cli-ui.js +179 -179
  29. package/src/lib/cloud-account.js +541 -541
  30. package/src/lib/cloud-device-token.js +141 -141
  31. package/src/lib/cloud-sync-prefs.js +14 -14
  32. package/src/lib/cloud-usage-reset.js +115 -115
  33. package/src/lib/codex-config.js +595 -595
  34. package/src/lib/codex-context-breakdown.js +806 -806
  35. package/src/lib/codex-model-attribution.js +128 -128
  36. package/src/lib/codex-rollout-parser.js +1471 -1471
  37. package/src/lib/codex-service-tier.js +49 -49
  38. package/src/lib/codex-token-refresh.js +140 -140
  39. package/src/lib/codex-token-usage.js +195 -195
  40. package/src/lib/codex-usage-record.js +195 -195
  41. package/src/lib/collection-pause.js +77 -0
  42. package/src/lib/command-runner.js +313 -313
  43. package/src/lib/commandcode-limits.js +317 -317
  44. package/src/lib/context-health.js +124 -124
  45. package/src/lib/cursor-config.js +605 -605
  46. package/src/lib/cursor-store.js +1918 -1918
  47. package/src/lib/debug-flags.js +7 -7
  48. package/src/lib/devin-limits.js +249 -249
  49. package/src/lib/devin-usage.js +212 -212
  50. package/src/lib/diagnostics.js +383 -383
  51. package/src/lib/doctor.js +385 -385
  52. package/src/lib/dumate.js +95 -95
  53. package/src/lib/fs.js +455 -455
  54. package/src/lib/function-url.js +14 -14
  55. package/src/lib/gemini-config.js +291 -291
  56. package/src/lib/git-outcomes.js +243 -243
  57. package/src/lib/grok-context-breakdown.js +890 -890
  58. package/src/lib/grok-hook.js +360 -360
  59. package/src/lib/grok-limits.js +865 -865
  60. package/src/lib/grok-usage.js +98 -98
  61. package/src/lib/heatmap-compact.js +114 -114
  62. package/src/lib/ingest-sanitize.js +41 -41
  63. package/src/lib/init-flow.js +42 -42
  64. package/src/lib/install-resolver.js +170 -170
  65. package/src/lib/jsonl-lines.js +134 -134
  66. package/src/lib/kimi-model-repair.js +173 -173
  67. package/src/lib/kimi-profile.js +61 -61
  68. package/src/lib/kimi-session.js +216 -216
  69. package/src/lib/local-api.js +3588 -3588
  70. package/src/lib/machine-id.js +238 -238
  71. package/src/lib/multi-install-parser.js +113 -113
  72. package/src/lib/notify-command.js +38 -38
  73. package/src/lib/omp-hook.js +589 -589
  74. package/src/lib/openclaw-cli.js +104 -104
  75. package/src/lib/openclaw-hook.js +375 -375
  76. package/src/lib/openclaw-session-plugin.js +560 -560
  77. package/src/lib/opencode-config.js +121 -121
  78. package/src/lib/opencode-go-limits.js +807 -807
  79. package/src/lib/outcomes-engine.js +253 -253
  80. package/src/lib/passive-mode.js +219 -219
  81. package/src/lib/pet-packages.js +722 -722
  82. package/src/lib/pricing/curated-overrides.json +822 -822
  83. package/src/lib/pricing/index.js +379 -379
  84. package/src/lib/pricing/litellm-fetcher.js +172 -172
  85. package/src/lib/pricing/matcher.js +456 -456
  86. package/src/lib/pricing/seed-snapshot.json +1 -1
  87. package/src/lib/progress.js +76 -76
  88. package/src/lib/project-usage-purge.js +117 -117
  89. package/src/lib/prompt.js +24 -24
  90. package/src/lib/provider-status.js +104 -104
  91. package/src/lib/proxy-env.js +489 -489
  92. package/src/lib/proxy-settings.js +163 -163
  93. package/src/lib/qoder-cn-cli-limits.js +119 -119
  94. package/src/lib/qoder-limits.js +880 -880
  95. package/src/lib/quiet-warnings.js +26 -26
  96. package/src/lib/rollout.js +26005 -25966
  97. package/src/lib/runtime-config.js +9 -4
  98. package/src/lib/scan-roots.js +277 -277
  99. package/src/lib/scheduled-task.js +145 -0
  100. package/src/lib/sentry.js +231 -231
  101. package/src/lib/session-analytics.js +2972 -2972
  102. package/src/lib/session-performance.js +237 -237
  103. package/src/lib/session-upload.js +379 -379
  104. package/src/lib/skill-usage.js +267 -267
  105. package/src/lib/skills-manager.js +1591 -1591
  106. package/src/lib/source-metadata.js +48 -48
  107. package/src/lib/sqlite-reader.js +215 -215
  108. package/src/lib/star-cta.js +73 -73
  109. package/src/lib/subscription-manager.js +293 -293
  110. package/src/lib/subscriptions.js +508 -508
  111. package/src/lib/sync-report.js +138 -0
  112. package/src/lib/telemetry.js +193 -193
  113. package/src/lib/tracker-paths.js +16 -16
  114. package/src/lib/trae-cn-config.js +548 -548
  115. package/src/lib/trae-db.js +127 -127
  116. package/src/lib/trae-snapshot.js +230 -230
  117. package/src/lib/trae-sqlite.js +143 -143
  118. package/src/lib/trae-usage.js +121 -121
  119. package/src/lib/upload-queue.js +134 -134
  120. package/src/lib/upload-throttle.js +171 -171
  121. package/src/lib/usage-limits.js +4552 -4552
  122. package/src/lib/wrapped-aggregator.js +236 -236
  123. package/src/lib/wsl-probe.js +281 -281
  124. package/src/lib/zcode-limits.js +1172 -1172
  125. package/src/lib/zcode-reset-credits.js +66 -66
  126. package/test/audit-fixes.test.js +200 -0
  127. package/test/cli-smoke.test.js +53 -53
  128. package/test/collection-controls.test.js +181 -0
  129. package/test/ingest-sanitize.test.js +162 -162
  130. package/test/quiet-warnings.test.js +53 -53
  131. package/test/session-upload.test.js +427 -427
@@ -1,312 +1,322 @@
1
- const os = require("node:os");
2
- const path = require("node:path");
3
- const fs = require("node:fs/promises");
4
- const { expandHome } = require("../lib/scan-roots");
5
-
6
- const {
7
- restoreCodexNotify,
8
- restoreAcodeNotify,
9
- restoreEveryCodeNotify,
10
- buildCodexNotifyCmd,
11
- buildAcodeNotifyCmd,
12
- buildEveryCodeNotifyCmd,
13
- } = require("../lib/codex-config");
14
- const {
15
- removeClaudeHook,
16
- removeClaudeUsageHooks,
17
- buildClaudeHookCommand,
18
- buildHookCommand,
19
- } = require("../lib/claude-config");
20
- const {
21
- resolveGeminiConfigDir,
22
- resolveGeminiSettingsPath,
23
- buildGeminiHookCommand,
24
- removeGeminiHook,
25
- } = require("../lib/gemini-config");
26
- const { resolveOpencodeConfigDir, removeOpencodePlugin } = require("../lib/opencode-config");
27
- const { removeOpenclawHookConfig } = require("../lib/openclaw-hook");
28
- const { removeOpenclawSessionPluginConfig } = require("../lib/openclaw-session-plugin");
29
- const { removeGrokHook } = require("../lib/grok-hook");
30
- const { removeOmpHook } = require("../lib/omp-hook");
31
- const { resolveTrackerPaths } = require("../lib/tracker-paths");
32
-
33
- async function cmdUninstall(argv) {
34
- const opts = parseArgs(argv);
35
- const home = os.homedir();
36
- const { trackerDir, binDir } = await resolveTrackerPaths({ home });
37
- const codexHome = process.env.CODEX_HOME || path.join(home, ".codex");
38
- const codexConfigPath = path.join(codexHome, "config.toml");
39
- const acodeHome = process.env.TOKENUSAGE_ACODE_HOME || path.join(home, ".acode");
40
- const acodeConfigPath = path.join(acodeHome, "config.toml");
41
- const codeHome = process.env.CODE_HOME || path.join(home, ".code");
42
- const codeConfigPath = path.join(codeHome, "config.toml");
43
- const claudeSettingsPath = path.join(home, ".claude", "settings.json");
44
- const codebuddyDir = process.env.CODEBUDDY_HOME || path.join(home, ".codebuddy");
45
- const codebuddySettingsPath = path.join(codebuddyDir, "settings.json");
46
- const workbuddyDir = process.env.WORKBUDDY_HOME || path.join(home, ".workbuddy");
47
- const workbuddySettingsPath = path.join(workbuddyDir, "settings.json");
48
- // WorkBuddy AI — international build, sibling home (~/.workbuddy-ai).
49
- const workbuddyAiDir = expandHome(process.env.WORKBUDDY_AI_HOME, home) || path.join(home, ".workbuddy-ai");
50
- const workbuddyAiSettingsPath = path.join(workbuddyAiDir, "settings.json");
51
- const geminiConfigDir = resolveGeminiConfigDir({ home, env: process.env });
52
- const geminiSettingsPath = resolveGeminiSettingsPath({ configDir: geminiConfigDir });
53
- const opencodeConfigDir = resolveOpencodeConfigDir({ home, env: process.env });
54
- const notifyPath = path.join(binDir, "notify.cjs");
55
- const notifyOriginalPath = path.join(trackerDir, "codex_notify_original.json");
56
- const acodeNotifyOriginalPath = path.join(trackerDir, "acode_notify_original.json");
57
- const codeNotifyOriginalPath = path.join(trackerDir, "code_notify_original.json");
58
- const codexNotifyCmd = buildCodexNotifyCmd(notifyPath);
59
- const acodeNotifyCmd = buildAcodeNotifyCmd(notifyPath);
60
- const codeNotifyCmd = buildEveryCodeNotifyCmd(notifyPath);
61
- const claudeHookCommand = buildClaudeHookCommand(notifyPath);
62
- const codebuddyHookCommand = buildHookCommand(notifyPath, "codebuddy");
63
- const workbuddyHookCommand = buildHookCommand(notifyPath, "workbuddy");
64
- const workbuddyAiHookCommand = buildHookCommand(notifyPath, "workbuddy-ai");
65
- const geminiHookCommand = buildGeminiHookCommand(notifyPath);
66
-
67
- const codexConfigExists = await isFile(codexConfigPath);
68
- const acodeConfigExists = await isFile(acodeConfigPath);
69
- const codeConfigExists = await isFile(codeConfigPath);
70
- const claudeConfigExists = await isFile(claudeSettingsPath);
71
- const codebuddyConfigExists = await isFile(codebuddySettingsPath);
72
- const workbuddyConfigExists = await isFile(workbuddySettingsPath);
73
- const workbuddyAiConfigExists = await isFile(workbuddyAiSettingsPath);
74
- const geminiConfigExists = await isDir(geminiConfigDir);
75
- const opencodeConfigExists = await isDir(opencodeConfigDir);
76
- const codexRestore = codexConfigExists
77
- ? await restoreCodexNotify({
78
- codexConfigPath,
79
- notifyOriginalPath,
80
- notifyCmd: codexNotifyCmd,
81
- })
82
- : { restored: false, skippedReason: "config-missing" };
83
- const acodeRestore = acodeConfigExists
84
- ? await restoreAcodeNotify({
85
- acodeConfigPath,
86
- notifyOriginalPath: acodeNotifyOriginalPath,
87
- notifyCmd: acodeNotifyCmd,
88
- })
89
- : { restored: false, skippedReason: "config-missing" };
90
- const codeRestore = codeConfigExists
91
- ? await restoreEveryCodeNotify({
92
- codeConfigPath,
93
- notifyOriginalPath: codeNotifyOriginalPath,
94
- notifyCmd: codeNotifyCmd,
95
- })
96
- : { restored: false, skippedReason: "config-missing" };
97
- const claudeRemove = claudeConfigExists
98
- ? await removeClaudeUsageHooks({
99
- settingsPath: claudeSettingsPath,
100
- hookCommand: claudeHookCommand,
101
- })
102
- : { removed: false, skippedReason: "config-missing" };
103
- const codebuddyRemove = codebuddyConfigExists
104
- ? await removeClaudeHook({
105
- settingsPath: codebuddySettingsPath,
106
- hookCommand: codebuddyHookCommand,
107
- })
108
- : { removed: false, skippedReason: "config-missing" };
109
- const workbuddyRemove = workbuddyConfigExists
110
- ? await removeClaudeHook({
111
- settingsPath: workbuddySettingsPath,
112
- hookCommand: workbuddyHookCommand,
113
- })
114
- : { removed: false, skippedReason: "config-missing" };
115
- const workbuddyAiRemove = workbuddyAiConfigExists
116
- ? await removeClaudeHook({
117
- settingsPath: workbuddyAiSettingsPath,
118
- hookCommand: workbuddyAiHookCommand,
119
- })
120
- : { removed: false, skippedReason: "config-missing" };
121
- const geminiRemove = geminiConfigExists
122
- ? await removeGeminiHook({ settingsPath: geminiSettingsPath, hookCommand: geminiHookCommand })
123
- : { removed: false, skippedReason: "config-missing" };
124
- const opencodeRemove = opencodeConfigExists
125
- ? await removeOpencodePlugin({ configDir: opencodeConfigDir })
126
- : { removed: false, skippedReason: "config-missing" };
127
- const openclawSessionPluginRemove = await removeOpenclawSessionPluginConfig({
128
- home,
129
- trackerDir,
130
- env: process.env,
131
- });
132
- const openclawHookRemove = await removeOpenclawHookConfig({ home, trackerDir, env: process.env });
133
- const grokHookRemove = await removeGrokHook({ home, trackerDir, env: process.env });
134
- const ompHookRemove = await removeOmpHook({ home, trackerDir, env: process.env });
135
-
136
- // Remove installed notify handler.
137
- await fs.unlink(notifyPath).catch(() => {});
138
-
139
- // Remove local app runtime (installed by init for notify-driven sync).
140
- await fs.rm(path.join(trackerDir, "app"), { recursive: true, force: true }).catch(() => {});
141
-
142
- // Deliberately NOT removed by --purge: the machine-identity seed lets a
143
- // reinstall reuse the same cloud device row instead of double-counting the
144
- // replayed history under a new device (issue #176).
145
- const machineIdSeedPath = path.join(home, ".config", "tokenusage", "machine-id");
146
- let machineIdSeedKept = false;
147
- if (opts.purge) {
148
- await fs.rm(path.join(home, ".tokenusage"), { recursive: true, force: true }).catch(() => {});
149
- machineIdSeedKept = await fs.access(machineIdSeedPath).then(
150
- () => true,
151
- () => false,
152
- );
153
- }
154
-
155
- process.stdout.write(
156
- [
157
- "Uninstalled:",
158
- codexConfigExists
159
- ? codexRestore?.restored
160
- ? `- Codex notify restored: ${codexConfigPath}`
161
- : codexRestore?.skippedReason === "no-backup-not-installed"
162
- ? "- Codex notify: skipped (no backup; not installed)"
163
- : codexRestore?.skippedReason === "current-not-managed"
164
- ? "- Codex notify: skipped (current notify is not managed by tokenusage)"
165
- : "- Codex notify: no change"
166
- : "- Codex notify: skipped (config.toml not found)",
167
- acodeConfigExists
168
- ? acodeRestore?.restored
169
- ? `- AStudio notify restored: ${acodeConfigPath}`
170
- : acodeRestore?.skippedReason === "no-backup-not-installed"
171
- ? "- AStudio notify: skipped (no backup; not installed)"
172
- : acodeRestore?.skippedReason === "current-not-managed"
173
- ? "- AStudio notify: skipped (current notify is not managed by tokenusage)"
174
- : "- AStudio notify: no change"
175
- : "- AStudio notify: skipped (config.toml not found)",
176
- codeConfigExists
177
- ? codeRestore?.restored
178
- ? `- Every Code notify restored: ${codeConfigPath}`
179
- : codeRestore?.skippedReason === "no-backup-not-installed"
180
- ? "- Every Code notify: skipped (no backup; not installed)"
181
- : codeRestore?.skippedReason === "current-not-managed"
182
- ? "- Every Code notify: skipped (current notify is not managed by tokenusage)"
183
- : "- Every Code notify: no change"
184
- : "- Every Code notify: skipped (config.toml not found)",
185
- claudeConfigExists
186
- ? claudeRemove?.removed
187
- ? `- Claude hooks removed: ${claudeSettingsPath}`
188
- : claudeRemove?.skippedReason === "hook-missing"
189
- ? "- Claude hooks: no change"
190
- : "- Claude hooks: skipped"
191
- : "- Claude hooks: skipped (settings.json not found)",
192
- codebuddyConfigExists
193
- ? codebuddyRemove?.removed
194
- ? `- CodeBuddy hooks removed: ${codebuddySettingsPath}`
195
- : codebuddyRemove?.skippedReason === "hook-missing"
196
- ? "- CodeBuddy hooks: no change"
197
- : "- CodeBuddy hooks: skipped"
198
- : "- CodeBuddy hooks: skipped (settings.json not found)",
199
- workbuddyConfigExists
200
- ? workbuddyRemove?.removed
201
- ? `- WorkBuddy hooks removed: ${workbuddySettingsPath}`
202
- : workbuddyRemove?.skippedReason === "hook-missing"
203
- ? "- WorkBuddy hooks: no change"
204
- : "- WorkBuddy hooks: skipped"
205
- : "- WorkBuddy hooks: skipped (settings.json not found)",
206
- workbuddyAiConfigExists
207
- ? workbuddyAiRemove?.removed
208
- ? `- WorkBuddy AI hooks removed: ${workbuddyAiSettingsPath}`
209
- : workbuddyAiRemove?.skippedReason === "hook-missing"
210
- ? "- WorkBuddy AI hooks: no change"
211
- : "- WorkBuddy AI hooks: skipped"
212
- : "- WorkBuddy AI hooks: skipped (settings.json not found)",
213
- geminiConfigExists
214
- ? geminiRemove?.removed
215
- ? `- Gemini hooks removed: ${geminiSettingsPath}`
216
- : geminiRemove?.skippedReason === "hook-missing"
217
- ? "- Gemini hooks: no change"
218
- : "- Gemini hooks: skipped"
219
- : `- Gemini hooks: skipped (${geminiConfigDir} not found)`,
220
- opencodeConfigExists
221
- ? opencodeRemove?.removed
222
- ? `- Opencode plugin removed: ${opencodeConfigDir}`
223
- : opencodeRemove?.skippedReason === "plugin-missing"
224
- ? "- Opencode plugin: no change"
225
- : opencodeRemove?.skippedReason === "unexpected-content"
226
- ? "- Opencode plugin: skipped (unexpected content)"
227
- : "- Opencode plugin: skipped"
228
- : `- Opencode plugin: skipped (${opencodeConfigDir} not found)`,
229
- openclawSessionPluginRemove?.removed
230
- ? `- OpenClaw session plugin removed: ${openclawSessionPluginRemove.openclawConfigPath}`
231
- : openclawSessionPluginRemove?.skippedReason === "openclaw-config-missing"
232
- ? "- OpenClaw session plugin: skipped (openclaw config not found)"
233
- : "- OpenClaw session plugin: no change",
234
- openclawHookRemove?.removed
235
- ? `- OpenClaw hook (legacy) removed: ${openclawHookRemove.openclawConfigPath}`
236
- : openclawHookRemove?.skippedReason === "openclaw-config-missing"
237
- ? "- OpenClaw hook (legacy): skipped (openclaw config not found)"
238
- : "- OpenClaw hook (legacy): no change",
239
- grokHookRemove?.removed
240
- ? `- Grok Build hook removed: ${grokHookRemove.hookPath}`
241
- : "- Grok Build hook: no change",
242
- formatOmpHookRemoveLine(ompHookRemove),
243
- opts.purge ? `- Purged: ${path.join(home, ".tokenusage")}` : "- Purge: skipped (use --purge)",
244
- ...(machineIdSeedKept
245
- ? [`- Kept: ${machineIdSeedPath} (cloud device identity — a reinstall reuses the same device; delete it to fully reset)`]
246
- : []),
247
- "",
248
- ].join("\n"),
249
- );
250
- }
251
-
252
- function formatOmpHookRemoveLine(ompHookRemove) {
253
- if (ompHookRemove?.removed) {
254
- return `- oh-my-pi notify extension removed: ${ompHookRemove.extensionPath}`;
255
- }
256
- const reason = ompHookRemove?.skippedReason;
257
- const residualPath = ompHookRemove?.stagedPath || ompHookRemove?.extensionPath;
258
- const residual = residualPath
259
- ? ` (left in place: ${residualPath})`
260
- : "";
261
- if (reason === "unmanaged") {
262
- return `- oh-my-pi notify extension: skipped (unmanaged file)${residual}`;
263
- }
264
- if (reason === "unlink-failed") {
265
- const detail = ompHookRemove?.error ? `: ${ompHookRemove.error}` : "";
266
- return `- oh-my-pi notify extension: failed to remove${detail}${residual}`;
267
- }
268
- if (reason === "extension-read-failed") {
269
- const detail = ompHookRemove?.error ? `: ${ompHookRemove.error}` : "";
270
- return `- oh-my-pi notify extension: failed to read${detail}${residual}`;
271
- }
272
- if (reason === "identity-changed") {
273
- return `- oh-my-pi notify extension: skipped (file changed during uninstall)${residual}`;
274
- }
275
- if (reason === "omp-agent-dir-unresolved") {
276
- return "- oh-my-pi notify extension: skipped (omp agent dir unresolved)";
277
- }
278
- if (reason === "missing") {
279
- return "- oh-my-pi notify extension: no change";
280
- }
281
- return "- oh-my-pi notify extension: no change";
282
- }
283
-
284
- function parseArgs(argv) {
285
- const out = { purge: false };
286
- for (let i = 0; i < argv.length; i++) {
287
- const a = argv[i];
288
- if (a === "--purge") out.purge = true;
289
- else throw new Error(`Unknown option: ${a}`);
290
- }
291
- return out;
292
- }
293
-
294
- module.exports = { cmdUninstall, formatOmpHookRemoveLine };
295
-
296
- async function isFile(p) {
297
- try {
298
- const st = await fs.stat(p);
299
- return st.isFile();
300
- } catch (_e) {
301
- return false;
302
- }
303
- }
304
-
305
- async function isDir(p) {
306
- try {
307
- const st = await fs.stat(p);
308
- return st.isDirectory();
309
- } catch (_e) {
310
- return false;
311
- }
312
- }
1
+ const os = require("node:os");
2
+ const path = require("node:path");
3
+ const fs = require("node:fs/promises");
4
+ const { expandHome } = require("../lib/scan-roots");
5
+
6
+ const {
7
+ restoreCodexNotify,
8
+ restoreAcodeNotify,
9
+ restoreEveryCodeNotify,
10
+ buildCodexNotifyCmd,
11
+ buildAcodeNotifyCmd,
12
+ buildEveryCodeNotifyCmd,
13
+ } = require("../lib/codex-config");
14
+ const {
15
+ removeClaudeHook,
16
+ removeClaudeUsageHooks,
17
+ buildClaudeHookCommand,
18
+ buildHookCommand,
19
+ } = require("../lib/claude-config");
20
+ const {
21
+ resolveGeminiConfigDir,
22
+ resolveGeminiSettingsPath,
23
+ buildGeminiHookCommand,
24
+ removeGeminiHook,
25
+ } = require("../lib/gemini-config");
26
+ const { resolveOpencodeConfigDir, removeOpencodePlugin } = require("../lib/opencode-config");
27
+ const { removeOpenclawHookConfig } = require("../lib/openclaw-hook");
28
+ const { removeOpenclawSessionPluginConfig } = require("../lib/openclaw-session-plugin");
29
+ const { removeGrokHook } = require("../lib/grok-hook");
30
+ const { removeOmpHook } = require("../lib/omp-hook");
31
+ const { resolveTrackerPaths } = require("../lib/tracker-paths");
32
+ const { deleteWindowsScheduledTask, TASK_NAME } = require("../lib/scheduled-task");
33
+
34
+ async function cmdUninstall(argv) {
35
+ const opts = parseArgs(argv);
36
+ const home = os.homedir();
37
+ const { trackerDir, binDir } = await resolveTrackerPaths({ home });
38
+ const codexHome = process.env.CODEX_HOME || path.join(home, ".codex");
39
+ const codexConfigPath = path.join(codexHome, "config.toml");
40
+ const acodeHome = process.env.TOKENUSAGE_ACODE_HOME || path.join(home, ".acode");
41
+ const acodeConfigPath = path.join(acodeHome, "config.toml");
42
+ const codeHome = process.env.CODE_HOME || path.join(home, ".code");
43
+ const codeConfigPath = path.join(codeHome, "config.toml");
44
+ const claudeSettingsPath = path.join(home, ".claude", "settings.json");
45
+ const codebuddyDir = process.env.CODEBUDDY_HOME || path.join(home, ".codebuddy");
46
+ const codebuddySettingsPath = path.join(codebuddyDir, "settings.json");
47
+ const workbuddyDir = process.env.WORKBUDDY_HOME || path.join(home, ".workbuddy");
48
+ const workbuddySettingsPath = path.join(workbuddyDir, "settings.json");
49
+ // WorkBuddy AI — international build, sibling home (~/.workbuddy-ai).
50
+ const workbuddyAiDir = expandHome(process.env.WORKBUDDY_AI_HOME, home) || path.join(home, ".workbuddy-ai");
51
+ const workbuddyAiSettingsPath = path.join(workbuddyAiDir, "settings.json");
52
+ const geminiConfigDir = resolveGeminiConfigDir({ home, env: process.env });
53
+ const geminiSettingsPath = resolveGeminiSettingsPath({ configDir: geminiConfigDir });
54
+ const opencodeConfigDir = resolveOpencodeConfigDir({ home, env: process.env });
55
+ const notifyPath = path.join(binDir, "notify.cjs");
56
+ const notifyOriginalPath = path.join(trackerDir, "codex_notify_original.json");
57
+ const acodeNotifyOriginalPath = path.join(trackerDir, "acode_notify_original.json");
58
+ const codeNotifyOriginalPath = path.join(trackerDir, "code_notify_original.json");
59
+ const codexNotifyCmd = buildCodexNotifyCmd(notifyPath);
60
+ const acodeNotifyCmd = buildAcodeNotifyCmd(notifyPath);
61
+ const codeNotifyCmd = buildEveryCodeNotifyCmd(notifyPath);
62
+ const claudeHookCommand = buildClaudeHookCommand(notifyPath);
63
+ const codebuddyHookCommand = buildHookCommand(notifyPath, "codebuddy");
64
+ const workbuddyHookCommand = buildHookCommand(notifyPath, "workbuddy");
65
+ const workbuddyAiHookCommand = buildHookCommand(notifyPath, "workbuddy-ai");
66
+ const geminiHookCommand = buildGeminiHookCommand(notifyPath);
67
+
68
+ const codexConfigExists = await isFile(codexConfigPath);
69
+ const acodeConfigExists = await isFile(acodeConfigPath);
70
+ const codeConfigExists = await isFile(codeConfigPath);
71
+ const claudeConfigExists = await isFile(claudeSettingsPath);
72
+ const codebuddyConfigExists = await isFile(codebuddySettingsPath);
73
+ const workbuddyConfigExists = await isFile(workbuddySettingsPath);
74
+ const workbuddyAiConfigExists = await isFile(workbuddyAiSettingsPath);
75
+ const geminiConfigExists = await isDir(geminiConfigDir);
76
+ const opencodeConfigExists = await isDir(opencodeConfigDir);
77
+ const codexRestore = codexConfigExists
78
+ ? await restoreCodexNotify({
79
+ codexConfigPath,
80
+ notifyOriginalPath,
81
+ notifyCmd: codexNotifyCmd,
82
+ })
83
+ : { restored: false, skippedReason: "config-missing" };
84
+ const acodeRestore = acodeConfigExists
85
+ ? await restoreAcodeNotify({
86
+ acodeConfigPath,
87
+ notifyOriginalPath: acodeNotifyOriginalPath,
88
+ notifyCmd: acodeNotifyCmd,
89
+ })
90
+ : { restored: false, skippedReason: "config-missing" };
91
+ const codeRestore = codeConfigExists
92
+ ? await restoreEveryCodeNotify({
93
+ codeConfigPath,
94
+ notifyOriginalPath: codeNotifyOriginalPath,
95
+ notifyCmd: codeNotifyCmd,
96
+ })
97
+ : { restored: false, skippedReason: "config-missing" };
98
+ const claudeRemove = claudeConfigExists
99
+ ? await removeClaudeUsageHooks({
100
+ settingsPath: claudeSettingsPath,
101
+ hookCommand: claudeHookCommand,
102
+ })
103
+ : { removed: false, skippedReason: "config-missing" };
104
+ const codebuddyRemove = codebuddyConfigExists
105
+ ? await removeClaudeHook({
106
+ settingsPath: codebuddySettingsPath,
107
+ hookCommand: codebuddyHookCommand,
108
+ })
109
+ : { removed: false, skippedReason: "config-missing" };
110
+ const workbuddyRemove = workbuddyConfigExists
111
+ ? await removeClaudeHook({
112
+ settingsPath: workbuddySettingsPath,
113
+ hookCommand: workbuddyHookCommand,
114
+ })
115
+ : { removed: false, skippedReason: "config-missing" };
116
+ const workbuddyAiRemove = workbuddyAiConfigExists
117
+ ? await removeClaudeHook({
118
+ settingsPath: workbuddyAiSettingsPath,
119
+ hookCommand: workbuddyAiHookCommand,
120
+ })
121
+ : { removed: false, skippedReason: "config-missing" };
122
+ const geminiRemove = geminiConfigExists
123
+ ? await removeGeminiHook({ settingsPath: geminiSettingsPath, hookCommand: geminiHookCommand })
124
+ : { removed: false, skippedReason: "config-missing" };
125
+ const opencodeRemove = opencodeConfigExists
126
+ ? await removeOpencodePlugin({ configDir: opencodeConfigDir })
127
+ : { removed: false, skippedReason: "config-missing" };
128
+ const openclawSessionPluginRemove = await removeOpenclawSessionPluginConfig({
129
+ home,
130
+ trackerDir,
131
+ env: process.env,
132
+ });
133
+ const openclawHookRemove = await removeOpenclawHookConfig({ home, trackerDir, env: process.env });
134
+ const grokHookRemove = await removeGrokHook({ home, trackerDir, env: process.env });
135
+ const ompHookRemove = await removeOmpHook({ home, trackerDir, env: process.env });
136
+
137
+ // Remove installed notify handler.
138
+ await fs.unlink(notifyPath).catch(() => {});
139
+
140
+ // Remove the hidden-runner wrapper and the hourly scheduled task init
141
+ // registered (0.2.0). Both best-effort: a machine that never registered
142
+ // them simply reports no change.
143
+ await fs.unlink(path.join(binDir, "run-sync-hidden.vbs")).catch(() => {});
144
+ const taskDelete = deleteWindowsScheduledTask();
145
+
146
+ // Remove local app runtime (installed by init for notify-driven sync).
147
+ await fs.rm(path.join(trackerDir, "app"), { recursive: true, force: true }).catch(() => {});
148
+
149
+ // Deliberately NOT removed by --purge: the machine-identity seed lets a
150
+ // reinstall reuse the same cloud device row instead of double-counting the
151
+ // replayed history under a new device (issue #176).
152
+ const machineIdSeedPath = path.join(home, ".config", "tokenusage", "machine-id");
153
+ let machineIdSeedKept = false;
154
+ if (opts.purge) {
155
+ await fs.rm(path.join(home, ".tokenusage"), { recursive: true, force: true }).catch(() => {});
156
+ machineIdSeedKept = await fs.access(machineIdSeedPath).then(
157
+ () => true,
158
+ () => false,
159
+ );
160
+ }
161
+
162
+ process.stdout.write(
163
+ [
164
+ "Uninstalled:",
165
+ codexConfigExists
166
+ ? codexRestore?.restored
167
+ ? `- Codex notify restored: ${codexConfigPath}`
168
+ : codexRestore?.skippedReason === "no-backup-not-installed"
169
+ ? "- Codex notify: skipped (no backup; not installed)"
170
+ : codexRestore?.skippedReason === "current-not-managed"
171
+ ? "- Codex notify: skipped (current notify is not managed by tokenusage)"
172
+ : "- Codex notify: no change"
173
+ : "- Codex notify: skipped (config.toml not found)",
174
+ acodeConfigExists
175
+ ? acodeRestore?.restored
176
+ ? `- AStudio notify restored: ${acodeConfigPath}`
177
+ : acodeRestore?.skippedReason === "no-backup-not-installed"
178
+ ? "- AStudio notify: skipped (no backup; not installed)"
179
+ : acodeRestore?.skippedReason === "current-not-managed"
180
+ ? "- AStudio notify: skipped (current notify is not managed by tokenusage)"
181
+ : "- AStudio notify: no change"
182
+ : "- AStudio notify: skipped (config.toml not found)",
183
+ codeConfigExists
184
+ ? codeRestore?.restored
185
+ ? `- Every Code notify restored: ${codeConfigPath}`
186
+ : codeRestore?.skippedReason === "no-backup-not-installed"
187
+ ? "- Every Code notify: skipped (no backup; not installed)"
188
+ : codeRestore?.skippedReason === "current-not-managed"
189
+ ? "- Every Code notify: skipped (current notify is not managed by tokenusage)"
190
+ : "- Every Code notify: no change"
191
+ : "- Every Code notify: skipped (config.toml not found)",
192
+ claudeConfigExists
193
+ ? claudeRemove?.removed
194
+ ? `- Claude hooks removed: ${claudeSettingsPath}`
195
+ : claudeRemove?.skippedReason === "hook-missing"
196
+ ? "- Claude hooks: no change"
197
+ : "- Claude hooks: skipped"
198
+ : "- Claude hooks: skipped (settings.json not found)",
199
+ codebuddyConfigExists
200
+ ? codebuddyRemove?.removed
201
+ ? `- CodeBuddy hooks removed: ${codebuddySettingsPath}`
202
+ : codebuddyRemove?.skippedReason === "hook-missing"
203
+ ? "- CodeBuddy hooks: no change"
204
+ : "- CodeBuddy hooks: skipped"
205
+ : "- CodeBuddy hooks: skipped (settings.json not found)",
206
+ workbuddyConfigExists
207
+ ? workbuddyRemove?.removed
208
+ ? `- WorkBuddy hooks removed: ${workbuddySettingsPath}`
209
+ : workbuddyRemove?.skippedReason === "hook-missing"
210
+ ? "- WorkBuddy hooks: no change"
211
+ : "- WorkBuddy hooks: skipped"
212
+ : "- WorkBuddy hooks: skipped (settings.json not found)",
213
+ workbuddyAiConfigExists
214
+ ? workbuddyAiRemove?.removed
215
+ ? `- WorkBuddy AI hooks removed: ${workbuddyAiSettingsPath}`
216
+ : workbuddyAiRemove?.skippedReason === "hook-missing"
217
+ ? "- WorkBuddy AI hooks: no change"
218
+ : "- WorkBuddy AI hooks: skipped"
219
+ : "- WorkBuddy AI hooks: skipped (settings.json not found)",
220
+ geminiConfigExists
221
+ ? geminiRemove?.removed
222
+ ? `- Gemini hooks removed: ${geminiSettingsPath}`
223
+ : geminiRemove?.skippedReason === "hook-missing"
224
+ ? "- Gemini hooks: no change"
225
+ : "- Gemini hooks: skipped"
226
+ : `- Gemini hooks: skipped (${geminiConfigDir} not found)`,
227
+ opencodeConfigExists
228
+ ? opencodeRemove?.removed
229
+ ? `- Opencode plugin removed: ${opencodeConfigDir}`
230
+ : opencodeRemove?.skippedReason === "plugin-missing"
231
+ ? "- Opencode plugin: no change"
232
+ : opencodeRemove?.skippedReason === "unexpected-content"
233
+ ? "- Opencode plugin: skipped (unexpected content)"
234
+ : "- Opencode plugin: skipped"
235
+ : `- Opencode plugin: skipped (${opencodeConfigDir} not found)`,
236
+ openclawSessionPluginRemove?.removed
237
+ ? `- OpenClaw session plugin removed: ${openclawSessionPluginRemove.openclawConfigPath}`
238
+ : openclawSessionPluginRemove?.skippedReason === "openclaw-config-missing"
239
+ ? "- OpenClaw session plugin: skipped (openclaw config not found)"
240
+ : "- OpenClaw session plugin: no change",
241
+ openclawHookRemove?.removed
242
+ ? `- OpenClaw hook (legacy) removed: ${openclawHookRemove.openclawConfigPath}`
243
+ : openclawHookRemove?.skippedReason === "openclaw-config-missing"
244
+ ? "- OpenClaw hook (legacy): skipped (openclaw config not found)"
245
+ : "- OpenClaw hook (legacy): no change",
246
+ grokHookRemove?.removed
247
+ ? `- Grok Build hook removed: ${grokHookRemove.hookPath}`
248
+ : "- Grok Build hook: no change",
249
+ formatOmpHookRemoveLine(ompHookRemove),
250
+ taskDelete.deleted
251
+ ? `- Scheduled task deleted: ${TASK_NAME}`
252
+ : "- Scheduled task: none",
253
+ opts.purge ? `- Purged: ${path.join(home, ".tokenusage")}` : "- Purge: skipped (use --purge)",
254
+ ...(machineIdSeedKept
255
+ ? [`- Kept: ${machineIdSeedPath} (cloud device identity — a reinstall reuses the same device; delete it to fully reset)`]
256
+ : []),
257
+ "",
258
+ ].join("\n"),
259
+ );
260
+ }
261
+
262
+ function formatOmpHookRemoveLine(ompHookRemove) {
263
+ if (ompHookRemove?.removed) {
264
+ return `- oh-my-pi notify extension removed: ${ompHookRemove.extensionPath}`;
265
+ }
266
+ const reason = ompHookRemove?.skippedReason;
267
+ const residualPath = ompHookRemove?.stagedPath || ompHookRemove?.extensionPath;
268
+ const residual = residualPath
269
+ ? ` (left in place: ${residualPath})`
270
+ : "";
271
+ if (reason === "unmanaged") {
272
+ return `- oh-my-pi notify extension: skipped (unmanaged file)${residual}`;
273
+ }
274
+ if (reason === "unlink-failed") {
275
+ const detail = ompHookRemove?.error ? `: ${ompHookRemove.error}` : "";
276
+ return `- oh-my-pi notify extension: failed to remove${detail}${residual}`;
277
+ }
278
+ if (reason === "extension-read-failed") {
279
+ const detail = ompHookRemove?.error ? `: ${ompHookRemove.error}` : "";
280
+ return `- oh-my-pi notify extension: failed to read${detail}${residual}`;
281
+ }
282
+ if (reason === "identity-changed") {
283
+ return `- oh-my-pi notify extension: skipped (file changed during uninstall)${residual}`;
284
+ }
285
+ if (reason === "omp-agent-dir-unresolved") {
286
+ return "- oh-my-pi notify extension: skipped (omp agent dir unresolved)";
287
+ }
288
+ if (reason === "missing") {
289
+ return "- oh-my-pi notify extension: no change";
290
+ }
291
+ return "- oh-my-pi notify extension: no change";
292
+ }
293
+
294
+ function parseArgs(argv) {
295
+ const out = { purge: false };
296
+ for (let i = 0; i < argv.length; i++) {
297
+ const a = argv[i];
298
+ if (a === "--purge") out.purge = true;
299
+ else throw new Error(`Unknown option: ${a}`);
300
+ }
301
+ return out;
302
+ }
303
+
304
+ module.exports = { cmdUninstall, formatOmpHookRemoveLine };
305
+
306
+ async function isFile(p) {
307
+ try {
308
+ const st = await fs.stat(p);
309
+ return st.isFile();
310
+ } catch (_e) {
311
+ return false;
312
+ }
313
+ }
314
+
315
+ async function isDir(p) {
316
+ try {
317
+ const st = await fs.stat(p);
318
+ return st.isDirectory();
319
+ } catch (_e) {
320
+ return false;
321
+ }
322
+ }