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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): idle-gate startup, parse Grok monthly windows, forward TUI provider args, fix percent invariant
[._.]/ Adam Eivy68bafaf948fa
diff --git a/server/lib/tuiUsageScrape.js b/server/lib/tuiUsageScrape.jsindex 8bdeb7b31e..404825cd2c 100644--- a/server/lib/tuiUsageScrape.js+++ b/server/lib/tuiUsageScrape.js@@ -44,7 +44,7 @@ const PTY_ROWS = 50;// Timing defaults (ms). The TUI needs a moment to sign in and paint its banner// before it accepts input; after the command we wait for the rendered panel to// stop changing (output-idle) rather than a fixed sleep, bounded by a hard cap.-const DEFAULT_READY_MS = 6000; // banner + sign-in settle before first keystroke+const DEFAULT_READY_CAP_MS = 15000; // cap on waiting for the banner/gate to paint + go idleconst DEFAULT_PRIMER_CAP_MS = 3000; // cap on waiting for the trust/menu dismissal to repaintconst DEFAULT_IDLE_MS = 2500; // "screen finished rendering" = no output for this longconst DEFAULT_RENDER_CAP_MS = 12000; // hard cap on waiting for the panel after the command@@ -78,7 +78,7 @@ export async function scrapeTuiUsage({env: extraEnv = {},readyMarker = null,sandboxDir = USAGE_SANDBOX_DIR,- readyMs = DEFAULT_READY_MS,+ readyCapMs = DEFAULT_READY_CAP_MS,primerCapMs = DEFAULT_PRIMER_CAP_MS,idleMs = DEFAULT_IDLE_MS,renderCapMs = DEFAULT_RENDER_CAP_MS,@@ -147,7 +147,13 @@ export async function scrapeTuiUsage({};try {- await Promise.race([sleep(readyMs), hardStop]);+ // Wait for the TUI's initial paint (banner, sign-in spinner, and the+ // trust/menu gate) to finish and go input-idle before touching the keyboard.+ // A fixed sleep would fire the primer Enter before a slow startup's gate had+ // rendered — the keystroke would be dropped or land mid-animation. Gating on+ // output-idle instead means we press Enter exactly when the gate is up and+ // waiting for input, however long sign-in took.+ await waitForIdle(readyCapMs, { requireOutput: true });// Dismiss the first-run gate: agy shows "Do you trust this folder?" (default// "Yes, I trust"), grok shows a start menu (default highlighted item). Onediff --git a/server/services/providerUsage.js b/server/services/providerUsage.jsindex 9fa92eb2d8..23215bf4c5 100644--- a/server/services/providerUsage.js+++ b/server/services/providerUsage.js@@ -305,13 +305,17 @@ export function parseAgyUsage(text, { now = Date.now() } = {}) {if (remaining === null) continue; // not a real limit blockconst windowLabel = agyWindowLabel(w[1]);const key = `${slug(group)}-${slug(windowLabel)}`;+ // Round ONE side and derive the other so used + remaining always == 100.+ // Independently rounding both (e.g. 98.50% remaining → 99 left + 2 used)+ // would show a card totalling 101%.+ const percentUsed = Math.min(100, Math.max(0, Math.round(100 - remaining)));byKey.set(key, {key,label: `${group} · ${windowLabel}`,scope: slug(windowLabel),model: group,- percentUsed: Math.max(0, Math.round(100 - remaining)),- percentRemaining: Math.round(remaining),+ percentUsed,+ percentRemaining: 100 - percentUsed,resetsAt: quotaAvailable ? null : resetsAt,timezone: null,});@@ -319,51 +323,64 @@ export function parseAgyUsage(text, { now = Date.now() } = {}) {return { limits: [...byKey.values()], groups: groups.size };}+// Grok's `/usage show` panel reports usage as `<Window> limit: N%` (percent+// USED). Its binary strings expose both `Weekly limit` and `Monthly limit`, so+// different plans surface different windows — parse whichever appear.+const GROK_WINDOWS = { weekly: { label: 'Weekly', scope: 'week' }, monthly: { label: 'Monthly', scope: 'month' } };+/**- * Parse the Grok Build `/usage show` panel text. Grok reports a single line- * `Weekly limit: N%` (percent USED) plus `Next reset: <date>`. Exported for- * tests. Pure.+ * Parse the Grok Build `/usage show` panel text. Emits one row per usage window+ * present (`Weekly limit: N%` and/or `Monthly limit: N%`, percent USED) plus a+ * shared `Next reset: <date>`. Exported for tests. Pure.** @returns {{ limits: Array }}*/export function parseGrokUsage(text) {const str = String(text || '');- // Append-only terminal stream: a repaint leaves an older copy of the line- // ahead of the newer one, so take the LAST match (the freshest frame), not- // the first (see parseAgyUsage for the same repaint hazard).- const lastMatch = (re) => { const all = [...str.matchAll(re)]; return all.length ? all[all.length - 1] : null; };- const wl = lastMatch(/weekly limit:\s*([\d.]+)\s*%/gi);- if (!wl) return { limits: [] };- const percentUsed = Math.round(parseFloat(wl[1]));- const nr = lastMatch(/next reset:\s*([A-Za-z0-9 ,:]+?)(?:\s{2,}|$)/gi);- return {- limits: [{- key: 'weekly',- label: 'Weekly',- scope: 'week',- model: null,- percentUsed,- percentRemaining: Math.max(0, 100 - percentUsed),- // Grok gives a local-time date string without a year/zone; pass it- // through verbatim (the UI renders non-ISO reset strings as-is).- resetsAt: nr ? nr[1].trim() : null,- timezone: null,- }],- };+ // Append-only terminal stream: a repaint leaves an older copy of a line ahead+ // of the newer one, so keep the LAST value seen per window (freshest frame) —+ // same repaint hazard as parseAgyUsage.+ const byWindow = new Map();+ for (const m of str.matchAll(/(weekly|monthly) limit:\s*([\d.]+)\s*%/gi)) {+ byWindow.set(m[1].toLowerCase(), Math.round(parseFloat(m[2])));+ }+ if (!byWindow.size) return { limits: [] };+ const resets = [...str.matchAll(/next reset:\s*([A-Za-z0-9 ,:]+?)(?:\s{2,}|$)/gi)];+ const resetsAt = resets.length ? resets[resets.length - 1][1].trim() : null;+ const limits = [...byWindow].map(([window, percentUsed]) => ({+ key: window,+ label: GROK_WINDOWS[window].label,+ scope: GROK_WINDOWS[window].scope,+ model: null,+ percentUsed,+ percentRemaining: Math.max(0, 100 - percentUsed),+ // Grok gives a local-time date string without a year/zone; pass it through+ // verbatim (the UI renders non-ISO reset strings as-is).+ resetsAt,+ timezone: null,+ }));+ return { limits };}/**- * Pick the enabled provider to actually drive for a TUI scrape: a `cli`/`tui`- * process provider (preferring one whose command basename is the expected- * `binary`), never an `api` provider. The `/usage` panel is a property of the- * local CLI/TUI, not the OpenAI-compatible API endpoint — an install that+ * Pick the enabled provider to actually drive for a TUI scrape: a `tui` or `cli`+ * process provider, never an `api` provider. The `/usage` panel is a property of+ * the local CLI/TUI, not the OpenAI-compatible API endpoint — an install that* enables only the API provider (e.g. the built-in `grok` API, matched by id)* has no scrapeable surface and no `command` to spawn, so return null and let* the fetcher report it unsupported rather than launching an unrelated binary.+ *+ * A `tui`-type provider is preferred over a `cli`-type one (and within a type,+ * the one whose command basename is `binary`): a TUI provider is configured for+ * interactive use, so its `args` are safe to forward, whereas a CLI provider's+ * args are one-shot/headless flags that would break the interactive `/usage`.*/function pickScrapeProvider(providers, binary) {const cliTui = (providers || []).filter((p) => p?.type === 'cli' || p?.type === 'tui');- return cliTui.find((p) => commandBasename(p.command) === binary) || cliTui[0] || null;+ const byBinary = (list) => list.find((p) => commandBasename(p.command) === binary) || list[0];+ const tui = cliTui.filter((p) => p.type === 'tui');+ const cli = cliTui.filter((p) => p.type === 'cli');+ return byBinary(tui) || byBinary(cli) || null;}/**@@ -386,11 +403,13 @@ function makeTuiUsageFetcher({ id, binary, slashCommand, label, parse, name, reareturn 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 () => {- // Only `command` + `envVars` are forwarded — deliberately NOT the- // provider's `args`. Stored args are headless-mode flags (`-p`, `exec -`,- // `--print`, `--prompt-file`) that would break the interactive TUI we need- // for `/usage`; the TUI launches bare.- const text = await scrapeTuiUsage({ command: provider.command || binary, slashCommand, env: provider.envVars, readyMarker });+ // 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 { limits } = parse(text);return limits.length? { ...base, supported: true, limits, note: `Scraped from the ${name} /usage panel — local, approximate.` }@@ -447,7 +466,7 @@ const FAMILIES = [id: 'grok',label: 'Grok',matches: (p) => isGrokCommand(p.command) || /grok/i.test(p.id || ''),- fetch: makeTuiUsageFetcher({ id: 'grok', binary: 'grok', slashCommand: '/usage show', label: 'Grok', parse: parseGrokUsage, name: 'Grok Build CLI', readyMarker: /Weekly limit:/i })+ fetch: makeTuiUsageFetcher({ id: 'grok', binary: 'grok', slashCommand: '/usage show', label: 'Grok', parse: parseGrokUsage, name: 'Grok Build CLI', readyMarker: /(Weekly|Monthly) limit:/i })}];diff --git a/server/services/providerUsage.test.js b/server/services/providerUsage.test.jsindex 8523cb284d..009872d0a9 100644--- a/server/services/providerUsage.test.js+++ b/server/services/providerUsage.test.js@@ -199,6 +199,14 @@ describe('parseAgyUsage', () => {expect(gem5h).toMatchObject({ label: 'Gemini · 5-hour', percentUsed: 20, percentRemaining: 80 });});+ it('keeps used + remaining summing to 100 on half-percent values', () => {+ // 98.50% remaining rounds to used 2 / left 99 = 101% if rounded independently.+ const panel = AGY_PANEL.replace('98.99%', '98.50%').replace('99% remaining', '98% remaining');+ const gemWeek = parseAgyUsage(panel, { now: NOW }).limits.find((l) => l.key === 'gemini-weekly');+ expect(gemWeek.percentUsed + gemWeek.percentRemaining).toBe(100);+ expect(gemWeek).toMatchObject({ percentUsed: 2, percentRemaining: 98 });+ });+it('preserves acronyms in group labels and null-resets a fully-available window', () => {const { limits } = parseAgyUsage(AGY_PANEL, { now: NOW });const cgWeek = limits.find((l) => l.key === 'claude-gpt-weekly');@@ -240,6 +248,13 @@ describe('parseGrokUsage', () => {const { limits } = parseGrokUsage(repainted);expect(limits[0]).toMatchObject({ percentUsed: 73, percentRemaining: 27, resetsAt: 'August 1, 06:07' });});++ it('parses a Monthly window and both windows when present', () => {+ expect(parseGrokUsage('Monthly limit: 30% Next reset: Sep 1').limits[0])+ .toMatchObject({ key: 'monthly', label: 'Monthly', scope: 'month', percentUsed: 30, percentRemaining: 70 });+ const both = parseGrokUsage('Weekly limit: 12% Monthly limit: 45% Next reset: Sep 1').limits;+ expect(both.map((l) => l.key).sort()).toEqual(['monthly', 'weekly']);+ });});describe('TUI usage fetchers (via getProviderQuotas)', () => {@@ -267,6 +282,36 @@ describe('TUI usage fetchers (via getProviderQuotas)', () => {expect(scrapeTuiUsage).toHaveBeenCalledWith(expect.objectContaining({ command: '/opt/tools/agy', env: { AGY_TOKEN: 'x' } }));});+ it('forwards a TUI provider\'s interactive args but drops a CLI provider\'s headless args', async () => {+ // TUI provider: args are interactive → forwarded.+ getAllProviders.mockResolvedValueOnce({ activeProvider: 'grok', providers: [+ { id: 'grok-tui', enabled: true, type: 'tui', command: 'grok', args: ['--project', 'p1'] },+ ] });+ scrapeTuiUsage.mockResolvedValueOnce('Weekly limit: 5% Next reset: Jan 1');+ await getProviderQuotas();+ expect(scrapeTuiUsage).toHaveBeenCalledWith(expect.objectContaining({ command: 'grok', args: ['--project', 'p1'] }));++ __resetUsageScrapeCache();+ scrapeTuiUsage.mockReset();+ // CLI provider: args are headless one-shot flags → dropped.+ getAllProviders.mockResolvedValueOnce({ activeProvider: 'grok', providers: [+ { id: 'grok-cli', enabled: true, type: 'cli', command: 'grok', args: ['--prompt-file', '/dev/stdin'] },+ ] });+ scrapeTuiUsage.mockResolvedValueOnce('Weekly limit: 5% Next reset: Jan 1');+ await getProviderQuotas();+ expect(scrapeTuiUsage).toHaveBeenCalledWith(expect.objectContaining({ command: 'grok', args: [] }));+ });++ it('prefers a TUI provider over a CLI provider when both are enabled', async () => {+ getAllProviders.mockResolvedValueOnce({ activeProvider: 'grok', providers: [+ { id: 'grok-cli', enabled: true, type: 'cli', command: 'grok', args: ['-p'] },+ { id: 'grok-tui', enabled: true, type: 'tui', command: 'grok', args: ['--project', 'p1'] },+ ] });+ scrapeTuiUsage.mockResolvedValueOnce('Weekly limit: 5% Next reset: Jan 1');+ await getProviderQuotas();+ expect(scrapeTuiUsage).toHaveBeenCalledWith(expect.objectContaining({ args: ['--project', 'p1'] }));+ });+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');