tokenusage-cli 0.3.0 → 0.4.1

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 (47) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +52 -11
  3. package/bin/tokenusage.js +14 -2
  4. package/package.json +1 -2
  5. package/src/cli.js +325 -78
  6. package/src/commands/device-login.js +3 -3
  7. package/src/commands/init.js +343 -153
  8. package/src/commands/pause.js +11 -23
  9. package/src/commands/reconcile.js +496 -0
  10. package/src/commands/sessions.js +57 -14
  11. package/src/commands/sync.js +69 -15
  12. package/src/commands/uninstall.js +6 -9
  13. package/src/commands/upload.js +147 -0
  14. package/src/lib/cli-errors.js +33 -0
  15. package/src/lib/cli-ui.js +93 -0
  16. package/src/lib/queue-row-normalize.js +55 -0
  17. package/src/lib/scheduled-task.js +597 -75
  18. package/src/lib/antigravity-paths.js +0 -48
  19. package/src/lib/async-queue-reader.js +0 -138
  20. package/src/lib/atomic-json.js +0 -27
  21. package/src/lib/bearer-token.js +0 -11
  22. package/src/lib/cloud-account.js +0 -541
  23. package/src/lib/codex-context-breakdown.js +0 -806
  24. package/src/lib/context-health.js +0 -124
  25. package/src/lib/grok-context-breakdown.js +0 -890
  26. package/src/lib/heatmap-compact.js +0 -114
  27. package/src/lib/local-api.js +0 -3574
  28. package/src/lib/outcomes-engine.js +0 -253
  29. package/src/lib/pet-packages.js +0 -722
  30. package/src/lib/skill-usage.js +0 -267
  31. package/src/lib/skills-manager.js +0 -1591
  32. package/src/lib/source-metadata.js +0 -48
  33. package/src/lib/subscription-manager.js +0 -293
  34. package/src/lib/wrapped-aggregator.js +0 -236
  35. package/test/audit-fixes.test.js +0 -249
  36. package/test/branding.test.js +0 -24
  37. package/test/cli-options.test.js +0 -43
  38. package/test/cli-smoke.test.js +0 -53
  39. package/test/cli-ui.test.js +0 -47
  40. package/test/collection-controls.test.js +0 -255
  41. package/test/ingest-sanitize.test.js +0 -162
  42. package/test/packaging.test.js +0 -107
  43. package/test/quiet-warnings.test.js +0 -53
  44. package/test/runtime-copy.test.js +0 -111
  45. package/test/session-upload.test.js +0 -427
  46. package/test/trae-db.test.js +0 -67
  47. package/test/zstd-native.test.js +0 -36
@@ -84,8 +84,13 @@ const {
84
84
  } = require("../lib/rollout");
85
85
  const { resolveRuntimeConfig, DEFAULT_BASE_URL, DEFAULT_DASHBOARD_URL, normalizeInstanceBaseUrl, assertInsforgeRuntime,
86
86
  clearDeviceIdentity, resetInstanceState } = require("../lib/runtime-config");
87
- const { registerWindowsScheduledTask, TASK_NAME, cliEntryPath } = require("../lib/scheduled-task");
87
+ const {
88
+ cliEntryPath,
89
+ registerAutoSyncSchedule,
90
+ formatScheduleRegistration,
91
+ } = require("../lib/scheduled-task");
88
92
  const { runtimeReadyMarkerPath } = require("../lib/runtime-entry");
