feat(copilot): plan-usage chip row for the monthly premium-request quota (#662)

Squash of PR #662. The header plan-usage chip gains a Copilot row: the
calendar-month premium-request quota, with the request counts and the
reset in the tooltip, read every 10 minutes from GitHub's
copilot_internal/user endpoint with the Copilot CLI's own sign-in token.
It rides showPlanUsageLimits and is on by default while the CLI is
installed and signed in; any failure leaves the chip without the row.

Merge-time fixes by the maintainer: the gates (setting, then token, then
the CLI probe) live in readCopilotPlanUsageIfEnabled() so a host with no
Copilot sign-in never runs the login-shell probe each tick, with a test
that Plan Usage Limits off means no request; the X-GitHub-Api-Version
header the Copilot CLI itself does not send is dropped; and CLAUDE.md,
architecture-invariants, README and copilot-integration describe the read.
The hooks-config import in server.ts was merged with #666's.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
DevvynandClaude Opus 5.5 authored and Codeman maintainer committed 2026-10-22 19:32:17 +02:00
1 parent f80f107445
commit 34cf798b10
22 files changed
+642 -26

No files matched your search

+44 -1
View File
@@ -100,6 +100,7 @@ import {
applyWorkspaceHooks,
pruneAgentSessionPreambles,
readKeepWarmSettingsOrThrow,
readPlanUsageTelemetryEnabled,
removeAgentSessionPreamble,
} from '../hooks-config.js';
import { PushSubscriptionStore } from '../push-store.js';
@@ -170,9 +171,15 @@ import { MAX_PASTE_IMAGE_BYTES } from '../config/buffer-limits.js';
import { resolveTerminalHistoryConfig } from '../config/terminal-history.js';
import { SseEvent } from './sse-events.js';
import { MAX_ROUTE_PARAM_LENGTH } from './route-helpers.js';
import { getLatestPlanUsage, setLatestCodexPlanUsage } from './plan-usage-latest.js';
import { getLatestPlanUsage, setLatestCodexPlanUsage, setLatestCopilotPlanUsage } from './plan-usage-latest.js';
import { telemetrySignature } from '../usage-telemetry.js';
import { readCodexPlanUsage, resolveCodexBinaryPath } from '../utils/codex-cli-resolver.js';
import { isCopilotAvailable } from '../utils/copilot-cli-resolver.js';
import {
readCopilotPlanUsage,
readCopilotPlanUsageIfEnabled,
resolveCopilotToken,
} from '../utils/copilot-plan-usage.js';
import type { ScheduledRun } from './ports/index.js';
import {
registerAuthMiddleware,
@@ -237,6 +244,8 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
// while capping growth of `sseClientsById` and blocking pathological inputs.
const SSE_CLIENT_ID_RE = /^[A-Za-z0-9_-]{8,64}$/;
const CODEX_USAGE_POLL_INTERVAL_MS = 5 * 60_000;
// Copilot's quota is read over the network from GitHub, and a monthly figure moves slowly.
const COPILOT_USAGE_POLL_INTERVAL_MS = 10 * 60_000;
const CUSTOM_MODEL_REDISCOVER_INTERVAL_MS = 5 * 60_000;
// Much shorter than the model-LIST refresh above on purpose: this catches an actual
// eviction (a session's model no longer loaded, silently swapped out by another
@@ -406,6 +415,8 @@ export class WebServer extends EventEmitter {
private lastRecordedTokens: Map<string, { input: number; output: number }> = new Map();
private codexUsageRefreshInFlight = false;
private lastCodexUsageSignature: string | null = null;
private copilotUsageRefreshInFlight = false;
private lastCopilotUsageSignature: string | null = null;
// Server startup time for respawn grace period calculation
private readonly serverStartTime: number = Date.now();
// Pending respawn start timers (for cleanup on shutdown)
@@ -2958,6 +2969,34 @@ export class WebServer extends EventEmitter {
}
}
/**
* Copilot's monthly premium-request quota for the shared plan-usage chip. Host-scoped and read-only
* like the Codex poll, and additionally gated by `readCopilotPlanUsageIfEnabled()`: only while
* plan-usage display is on (so turning the chip off also stops the outbound request), only with a
* sign-in token and only with the Copilot CLI installed; nothing is sent to GitHub otherwise.
*/
private async refreshCopilotPlanUsage(): Promise<void> {
if (this.copilotUsageRefreshInFlight) return;
this.copilotUsageRefreshInFlight = true;
try {
// A null clears the row, so switching the chip off also takes the last quota off the page.
const usage = await readCopilotPlanUsageIfEnabled({
planUsageEnabled: readPlanUsageTelemetryEnabled,
resolveToken: () => resolveCopilotToken(),
copilotInstalled: isCopilotAvailable,
read: (token) => readCopilotPlanUsage({ token }),
});
const signature = usage ? telemetrySignature(usage) : '';
if (signature === this.lastCopilotUsageSignature) return;
this.lastCopilotUsageSignature = signature;
const snapshot = setLatestCopilotPlanUsage(usage);
this.cachedLightState = null;
this.broadcast(SseEvent.SessionStatusTelemetry, snapshot);
} finally {
this.copilotUsageRefreshInFlight = false;
}
}
async start(): Promise<void> {
// Multi-user first boot: create the initial admin from CODEMAN_USERNAME/PASSWORD
// if there are no users yet, else refuse to start (there would be no way in).
@@ -3132,6 +3171,10 @@ export class WebServer extends EventEmitter {
this.cleanup.setInterval(() => void this.refreshCodexPlanUsage(), CODEX_USAGE_POLL_INTERVAL_MS, {
description: 'Codex plan-usage refresh',
});
void this.refreshCopilotPlanUsage();
this.cleanup.setInterval(() => void this.refreshCopilotPlanUsage(), COPILOT_USAGE_POLL_INTERVAL_MS, {
description: 'Copilot plan-usage refresh',
});
}
// Custom Model Endpoint Profiles (docs/custom-model-endpoints-plan.md): keeps