-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Expand file tree
/
Copy pathdebug.ts
More file actions
281 lines (252 loc) · 8.45 KB
/
Copy pathdebug.ts
File metadata and controls
281 lines (252 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { appendFile, mkdir, symlink, unlink } from 'fs/promises'
import memoize from 'lodash-es/memoize.js'
import { dirname, join, resolve } from 'path'
import { getSessionId } from 'src/bootstrap/state.js'
import { type BufferedWriter, createBufferedWriter } from './bufferedWriter.js'
import { registerCleanup } from './cleanupRegistry.js'
import {
type DebugFilter,
parseDebugFilter,
shouldShowDebugMessage,
} from './debugFilter.js'
import { getClaudeConfigHomeDir, isEnvTruthy } from './envUtils.js'
import { getFsImplementation } from './fsOperations.js'
import { writeToStderr } from './process.js'
import { jsonStringify } from './slowOperations.js'
export type DebugLogLevel = 'verbose' | 'debug' | 'info' | 'warn' | 'error'
const LEVEL_ORDER: Record<DebugLogLevel, number> = {
verbose: 0,
debug: 1,
info: 2,
warn: 3,
error: 4,
}
/**
* Minimum log level to include in debug output. Defaults to 'debug', which
* filters out 'verbose' messages. Set CLAUDE_CODE_DEBUG_LOG_LEVEL=verbose to
* include high-volume diagnostics (e.g. full statusLine command, shell, cwd,
* stdout/stderr) that would otherwise drown out useful debug output.
*/
export const getMinDebugLogLevel = memoize((): DebugLogLevel => {
const raw = process.env.CLAUDE_CODE_DEBUG_LOG_LEVEL?.toLowerCase().trim()
if (raw && Object.hasOwn(LEVEL_ORDER, raw)) {
return raw as DebugLogLevel
}
return 'debug'
})
let runtimeDebugEnabled = false
export const isDebugMode = memoize((): boolean => {
return (
runtimeDebugEnabled ||
isEnvTruthy(process.env.DEBUG) ||
isEnvTruthy(process.env.DEBUG_SDK) ||
process.argv.includes('--debug') ||
process.argv.includes('-d') ||
isDebugToStdErr() ||
// Also check for --debug=pattern syntax
process.argv.some(arg => arg.startsWith('--debug=')) ||
// --debug-file implicitly enables debug mode
getDebugFilePath() !== null
)
})
/**
* Enables debug logging mid-session (e.g. via /debug). Non-ants don't write
* debug logs by default, so this lets them start capturing without restarting
* with --debug. Returns true if logging was already active.
*/
export function enableDebugLogging(): boolean {
const wasActive = isDebugMode() || process.env.USER_TYPE === 'ant'
runtimeDebugEnabled = true
isDebugMode.cache.clear?.()
return wasActive
}
// Extract and parse debug filter from command line arguments
// Exported for testing purposes
export const getDebugFilter = memoize((): DebugFilter | null => {
// Look for --debug=pattern in argv
const debugArg = process.argv.find(arg => arg.startsWith('--debug='))
if (!debugArg) {
return null
}
// Extract the pattern after the equals sign
const filterPattern = debugArg.substring('--debug='.length)
return parseDebugFilter(filterPattern)
})
export const isDebugToStdErr = memoize((): boolean => {
return process.argv.includes('--debug-to-stderr')
})
export const getDebugFilePath = memoize((): string | null => {
for (let i = 0; i < process.argv.length; i++) {
const arg = process.argv[i]!
if (arg.startsWith('--debug-file=')) {
return resolve(arg.substring('--debug-file='.length))
}
if (arg === '--debug-file' && i + 1 < process.argv.length) {
return resolve(process.argv[i + 1]!)
}
}
return null
})
function shouldLogDebugMessage(message: string): boolean {
if (process.env.NODE_ENV === 'test' && !isDebugToStdErr()) {
return false
}
// Non-ants only write debug logs when debug mode is active (via --debug at
// startup or /debug mid-session). Ants always log for /share, bug reports.
if (process.env.USER_TYPE !== 'ant' && !isDebugMode()) {
return false
}
if (
typeof process === 'undefined' ||
typeof process.versions === 'undefined' ||
typeof process.versions.node === 'undefined'
) {
return false
}
const filter = getDebugFilter()
return shouldShowDebugMessage(message, filter)
}
let hasFormattedOutput = false
export function setHasFormattedOutput(value: boolean): void {
hasFormattedOutput = value
}
export function getHasFormattedOutput(): boolean {
return hasFormattedOutput
}
let debugWriter: BufferedWriter | null = null
let pendingWrite: Promise<void> = Promise.resolve()
// Module-level so .bind captures only its explicit args, not the
// writeFn closure's parent scope (Jarred, #22257).
async function appendAsync(
needMkdir: boolean,
dir: string,
path: string,
content: string,
): Promise<void> {
if (needMkdir) {
await mkdir(dir, { recursive: true }).catch(() => {})
}
await appendFile(path, content)
void updateLatestDebugLogSymlink()
}
function noop(): void {}
function getDebugWriter(): BufferedWriter {
if (!debugWriter) {
let ensuredDir: string | null = null
debugWriter = createBufferedWriter({
writeFn: content => {
const path = getDebugLogPath()
const dir = dirname(path)
const needMkdir = ensuredDir !== dir
ensuredDir = dir
if (isDebugMode()) {
// immediateMode: must stay sync. Async writes are lost on direct
// process.exit() and keep the event loop alive in beforeExit
// handlers (infinite loop with Perfetto tracing). See #22257.
if (needMkdir) {
try {
getFsImplementation().mkdirSync(dir)
} catch {
// Directory already exists
}
}
try {
getFsImplementation().appendFileSync(path, content)
} catch (e) {
// If the directory was removed between mkdirSync and
// appendFileSync (or the path was relative and CWD changed),
// retry once with a fresh mkdirSync. Swallow on second failure
// to avoid crashing the React render tree via the error boundary.
try {
getFsImplementation().mkdirSync(dir)
getFsImplementation().appendFileSync(path, content)
} catch {
// Best-effort: debug log write failed, don't crash the UI
}
}
void updateLatestDebugLogSymlink()
return
}
// Buffered path (ants without --debug): flushes ~1/sec so chain
// depth stays ~1. .bind over a closure so only the bound args are
// retained, not this scope.
pendingWrite = pendingWrite
.then(appendAsync.bind(null, needMkdir, dir, path, content))
.catch(noop)
},
flushIntervalMs: 1000,
maxBufferSize: 100,
immediateMode: isDebugMode(),
})
registerCleanup(async () => {
debugWriter?.dispose()
await pendingWrite
})
}
return debugWriter
}
export async function flushDebugLogs(): Promise<void> {
debugWriter?.flush()
await pendingWrite
}
export function logForDebugging(
message: string,
{ level }: { level: DebugLogLevel } = {
level: 'debug',
},
): void {
if (LEVEL_ORDER[level] < LEVEL_ORDER[getMinDebugLogLevel()]) {
return
}
if (!shouldLogDebugMessage(message)) {
return
}
// Multiline messages break the jsonl output format, so make any multiline messages JSON.
if (hasFormattedOutput && message.includes('\n')) {
message = jsonStringify(message)
}
const timestamp = new Date().toISOString()
const output = `${timestamp} [${level.toUpperCase()}] ${message.trim()}\n`
if (isDebugToStdErr()) {
writeToStderr(output)
return
}
getDebugWriter().write(output)
}
export function getDebugLogPath(): string {
return (
getDebugFilePath() ??
(process.env.CLAUDE_CODE_DEBUG_LOGS_DIR
? resolve(process.env.CLAUDE_CODE_DEBUG_LOGS_DIR)
: null) ??
join(getClaudeConfigHomeDir(), 'debug', `${getSessionId()}.txt`)
)
}
/**
* Updates the latest debug log symlink to point to the current debug log file.
* Creates or updates a symlink at ~/.claude/debug/latest
*/
const updateLatestDebugLogSymlink = memoize(async (): Promise<void> => {
try {
const debugLogPath = getDebugLogPath()
const debugLogsDir = dirname(debugLogPath)
const latestSymlinkPath = join(debugLogsDir, 'latest')
await unlink(latestSymlinkPath).catch(() => {})
await symlink(debugLogPath, latestSymlinkPath)
} catch {
// Silently fail if symlink creation fails
}
})
/**
* Logs errors for Ants only, always visible in production.
*/
export function logAntError(context: string, error: unknown): void {
if (process.env.USER_TYPE !== 'ant') {
return
}
if (error instanceof Error && error.stack) {
logForDebugging(`[ANT-ONLY] ${context} stack trace:\n${error.stack}`, {
level: 'error',
})
}
}