tokenusage-cli 0.4.3 → 0.4.4

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.
@@ -264,6 +264,29 @@ function grokEntryAccessToken(entry) {
264
264
  return entry && typeof entry.key === "string" ? entry.key.trim() : "";
265
265
  }
266
266
 
267
+ // auth.x.ai issues JWT access tokens (RFC 9068), so the OIDC `sub` claim is a
268
+ // non-secret, locally derivable account id — the cache-partition key for the
269
+ // limits disk-cache fallback. An opaque token yields null (fail-closed).
270
+ function grokTokenSubject(token) {
271
+ if (typeof token !== "string" || !token.trim()) return null;
272
+ try {
273
+ const parts = token.split(".");
274
+ if (parts.length !== 3) return null;
275
+ const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
276
+ const subject = payload?.sub;
277
+ return typeof subject === "string" && subject.trim() ? subject.trim() : null;
278
+ } catch (_error) {
279
+ return null;
280
+ }
281
+ }
282
+
283
+ // Errors that tell the user to re-login. They are actionable and must surface
284
+ // instead of being masked by the stale disk-cache fallback (opencode-go
285
+ // auth_error precedent).
286
+ function isGrokAuthErrorCode(error) {
287
+ return error?.code === "GROK_AUTH_REQUIRED" || error?.code === "GROK_REAUTH_REQUIRED";
288
+ }
289
+
267
290
  /**
268
291
  * Public xAI OIDC client refresh (auth method "none").
269
292
  * Matches Grok Build CLI's refresh against https://auth.x.ai/oauth2/token.
@@ -775,6 +798,10 @@ async function fetchGrokLimits({ home, env, fetchImpl = fetch, timeoutMs, nowMs
775
798
  return {
776
799
  configured: true,
777
800
  error: error?.message || "Unknown error",
801
+ // A rejected refresh is actionable (re-login) and must not be hidden
802
+ // behind the stale disk-cache fallback. Internal marker — stripped
803
+ // before the payload is served.
804
+ ...(isGrokAuthErrorCode(error) ? { auth_error: true } : {}),
778
805
  };
779
806
  }
780
807
 
@@ -785,13 +812,16 @@ async function fetchGrokLimits({ home, env, fetchImpl = fetch, timeoutMs, nowMs
785
812
  return {
786
813
  configured: true,
787
814
  error: resolved.error?.message || grokAuthError().message,
815
+ ...(isGrokAuthErrorCode(resolved.error) ? { auth_error: true } : {}),
788
816
  };
789
817
  }
790
818
 
791
819
  const startedAtMs = Date.now();
820
+ // Hoisted so the catch below can still derive the cache identity from the
821
+ // token the failing attempt used.
822
+ let accessToken = resolved.accessToken;
792
823
  try {
793
824
  let body;
794
- let accessToken = resolved.accessToken;
795
825
  try {
796
826
  body = await fetchGrokBilling(resolved.accessToken, { fetchImpl, env, timeoutMs });
797
827
  } catch (error) {
@@ -830,11 +860,19 @@ async function fetchGrokLimits({ home, env, fetchImpl = fetch, timeoutMs, nowMs
830
860
  configured: true,
831
861
  error: null,
832
862
  ...limits,
863
+ // Internal cache-partition key (stripped before the payload is served).
864
+ // An opaque token yields null → the cache write/read is refused, so an
865
+ // unattributable login can never pin another account's bars (no token
866
+ // fingerprint fallback: credentials must not enter even a label hash —
867
+ // CodeQL js/insufficient-password-hash precedent).
868
+ cache_identity: grokTokenSubject(accessToken),
833
869
  };
834
870
  } catch (error) {
835
871
  return {
836
872
  configured: true,
837
873
  error: error?.message || "Unknown error",
874
+ ...(isGrokAuthErrorCode(error) ? { auth_error: true } : {}),
875
+ cache_identity: grokTokenSubject(accessToken),
838
876
  };
839
877
  }
840
878
  }
@@ -848,6 +886,7 @@ module.exports = {
848
886
  loadGrokAuthEntry,
849
887
  readGrokAccessToken,
850
888
  isGrokAccessTokenExpired,
889
+ grokTokenSubject,
851
890
  resolveGrokOidcClientId,
852
891
  resolveGrokOidcIssuer,
853
892
  resolveGrokTokenEndpoint,
@@ -44,6 +44,15 @@ function sanitizeBucketForUpload(bucket) {
44
44
  } else {
45
45
  delete sanitized.credits;
46
46
  }
47
+ // project_key (contract v2, 2026-11-03): optional pass-through, string only
48
+ // (anything else is a producer bug — drop the field, not the row); the
49
+ // server applies the basename/control-char cleanup. Trimmed-empty is
50
+ // dropped so unattributed rows keep the old wire shape.
51
+ if (typeof bucket.project_key === "string" && bucket.project_key.trim()) {
52
+ sanitized.project_key = bucket.project_key;
53
+ } else {
54
+ delete sanitized.project_key;
55
+ }
47
56
  return sanitized;
48
57
  }
49
58