axios+telemetry cleanup

This commit is contained in:
2026-04-02 15:19:11 +03:00
parent a3cbca1e11
commit 7e1eac8002
100 changed files with 3048 additions and 4491 deletions

View File

@@ -1,10 +1,10 @@
import axios, { type AxiosError } from 'axios'
import type { UUID } from 'crypto'
import { getOauthConfig } from '../../constants/oauth.js'
import type { Entry, TranscriptMessage } from '../../types/logs.js'
import { logForDebugging } from '../../utils/debug.js'
import { logForDiagnosticsNoPII } from '../../utils/diagLogs.js'
import { isEnvTruthy } from '../../utils/envUtils.js'
import { isHttpError, nativeRequest } from '../../utils/http.js'
import { logError } from '../../utils/log.js'
import { sequential } from '../../utils/sequential.js'
import { getSessionIngressAuthToken } from '../../utils/sessionIngressAuth.js'
@@ -74,9 +74,10 @@ async function appendSessionLogImpl(
requestHeaders['Last-Uuid'] = lastUuid
}
const response = await axios.put(url, entry, {
const response = await nativeRequest(url, {
method: 'PUT',
body: entry,
headers: requestHeaders,
validateStatus: status => status < 500,
})
if (response.status === 200 || response.status === 201) {
@@ -118,11 +119,11 @@ async function appendSessionLogImpl(
if (adoptedUuid) {
lastUuidMap.set(sessionId, adoptedUuid)
logForDebugging(
`Session 409: re-fetched ${logs!.length} entries, adopting lastUuid=${adoptedUuid}, retrying entry ${entry.uuid}`,
`Session 409: re-fetched ${(logs as any)!.length} entries, adopting lastUuid=${adoptedUuid}, retrying entry ${entry.uuid}`,
)
} else {
// Can't determine server state — give up
const errorData = response.data as SessionIngressError
const errorData = response.data as any as SessionIngressError
const errorMessage =
errorData.error?.message || 'Concurrent modification detected'
logError(
@@ -148,21 +149,22 @@ async function appendSessionLogImpl(
}
// Other 4xx (429, etc.) - retryable
logForDebugging(
`Failed to persist session log: ${response.status} ${response.statusText}`,
)
logForDebugging(`Failed to persist session log: ${response.status}`)
logForDiagnosticsNoPII('error', 'session_persist_fail_status', {
status: response.status,
attempt,
})
} catch (error) {
// Network errors, 5xx - retryable
const axiosError = error as AxiosError<SessionIngressError>
logError(new Error(`Error persisting session log: ${axiosError.message}`))
logForDiagnosticsNoPII('error', 'session_persist_fail_status', {
status: axiosError.status,
attempt,
})
if (isHttpError(error)) {
logError(new Error(`Error persisting session log: ${error.message}`))
logForDiagnosticsNoPII('error', 'session_persist_fail_status', {
status: error.status,
attempt,
})
} else {
logError(error)
}
}
if (attempt === MAX_RETRIES) {
@@ -318,15 +320,19 @@ export async function getTeleportEvents(
let response
try {
response = await axios.get<TeleportEventsResponse>(baseUrl, {
const queryParams = new URLSearchParams(params as any).toString()
const fullUrl = `${baseUrl}${queryParams ? `?${queryParams}` : ''}`
response = await nativeRequest<TeleportEventsResponse>(fullUrl, {
method: 'GET',
headers,
params,
timeout: 20000,
validateStatus: status => status < 500,
})
} catch (e) {
const err = e as AxiosError
logError(new Error(`Teleport events fetch failed: ${err.message}`))
if (isHttpError(e)) {
logError(new Error(`Teleport events fetch failed: ${e.message}`))
} else {
logError(e)
}
logForDiagnosticsNoPII('error', 'teleport_events_fetch_fail')
return null
}
@@ -423,13 +429,17 @@ async function fetchSessionLogsFromUrl(
headers: Record<string, string>,
): Promise<Entry[] | null> {
try {
const response = await axios.get(url, {
const queryParams: Record<string, any> = {}
if (isEnvTruthy(process.env.CLAUDE_AFTER_LAST_COMPACT)) {
queryParams.after_last_compact = true
}
const queryString = new URLSearchParams(queryParams).toString()
const fullUrl = `${url}${queryString ? `?${queryString}` : ''}`
const response = await nativeRequest<any>(fullUrl, {
method: 'GET',
headers,
timeout: 20000,
validateStatus: status => status < 500,
params: isEnvTruthy(process.env.CLAUDE_AFTER_LAST_COMPACT)
? { after_last_compact: true }
: undefined,
})
if (response.status === 200) {
@@ -467,19 +477,20 @@ async function fetchSessionLogsFromUrl(
)
}
logForDebugging(
`Failed to fetch session logs: ${response.status} ${response.statusText}`,
)
logForDebugging(`Failed to fetch session logs: ${response.status}`)
logForDiagnosticsNoPII('error', 'session_get_fail_status', {
status: response.status,
})
return null
} catch (error) {
const axiosError = error as AxiosError<SessionIngressError>
logError(new Error(`Error fetching session logs: ${axiosError.message}`))
logForDiagnosticsNoPII('error', 'session_get_fail_status', {
status: axiosError.status,
})
if (isHttpError(error)) {
logError(new Error(`Error fetching session logs: ${error.message}`))
logForDiagnosticsNoPII('error', 'session_get_fail_status', {
status: error.status,
})
} else {
logError(error)
}
return null
}
}