UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

217 lines • 9.06 kB
/** * Reading a session JSONL file into entries, shared by the synchronous loader and the * time-sliced one a multi-session host uses, so both parse byte-for-byte the same way. * * A host runs every session on one event loop: parsing a 231 MB transcript in one go held * every other session's requests for seconds (senpi#3518). `loadEntriesFromFileAsync` reads * with asynchronous I/O and hands the loop back whenever its slice budget is spent. */ import { closeSync, existsSync, fstatSync, ftruncateSync, openSync, readSync, statSync, writeSync } from "node:fs"; import { open } from "node:fs/promises"; import { StringDecoder } from "node:string_decoder"; import { normalizePath } from "../utils/paths.js"; import { EventLoopSlicer } from "./event-loop-slicer.js"; import { BaseContentHash, captureAndCloseFileIdentity, captureFileIdentity, loadedContentFingerprints, loadedFileIdentities, } from "./session-file-identity.js"; import { backupTornBytes } from "./session-write-recovery.js"; export const SESSION_READ_BUFFER_SIZE = 1024 * 1024; export function parseSessionEntryLine(line) { if (!line.trim()) return null; try { return JSON.parse(line); } catch { // Skip malformed lines return null; } } export class SessionLineSplitter { constructor() { this.decoder = new StringDecoder("utf8"); this.pending = ""; } push(chunk) { this.pending += this.decoder.write(chunk); const lines = []; let lineStart = 0; let newlineIndex = this.pending.indexOf("\n", lineStart); while (newlineIndex !== -1) { lines.push(this.pending.slice(lineStart, newlineIndex)); lineStart = newlineIndex + 1; newlineIndex = this.pending.indexOf("\n", lineStart); } this.pending = this.pending.slice(lineStart); return lines; } finish() { return this.pending + this.decoder.end(); } } /** A parsed line becomes an entry; a capturing load also feeds its line to the content fingerprint (RULING-72). */ function countEntry(entries, line, content) { const entry = parseSessionEntryLine(line); if (!entry) return; entries.push(entry); content?.line(line); } function acceptSessionEntries(entries) { if (entries.length === 0) return entries; const header = entries[0]; if (header?.type !== "session" || typeof header.id !== "string") return []; return entries; } /** Exported for testing */ export function loadEntriesFromFile(filePath, captureIdentity = false) { return acceptSessionEntries(readRawEntriesFromFile(filePath, captureIdentity)); } const isSessionShapedEntry = (entry) => { const candidate = entry; return (typeof candidate.type === "string" && candidate.type !== "session" && typeof candidate.id === "string" && (candidate.parentId === null || typeof candidate.parentId === "string")); }; /** * Read the source of a fork. A file whose line-1 header was lost but whose every other line is a session * entry (`type`, `id`, `parentId`) is still a whole conversation, and a fork writes its own new header anyway, * so it is forked rather than refused (omo#10375). Anything else that is not a headed session is reported as * what it is: no parsable line at all is empty, parsable lines of another shape are not a session. */ export function readForkSource(filePath) { const raw = readRawEntriesFromFile(filePath); if (raw.length === 0) return { kind: "empty" }; const accepted = acceptSessionEntries(raw); if (accepted.length > 0) return { kind: "session", entries: accepted }; if (raw.every(isSessionShapedEntry)) return { kind: "headerless", entries: raw }; return { kind: "not-a-session" }; } function readRawEntriesFromFile(filePath, captureIdentity = false) { const resolvedFilePath = normalizePath(filePath); if (!existsSync(resolvedFilePath)) return []; const entries = []; const splitter = new SessionLineSplitter(); const content = captureIdentity ? new BaseContentHash() : undefined; const fd = openSync(resolvedFilePath, "r"); try { const buffer = Buffer.allocUnsafe(SESSION_READ_BUFFER_SIZE); while (true) { const bytesRead = readSync(fd, buffer, 0, buffer.length, null); if (bytesRead === 0) break; for (const line of splitter.push(buffer.subarray(0, bytesRead))) countEntry(entries, line, content); } countEntry(entries, splitter.finish(), content); } finally { if (captureIdentity) loadedFileIdentities.set(entries, captureAndCloseFileIdentity(fd, resolvedFilePath)); else closeSync(fd); } if (content) loadedContentFingerprints.set(entries, content.digest()); return entries; } /** * `loadEntriesFromFile` without holding the event loop: the same entries, read with * asynchronous I/O and parsed in slices that yield once `slicer`'s budget is spent. */ export async function loadEntriesFromFileAsync(filePath, slicer = new EventLoopSlicer(), captureIdentity = false) { const resolvedFilePath = normalizePath(filePath); if (!existsSync(resolvedFilePath)) return []; const entries = []; const splitter = new SessionLineSplitter(); const content = captureIdentity ? new BaseContentHash() : undefined; const handle = await open(resolvedFilePath, "r"); try { const buffer = Buffer.allocUnsafe(SESSION_READ_BUFFER_SIZE); while (true) { const { bytesRead } = await handle.read(buffer, 0, buffer.length, null); if (bytesRead === 0) break; for (const line of splitter.push(buffer.subarray(0, bytesRead))) { countEntry(entries, line, content); if (slicer.due) await slicer.yield(); } } countEntry(entries, splitter.finish(), content); if (content) loadedContentFingerprints.set(entries, content.digest()); } finally { if (captureIdentity) { const identity = captureFileIdentity(handle.fd, resolvedFilePath); loadedFileIdentities.set(entries, identity); await identity; } await handle.close(); } return acceptSessionEntries(entries); } /** * Make this process's appends start on a new line. A final line with no newline is either a whole entry written * without one (it parses: it is terminated and kept) or a line a crash tore part-way (it does not parse: it was * never committed, so it is copied to `<file>.torn-<time>` and the file is cut back to its last complete line, * senpi#3549, senpi#4295). Terminating a torn line instead would turn it into a complete, corrupt line that no reader * can tell from real corruption. Only the final segment is read, never the whole transcript; anything but a regular * file is left to the loader, as before. */ export function terminateFinalSessionLine(filePath, cause = "unknown") { if (!existsSync(filePath) || !statSync(filePath).isFile()) return undefined; const fd = openSync(filePath, "r+"); try { const size = fstatSync(fd).size; const segmentStart = finalSegmentStart(fd, size); if (segmentStart === size) return undefined; const segment = Buffer.alloc(size - segmentStart); readSync(fd, segment, 0, segment.length, segmentStart); if (parseSessionEntryLine(segment.toString("utf8")) !== null) { writeSync(fd, "\n", size); return undefined; } let recovery; try { recovery = backupTornBytes(filePath, segment, cause); } catch (error) { // Deferred, not dropped: a full disk can refuse the copy, so the file is left as it is and the writer // sets the tail aside before its first append, which fails with this error if the disk is still full. if (error instanceof Error) return { kind: "deferred" }; throw error; } // Not synced on purpose: a lost truncate only brings back an uncommitted tail, and the next append syncs. ftruncateSync(fd, segmentStart); return { kind: "set_aside", recovery }; } finally { closeSync(fd); } } /** The offset just after the file's last newline (0 when it has none); `size` when it ends in one. */ function finalSegmentStart(fd, size) { const chunk = Buffer.alloc(Math.min(64 * 1024, size)); let end = size; while (end > 0) { const start = Math.max(0, end - chunk.length); const read = readSync(fd, chunk, 0, end - start, start); const newline = chunk.subarray(0, read).lastIndexOf(10); if (newline >= 0) return start + newline + 1; end = start; } return 0; } //# sourceMappingURL=session-entry-file.js.map