Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): scrape in the machine timezone, scope the usage cache to the provider config

[._.]/ Adam Eivy01096a6ef753
diff --git a/server/services/claudeCodeUsage.js b/server/services/claudeCodeUsage.js
index 358febdf3b..ced1923bcd 100644
--- a/server/services/claudeCodeUsage.js
+++ b/server/services/claudeCodeUsage.js
@@ -49,7 +49,7 @@ export function zoneFromLocaltimeLink(link) {
// child so headless output matches the interactive TUI. Memoized; best-effort
// (null → inherit the process env unchanged).
let cachedSystemTz;
-function systemTimeZone() {
+export function systemTimeZone() {
if (cachedSystemTz !== undefined) return cachedSystemTz;
cachedSystemTz = existsSync('/etc/localtime') && lstatSync('/etc/localtime').isSymbolicLink()
? zoneFromLocaltimeLink(readlinkSync('/etc/localtime'))
diff --git a/server/services/providerUsage.js b/server/services/providerUsage.js
index 23215bf4c5..76b1793d22 100644
--- a/server/services/providerUsage.js
+++ b/server/services/providerUsage.js
@@ -2,7 +2,7 @@ import { open, readdir, readFile, stat } from 'fs/promises';
import { homedir } from 'os';
import { join } from 'path';
import { getAllProviders } from './providers.js';
-import { getClaudeCodeUsage } from './claudeCodeUsage.js';
+import { getClaudeCodeUsage, systemTimeZone } from './claudeCodeUsage.js';
import { commandBasename, isClaudeCommand } from '../lib/providerModels.js';
import { isGrokCommand } from '../lib/grok.js';
import { scrapeTuiUsage } from '../lib/tuiUsageScrape.js';
@@ -402,14 +402,23 @@ function makeTuiUsageFetcher({ id, binary, slashCommand, label, parse, name, rea
if (!provider) {
return Promise.resolve({ ...base, supported: false, limits: [], note: `${name} usage is read from the local CLI/TUI — enable the ${name} to see quota (the API provider has no queryable usage surface).` });
}
- return cachedScrape(id, { refresh }, async () => {
- // Forward the matched provider's command + envVars. Args are forwarded
- // ONLY for a `tui`-type provider — those are interactive args (a wrapper
- // script path, `--project <id>`, etc.). A `cli`-type provider's args are
- // one-shot/headless flags (`-p`, `exec -`, `--print`, `--prompt-file`)
- // that would break the interactive TUI `/usage` needs, so they're dropped.
- const args = provider.type === 'tui' && Array.isArray(provider.args) ? provider.args : [];
- const text = await scrapeTuiUsage({ command: provider.command || binary, args, slashCommand, env: provider.envVars, readyMarker });
+ const command = provider.command || binary;
+ // Args are forwarded ONLY for a `tui`-type provider — those are interactive
+ // args (a wrapper script path, `--project <id>`, etc.). A `cli`-type
+ // provider's args are one-shot/headless flags (`-p`, `exec -`, `--print`,
+ // `--prompt-file`) that would break the interactive TUI `/usage` needs.
+ const args = provider.type === 'tui' && Array.isArray(provider.args) ? provider.args : [];
+ // The TUI renders reset times in its own timezone; a server under PM2 runs
+ // in UTC, so pass the machine's real zone (as claudeCodeUsage.js does) or
+ // Grok's zoneless "Next reset" is off for non-UTC installs.
+ const tz = systemTimeZone();
+ const env = { ...(provider.envVars || {}), ...(tz ? { TZ: tz } : {}) };
+ // Key the cache by the resolved invocation, not just the family id — so a
+ // provider edit (different account via envVars, tui↔cli switch, arg change)
+ // doesn't serve the previous account's quota from a stale entry.
+ const cacheKey = `${id}:${command}:${provider.type}:${JSON.stringify(env)}:${JSON.stringify(args)}`;
+ return cachedScrape(cacheKey, { refresh }, async () => {
+ const text = await scrapeTuiUsage({ command, args, slashCommand, env, readyMarker });
const { limits } = parse(text);
return limits.length
? { ...base, supported: true, limits, note: `Scraped from the ${name} /usage panel — local, approximate.` }
diff --git a/server/services/providerUsage.test.js b/server/services/providerUsage.test.js
index 009872d0a9..652a632537 100644
--- a/server/services/providerUsage.test.js
+++ b/server/services/providerUsage.test.js
@@ -8,7 +8,8 @@ vi.mock('./providers.js', () => ({
getAllProviders: vi.fn().mockResolvedValue({ activeProvider: null, providers: [] })
}));
vi.mock('./claudeCodeUsage.js', () => ({
- getClaudeCodeUsage: vi.fn()
+ getClaudeCodeUsage: vi.fn(),
+ systemTimeZone: vi.fn(() => null) // default: no machine TZ → env unchanged
}));
// The agy/grok adapters drive a real TUI over a PTY — mock the scrape so these
// tests exercise the parse + fetch wiring without spawning a subprocess.
@@ -22,6 +23,7 @@ import {
} from './providerUsage.js';
import { getAllProviders } from './providers.js';
import { scrapeTuiUsage } from '../lib/tuiUsageScrape.js';
+import { systemTimeZone } from './claudeCodeUsage.js';
// Synthetic Antigravity `/usage` panel — invented values, redacted account, in
// the real rendered shape. The bar percentage is percent REMAINING; a full bar
@@ -312,6 +314,25 @@ describe('TUI usage fetchers (via getProviderQuotas)', () => {
expect(scrapeTuiUsage).toHaveBeenCalledWith(expect.objectContaining({ args: ['--project', 'p1'] }));
});
+ it('passes the machine timezone into the scrape env when resolved', async () => {
+ systemTimeZone.mockReturnValueOnce('America/New_York');
+ getAllProviders.mockResolvedValueOnce({ activeProvider: 'grok', providers: [{ id: 'grok-tui', enabled: true, type: 'tui', command: 'grok' }] });
+ scrapeTuiUsage.mockResolvedValueOnce('Weekly limit: 5% Next reset: Jan 1');
+ await getProviderQuotas();
+ expect(scrapeTuiUsage).toHaveBeenCalledWith(expect.objectContaining({ env: expect.objectContaining({ TZ: 'America/New_York' }) }));
+ });
+
+ it('re-scrapes (does not serve a stale cache entry) when the provider config changes', async () => {
+ getAllProviders.mockResolvedValueOnce({ activeProvider: 'agy', providers: [{ id: 'antigravity-cli', enabled: true, type: 'cli', command: 'agy', envVars: { ACCT: 'a' } }] });
+ scrapeTuiUsage.mockResolvedValueOnce(AGY_PANEL);
+ await getProviderQuotas();
+ // Same family, different account (envVars) → different cache key → fresh scrape.
+ getAllProviders.mockResolvedValueOnce({ activeProvider: 'agy', providers: [{ id: 'antigravity-cli', enabled: true, type: 'cli', command: 'agy', envVars: { ACCT: 'b' } }] });
+ scrapeTuiUsage.mockResolvedValueOnce(AGY_PANEL);
+ await getProviderQuotas();
+ expect(scrapeTuiUsage).toHaveBeenCalledTimes(2);
+ });
+
it('surfaces a supported-but-error card when a CLI provider scrape yields no parseable data', async () => {
getAllProviders.mockResolvedValueOnce({ activeProvider: 'grok', providers: [{ id: 'grok-tui', enabled: true, type: 'tui', command: 'grok' }] });
scrapeTuiUsage.mockResolvedValueOnce('unrecognized banner, no usage line');