mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-04 10:16:34 +00:00
feat(providers): instrument copilot, cursor, and glmt across daemons and executors
Provider modules now emit structured stage tags around daemon spawn / ready / stop, executor invocations, and upstream dispatch. glmt-transformer gets cleanup-stage error conversion; legacy glmt-proxy adds minimal listen + retry instrumentation (full per-request stages live in proxy-server since glmt-proxy is compat-only). Refs #1141, #1138
This commit is contained in:
@@ -14,6 +14,9 @@ import {
|
||||
isCopilotApiInstalled as checkInstalled,
|
||||
getCopilotApiBinPath,
|
||||
} from './copilot-package-manager';
|
||||
import { createLogger } from '../services/logging';
|
||||
|
||||
const logger = createLogger('copilot:auth');
|
||||
|
||||
/**
|
||||
* Get the path to copilot-api's GitHub token file.
|
||||
@@ -108,6 +111,10 @@ export async function getCopilotDebugInfo(): Promise<CopilotDebugInfo | null> {
|
||||
export async function checkAuthStatus(): Promise<CopilotAuthStatus> {
|
||||
// Fast path: check if token file exists (instant, no subprocess)
|
||||
if (hasTokenFile()) {
|
||||
logger.stage('auth', 'copilot.auth.token_present', 'Copilot token file present', {
|
||||
provider: 'copilot',
|
||||
method: 'token_file',
|
||||
});
|
||||
return { authenticated: true };
|
||||
}
|
||||
|
||||
@@ -116,9 +123,20 @@ export async function checkAuthStatus(): Promise<CopilotAuthStatus> {
|
||||
const debugInfo = await getCopilotDebugInfo();
|
||||
|
||||
if (debugInfo?.authenticated) {
|
||||
logger.stage('auth', 'copilot.auth.debug_ok', 'Copilot reports authenticated via debug', {
|
||||
provider: 'copilot',
|
||||
method: 'debug',
|
||||
});
|
||||
return { authenticated: true };
|
||||
}
|
||||
|
||||
logger.stage(
|
||||
'auth',
|
||||
'copilot.auth.unauthenticated',
|
||||
'Copilot is not authenticated',
|
||||
{ provider: 'copilot' },
|
||||
{ level: 'warn' }
|
||||
);
|
||||
return {
|
||||
authenticated: false,
|
||||
};
|
||||
@@ -149,6 +167,11 @@ export function startAuthFlow(): Promise<AuthFlowResult> {
|
||||
console.log('[i] Starting GitHub authentication for Copilot...');
|
||||
console.log('');
|
||||
|
||||
const startedAt = Date.now();
|
||||
logger.stage('auth', 'copilot.auth.start', 'Starting Copilot OAuth device flow', {
|
||||
provider: 'copilot',
|
||||
});
|
||||
|
||||
const proc = spawn(binPath, ['auth'], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
shell: process.platform === 'win32',
|
||||
@@ -185,8 +208,22 @@ export function startAuthFlow(): Promise<AuthFlowResult> {
|
||||
|
||||
proc.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
logger.stage(
|
||||
'auth',
|
||||
'copilot.auth.complete',
|
||||
'Copilot OAuth device flow completed',
|
||||
{ provider: 'copilot', exitCode: code },
|
||||
{ latencyMs: Date.now() - startedAt }
|
||||
);
|
||||
resolve({ success: true, deviceCode, verificationUrl });
|
||||
} else {
|
||||
logger.stage(
|
||||
'cleanup',
|
||||
'copilot.auth.failed',
|
||||
'Copilot OAuth device flow failed',
|
||||
{ provider: 'copilot', exitCode: code },
|
||||
{ level: 'error', latencyMs: Date.now() - startedAt }
|
||||
);
|
||||
resolve({
|
||||
success: false,
|
||||
error: `Authentication failed with exit code ${code}`,
|
||||
@@ -197,6 +234,17 @@ export function startAuthFlow(): Promise<AuthFlowResult> {
|
||||
});
|
||||
|
||||
proc.on('error', (err) => {
|
||||
logger.stage(
|
||||
'cleanup',
|
||||
'copilot.auth.error',
|
||||
'Failed to start Copilot OAuth process',
|
||||
{ provider: 'copilot' },
|
||||
{
|
||||
level: 'error',
|
||||
latencyMs: Date.now() - startedAt,
|
||||
error: { name: err.name, message: err.message },
|
||||
}
|
||||
);
|
||||
resolve({
|
||||
success: false,
|
||||
error: `Failed to start auth: ${err.message}`,
|
||||
|
||||
@@ -14,6 +14,9 @@ import { CopilotConfig, DEFAULT_COPILOT_CONFIG } from '../config/unified-config-
|
||||
import { loadOrCreateUnifiedConfig } from '../config/unified-config-loader';
|
||||
import { getCopilotDir, getCopilotApiBinPath } from './copilot-package-manager';
|
||||
import { verifyProcessOwnership } from '../cursor/daemon-process-ownership';
|
||||
import { createLogger } from '../services/logging';
|
||||
|
||||
const logger = createLogger('copilot:daemon');
|
||||
|
||||
const DAEMON_HEALTH_MARKER = 'server running';
|
||||
const MIN_PORT = 1;
|
||||
@@ -166,6 +169,10 @@ export async function startDaemon(
|
||||
|
||||
// Check if already running
|
||||
if (await isDaemonRunning(config.port)) {
|
||||
logger.stage('dispatch', 'copilot.daemon.already_running', 'Copilot daemon already running', {
|
||||
provider: 'copilot',
|
||||
port: config.port,
|
||||
});
|
||||
return { success: true, pid: getPidFromFile() ?? undefined };
|
||||
}
|
||||
|
||||
@@ -185,6 +192,13 @@ export async function startDaemon(
|
||||
}
|
||||
}
|
||||
|
||||
const startedAt = Date.now();
|
||||
logger.stage('dispatch', 'copilot.daemon.spawn', 'Spawning Copilot daemon', {
|
||||
provider: 'copilot',
|
||||
port: config.port,
|
||||
accountType: config.account_type,
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let proc: ChildProcess;
|
||||
let resolved = false;
|
||||
@@ -198,6 +212,27 @@ export async function startDaemon(
|
||||
}
|
||||
if (!result.success) {
|
||||
removePidFile();
|
||||
logger.stage(
|
||||
'cleanup',
|
||||
'copilot.daemon.start_failed',
|
||||
'Copilot daemon failed to start',
|
||||
{ provider: 'copilot', port: config.port },
|
||||
{
|
||||
level: 'error',
|
||||
latencyMs: Date.now() - startedAt,
|
||||
error: result.error
|
||||
? { name: 'CopilotDaemonStartError', message: result.error }
|
||||
: undefined,
|
||||
}
|
||||
);
|
||||
} else {
|
||||
logger.stage(
|
||||
'upstream',
|
||||
'copilot.daemon.ready',
|
||||
'Copilot daemon is ready',
|
||||
{ provider: 'copilot', port: config.port, pid: result.pid },
|
||||
{ latencyMs: Date.now() - startedAt }
|
||||
);
|
||||
}
|
||||
resolve(result);
|
||||
};
|
||||
@@ -326,6 +361,10 @@ export async function stopDaemon(): Promise<{ success: boolean; error?: string }
|
||||
}
|
||||
|
||||
// Send SIGTERM to the process
|
||||
logger.stage('cleanup', 'copilot.daemon.stop', 'Sending SIGTERM to Copilot daemon', {
|
||||
provider: 'copilot',
|
||||
pid,
|
||||
});
|
||||
process.kill(pid, 'SIGTERM');
|
||||
|
||||
// Wait for process to exit (up to 5 seconds)
|
||||
|
||||
@@ -35,6 +35,9 @@ import {
|
||||
resolveImageAnalysisRuntimeStatus,
|
||||
} from '../utils/hooks';
|
||||
import { stripClaudeCodeEnv } from '../utils/shell-executor';
|
||||
import { createLogger } from '../services/logging';
|
||||
|
||||
const logger = createLogger('copilot:executor');
|
||||
|
||||
interface CopilotImageAnalysisDeps {
|
||||
ensureCliproxyService: typeof ensureCliproxyService;
|
||||
@@ -185,6 +188,12 @@ export async function executeCopilotProfile(
|
||||
): Promise<number> {
|
||||
const { config: normalizedConfig, warnings } = normalizeCopilotConfigWithWarnings(config);
|
||||
|
||||
logger.stage('intake', 'copilot.execute.start', 'Starting Copilot profile execution', {
|
||||
provider: 'copilot',
|
||||
model: normalizedConfig.model,
|
||||
port: normalizedConfig.port,
|
||||
});
|
||||
|
||||
if (warnings.length > 0) {
|
||||
warnings.forEach(({ message }) => console.log(warn(message)));
|
||||
console.log(
|
||||
@@ -216,6 +225,10 @@ export async function executeCopilotProfile(
|
||||
|
||||
// Check authentication
|
||||
const authStatus = await checkAuthStatus();
|
||||
logger.stage('auth', 'copilot.execute.auth_check', 'Checked Copilot auth status', {
|
||||
provider: 'copilot',
|
||||
authenticated: authStatus.authenticated,
|
||||
});
|
||||
if (!authStatus.authenticated) {
|
||||
console.error(fail('Not authenticated with GitHub.'));
|
||||
console.error('');
|
||||
@@ -289,6 +302,7 @@ export async function executeCopilotProfile(
|
||||
console.log('');
|
||||
|
||||
// Spawn Claude CLI
|
||||
const spawnStartedAt = Date.now();
|
||||
return new Promise((resolve) => {
|
||||
const imageAnalysisArgs = imageAnalysisMcpReady
|
||||
? appendThirdPartyImageAnalysisToolArgs(claudeArgs)
|
||||
@@ -302,6 +316,11 @@ export async function executeCopilotProfile(
|
||||
claudeConfigDir,
|
||||
});
|
||||
|
||||
logger.stage('dispatch', 'copilot.execute.spawn', 'Spawning Claude via Copilot proxy', {
|
||||
provider: 'copilot',
|
||||
argCount: launchArgs.length,
|
||||
});
|
||||
|
||||
const proc = spawn(claudeCliPath, launchArgs, {
|
||||
stdio: 'inherit',
|
||||
env: { ...env, ...traceEnv },
|
||||
@@ -309,10 +328,28 @@ export async function executeCopilotProfile(
|
||||
});
|
||||
|
||||
proc.on('close', (code) => {
|
||||
logger.stage(
|
||||
'respond',
|
||||
'copilot.execute.exit',
|
||||
'Claude process exited (Copilot)',
|
||||
{ provider: 'copilot', exitCode: code },
|
||||
{ latencyMs: Date.now() - spawnStartedAt }
|
||||
);
|
||||
resolve(code ?? 0);
|
||||
});
|
||||
|
||||
proc.on('error', (err) => {
|
||||
logger.stage(
|
||||
'cleanup',
|
||||
'copilot.execute.error',
|
||||
'Failed to spawn Claude (Copilot)',
|
||||
{ provider: 'copilot' },
|
||||
{
|
||||
level: 'error',
|
||||
latencyMs: Date.now() - spawnStartedAt,
|
||||
error: { name: err.name, message: err.message },
|
||||
}
|
||||
);
|
||||
console.error(fail(`Failed to start Claude: ${err.message}`));
|
||||
resolve(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user