Skip to content

Commit 29d8fb3

Browse files
committed
perf(agents): bound claude orphan transcript scan
1 parent 4c0ede9 commit 29d8fb3

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/agents/command/attempt-execution.helpers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export function claudeCliSessionTranscriptPath(params: {
106106
}
107107

108108
const CLAUDE_CLI_TRANSCRIPT_FLUSH_GRACE_MS = 250;
109+
const CLAUDE_CLI_ORPHAN_PROBE_TAIL_BYTES = 1024 * 1024;
109110

110111
export async function claudeCliSessionTranscriptHasContent(params: {
111112
sessionId: string | undefined;
@@ -166,10 +167,18 @@ async function jsonlFileHasOrphanedTrailingToolUse(filePath: string): Promise<bo
166167

167168
const fh = await fs.open(filePath, "r");
168169
try {
169-
const rl = readline.createInterface({ input: fh.createReadStream({ encoding: "utf-8" }) });
170+
const tailBytes = Math.min(stat.size, CLAUDE_CLI_ORPHAN_PROBE_TAIL_BYTES);
171+
const start = stat.size - tailBytes;
172+
const buffer = Buffer.alloc(tailBytes);
173+
const { bytesRead } = await fh.read(buffer, 0, tailBytes, start);
174+
let tailText = buffer.toString("utf-8", 0, bytesRead);
175+
if (start > 0) {
176+
const firstNewline = tailText.indexOf("\n");
177+
tailText = firstNewline === -1 ? "" : tailText.slice(firstNewline + 1);
178+
}
170179
let lastAssistantToolUseIds: Set<string> = new Set();
171180
let answeredToolResultIds: Set<string> = new Set();
172-
for await (const line of rl) {
181+
for (const line of tailText.split(/\r?\n/)) {
173182
if (!line.trim()) {
174183
continue;
175184
}

0 commit comments

Comments
 (0)