shadok-ai 0.18.2 → 0.19.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.
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, chooseUsageToken, 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,24 @@ 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 token the usage gauge authenticates with: the account the agents actually
181
+ // run on (the vault long-lived token) first, else the CLI's live store / env.
182
+ // Shared by the /usage endpoint and both pace-guard checks so all three measure
183
+ // the same account.
184
+ const usageToken = () => chooseUsageToken(instanceLongLivedToken(), readOAuthToken);
167
185
  // The server drives channels it manages over its OWN loopback WS (cron fires,
168
186
  // parent notifications, the reload nudge — all through `driveChannel`). Those
169
187
  // deliveries authenticate as the bootstrap admin, but in multi-tenant the
@@ -1835,7 +1853,7 @@ app.get("/live", (req, res) => {
1835
1853
  // Current 5-hour and 7-day subscription usage, each window enriched with how it
1836
1854
  // compares to the time already elapsed (for the quota gauges and the send guard).
1837
1855
  app.get("/usage", async (_req, res) => {
1838
- const u = await getUsage();
1856
+ const u = await getUsage(usageToken);
1839
1857
  const now = Date.now();
1840
1858
  // The pace is derived per request, not per fetch: getUsage() caches for 60 s
1841
1859
  // and a frozen pace would drift away from the clock.
@@ -8767,7 +8785,7 @@ function maybeScheduleRetry(s) {
8767
8785
  // A custom backend spends no subscription quota, so it is never parked.
8768
8786
  const verdict = s.backend === "custom"
8769
8787
  ? paceBlock(null, Date.now())
8770
- : paceBlock(await getUsage().catch(() => null), Date.now());
8788
+ : paceBlock(await getUsage(usageToken).catch(() => null), Date.now());
8771
8789
  // A takeover (or teardown) replaced or cleared our timer while we were
8772
8790
  // fetching: this chain is no longer the live one, so stand down — and
8773
8791
  // leave s.retryTimer alone, it now belongs to whoever replaced us.
@@ -9484,7 +9502,7 @@ wss.on("connection", (ws, req) => {
9484
9502
  // goes through — including the pilotctl thin client.
9485
9503
  // A custom backend spends no subscription quota: nothing to pace.
9486
9504
  if (!msg.force && session.backend !== "custom") {
9487
- const verdict = paceBlock(await getUsage(), Date.now());
9505
+ const verdict = paceBlock(await getUsage(usageToken), Date.now());
9488
9506
  if (verdict.blocked)
9489
9507
  return send({ type: "pace-blocked", reason: verdict.reason, text });
9490
9508
  // The busy test above is now stale: a parked fire() (or another