93
+ const { UsageError, unknownOptionError } = require("../lib/cli-errors");
89
94
  const {
90
95
  BOLD,
91
96
  DIM,
@@ -94,6 +99,7 @@ const {
94
99
  color,
95
100
  isInteractive,
96
101
  promptMenu,
102
+ promptMultiSelect,
97
103
  createSpinner,
98
104
  } = require("../lib/cli-ui");
99
105
  const { renderLocalReport, renderAuthTransition, renderSuccessBox } = require("../lib/init-flow");
@@ -172,6 +178,89 @@ const SUPPORTED_PROVIDERS = [
172
178
  "Command Code",
173
179
  ];
174
180
 
181
+ // Hook-writing integrations only. These modify another tool's config, so they
182
+ // are the ones a user may want to opt out of one by one (`--integrations` or
183
+ // the interactive picker). Passive readers are read-only: they are detected and
184
+ // reported but never gated, because there is nothing to opt out of.
185
+ const SELECTABLE_INTEGRATIONS = [
186
+ { id: "codex", label: "Codex CLI" },
187
+ { id: "astudio", label: "AStudio" },
188
+ { id: "claude", label: "Claude" },
189
+ { id: "codebuddy", label: "CodeBuddy" },
190
+ { id: "workbuddy", label: "WorkBuddy" },
191
+ { id: "workbuddy-ai", label: "WorkBuddy AI" },
192
+ { id: "gemini", label: "Gemini" },
193
+ { id: "opencode", label: "Opencode Plugin" },
194
+ { id: "openclaw", label: "OpenClaw Session Plugin" },
195
+ { id: "grok", label: "Grok Build" },
196
+ { id: "omp", label: "oh-my-pi" },
197
+ { id: "every-code", label: "Every Code" },
198
+ ];
199
+
200
+ const SELECTABLE_INTEGRATION_IDS = SELECTABLE_INTEGRATIONS.map((entry) => entry.id);
201
+
202
+ // "null" means no explicit selection (configure everything detected). A Set
203
+ // means only the listed ids may write to tool configs.
204
+ function resolveIntegrationSelection(value) {
205
+ if (value == null) return null;
206
+ return new Set(value);
207
+ }
208
+
209
+ function integrationLabel(id) {
210
+ return (SELECTABLE_INTEGRATIONS.find((entry) => entry.id === id) || { label: id }).label;
211
+ }
212
+
213
+ function notSelectedItem(id) {
214
+ return { label: integrationLabel(id), status: "skipped", detail: "Not selected (--integrations)" };
215
+ }
216
+
217
+ // Interactive picker: only offer integrations whose config actually exists on
218
+ // this machine, so nobody toggles a tool they do not have. Returns null
219
+ // (configure everything) when the picker is unavailable.
220
+ async function pickIntegrationsInteractively({ home, trackerDir, notifyPath }) {
221
+ const detected = await detectSelectableIntegrations({ home, trackerDir, notifyPath });
222
+ if (detected.length === 0) return null;
223
+ const chosen = await promptMultiSelect({
224
+ message: "? Which tools should Token Usage configure? (space toggles, Enter confirms)",
225
+ options: detected.map((id) => ({ id, label: integrationLabel(id) })),
226
+ defaultSelected: detected,
227
+ });
228
+ const ids = chosen.filter((id) => SELECTABLE_INTEGRATION_IDS.includes(id));
229
+ process.stdout.write(
230
+ ids.length === 0
231
+ ? "No tool configs selected — switching to --no-integrations semantics.\n"
232
+ : `Configuring: ${ids.map(integrationLabel).join(", ")}\n`,
233
+ );
234
+ return new Set(ids);
235
+ }
236
+
237
+ async function detectSelectableIntegrations({ home, trackerDir, notifyPath }) {
238
+ const context = buildIntegrationTargets({ home, trackerDir, notifyPath });
239
+ const detected = [];
240
+ const hasFile = async (target) => (await probeFile(target)).exists;
241
+ const hasDir = async (target) => isDir(target);
242
+
243
+ if (await hasFile(context.codexConfigPath)) detected.push("codex");
244
+ if (await hasFile(context.acodeConfigPath)) detected.push("astudio");
245
+ if (await hasDir(context.claudeDir)) detected.push("claude");
246
+ if (await hasDir(context.codebuddyDir)) detected.push("codebuddy");
247
+ if (await hasDir(context.workbuddyDir)) detected.push("workbuddy");
248
+ if (await hasDir(context.workbuddyAiDir)) detected.push("workbuddy-ai");
249
+ if (await hasDir(context.geminiConfigDir)) detected.push("gemini");
250
+ if (await hasDir(context.opencodeConfigDir)) detected.push("opencode");
251
+ if (await hasFile(context.codeConfigPath)) detected.push("every-code");
252
+ const openclawState = await probeOpenclawSessionPluginState({
253
+ home,
254
+ trackerDir,
255
+ env: process.env,
256
+ }).catch(() => null);
257
+ if (openclawState && !openclawState.skippedReason) detected.push("openclaw");
258
+ const grokState = await probeGrokHookState({ home, trackerDir, env: process.env }).catch(() => null);
259
+ if (grokState?.hasGrokInstall) detected.push("grok");
260
+ if (resolveOmpAgentDir(process.env)) detected.push("omp");
261
+ return detected;
262
+ }
263
+
175
264
  async function cmdInit(argv, options = {}) {
176
265
  const opts = parseArgs(argv);
177
266
  const home = options.home || os.homedir();
@@ -181,7 +270,6 @@ async function cmdInit(argv, options = {}) {
181
270
 
182
271
  const configPath = path.join(trackerDir, "config.json");
183
272
  const notifyOriginalPath = path.join(trackerDir, "codex_notify_original.json");
184
- const linkCodeStatePath = path.join(trackerDir, "link_code_state.json");
185
273
 
186
274
  const existingConfig = await readJson(configPath);
187
275
  const runtime = resolveRuntimeConfig({
@@ -213,8 +301,13 @@ async function cmdInit(argv, options = {}) {
213
301
  process.stdout.write("Setup cancelled.\n");
214
302
  return;
215
303
  }
304
+ if (opts.integrationSelection == null) {
305
+ opts.integrationSelection = await pickIntegrationsInteractively({ home, trackerDir, notifyPath });
306
+ }
216
307
  }
217
308
 
309
+ const integrationSelection = opts.integrationSelection;
310
+
218
311
  if (opts.dryRun) {
219
312
  const preview = await buildDryRunSummary({
220
313
  opts,
@@ -222,6 +315,7 @@ async function cmdInit(argv, options = {}) {
222
315
  trackerDir,
223
316
  notifyPath,
224
317
  runtime,
318
+ integrationSelection,
225
319
  });
226
320
  renderLocalReport({ summary: preview.summary, isDryRun: true });
227
321
  renderAccountNotLinked({ context: "dry-run" });
@@ -239,12 +333,12 @@ async function cmdInit(argv, options = {}) {
239
333
  binDir,
240
334
  configPath,
241
335
  notifyOriginalPath,
242
- linkCodeStatePath,
243
336
  notifyPath,
244
337
  appDir,
245
338
  trackerBinPath,
246
339
  runtime,
247
340
  existingConfig,
341
+ integrationSelection,
248
342
  });
249
343
  } catch (err) {
250
344
  spinner.stop();
@@ -341,17 +435,7 @@ function renderAccountNotLinked({ context } = {}) {
341
435
  renderLocalSuccess();
342
436
  }
343
437
 
344
- function shouldUseBrowserAuth({ deviceToken, opts }) {
345
- if (deviceToken) return false;
346
- if (opts.noAuth) return false;
347
- if (opts.linkCode) return false;
348
- if (opts.email || opts.password) return false;
349
- return true;
350
- }
351
-
352
- async function buildDryRunSummary({ opts, home, trackerDir, notifyPath, runtime }) {
353
- const deviceToken = runtime?.deviceToken || null;
354
- const pendingBrowserAuth = shouldUseBrowserAuth({ deviceToken, opts });
438
+ async function buildDryRunSummary({ opts, home, trackerDir, notifyPath, integrationSelection = null }) {
355
439
  if (opts.noIntegrations) {
356
440
  return {
357
441
  summary: [{
@@ -359,13 +443,11 @@ async function buildDryRunSummary({ opts, home, trackerDir, notifyPath, runtime
359
443
  status: "skipped",
360
444
  detail: "Skipped (--no-integrations); tool configs stay untouched",
361
445
  }],
362
- pendingBrowserAuth,
363
- deviceToken,
364
446
  };
365
447
  }
366
448
  const context = buildIntegrationTargets({ home, trackerDir, notifyPath });
367
- const summary = await previewIntegrations({ context });
368
- return { summary, pendingBrowserAuth, deviceToken };
449
+ const summary = await previewIntegrations({ context, integrationSelection });
450
+ return { summary };
369
451
  }
370
452
 
371
453
  async function runSetup({
@@ -375,19 +457,18 @@ async function runSetup({
375
457
  binDir,
376
458
  configPath,
377
459
  notifyOriginalPath,
378
- linkCodeStatePath,
379
460
  notifyPath,
380
461
  appDir,
381
462
  trackerBinPath,
382
463
  runtime,
383
464
  existingConfig,
465
+ integrationSelection = null,
384
466
  }) {
385
467
  await ensureDir(trackerDir);
386
468
  await ensureDir(binDir);
387
469
  let deviceToken = runtime?.deviceToken || null;
388
470
  let deviceId = existingConfig?.deviceId || null;
389
471
  const installedAt = existingConfig?.installedAt || new Date().toISOString();
390
- let pendingBrowserAuth = false;
391
472
 
392
473
  const runtimeCopy = await installLocalTrackerApp({ appDir });
393
474
 
@@ -442,6 +523,7 @@ async function runSetup({
442
523
  notifyPath,
443
524
  notifyOriginalPath,
444
525
  dryRun: Boolean(opts.dryRun),
526
+ integrationSelection,
445
527
  });
446
528
 
447
529
  if (runtimeCopy.ok === false) {
@@ -455,40 +537,37 @@ async function runSetup({
455
537
  summary.push({ label: "Runtime copy", status: "skipped", detail });
456
538
  }
457
539
 
458
- // Windows scheduled task takeover (0.2.0): hourly auto sync behind a hidden
459
- // VBS runner — no conhost flash, no shell redirect (sync self-logs to the
460
- // rolling logs/sync.log). Best-effort; pause/resume toggle this task.
540
+ // Auto-sync schedule takeover: hourly auto sync behind a hidden runner —
541
+ // Task Scheduler on Windows (VBS, no conhost flash), launchd on macOS and a
542
+ // systemd --user timer on Linux (both via a generated /bin/sh launcher). No
543
+ // shell redirect: sync self-logs into the rolling logs/sync.log. Best-effort;
544
+ // pause/resume toggle the registered schedule.
461
545
  if (opts.noTask) {
462
546
  summary.push({
463
- label: "Scheduled task",
547
+ label: "Scheduled sync",
464
548
  status: "skipped",
465
549
  detail: "Skipped (--no-task); auto syncs stay manual",
466
550
  });
467
551
  } else {
468
- // The VBS targets the package's bin entry (scheduled-task.js owns that
469
- // path): pointing it at src/cli.js makes the hidden task a no-op. The
470
- // runtime copy refresh below is also the task's fallback, so an nvm switch
471
- // (which drops the version-specific npm prefix) does not kill collection —
472
- // but only a copy with its readiness marker counts as usable.
552
+ // The generated runner targets the package's bin entry (scheduled-task.js
553
+ // owns that path): pointing it at src/cli.js makes the hidden run a no-op.
554
+ // The runtime copy refresh below is also the runner's fallback, so an nvm
555
+ // switch (which drops the version-specific npm prefix) does not kill
556
+ // collection — but only a copy with its readiness marker counts as usable.
473
557
  const fallbackCliJs =
474
558
  runtimeCopy.ok === true && fssync.existsSync(runtimeReadyMarkerPath(appDir))
475
559
  ? trackerBinPath
476
560
  : undefined;
477
- const taskOutcome = registerWindowsScheduledTask({ binDir, fallbackCliJs });
561
+ const scheduleOutcome = registerAutoSyncSchedule({ binDir, home, fallbackCliJs });
478
562
  summary.push({
479
- label: "Scheduled task",
480
- status: taskOutcome.registered ? "installed" : "skipped",
481
- detail: taskOutcome.registered
482
- ? `${TASK_NAME}: hourly, hidden window (pause/resume toggles it)`
483
- : taskOutcome.reason === "not_windows"
484
- ? "Windows-only; use cron/launchd + `tokenusage pause` elsewhere"
485
- : `Not registered: ${taskOutcome.reason}`,
563
+ label: "Scheduled sync",
564
+ status: scheduleOutcome.registered ? "installed" : "skipped",
565
+ detail: formatScheduleRegistration(scheduleOutcome),
486
566
  });
487
567
  }
488
568
 
489
569
  return {
490
570
  summary,
491
- pendingBrowserAuth,
492
571
  deviceToken,
493
572
  deviceId,
494
573
  installedAt,
@@ -750,14 +829,20 @@ async function applyIntegrationSetup({
750
829
  notifyPath,
751
830
  notifyOriginalPath,
752
831
  dryRun = false,
832
+ integrationSelection = null,
753
833
  }) {
754
834
  const context = buildIntegrationTargets({ home, trackerDir, notifyPath });
755
835
  context.notifyOriginalPath = notifyOriginalPath;
756
836
 
837
+ const selection = resolveIntegrationSelection(integrationSelection);
838
+ const want = (id) => selection === null || selection.has(id);
839
+
757
840
  const summary = [];
758
841
 
759
- const codexProbe = await probeFile(context.codexConfigPath);
760
- if (codexProbe.exists) {
842
+ const codexProbe = want("codex") ? await probeFile(context.codexConfigPath) : { exists: false };
843
+ if (!want("codex")) {
844
+ summary.push(notSelectedItem("codex"));
845
+ } else if (codexProbe.exists) {
761
846
  const currentNotify = await readCodexNotify(context.codexConfigPath);
762
847
  const result = await upsertCodexNotify({
763
848
  codexConfigPath: context.codexConfigPath,
@@ -774,8 +859,10 @@ async function applyIntegrationSetup({
774
859
  summary.push({ label: "Codex CLI", status: "skipped", detail: renderSkipDetail(codexProbe) });
775
860
  }
776
861
 
777
- const acodeProbe = await probeFile(context.acodeConfigPath);
778
- if (acodeProbe.exists) {
862
+ const acodeProbe = want("astudio") ? await probeFile(context.acodeConfigPath) : { exists: false };
863
+ if (!want("astudio")) {
864
+ summary.push(notSelectedItem("astudio"));
865
+ } else if (acodeProbe.exists) {
779
866
  const currentNotify = await readAcodeNotify(context.acodeConfigPath);
780
867
  const result = await upsertAcodeNotify({
781
868
  acodeConfigPath: context.acodeConfigPath,
@@ -792,8 +879,10 @@ async function applyIntegrationSetup({
792
879
  summary.push({ label: "AStudio", status: "skipped", detail: renderSkipDetail(acodeProbe) });
793
880
  }
794
881
 
795
- const claudeDirExists = await isDir(context.claudeDir);
796
- if (claudeDirExists) {
882
+ const claudeDirExists = want("claude") && await isDir(context.claudeDir);
883
+ if (!want("claude")) {
884
+ summary.push(notSelectedItem("claude"));
885
+ } else if (claudeDirExists) {
797
886
  await upsertClaudeUsageHooks({
798
887
  settingsPath: context.claudeSettingsPath,
799
888
  hookCommand: context.claudeHookCommand,
@@ -803,8 +892,10 @@ async function applyIntegrationSetup({
803
892
  summary.push({ label: "Claude", status: "skipped", detail: "Config not found" });
804
893
  }
805
894
 
806
- const geminiConfigExists = await isDir(context.geminiConfigDir);
807
- if (geminiConfigExists) {
895
+ const geminiConfigExists = want("gemini") && await isDir(context.geminiConfigDir);
896
+ if (!want("gemini")) {
897
+ summary.push(notSelectedItem("gemini"));
898
+ } else if (geminiConfigExists) {
808
899
  await upsertGeminiHook({
809
900
  settingsPath: context.geminiSettingsPath,
810
901
  hookCommand: context.geminiHookCommand,
@@ -814,18 +905,22 @@ async function applyIntegrationSetup({
814
905
  summary.push({ label: "Gemini", status: "skipped", detail: "Config not found" });
815
906
  }
816
907
 
817
- const opencodeResult = await upsertOpencodePlugin({
818
- configDir: context.opencodeConfigDir,
819
- notifyPath,
820
- });
821
- if (opencodeResult?.skippedReason === "config-missing") {
822
- summary.push({ label: "Opencode Plugin", status: "skipped", detail: "Config not found" });
908
+ if (!want("opencode")) {
909
+ summary.push(notSelectedItem("opencode"));
823
910
  } else {
824
- summary.push({
825
- label: "Opencode Plugin",
826
- status: opencodeResult?.changed ? "installed" : "set",
827
- detail: "Plugin installed",
911
+ const opencodeResult = await upsertOpencodePlugin({
912
+ configDir: context.opencodeConfigDir,
913
+ notifyPath,
828
914
  });
915
+ if (opencodeResult?.skippedReason === "config-missing") {
916
+ summary.push({ label: "Opencode Plugin", status: "skipped", detail: "Config not found" });
917
+ } else {
918
+ summary.push({
919
+ label: "Opencode Plugin",
920
+ status: opencodeResult?.changed ? "installed" : "set",
921
+ detail: "Plugin installed",
922
+ });
923
+ }
829
924
  }
830
925
 
831
926
  // Cursor (API-based, no hooks needed)
@@ -862,39 +957,43 @@ async function applyIntegrationSetup({
862
957
  // oh-my-pi: passive session scan always works; also install an optional
863
958
  // notify extension so turn_end triggers sync --source omp near-real-time.
864
959
  {
865
- const fssyncLocal = require("node:fs");
866
- const ompAgentDir = resolveOmpAgentDir(process.env);
867
- const ompSessions = ompAgentDir && fssyncLocal.existsSync(path.join(ompAgentDir, "sessions"));
868
- if (ompAgentDir && (ompSessions || fssyncLocal.existsSync(ompAgentDir))) {
869
- if (dryRun) {
870
- const probe = await probeOmpHookState({ home, trackerDir, env: process.env });
871
- summary.push({
872
- label: "oh-my-pi",
873
- status: "detected",
874
- detail: probe.configured
875
- ? "Notify extension already installed (passive scan still runs)"
876
- : "Will install notify extension + keep passive scan",
877
- });
878
- } else {
879
- const result = await upsertOmpHook({ home, trackerDir, env: process.env });
880
- if (result.written) {
881
- summary.push({
882
- label: "oh-my-pi",
883
- status: "installed",
884
- detail: `Notify extension → ${result.extensionPath} (passive scan still runs)`,
885
- });
886
- } else if (result.skippedReason === "unmanaged-extension-present") {
887
- summary.push({
888
- label: "oh-my-pi",
889
- status: "skipped",
890
- detail: "Existing unmanaged tokenusage-notify extension left untouched",
891
- });
892
- } else {
960
+ if (!want("omp")) {
961
+ summary.push(notSelectedItem("omp"));
962
+ } else {
963
+ const fssyncLocal = require("node:fs");
964
+ const ompAgentDir = resolveOmpAgentDir(process.env);
965
+ const ompSessions = ompAgentDir && fssyncLocal.existsSync(path.join(ompAgentDir, "sessions"));
966
+ if (ompAgentDir && (ompSessions || fssyncLocal.existsSync(ompAgentDir))) {
967
+ if (dryRun) {
968
+ const probe = await probeOmpHookState({ home, trackerDir, env: process.env });
893
969
  summary.push({
894
970
  label: "oh-my-pi",
895
971
  status: "detected",
896
- detail: "Passive reader (notify extension not written)",
972
+ detail: probe.configured
973
+ ? "Notify extension already installed (passive scan still runs)"
974
+ : "Will install notify extension + keep passive scan",
897
975
  });
976
+ } else {
977
+ const result = await upsertOmpHook({ home, trackerDir, env: process.env });
978
+ if (result.written) {
979
+ summary.push({
980
+ label: "oh-my-pi",
981
+ status: "installed",
982
+ detail: `Notify extension → ${result.extensionPath} (passive scan still runs)`,
983
+ });
984
+ } else if (result.skippedReason === "unmanaged-extension-present") {
985
+ summary.push({
986
+ label: "oh-my-pi",
987
+ status: "skipped",
988
+ detail: "Existing unmanaged tokenusage-notify extension left untouched",
989
+ });
990
+ } else {
991
+ summary.push({
992
+ label: "oh-my-pi",
993
+ status: "detected",
994
+ detail: "Passive reader (notify extension not written)",
995
+ });
996
+ }
898
997
  }
899
998
  }
900
999
  }
@@ -980,6 +1079,9 @@ async function applyIntegrationSetup({
980
1079
 
981
1080
  // Grok Build (xAI): SessionEnd hook in ~/.grok/hooks/ + handler in ~/.tokenusage/bin/
982
1081
  {
1082
+ if (!want("grok")) {
1083
+ summary.push(notSelectedItem("grok"));
1084
+ } else {
983
1085
  try {
984
1086
  const grokState = await probeGrokHookState({ home, trackerDir, env: process.env });
985
1087
  if (grokState.hasGrokInstall) {
@@ -993,6 +1095,7 @@ async function applyIntegrationSetup({
993
1095
  } catch (err) {
994
1096
  summary.push({ label: "Grok Build", status: "error", detail: String(err?.message || err) });
995
1097
  }
1098
+ }
996
1099
  }
997
1100
 
998
1101
  // Kilo CLI (kilo.ai @kilocode/plugin): passive reader — no hook installation
@@ -1114,8 +1217,10 @@ async function applyIntegrationSetup({
1114
1217
  // CodeBuddy: Claude-Code fork. Install the SessionEnd hook so finished
1115
1218
  // sessions trigger notify.cjs → tracker sync; passive scan still runs as a
1116
1219
  // safety net for sessions that don't fire SessionEnd cleanly.
1117
- const codebuddyDirExists = await isDir(context.codebuddyDir);
1118
- if (codebuddyDirExists) {
1220
+ const codebuddyDirExists = want("codebuddy") && await isDir(context.codebuddyDir);
1221
+ if (!want("codebuddy")) {
1222
+ summary.push(notSelectedItem("codebuddy"));
1223
+ } else if (codebuddyDirExists) {
1119
1224
  await upsertClaudeHook({
1120
1225
  settingsPath: context.codebuddySettingsPath,
1121
1226
  hookCommand: context.codebuddyHookCommand,
@@ -1127,8 +1232,10 @@ async function applyIntegrationSetup({
1127
1232
 
1128
1233
  // WorkBuddy: sibling Claude-Code fork. Same SessionEnd hook → notify.cjs →
1129
1234
  // tracker sync; passive scan still runs as a safety net.
1130
- const workbuddyDirExists = await isDir(context.workbuddyDir);
1131
- if (workbuddyDirExists) {
1235
+ const workbuddyDirExists = want("workbuddy") && await isDir(context.workbuddyDir);
1236
+ if (!want("workbuddy")) {
1237
+ summary.push(notSelectedItem("workbuddy"));
1238
+ } else if (workbuddyDirExists) {
1132
1239
  await upsertClaudeHook({
1133
1240
  settingsPath: context.workbuddySettingsPath,
1134
1241
  hookCommand: context.workbuddyHookCommand,
@@ -1139,8 +1246,10 @@ async function applyIntegrationSetup({
1139
1246
  }
1140
1247
 
1141
1248
  // WorkBuddy AI: international build, same hook schema, sibling home.
1142
- const workbuddyAiDirExists = await isDir(context.workbuddyAiDir);
1143
- if (workbuddyAiDirExists) {
1249
+ const workbuddyAiDirExists = want("workbuddy-ai") && await isDir(context.workbuddyAiDir);
1250
+ if (!want("workbuddy-ai")) {
1251
+ summary.push(notSelectedItem("workbuddy-ai"));
1252
+ } else if (workbuddyAiDirExists) {
1144
1253
  await upsertClaudeHook({
1145
1254
  settingsPath: context.workbuddyAiSettingsPath,
1146
1255
  hookCommand: context.workbuddyAiHookCommand,
@@ -1150,18 +1259,20 @@ async function applyIntegrationSetup({
1150
1259
  summary.push({ label: "WorkBuddy AI", status: "skipped", detail: "Config not found" });
1151
1260
  }
1152
1261
 
1153
- const openclawBefore = await probeOpenclawSessionPluginState({
1154
- home,
1155
- trackerDir,
1156
- env: process.env,
1157
- });
1158
- const openclawInstall = await installOpenclawSessionPlugin({
1159
- home,
1160
- trackerDir,
1161
- packageName: "tokenusage-cli",
1162
- env: process.env,
1163
- });
1164
- if (openclawInstall?.skippedReason === "openclaw-cli-missing") {
1262
+ const openclawBefore = want("openclaw")
1263
+ ? await probeOpenclawSessionPluginState({ home, trackerDir, env: process.env })
1264
+ : null;
1265
+ const openclawInstall = want("openclaw")
1266
+ ? await installOpenclawSessionPlugin({
1267
+ home,
1268
+ trackerDir,
1269
+ packageName: "tokenusage-cli",
1270
+ env: process.env,
1271
+ })
1272
+ : null;
1273
+ if (!want("openclaw")) {
1274
+ summary.push(notSelectedItem("openclaw"));
1275
+ } else if (openclawInstall?.skippedReason === "openclaw-cli-missing") {
1165
1276
  summary.push({
1166
1277
  label: "OpenClaw Session Plugin",
1167
1278
  status: "skipped",
@@ -1197,7 +1308,11 @@ async function applyIntegrationSetup({
1197
1308
  });
1198
1309
  }
1199
1310
 
1200
- const legacyHookState = await probeOpenclawHookState({ home, trackerDir, env: process.env });
1311
+ // Legacy command-hook cleanup only runs when OpenClaw is selected: opting
1312
+ // out must not rewrite that tool's config.
1313
+ const legacyHookState = want("openclaw")
1314
+ ? await probeOpenclawHookState({ home, trackerDir, env: process.env })
1315
+ : null;
1201
1316
  if (legacyHookState?.configured || legacyHookState?.linked || legacyHookState?.enabled) {
1202
1317
  await removeOpenclawHookConfig({ home, trackerDir, env: process.env });
1203
1318
  summary.push({
@@ -1207,8 +1322,10 @@ async function applyIntegrationSetup({
1207
1322
  });
1208
1323
  }
1209
1324
 
1210
- const codeProbe = await probeFile(context.codeConfigPath);
1211
- if (codeProbe.exists) {
1325
+ const codeProbe = want("every-code") ? await probeFile(context.codeConfigPath) : { exists: false };
1326
+ if (!want("every-code")) {
1327
+ summary.push(notSelectedItem("every-code"));
1328
+ } else if (codeProbe.exists) {
1212
1329
  const currentNotify = await readEveryCodeNotify(context.codeConfigPath);
1213
1330
  const result = await upsertEveryCodeNotify({
1214
1331
  codeConfigPath: context.codeConfigPath,
@@ -1228,12 +1345,16 @@ async function applyIntegrationSetup({
1228
1345
  return summary;
1229
1346
  }
1230
1347
 
1231
- async function previewIntegrations({ context }) {
1348
+ async function previewIntegrations({ context, integrationSelection = null }) {
1232
1349
  const summary = [];
1233
1350
  const home = os.homedir();
1351
+ const selection = resolveIntegrationSelection(integrationSelection);
1352
+ const want = (id) => selection === null || selection.has(id);
1234
1353
 
1235
- const codexProbe = await probeFile(context.codexConfigPath);
1236
- if (codexProbe.exists) {
1354
+ const codexProbe = want("codex") ? await probeFile(context.codexConfigPath) : { exists: false };
1355
+ if (!want("codex")) {
1356
+ summary.push(notSelectedItem("codex"));
1357
+ } else if (codexProbe.exists) {
1237
1358
  const existing = await readCodexNotify(context.codexConfigPath);
1238
1359
  const matches = arraysEqual(existing, context.notifyCmd);
1239
1360
  summary.push({
@@ -1245,8 +1366,10 @@ async function previewIntegrations({ context }) {
1245
1366
  summary.push({ label: "Codex CLI", status: "skipped", detail: renderSkipDetail(codexProbe) });
1246
1367
  }
1247
1368
 
1248
- const acodeProbe = await probeFile(context.acodeConfigPath);
1249
- if (acodeProbe.exists) {
1369
+ const acodeProbe = want("astudio") ? await probeFile(context.acodeConfigPath) : { exists: false };
1370
+ if (!want("astudio")) {
1371
+ summary.push(notSelectedItem("astudio"));
1372
+ } else if (acodeProbe.exists) {
1250
1373
  const existing = await readAcodeNotify(context.acodeConfigPath);
1251
1374
  const matches = arraysEqual(existing, context.acodeNotifyCmd);
1252
1375
  summary.push({
@@ -1258,8 +1381,10 @@ async function previewIntegrations({ context }) {
1258
1381
  summary.push({ label: "AStudio", status: "skipped", detail: renderSkipDetail(acodeProbe) });
1259
1382
  }
1260
1383
 
1261
- const claudeDirExists = await isDir(context.claudeDir);
1262
- if (claudeDirExists) {
1384
+ const claudeDirExists = want("claude") && await isDir(context.claudeDir);
1385
+ if (!want("claude")) {
1386
+ summary.push(notSelectedItem("claude"));
1387
+ } else if (claudeDirExists) {
1263
1388
  const configured = await areClaudeUsageHooksConfigured({
1264
1389
  settingsPath: context.claudeSettingsPath,
1265
1390
  hookCommand: context.claudeHookCommand,
@@ -1273,8 +1398,10 @@ async function previewIntegrations({ context }) {
1273
1398
  summary.push({ label: "Claude", status: "skipped", detail: "Config not found" });
1274
1399
  }
1275
1400
 
1276
- const codebuddyDirExists = await isDir(context.codebuddyDir);
1277
- if (codebuddyDirExists) {
1401
+ const codebuddyDirExists = want("codebuddy") && await isDir(context.codebuddyDir);
1402
+ if (!want("codebuddy")) {
1403
+ summary.push(notSelectedItem("codebuddy"));
1404
+ } else if (codebuddyDirExists) {
1278
1405
  const configured = await isClaudeHookConfigured({
1279
1406
  settingsPath: context.codebuddySettingsPath,
1280
1407
  hookCommand: context.codebuddyHookCommand,
@@ -1288,8 +1415,10 @@ async function previewIntegrations({ context }) {
1288
1415
  summary.push({ label: "CodeBuddy", status: "skipped", detail: "Config not found" });
1289
1416
  }
1290
1417
 
1291
- const workbuddyDirExists = await isDir(context.workbuddyDir);
1292
- if (workbuddyDirExists) {
1418
+ const workbuddyDirExists = want("workbuddy") && await isDir(context.workbuddyDir);
1419
+ if (!want("workbuddy")) {
1420
+ summary.push(notSelectedItem("workbuddy"));
1421
+ } else if (workbuddyDirExists) {
1293
1422
  const configured = await isClaudeHookConfigured({
1294
1423
  settingsPath: context.workbuddySettingsPath,
1295
1424
  hookCommand: context.workbuddyHookCommand,
@@ -1303,8 +1432,10 @@ async function previewIntegrations({ context }) {
1303
1432
  summary.push({ label: "WorkBuddy", status: "skipped", detail: "Config not found" });
1304
1433
  }
1305
1434
 
1306
- const workbuddyAiDirExists = await isDir(context.workbuddyAiDir);
1307
- if (workbuddyAiDirExists) {
1435
+ const workbuddyAiDirExists = want("workbuddy-ai") && await isDir(context.workbuddyAiDir);
1436
+ if (!want("workbuddy-ai")) {
1437
+ summary.push(notSelectedItem("workbuddy-ai"));
1438
+ } else if (workbuddyAiDirExists) {
1308
1439
  const configured = await isClaudeHookConfigured({
1309
1440
  settingsPath: context.workbuddyAiSettingsPath,
1310
1441
  hookCommand: context.workbuddyAiHookCommand,
@@ -1318,8 +1449,10 @@ async function previewIntegrations({ context }) {
1318
1449
  summary.push({ label: "WorkBuddy AI", status: "skipped", detail: "Config not found" });
1319
1450
  }
1320
1451
 
1321
- const geminiConfigExists = await isDir(context.geminiConfigDir);
1322
- if (geminiConfigExists) {
1452
+ const geminiConfigExists = want("gemini") && await isDir(context.geminiConfigDir);
1453
+ if (!want("gemini")) {
1454
+ summary.push(notSelectedItem("gemini"));
1455
+ } else if (geminiConfigExists) {
1323
1456
  const configured = await isGeminiHookConfigured({
1324
1457
  settingsPath: context.geminiSettingsPath,
1325
1458
  hookCommand: context.geminiHookCommand,
@@ -1333,25 +1466,33 @@ async function previewIntegrations({ context }) {
1333
1466
  summary.push({ label: "Gemini", status: "skipped", detail: "Config not found" });
1334
1467
  }
1335
1468
 
1336
- const opencodeDirExists = await isDir(context.opencodeConfigDir);
1337
- const installed = await isOpencodePluginInstalled({ configDir: context.opencodeConfigDir });
1338
- const opencodeDetail = installed
1339
- ? "Plugin already installed"
1340
- : opencodeDirExists
1341
- ? "Will install plugin"
1342
- : "Will create config and install plugin";
1343
- summary.push({
1344
- label: "Opencode Plugin",
1345
- status: "installed",
1346
- detail: opencodeDetail,
1347
- });
1469
+ if (!want("opencode")) {
1470
+ summary.push(notSelectedItem("opencode"));
1471
+ } else {
1472
+ const opencodeDirExists = await isDir(context.opencodeConfigDir);
1473
+ const installed = await isOpencodePluginInstalled({ configDir: context.opencodeConfigDir });
1474
+ const opencodeDetail = installed
1475
+ ? "Plugin already installed"
1476
+ : opencodeDirExists
1477
+ ? "Will install plugin"
1478
+ : "Will create config and install plugin";
1479
+ summary.push({
1480
+ label: "Opencode Plugin",
1481
+ status: "installed",
1482
+ detail: opencodeDetail,
1483
+ });
1484
+ }
1348
1485
 
1349
- const openclawState = await probeOpenclawSessionPluginState({
1350
- home,
1351
- trackerDir: context.trackerDir,
1352
- env: process.env,
1353
- });
1354
- if (openclawState?.skippedReason === "openclaw-config-missing") {
1486
+ const openclawState = want("openclaw")
1487
+ ? await probeOpenclawSessionPluginState({
1488
+ home,
1489
+ trackerDir: context.trackerDir,
1490
+ env: process.env,
1491
+ })
1492
+ : null;
1493
+ if (!want("openclaw")) {
1494
+ summary.push(notSelectedItem("openclaw"));
1495
+ } else if (openclawState?.skippedReason === "openclaw-config-missing") {
1355
1496
  summary.push({
1356
1497
  label: "OpenClaw Session Plugin",
1357
1498
  status: "skipped",
@@ -1388,8 +1529,10 @@ async function previewIntegrations({ context }) {
1388
1529
  });
1389
1530
  }
1390
1531
 
1391
- const codeProbe = await probeFile(context.codeConfigPath);
1392
- if (codeProbe.exists) {
1532
+ const codeProbe = want("every-code") ? await probeFile(context.codeConfigPath) : { exists: false };
1533
+ if (!want("every-code")) {
1534
+ summary.push(notSelectedItem("every-code"));
1535
+ } else if (codeProbe.exists) {
1393
1536
  const existing = await readEveryCodeNotify(context.codeConfigPath);
1394
1537
  const matches = arraysEqual(existing, context.codeNotifyCmd);
1395
1538
  summary.push({
@@ -1475,23 +1618,60 @@ function containsNestedTokenUsageNotify(cmd, expectedNotify) {
1475
1618
  return false;
1476
1619
  }
1477
1620
 
1621
+ // Flags from the browser sign-in era (device-flow pairing replaced it in
1622
+ // 0.2.x). They used to parse silently into fields nobody read, so a user
1623
+ // following an old blog post got an interactive init instead of an error —
1624
+ // they now fail with a pointer at the replacement.
1625
+ const REMOVED_AUTH_FLAGS = ["--email", "--password", "--device-name", "--link-code", "--no-auth"];
1626
+
1627
+ // `--integrations all|none|<id,...>`: "all" (or an absent flag) keeps the
1628
+ // original configure-everything-detected behavior (null selection); "none" is
1629
+ // exactly --no-integrations; a list configures only those tools and reports
1630
+ // the rest as skipped instead of touching their configs.
1631
+ function parseIntegrationsFlag(value) {
1632
+ const raw = String(value ?? "").trim();
1633
+ if (!raw) {
1634
+ throw new UsageError("--integrations requires a value", {
1635
+ code: "INTEGRATIONS_MISSING",
1636
+ hint: `Use all, none, or a comma-separated list of ids: ${SELECTABLE_INTEGRATION_IDS.join(", ")}`,
1637
+ });
1638
+ }
1639
+ const normalized = raw.toLowerCase();
1640
+ if (normalized === "all") return null;
1641
+ if (normalized === "none") return new Set();
1642
+ const ids = normalized
1643
+ .split(",")
1644
+ .map((id) => id.trim())
1645
+ .filter(Boolean);
1646
+ if (ids.length === 0) {
1647
+ throw new UsageError(`Unknown --integrations value: ${raw}`, {
1648
+ code: "INTEGRATIONS_UNKNOWN",
1649
+ hint: `Available ids: ${SELECTABLE_INTEGRATION_IDS.join(", ")}`,
1650
+ });
1651
+ }
1652
+ const unknown = ids.filter((id) => !SELECTABLE_INTEGRATION_IDS.includes(id));
1653
+ if (unknown.length > 0) {
1654
+ throw new UsageError(`Unknown integration id${unknown.length > 1 ? "s" : ""}: ${unknown.join(", ")}`, {
1655
+ code: "INTEGRATIONS_UNKNOWN",
1656
+ hint: `Available ids: ${SELECTABLE_INTEGRATION_IDS.join(", ")}`,
1657
+ });
1658
+ }
1659
+ return new Set(ids);
1660
+ }
1661
+
1478
1662
  function parseArgs(argv) {
1479
1663
  const out = {
1480
1664
  baseUrl: null,
1481
1665
  dashboardUrl: null,
1482
1666
  anonKey: null,
1483
1667
  anonKeyFile: null,
1484
- email: null,
1485
- password: null,
1486
- deviceName: null,
1487
- linkCode: null,
1488
- noAuth: false,
1489
1668
  noOpen: false,
1490
1669
  yes: false,
1491
1670
  dryRun: false,
1492
1671
  runReport: null,
1493
1672
  noTask: false,
1494
1673
  noIntegrations: false,
1674
+ integrationSelection: null,
1495
1675
  };
1496
1676
 
1497
1677
  for (let i = 0; i < argv.length; i++) {
@@ -1500,20 +1680,24 @@ function parseArgs(argv) {
1500
1680
  else if (a === "--dashboard-url") out.dashboardUrl = argv[++i] || null;
1501
1681
  else if (a === "--anon-key") out.anonKey = argv[++i] || null;
1502
1682
  else if (a === "--anon-key-file") out.anonKeyFile = argv[++i] || null;
1503
- else if (a === "--email") out.email = argv[++i] || null;
1504
- else if (a === "--password") out.password = argv[++i] || null;
1505
- else if (a === "--device-name") out.deviceName = argv[++i] || null;
1506
- else if (a === "--link-code") out.linkCode = argv[++i] || null;
1507
1683
  else if (a === "--run-report") out.runReport = normalizeRunReportFlag(argv[++i]);
1508
1684
  else if (a.startsWith("--run-report=")) out.runReport = normalizeRunReportFlag(a.slice("--run-report=".length));
1685
+ else if (a === "--integrations") out.integrationSelection = parseIntegrationsFlag(argv[++i]);
1686
+ else if (a.startsWith("--integrations=")) out.integrationSelection = parseIntegrationsFlag(a.slice("--integrations=".length));
1509
1687
  else if (a === "--no-task") out.noTask = true;
1510
1688
  else if (a === "--no-integrations") out.noIntegrations = true;
1511
- else if (a === "--no-auth") out.noAuth = true;
1512
1689
  else if (a === "--no-open") out.noOpen = true;
1513
1690
  else if (a === "--yes") out.yes = true;
1514
1691
  else if (a === "--dry-run") out.dryRun = true;
1515
- else throw new Error(`Unknown option: ${a}`);
1692
+ else if (REMOVED_AUTH_FLAGS.includes(a.split("=")[0])) {
1693
+ throw new UsageError(`${a.split("=")[0]} was removed: browser sign-in is gone.`, {
1694
+ code: "FLAG_REMOVED",
1695
+ hint: "Pair this machine with `tokenusage device-login` (or just run `tokenusage init`).",
1696
+ });
1697
+ }
1698
+ else throw unknownOptionError(a, "init");
1516
1699
  }
1700
+ if (out.noIntegrations) out.integrationSelection = new Set();
1517
1701
  return out;
1518
1702
  }
1519
1703
 
@@ -1529,7 +1713,10 @@ function normalizeRunReportFlag(value) {
1529
1713
  const normalized = String(value ?? "").trim().toLowerCase();
1530
1714
  if (normalized === "on" || normalized === "true" || normalized === "1") return true;
1531
1715
  if (normalized === "off" || normalized === "false" || normalized === "0") return false;
1532
- throw new Error(`Unknown value for --run-report: ${value} (use on|off)`);
1716
+ throw new UsageError(`Unknown value for --run-report: ${value} (use on|off)`, {
1717
+ code: "RUN_REPORT_INVALID",
1718
+ hint: "`--run-report=off` disables the per-run telemetry POST; `on` (the default) keeps it.",
1719
+ });
1533
1720
  }
1534
1721
 
1535
1722
  function normalizePlatform(value) {
@@ -2338,6 +2525,9 @@ function containsSequence(haystack, needle) {
2338
2525
 
2339
2526
  module.exports = {
2340
2527
  cmdInit,
2528
+ parseArgs,
2529
+ parseIntegrationsFlag,
2530
+ SELECTABLE_INTEGRATIONS,
2341
2531
  buildNotifyHandler,
2342
2532
  installLocalTrackerApp,
2343
2533
  repairCodexNotifyIntegration,