forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskOutput.ts
More file actions
390 lines (352 loc) · 12.2 KB
/
TaskOutput.ts
File metadata and controls
390 lines (352 loc) · 12.2 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { unlink } from 'fs/promises'
import { CircularBuffer } from '../CircularBuffer.js'
import { logForDebugging } from '../debug.js'
import { readFileRange, tailFile } from '../fsOperations.js'
import { getMaxOutputLength } from '../shell/outputLimits.js'
import { safeJoinLines } from '../stringUtils.js'
import { DiskTaskOutput, getTaskOutputPath } from './diskOutput.js'
const DEFAULT_MAX_MEMORY = 8 * 1024 * 1024 // 8MB
const POLL_INTERVAL_MS = 1000
const PROGRESS_TAIL_BYTES = 4096
type ProgressCallback = (
lastLines: string,
allLines: string,
totalLines: number,
totalBytes: number,
isIncomplete: boolean,
) => void
/**
* Single source of truth for a shell command's output.
*
* For bash commands (file mode): both stdout and stderr go directly to
* a file via stdio fds — neither enters JS. Progress is extracted by
* polling the file tail. getStderr() returns '' since stderr is
* interleaved in the output file.
*
* For hooks (pipe mode): data flows through writeStdout()/writeStderr()
* and is buffered in memory, spilling to disk if it exceeds the limit.
*/
export class TaskOutput {
readonly taskId: string
readonly path: string
/** True when stdout goes to a file fd (bypassing JS). False for pipe mode (hooks). */
readonly stdoutToFile: boolean
#stdoutBuffer = ''
#stderrBuffer = ''
#disk: DiskTaskOutput | null = null
#recentLines = new CircularBuffer<string>(1000)
#totalLines = 0
#totalBytes = 0
#maxMemory: number
#onProgress: ProgressCallback | null
/** Set by getStdout() — true when the file was fully read (≤ maxOutputLength). */
#outputFileRedundant = false
/** Set by getStdout() — total file size in bytes. */
#outputFileSize = 0
// --- Shared poller state ---
/** Registry of all file-mode TaskOutput instances with onProgress callbacks. */
static #registry = new Map<string, TaskOutput>()
/** Subset of #registry currently being polled (visibility-driven by React). */
static #activePolling = new Map<string, TaskOutput>()
static #pollInterval: ReturnType<typeof setInterval> | null = null
constructor(
taskId: string,
onProgress: ProgressCallback | null,
stdoutToFile = false,
maxMemory: number = DEFAULT_MAX_MEMORY,
) {
this.taskId = taskId
this.path = getTaskOutputPath(taskId)
this.stdoutToFile = stdoutToFile
this.#maxMemory = maxMemory
this.#onProgress = onProgress
// Register for polling when stdout goes to a file and progress is needed.
// Actual polling is started/stopped by React via startPolling/stopPolling.
if (stdoutToFile && onProgress) {
TaskOutput.#registry.set(taskId, this)
}
}
/**
* Begin polling the output file for progress. Called from React
* useEffect when the progress component mounts.
*/
static startPolling(taskId: string): void {
const instance = TaskOutput.#registry.get(taskId)
if (!instance || !instance.#onProgress) {
return
}
TaskOutput.#activePolling.set(taskId, instance)
if (!TaskOutput.#pollInterval) {
TaskOutput.#pollInterval = setInterval(TaskOutput.#tick, POLL_INTERVAL_MS)
TaskOutput.#pollInterval.unref()
}
}
/**
* Stop polling the output file. Called from React useEffect cleanup
* when the progress component unmounts.
*/
static stopPolling(taskId: string): void {
TaskOutput.#activePolling.delete(taskId)
if (TaskOutput.#activePolling.size === 0 && TaskOutput.#pollInterval) {
clearInterval(TaskOutput.#pollInterval)
TaskOutput.#pollInterval = null
}
}
/**
* Shared tick: reads the file tail for every actively-polled task.
* Non-async body (.then) to avoid stacking if I/O is slow.
*/
static #tick(): void {
for (const [, entry] of TaskOutput.#activePolling) {
if (!entry.#onProgress) {
continue
}
void tailFile(entry.path, PROGRESS_TAIL_BYTES).then(
({ content, bytesRead, bytesTotal }) => {
if (!entry.#onProgress) {
return
}
// Always call onProgress even when content is empty, so the
// progress loop wakes up and can check for backgrounding.
// Commands like `git log -S` produce no output for long periods.
if (!content) {
entry.#onProgress('', '', entry.#totalLines, bytesTotal, false)
return
}
// Count all newlines in the tail and capture slice points for the
// last 5 and last 100 lines. Uncapped so extrapolation stays accurate
// for dense output (short lines → >100 newlines in 4KB).
let pos = content.length
let n5 = 0
let n100 = 0
let lineCount = 0
while (pos > 0) {
pos = content.lastIndexOf('\n', pos - 1)
lineCount++
if (lineCount === 5) n5 = pos <= 0 ? 0 : pos + 1
if (lineCount === 100) n100 = pos <= 0 ? 0 : pos + 1
}
// lineCount is exact when the whole file fits in PROGRESS_TAIL_BYTES.
// Otherwise extrapolate from the tail sample; monotone max keeps the
// counter from going backwards when the tail has longer lines on one tick.
const totalLines =
bytesRead >= bytesTotal
? lineCount
: Math.max(
entry.#totalLines,
Math.round((bytesTotal / bytesRead) * lineCount),
)
entry.#totalLines = totalLines
entry.#totalBytes = bytesTotal
entry.#onProgress(
content.slice(n5),
content.slice(n100),
totalLines,
bytesTotal,
bytesRead < bytesTotal,
)
},
() => {
// File may not exist yet
},
)
}
}
/** Write stdout data (pipe mode only — used by hooks). */
writeStdout(data: string): void {
this.#writeBuffered(data, false)
}
/** Write stderr data (always piped). */
writeStderr(data: string): void {
this.#writeBuffered(data, true)
}
#writeBuffered(data: string, isStderr: boolean): void {
this.#totalBytes += data.length
this.#updateProgress(data)
// Write to disk if already overflowed
if (this.#disk) {
this.#disk.append(isStderr ? `[stderr] ${data}` : data)
return
}
// Check if this chunk would exceed the in-memory limit
const totalMem =
this.#stdoutBuffer.length + this.#stderrBuffer.length + data.length
if (totalMem > this.#maxMemory) {
this.#spillToDisk(isStderr ? data : null, isStderr ? null : data)
return
}
if (isStderr) {
this.#stderrBuffer += data
} else {
this.#stdoutBuffer += data
}
}
/**
* Single backward pass: count all newlines (for totalLines) and extract
* the last few lines as flat copies (for the CircularBuffer / progress).
* Only used in pipe mode (hooks). File mode uses the shared poller.
*/
#updateProgress(data: string): void {
const MAX_PROGRESS_BYTES = 4096
const MAX_PROGRESS_LINES = 100
let lineCount = 0
const lines: string[] = []
let extractedBytes = 0
let pos = data.length
while (pos > 0) {
const prev = data.lastIndexOf('\n', pos - 1)
if (prev === -1) {
break
}
lineCount++
if (
lines.length < MAX_PROGRESS_LINES &&
extractedBytes < MAX_PROGRESS_BYTES
) {
const lineLen = pos - prev - 1
if (lineLen > 0 && lineLen <= MAX_PROGRESS_BYTES - extractedBytes) {
const line = data.slice(prev + 1, pos)
if (line.trim()) {
lines.push(Buffer.from(line).toString())
extractedBytes += lineLen
}
}
}
pos = prev
}
this.#totalLines += lineCount
for (let i = lines.length - 1; i >= 0; i--) {
this.#recentLines.add(lines[i]!)
}
if (this.#onProgress && lines.length > 0) {
const recent = this.#recentLines.getRecent(5)
this.#onProgress(
safeJoinLines(recent, '\n'),
safeJoinLines(this.#recentLines.getRecent(100), '\n'),
this.#totalLines,
this.#totalBytes,
this.#disk !== null,
)
}
}
#spillToDisk(stderrChunk: string | null, stdoutChunk: string | null): void {
this.#disk = new DiskTaskOutput(this.taskId)
// Flush existing buffers
if (this.#stdoutBuffer) {
this.#disk.append(this.#stdoutBuffer)
this.#stdoutBuffer = ''
}
if (this.#stderrBuffer) {
this.#disk.append(`[stderr] ${this.#stderrBuffer}`)
this.#stderrBuffer = ''
}
// Write the chunk that triggered overflow
if (stdoutChunk) {
this.#disk.append(stdoutChunk)
}
if (stderrChunk) {
this.#disk.append(`[stderr] ${stderrChunk}`)
}
}
/**
* Get stdout. In file mode, reads from the output file.
* In pipe mode, returns the in-memory buffer or tail from CircularBuffer.
*/
async getStdout(): Promise<string> {
if (this.stdoutToFile) {
return this.#readStdoutFromFile()
}
// Pipe mode (hooks) — use in-memory data
if (this.#disk) {
const recent = this.#recentLines.getRecent(5)
const tail = safeJoinLines(recent, '\n')
const sizeKB = Math.round(this.#totalBytes / 1024)
const notice = `\nOutput truncated (${sizeKB}KB total). Full output saved to: ${this.path}`
return tail ? tail + notice : notice.trimStart()
}
return this.#stdoutBuffer
}
async #readStdoutFromFile(): Promise<string> {
const maxBytes = getMaxOutputLength()
try {
const result = await readFileRange(this.path, 0, maxBytes)
if (!result) {
this.#outputFileRedundant = true
return ''
}
const { content, bytesRead, bytesTotal } = result
// If the file fits, it's fully captured inline and can be deleted.
// If not, return what we read — processToolResultBlock handles
// the <persisted-output> formatting and persistence downstream.
this.#outputFileSize = bytesTotal
this.#outputFileRedundant = bytesTotal <= bytesRead
return content
} catch (err) {
// Surface the error instead of silently returning empty. An ENOENT here
// means the output file was deleted while the command was running
// (historically: cross-session startup cleanup in the same project dir).
// Returning a diagnostic string keeps the tool_result non-empty, which
// avoids reminder-only-at-tail confusion downstream and tells the model
// (and us, via the transcript) what actually happened.
const code =
err instanceof Error && 'code' in err ? String(err.code) : 'unknown'
logForDebugging(
`TaskOutput.#readStdoutFromFile: failed to read ${this.path} (${code}): ${err}`,
)
return `<bash output unavailable: output file ${this.path} could not be read (${code}). This usually means another Claude Code process in the same project deleted it during startup cleanup.>`
}
}
/** Sync getter for ExecResult.stderr */
getStderr(): string {
if (this.#disk) {
return ''
}
return this.#stderrBuffer
}
get isOverflowed(): boolean {
return this.#disk !== null
}
get totalLines(): number {
return this.#totalLines
}
get totalBytes(): number {
return this.#totalBytes
}
/**
* True after getStdout() when the output file was fully read.
* The file content is redundant (fully in ExecResult.stdout) and can be deleted.
*/
get outputFileRedundant(): boolean {
return this.#outputFileRedundant
}
/** Total file size in bytes, set after getStdout() reads the file. */
get outputFileSize(): number {
return this.#outputFileSize
}
/** Force all buffered content to disk. Call when backgrounding. */
spillToDisk(): void {
if (!this.#disk) {
this.#spillToDisk(null, null)
}
}
async flush(): Promise<void> {
await this.#disk?.flush()
}
/** Delete the output file (fire-and-forget safe). */
async deleteOutputFile(): Promise<void> {
try {
await unlink(this.path)
} catch {
// File may already be deleted or not exist
}
}
clear(): void {
this.#stdoutBuffer = ''
this.#stderrBuffer = ''
this.#recentLines.clear()
this.#onProgress = null
this.#disk?.cancel()
TaskOutput.stopPolling(this.taskId)
TaskOutput.#registry.delete(this.taskId)
}
}