shadok-ai 0.19.0 → 0.19.2

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/dist/server.js CHANGED
@@ -49,7 +49,7 @@ import { windowStats } from "./token-stats.js";
49
49
  import { strandedWorktreePlan } from "./worktree-relocate.js";
50
50
  import { detectFork, rootIdOfFile } from "./forktrace.js";
51
51
  import { computePace, paceBlock, WINDOW_SEC } from "./pace.js";
52
- import { getUsage } from "./usage.js";
52
+ import { getUsage, readOAuthToken } from "./usage.js";
53
53
  import { loadChannels, loadGroups, saveGroups, normalizeGroups, ensureMainGroup, mainGroupHeal, upsertChannel, removeChannel, mergeClientChannels, channelWriteAllowed, profilePatch, loadTgGroup, saveTgGroup, saveTgOperators, isHomeChannel, homeAdoptionTarget, resolveSessionTarget, resolveResumeId, resumeTarget, } from "./channels.js";
54
54
  import { deliverWithRetry } from "./reload.js";
55
55
  import { startTelegram, renameTelegramTopic, closeTelegramTopic, probeToken, isStalePreface, announceLoggedOut, resetLoggedOutNotice, attachmentPrompt, pasteFileName, mediaDir, } from "./telegram.js";
@@ -164,6 +164,26 @@ const DEPLOYMENT_MODE = deploymentMode({
164
164
  accountsEnabled: accountsEnabled(GUI_PASSWORD),
165
165
  });
166
166
  console.log(`deployment mode: ${DEPLOYMENT_MODE}`);
167
+ // The long-lived token this instance's AGENTS run on (multi-tenant only): the
168
+ // vault's `MT_CLAUDE_OAUTH_TOKEN`, the very token injected into each agent at
169
+ // spawn. Null when it does not apply, so the usage gauge falls back to the live
170
+ // store / env. Read live (not frozen), so re-pasting the token updates the gauge.
171
+ function instanceLongLivedToken() {
172
+ if (DEPLOYMENT_MODE !== "multi-tenant")
173
+ return null;
174
+ const vault = loadVault();
175
+ if (!longLivedTokenInfo(vault, "multi-tenant").longLived)
176
+ return null;
177
+ const t = vault[MAIN_GROUP_ID]?.[MT_CLAUDE_OAUTH_TOKEN_NAME]?.value;
178
+ return typeof t === "string" && t.trim() ? t.trim() : null;
179
+ }
180
+ // The candidate Bearers for the usage gauge, in preference order: the account
181
+ // the agents actually run on (the vault long-lived token) FIRST, then the CLI's
182
+ // live store / env. `getUsage` takes the first that actually reports a window —
183
+ // so a long-lived `setup-token` (which authenticates agents but reports no usage)
184
+ // falls through to the env token instead of blanking the gauge. Shared by the
185
+ // /usage endpoint and both pace-guard checks so all three measure one account.
186
+ const usageToken = () => [instanceLongLivedToken(), readOAuthToken()];
167
187
  // The server drives channels it manages over its OWN loopback WS (cron fires,
168
188
  // parent notifications, the reload nudge — all through `driveChannel`). Those
169
189
  // deliveries authenticate as the bootstrap admin, but in multi-tenant the
@@ -1835,7 +1855,7 @@ app.get("/live", (req, res) => {
1835
1855
  // Current 5-hour and 7-day subscription usage, each window enriched with how it
1836
1856
  // compares to the time already elapsed (for the quota gauges and the send guard).
1837
1857
  app.get("/usage", async (_req, res) => {
1838
- const u = await getUsage();
1858
+ const u = await getUsage(usageToken);
1839
1859
  const now = Date.now();
1840
1860
  // The pace is derived per request, not per fetch: getUsage() caches for 60 s
1841
1861
  // and a frozen pace would drift away from the clock.
@@ -8767,7 +8787,7 @@ function maybeScheduleRetry(s) {
8767
8787
  // A custom backend spends no subscription quota, so it is never parked.
8768
8788
  const verdict = s.backend === "custom"
8769
8789
  ? paceBlock(null, Date.now())
8770
- : paceBlock(await getUsage().catch(() => null), Date.now());
8790
+ : paceBlock(await getUsage(usageToken).catch(() => null), Date.now());
8771
8791
  // A takeover (or teardown) replaced or cleared our timer while we were
8772
8792
  // fetching: this chain is no longer the live one, so stand down — and
8773
8793
  // leave s.retryTimer alone, it now belongs to whoever replaced us.
@@ -9484,7 +9504,7 @@ wss.on("connection", (ws, req) => {
9484
9504
  // goes through — including the pilotctl thin client.
9485
9505
  // A custom backend spends no subscription quota: nothing to pace.
9486
9506
  if (!msg.force && session.backend !== "custom") {
9487
- const verdict = paceBlock(await getUsage(), Date.now());
9507
+ const verdict = paceBlock(await getUsage(usageToken), Date.now());
9488
9508
  if (verdict.blocked)
9489
9509
  return send({ type: "pace-blocked", reason: verdict.reason, text });
9490
9510
  // The busy test above is now stale: a parked fire() (or another