@@ -15,6 +15,7 @@ import type {
|
15 | 15 | SessionEntry, |
16 | 16 | } from "../config/sessions.js"; |
17 | 17 | import { isCompactionCheckpointTranscriptFileName } from "../config/sessions/artifacts.js"; |
| 18 | +import { readFileRangeAsync } from "../config/sessions/file-range.js"; |
18 | 19 | import { streamSessionTranscriptLines } from "../config/sessions/transcript-stream.js"; |
19 | 20 | import { scanSessionTranscriptTree } from "../config/sessions/transcript-tree.js"; |
20 | 21 | import { CURRENT_SESSION_VERSION } from "../config/sessions/version.js"; |
@@ -158,29 +159,10 @@ export function resolveSessionCompactionCheckpointReason(params: {
|
158 | 159 | const SESSION_HEADER_READ_MAX_BYTES = 64 * 1024; |
159 | 160 | const SESSION_TAIL_READ_INITIAL_BYTES = 64 * 1024; |
160 | 161 | |
161 | | -type AsyncTranscriptFileHandle = Awaited<ReturnType<typeof fs.open>>; |
162 | | - |
163 | | -async function readFileRangeAsync( |
164 | | -fileHandle: AsyncTranscriptFileHandle, |
165 | | -position: number, |
166 | | -length: number, |
167 | | -): Promise<Buffer> { |
168 | | -const buffer = Buffer.alloc(length); |
169 | | -let offset = 0; |
170 | | -while (offset < length) { |
171 | | -const { bytesRead } = await fileHandle.read(buffer, offset, length - offset, position + offset); |
172 | | -if (bytesRead <= 0) { |
173 | | -break; |
174 | | -} |
175 | | -offset += bytesRead; |
176 | | -} |
177 | | -return offset === length ? buffer : buffer.subarray(0, offset); |
178 | | -} |
179 | | - |
180 | 162 | async function readSessionHeaderFromTranscriptAsync( |
181 | 163 | sessionFile: string, |
182 | 164 | ): Promise<{ id: string; cwd?: string } | null> { |
183 | | -let fileHandle: AsyncTranscriptFileHandle | undefined; |
| 165 | +let fileHandle: Awaited<ReturnType<typeof fs.open>> | undefined; |
184 | 166 | try { |
185 | 167 | fileHandle = await fs.open(sessionFile, "r"); |
186 | 168 | const buffer = await readFileRangeAsync(fileHandle, 0, SESSION_HEADER_READ_MAX_BYTES); |
@@ -289,7 +271,7 @@ export async function readSessionLeafStateFromTranscriptAsync(
|
289 | 271 | sessionFile: string, |
290 | 272 | maxBytes = MAX_COMPACTION_CHECKPOINT_LEAF_SCAN_BYTES, |
291 | 273 | ): Promise<{ entryId: string; leafId: string | null } | null> { |
292 | | -let fileHandle: AsyncTranscriptFileHandle | undefined; |
| 274 | +let fileHandle: Awaited<ReturnType<typeof fs.open>> | undefined; |
293 | 275 | try { |
294 | 276 | fileHandle = await fs.open(sessionFile, "r"); |
295 | 277 | const stat = await fileHandle.stat(); |
|