forked from getagentseal/codeburn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs-utils.ts
More file actions
215 lines (191 loc) · 6.62 KB
/
Copy pathfs-utils.ts
File metadata and controls
215 lines (191 loc) · 6.62 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
import { readFile, stat } from 'fs/promises'
import { readFileSync, statSync, createReadStream } from 'fs'
// Hard cap well below V8's 512 MB string limit. Callers that need line-by-line
// processing should use readSessionLines(), which avoids materializing the
// whole file and can return large lines as Buffers.
export const MAX_SESSION_FILE_BYTES = 128 * 1024 * 1024
export const LARGE_STREAM_LINE_BYTES = 32 * 1024
// Line-by-line streaming has bounded memory (one line at a time) and is not
// constrained by V8's string limit, so it can safely handle multi-GB session
// files. The cap here is purely a sanity check against pathological inputs;
// real Codex sessions for heavy users have been observed at 250+ MB and will
// continue to grow as context windows expand.
export const MAX_STREAM_SESSION_FILE_BYTES = 2 * 1024 * 1024 * 1024
function verbose(): boolean {
return process.env.CODEBURN_VERBOSE === '1'
}
function warn(msg: string): void {
if (verbose()) process.stderr.write(`codeburn: ${msg}\n`)
}
export async function readSessionFile(filePath: string): Promise<string | null> {
let size: number
try {
size = (await stat(filePath)).size
} catch (err) {
warn(`stat failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
return null
}
if (size > MAX_SESSION_FILE_BYTES) {
warn(`skipped oversize file ${filePath} (${size} bytes > cap ${MAX_SESSION_FILE_BYTES})`)
return null
}
try {
return await readFile(filePath, 'utf-8')
} catch (err) {
warn(`read failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
return null
}
}
export function readSessionFileSync(filePath: string): string | null {
let size: number
try {
size = statSync(filePath).size
} catch (err) {
warn(`stat failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
return null
}
if (size > MAX_SESSION_FILE_BYTES) {
warn(`skipped oversize file ${filePath} (${size} bytes > cap ${MAX_SESSION_FILE_BYTES})`)
return null
}
try {
return readFileSync(filePath, 'utf-8')
} catch (err) {
warn(`read failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
return null
}
}
export type SessionLine = string | Buffer
type ReadSessionLinesOptions = {
largeLineAsBuffer?: boolean
largeLineThresholdBytes?: number
startByteOffset?: number
byteOffsetTracker?: { lastCompleteLineOffset: number }
}
export function readSessionLines(
filePath: string,
shouldSkipHead?: (head: string) => boolean,
): AsyncGenerator<string>
export function readSessionLines(
filePath: string,
shouldSkipHead?: (head: string) => boolean,
options?: ReadSessionLinesOptions & { largeLineAsBuffer: true },
): AsyncGenerator<SessionLine>
export async function* readSessionLines(
filePath: string,
shouldSkipHead?: (head: string) => boolean,
options: ReadSessionLinesOptions = {},
): AsyncGenerator<SessionLine> {
let size: number
try {
size = (await stat(filePath)).size
} catch (err) {
warn(`stat failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
return
}
if (size > MAX_STREAM_SESSION_FILE_BYTES) {
warn(
`skipped oversize file ${filePath} (${size} bytes > stream cap ${MAX_STREAM_SESSION_FILE_BYTES})`,
)
return
}
const stream = createReadStream(
filePath,
options.startByteOffset !== undefined ? { start: options.startByteOffset } : undefined,
)
const SKIP_HEAD = 2048
const largeLineThreshold = options.largeLineThresholdBytes ?? LARGE_STREAM_LINE_BYTES
const formatLine = (buf: Buffer, lineLen: number, head?: string): SessionLine => {
if (options.largeLineAsBuffer && lineLen > largeLineThreshold) return buf
return head !== undefined && lineLen <= SKIP_HEAD ? head : buf.toString('utf-8')
}
let parts: Buffer[] = []
let len = 0
let skipping = false
let headChecked = false
let chunkBase = options.startByteOffset ?? 0
const tracker = options.byteOffsetTracker
try {
for await (const raw of stream) {
const chunk = raw as Buffer
let pos = 0
while (pos < chunk.length) {
const nl = chunk.indexOf(0x0a, pos)
if (skipping) {
if (nl === -1) {
pos = chunk.length
} else {
if (tracker) tracker.lastCompleteLineOffset = chunkBase + nl + 1
skipping = false
pos = nl + 1
}
continue
}
if (nl !== -1) {
if (pos < nl) {
parts.push(chunk.subarray(pos, nl))
len += nl - pos
}
pos = nl + 1
if (tracker) tracker.lastCompleteLineOffset = chunkBase + pos
if (len === 0) {
parts = []
headChecked = false
continue
}
const buf = parts.length === 1 ? parts[0]! : Buffer.concat(parts, len)
const lineLen = len
parts = []
len = 0
headChecked = false
if (shouldSkipHead) {
const head = lineLen > SKIP_HEAD
? buf.subarray(0, SKIP_HEAD).toString('utf-8')
: buf.toString('utf-8')
if (shouldSkipHead(head)) continue
yield formatLine(buf, lineLen, head)
} else {
yield formatLine(buf, lineLen)
}
} else {
const slice = chunk.subarray(pos)
parts.push(slice)
len += slice.length
pos = chunk.length
// Mid-line skip: once we have enough bytes to check the head,
// enter scanning mode — just look for \n without accumulating.
if (shouldSkipHead && !headChecked && len >= SKIP_HEAD) {
headChecked = true
const headBuf = parts.length === 1
? parts[0]!.subarray(0, SKIP_HEAD)
: Buffer.concat(parts, len).subarray(0, SKIP_HEAD)
if (shouldSkipHead(headBuf.toString('utf-8'))) {
skipping = true
parts = []
len = 0
}
}
}
}
chunkBase += chunk.length
}
if (!skipping && len > 0) {
const buf = parts.length === 1 ? parts[0]! : Buffer.concat(parts, len)
const lineLen = len
if (shouldSkipHead) {
const head = lineLen > SKIP_HEAD
? buf.subarray(0, SKIP_HEAD).toString('utf-8')
: buf.toString('utf-8')
if (!shouldSkipHead(head)) {
yield formatLine(buf, lineLen, head)
}
} else {
yield formatLine(buf, lineLen)
}
}
} catch (err) {
warn(`stream read failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
} finally {
stream.destroy()
}
}